"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = _interopRequireDefault(require("immutable"));

var _SearchToSelectModalContainer = _interopRequireDefault(require("../../containers/search/SearchToSelectModalContainer"));

var _InvocationTargetInput = _interopRequireDefault(require("./InvocationTargetInput"));

var _ModePickerInput = _interopRequireDefault(require("./ModePickerInput"));

var _InvocationDescriptorEditor = _interopRequireDefault(require("../../../styles/cspace-ui/InvocationDescriptorEditor.css"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const initMode = props => {
  const {
    invocationDescriptor,
    modes,
    onCommit
  } = props;
  const mode = invocationDescriptor && invocationDescriptor.get('mode');

  if ((!mode || !modes.includes(mode)) && modes.length > 0 && onCommit) {
    onCommit(invocationDescriptor.set('mode', modes[0]));
  }
};

const propTypes = {
  config: _propTypes.default.object,
  invocationDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  modes: _propTypes.default.arrayOf(_propTypes.default.string),
  modeReadOnly: _propTypes.default.bool,
  invocationTargetReadOnly: _propTypes.default.bool,
  recordTypes: _propTypes.default.arrayOf(_propTypes.default.string),
  onCommit: _propTypes.default.func
};
const defaultProps = {
  invocationDescriptor: _immutable.default.Map(),
  modes: []
};

class InvocationDescriptorEditor extends _react.Component {
  constructor(props) {
    super(props);
    this.handleModePickerCommit = this.handleModePickerCommit.bind(this);
    this.handleSearchModalAccept = this.handleSearchModalAccept.bind(this);
    this.handleSearchModalCancelButtonClick = this.handleSearchModalCancelButtonClick.bind(this);
    this.openSearchModal = this.openSearchModal.bind(this);
    this.state = {
      isSearchModalOpen: false
    };
  }

  componentDidMount() {
    initMode(this.props);
  }

  componentWillReceiveProps(nextProps) {
    initMode(nextProps);
  }

  handleSearchModalAccept(selectedItems, searchDescriptor) {
    const {
      invocationDescriptor,
      onCommit
    } = this.props;

    if (onCommit) {
      onCommit(invocationDescriptor.set('recordType', searchDescriptor.get('recordType')).set('items', selectedItems));
    }

    this.setState({
      isSearchModalOpen: false
    });
  }

  handleSearchModalCancelButtonClick() {
    this.setState({
      isSearchModalOpen: false
    });
  }

  handleModePickerCommit(path, value) {
    const {
      invocationDescriptor,
      invocationTargetReadOnly,
      onCommit
    } = this.props;

    if (onCommit) {
      let nextInvocationDescriptor = invocationDescriptor.set('mode', value);

      if (!invocationTargetReadOnly) {
        nextInvocationDescriptor = nextInvocationDescriptor.delete('csid').delete('recordType').delete('items');
      }

      onCommit(nextInvocationDescriptor);
    }
  }

  openSearchModal() {
    this.setState({
      isSearchModalOpen: true
    });
  }

  render() {
    const {
      config,
      invocationDescriptor,
      modes,
      modeReadOnly,
      invocationTargetReadOnly,
      recordTypes
    } = this.props;
    const {
      isSearchModalOpen
    } = this.state;
    const mode = invocationDescriptor.get('mode');
    const recordType = invocationDescriptor.get('recordType');
    const allowedRecordTypes = mode === 'group' ? ['group'] : recordTypes;
    return _react.default.createElement("div", {
      className: _InvocationDescriptorEditor.default.common
    }, _react.default.createElement("div", null, _react.default.createElement(_ModePickerInput.default, {
      modes: modes,
      readOnly: modeReadOnly || modes.length < 2,
      value: mode,
      onCommit: this.handleModePickerCommit
    }), _react.default.createElement(_InvocationTargetInput.default, {
      config: config,
      mode: mode,
      readOnly: invocationTargetReadOnly,
      openSearchModal: this.openSearchModal,
      value: invocationDescriptor.get('items')
    })), _react.default.createElement(_SearchToSelectModalContainer.default, {
      allowedRecordTypes: allowedRecordTypes,
      config: config,
      isOpen: isSearchModalOpen,
      recordTypeValue: recordType,
      singleSelect: mode === 'single' || mode === 'group',
      onAccept: this.handleSearchModalAccept,
      onCancelButtonClick: this.handleSearchModalCancelButtonClick,
      onCloseButtonClick: this.handleSearchModalCancelButtonClick
    }));
  }

}

exports.default = InvocationDescriptorEditor;
InvocationDescriptorEditor.propTypes = propTypes;
InvocationDescriptorEditor.defaultProps = defaultProps;