'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BaseUntypedHierarchyEditor = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactIntl = require('react-intl');

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _cspaceInput = require('cspace-input');

var _MiniViewPopupAutocompleteInputContainer = require('../../containers/record/MiniViewPopupAutocompleteInputContainer');

var _MiniViewPopupAutocompleteInputContainer2 = _interopRequireDefault(_MiniViewPopupAutocompleteInputContainer);

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

const CompoundInput = _cspaceInput.components.CompoundInput;


const propTypes = {
  csid: _propTypes2.default.string,
  intl: _reactIntl.intlShape,
  messages: _propTypes2.default.object,
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  value: _propTypes2.default.instanceOf(_immutable2.default.Map),
  readOnly: _propTypes2.default.bool,
  isRecordModified: _propTypes2.default.bool,
  showParent: _propTypes2.default.bool,
  showChildren: _propTypes2.default.bool,
  onCommit: _propTypes2.default.func,
  onAddChild: _propTypes2.default.func,
  onRemoveChild: _propTypes2.default.func
};

const defaultProps = {
  showParent: true,
  showChildren: true
};

class BaseUntypedHierarchyEditor extends _react.Component {
  constructor() {
    super();

    this.filterMatch = this.filterMatch.bind(this);
    this.handleAddChild = this.handleAddChild.bind(this);
    this.handleRemoveChild = this.handleRemoveChild.bind(this);
    this.handleChildCommit = this.handleChildCommit.bind(this);
    this.handleParentCommit = this.handleParentCommit.bind(this);
  }

  filterMatch(item) {
    return item.csid !== this.props.csid;
  }

  handleAddChild() {
    const onAddChild = this.props.onAddChild;


    if (onAddChild) {
      onAddChild();
    }
  }

  handleRemoveChild(path) {
    const onRemoveChild = this.props.onRemoveChild;


    if (onRemoveChild) {
      const position = parseInt(path[path.length - 1], 10);

      onRemoveChild(position);
    }
  }

  handleChildCommit(path, value) {
    const onCommit = this.props.onCommit;


    if (onCommit) {
      const index = path[path.length - 1];

      onCommit(['children', index, 'refName'], value);
    }
  }

  handleParentCommit(path, value, csid) {
    const onCommit = this.props.onCommit;


    if (onCommit) {
      onCommit(['parent'], _immutable2.default.Map({
        csid,
        refName: value
      }));
    }
  }

  renderParent() {
    var _props = this.props;
    const intl = _props.intl,
          messages = _props.messages,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary,
          value = _props.value,
          readOnly = _props.readOnly,
          isRecordModified = _props.isRecordModified,
          showParent = _props.showParent;


    if (!showParent) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');
    const parentRefName = value.getIn(['parent', 'refName']);

    return _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
      label: intl.formatMessage(messages.parent),
      source: source,
      value: parentRefName,
      readOnly: readOnly,
      showQuickAddCloneOption: true,
      quickAddCloneOptionDisabled: isRecordModified,
      onCommit: this.handleParentCommit,
      matchFilter: this.filterMatch
    });
  }

  renderChildren() {
    var _props2 = this.props;
    const intl = _props2.intl,
          messages = _props2.messages,
          recordType = _props2.recordType,
          vocabulary = _props2.vocabulary,
          value = _props2.value,
          readOnly = _props2.readOnly,
          isRecordModified = _props2.isRecordModified,
          showChildren = _props2.showChildren;


    if (!showChildren) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');

    const childRefNames = value.get('children').map(child => child.get('refName'));

    return _react2.default.createElement(
      CompoundInput,
      {
        label: intl.formatMessage(messages.children),
        value: { childRefNames },
        readOnly: readOnly
      },
      _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
        name: 'childRefNames',
        repeating: true,
        reorderable: false,
        source: source,
        showQuickAddCloneOption: true,
        quickAddCloneOptionDisabled: isRecordModified,
        matchFilter: this.filterMatch,
        onCommit: this.handleChildCommit,
        onAddInstance: this.handleAddChild,
        onRemoveInstance: this.handleRemoveChild
      })
    );
  }

  render() {
    return _react2.default.createElement(
      'div',
      null,
      this.renderParent(),
      this.renderChildren()
    );
  }
}

exports.BaseUntypedHierarchyEditor = BaseUntypedHierarchyEditor;
BaseUntypedHierarchyEditor.propTypes = propTypes;
BaseUntypedHierarchyEditor.defaultProps = defaultProps;

exports.default = (0, _reactIntl.injectIntl)(BaseUntypedHierarchyEditor);