"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MediaEdit = exports.MEDIA_TYPE_IMAGE = exports.MEDIA_EDITOR = void 0;
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _reactNativeBridge = require("@wordpress/react-native-bridge");
var _picker = _interopRequireDefault(require("../picker"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const MEDIA_TYPE_IMAGE = exports.MEDIA_TYPE_IMAGE = 'image';
const MEDIA_EDITOR = exports.MEDIA_EDITOR = 'MEDIA_EDITOR';
const editOption = {
  id: MEDIA_EDITOR,
  value: MEDIA_EDITOR,
  label: (0, _i18n.__)('Edit'),
  requiresModal: true,
  types: [MEDIA_TYPE_IMAGE]
};
const replaceOption = {
  id: _reactNativeBridge.mediaSources.deviceLibrary,
  value: _reactNativeBridge.mediaSources.deviceLibrary,
  label: (0, _i18n.__)('Replace'),
  types: [MEDIA_TYPE_IMAGE]
};
class MediaEdit extends _element.Component {
  constructor(props) {
    super(props);
    this.onPickerPresent = this.onPickerPresent.bind(this);
    this.onPickerSelect = this.onPickerSelect.bind(this);
    this.getMediaOptionsItems = this.getMediaOptionsItems.bind(this);
    this.getDestructiveButtonIndex = this.getDestructiveButtonIndex.bind(this);
  }
  getMediaOptionsItems() {
    const {
      pickerOptions,
      openReplaceMediaOptions,
      source
    } = this.props;
    return [source?.uri && editOption, openReplaceMediaOptions && replaceOption, ...(pickerOptions ? pickerOptions : [])].filter(Boolean);
  }
  getDestructiveButtonIndex() {
    const options = this.getMediaOptionsItems();
    const destructiveButtonIndex = options.findIndex(option => option?.destructiveButton);
    return destructiveButtonIndex !== -1 ? destructiveButtonIndex + 1 : undefined;
  }
  onPickerPresent() {
    if (this.picker) {
      this.picker.presentPicker();
    }
  }
  onPickerSelect(value) {
    const {
      onSelect,
      pickerOptions,
      multiple = false,
      openReplaceMediaOptions
    } = this.props;
    switch (value) {
      case MEDIA_EDITOR:
        (0, _reactNativeBridge.requestMediaEditor)(this.props.source.uri, media => {
          if (multiple && media || media && media.id) {
            onSelect(media);
          }
        });
        break;
      default:
        const optionSelected = pickerOptions && pickerOptions.find(option => option.id === value);
        if (optionSelected && optionSelected.onPress) {
          optionSelected.onPress();
          return;
        }
        if (openReplaceMediaOptions) {
          openReplaceMediaOptions();
        }
    }
  }
  render() {
    const mediaOptions = () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_picker.default, {
      hideCancelButton: true,
      ref: instance => this.picker = instance,
      options: this.getMediaOptionsItems(),
      leftAlign: true,
      onChange: this.onPickerSelect
      // translators: %s: block title e.g: "Paragraph".
      ,
      title: (0, _i18n.__)('Media options'),
      destructiveButtonIndex: this.getDestructiveButtonIndex()
    });
    return this.props.render({
      open: this.onPickerPresent,
      mediaOptions
    });
  }
}
exports.MediaEdit = MediaEdit;
var _default = exports.default = MediaEdit;
//# sourceMappingURL=index.native.js.map