'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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


var 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
};

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

var BaseTypedHierarchyEditor = exports.BaseTypedHierarchyEditor = function (_Component) {
  _inherits(BaseTypedHierarchyEditor, _Component);

  function BaseTypedHierarchyEditor() {
    _classCallCheck(this, BaseTypedHierarchyEditor);

    var _this = _possibleConstructorReturn(this, (BaseTypedHierarchyEditor.__proto__ || Object.getPrototypeOf(BaseTypedHierarchyEditor)).call(this));

    _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);
    return _this;
  }

  _createClass(BaseTypedHierarchyEditor, [{
    key: 'filterMatch',
    value: function filterMatch(item) {
      return item.csid !== this.props.csid;
    }
  }, {
    key: 'handleAddChild',
    value: function handleAddChild() {
      var onAddChild = this.props.onAddChild;


      if (onAddChild) {
        onAddChild();
      }
    }
  }, {
    key: 'handleRemoveChild',
    value: function handleRemoveChild(path) {
      var onRemoveChild = this.props.onRemoveChild;


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

        onRemoveChild(position);
      }
    }
  }, {
    key: 'handleChildRefNameCommit',
    value: function handleChildRefNameCommit(path, value) {
      var onCommit = this.props.onCommit;


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

        onCommit(['children', index, 'refName'], value);
      }
    }
  }, {
    key: 'handleChildTypeCommit',
    value: function handleChildTypeCommit(path, value) {
      var onCommit = this.props.onCommit;


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

        onCommit(['children', index, 'type'], value);
      }
    }
  }, {
    key: 'handleParentRefNameCommit',
    value: function handleParentRefNameCommit(path, value, csid) {
      var _props = this.props,
          hierarchy = _props.value,
          onCommit = _props.onCommit;


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

      if (onCommit) {
        onCommit(['parent'], parent.set('refName', value).set('csid', csid));
      }
    }
  }, {
    key: 'handleParentTypeCommit',
    value: function handleParentTypeCommit(path, value) {
      var onCommit = this.props.onCommit;


      if (onCommit) {
        onCommit(['parent', 'type'], value);
      }
    }
  }, {
    key: 'renderParent',
    value: function renderParent() {
      var _props2 = this.props,
          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;
      }

      var source = [recordType, vocabulary].filter(function (part) {
        return !!part;
      }).join('/');
      var parentRefName = value.getIn(['parent', 'refName']);
      var parentType = value.getIn(['parent', 'type']);

      return _react2.default.createElement(
        InputTable,
        null,
        _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
          label: intl.formatMessage(messages.parent),
          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
        })
      );
    }
  }, {
    key: 'renderChildren',
    value: function renderChildren() {
      var _props3 = this.props,
          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;
      }

      var source = [recordType, vocabulary].filter(function (part) {
        return !!part;
      }).join('/');
      var 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.child),
            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
          })
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        null,
        this.renderParent(),
        this.renderChildren()
      );
    }
  }]);

  return BaseTypedHierarchyEditor;
}(_react.Component);

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

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