"use strict";

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

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

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

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

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

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

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

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

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

const {
  CompoundInput
} = _cspaceInput.components;
const propTypes = {
  csid: _propTypes.default.string,
  intl: _reactIntl.intlShape,
  messages: _propTypes.default.object,
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  value: _propTypes.default.instanceOf(_immutable.default.Map),
  readOnly: _propTypes.default.bool,
  isRecordModified: _propTypes.default.bool,
  showParent: _propTypes.default.bool,
  showChildren: _propTypes.default.bool,
  onCommit: _propTypes.default.func,
  onAddChild: _propTypes.default.func,
  onRemoveChild: _propTypes.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;

    if (onAddChild) {
      onAddChild();
    }
  }

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

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

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

    if (onCommit) {
      const index = path[path.length - 1];
      onCommit(['children', index, 'refName'], value);
    }
  }

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

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

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

    if (!showParent) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');
    const parentRefName = value.getIn(['parent', 'refName']);
    return _react.default.createElement(_MiniViewPopupAutocompleteInputContainer.default, {
      label: intl.formatMessage(messages.parent),
      source: source,
      value: parentRefName,
      readOnly: readOnly,
      showQuickAddCloneOption: true,
      quickAddCloneOptionDisabled: isRecordModified,
      onCommit: this.handleParentCommit,
      matchFilter: this.filterMatch
    });
  }

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

    if (!showChildren) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');
    const childRefNames = value.get('children').map(child => child.get('refName'));
    return _react.default.createElement(CompoundInput, {
      label: intl.formatMessage(messages.children),
      value: {
        childRefNames
      },
      readOnly: readOnly
    }, _react.default.createElement(_MiniViewPopupAutocompleteInputContainer.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 _react.default.createElement("div", null, this.renderParent(), this.renderChildren());
  }

}

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

var _default = (0, _reactIntl.injectIntl)(BaseUntypedHierarchyEditor);

exports.default = _default;