'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BaseTypedHierarchyEditor = 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);

var _OptionPickerInputContainer = require('../../containers/input/OptionPickerInputContainer');

var _OptionPickerInputContainer2 = _interopRequireDefault(_OptionPickerInputContainer);

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

const CompoundInput = _cspaceInput.components.CompoundInput,
      InputTable = _cspaceInput.components.InputTable;


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,
  showParent: _propTypes2.default.bool,
  showChildren: _propTypes2.default.bool,
  parentTypeOptionListName: _propTypes2.default.string,
  childTypeOptionListName: _propTypes2.default.string,
  onCommit: _propTypes2.default.func,
  onAddChild: _propTypes2.default.func,
  onRemoveChild: _propTypes2.default.func
};

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

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

    this.filterMatch = this.filterMatch.bind(this);
    this.handleAddChild = this.handleAddChild.bind(this);
    this.handleRemoveChild = this.handleRemoveChild.bind(this);
    this.handleChildRefNameCommit = this.handleChildRefNameCommit.bind(this);
    this.handleChildTypeCommit = this.handleChildTypeCommit.bind(this);
    this.handleParentRefNameCommit = this.handleParentRefNameCommit.bind(this);
    this.handleParentTypeCommit = this.handleParentTypeCommit.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);
    }
  }

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


    if (onCommit) {
      const index = path[0];

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

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


    if (onCommit) {
      const index = path[0];

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

  handleParentRefNameCommit(path, value, csid) {
    var _props = this.props;
    const hierarchy = _props.value,
          onCommit = _props.onCommit;


    const parent = hierarchy.get('parent');

    if (onCommit) {
      onCommit(['parent'], parent.set('refName', value).set('csid', csid));
    }
  }

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


    if (onCommit) {
      onCommit(['parent', 'type'], value);
    }
  }

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


    if (!showParent) {
      return undefined;
    }

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

    return _react2.default.createElement(
      InputTable,
      { label: intl.formatMessage(messages.parent) },
      _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
        label: intl.formatMessage(messages.parentName),
        name: 'parentRefName',
        source: source,
        value: parentRefName,
        readOnly: readOnly,
        onCommit: this.handleParentRefNameCommit,
        matchFilter: this.filterMatch
      }),
      _react2.default.createElement(_OptionPickerInputContainer2.default, {
        label: intl.formatMessage(messages.parentType),
        name: 'parentType',
        source: parentTypeOptionListName,
        value: parentType,
        readOnly: readOnly,
        onCommit: this.handleParentTypeCommit
      })
    );
  }

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


    if (!showChildren) {
      return undefined;
    }

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

    return _react2.default.createElement(
      CompoundInput,
      {
        label: intl.formatMessage(messages.children),
        value: children,
        readOnly: readOnly
      },
      _react2.default.createElement(
        CompoundInput,
        {
          tabular: true,
          repeating: true,
          reorderable: false,
          onAddInstance: this.handleAddChild,
          onRemoveInstance: this.handleRemoveChild
        },
        _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
          label: intl.formatMessage(messages.childName),
          name: 'refName',
          source: source,
          matchFilter: this.filterMatch,
          onCommit: this.handleChildRefNameCommit
        }),
        _react2.default.createElement(_OptionPickerInputContainer2.default, {
          label: intl.formatMessage(messages.childType),
          name: 'type',
          source: childTypeOptionListName,
          onCommit: this.handleChildTypeCommit
        })
      )
    );
  }

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

exports.BaseTypedHierarchyEditor = BaseTypedHierarchyEditor;
BaseTypedHierarchyEditor.propTypes = propTypes;
BaseTypedHierarchyEditor.defaultProps = defaultProps;

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