'use strict';

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

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;


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,
  onCommit: _propTypes2.default.func,
  onAddChild: _propTypes2.default.func,
  onRemoveChild: _propTypes2.default.func
};

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

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

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

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

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

  _createClass(BaseUntypedHierarchyEditor, [{
    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: 'handleChildCommit',
    value: function handleChildCommit(path, value) {
      var onCommit = this.props.onCommit;


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

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


      if (onCommit) {
        onCommit(['parent'], _immutable2.default.Map({
          csid: csid,
          refName: value
        }));
      }
    }
  }, {
    key: 'renderParent',
    value: function renderParent() {
      var _props = this.props,
          intl = _props.intl,
          messages = _props.messages,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary,
          value = _props.value,
          readOnly = _props.readOnly,
          showParent = _props.showParent;


      if (!showParent) {
        return undefined;
      }

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

      return _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
        label: intl.formatMessage(messages.parent),
        source: source,
        value: parentRefName,
        readOnly: readOnly,
        onCommit: this.handleParentCommit,
        matchFilter: this.filterMatch
      });
    }
  }, {
    key: 'renderChildren',
    value: function renderChildren() {
      var _props2 = this.props,
          intl = _props2.intl,
          messages = _props2.messages,
          recordType = _props2.recordType,
          vocabulary = _props2.vocabulary,
          value = _props2.value,
          readOnly = _props2.readOnly,
          showChildren = _props2.showChildren;


      if (!showChildren) {
        return undefined;
      }

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

      var childRefNames = value.get('children').map(function (child) {
        return child.get('refName');
      });

      return _react2.default.createElement(
        CompoundInput,
        {
          label: intl.formatMessage(messages.children),
          value: { childRefNames: childRefNames },
          readOnly: readOnly
        },
        _react2.default.createElement(_MiniViewPopupAutocompleteInputContainer2.default, {
          name: 'childRefNames',
          repeating: true,
          reorderable: false,
          source: source,
          matchFilter: this.filterMatch,
          onCommit: this.handleChildCommit,
          onAddInstance: this.handleAddChild,
          onRemoveInstance: this.handleRemoveChild
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        null,
        this.renderParent(),
        this.renderChildren()
      );
    }
  }]);

  return BaseUntypedHierarchyEditor;
}(_react.Component);

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

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