"use strict";

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

var _OptionPickerInputContainer = _interopRequireDefault(require("../../containers/record/OptionPickerInputContainer"));

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,
  InputTable
} = _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,
  parentTypeOptionListName: _propTypes.default.string,
  childTypeOptionListName: _propTypes.default.string,
  onCommit: _propTypes.default.func,
  onAddChild: _propTypes.default.func,
  onRemoveChild: _propTypes.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;

    if (onAddChild) {
      onAddChild();
    }
  }

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

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

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

    if (onCommit) {
      const index = path[0];
      onCommit(['children', index, 'refName'], value);
    }
  }

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

    if (onCommit) {
      const index = path[0];
      onCommit(['children', index, 'type'], value);
    }
  }

  handleParentRefNameCommit(path, value, csid) {
    const {
      value: hierarchy,
      onCommit
    } = this.props;
    const parent = hierarchy.get('parent');

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

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

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

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

    if (!showParent) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');
    const parentRefName = value.getIn(['parent', 'refName']);
    const parentType = value.getIn(['parent', 'type']);
    return _react.default.createElement(InputTable, {
      label: intl.formatMessage(messages.parent)
    }, _react.default.createElement(_MiniViewPopupAutocompleteInputContainer.default, {
      label: intl.formatMessage(messages.parentName),
      name: "parentRefName",
      source: source,
      value: parentRefName,
      readOnly: readOnly,
      showQuickAddCloneOption: true,
      quickAddCloneOptionDisabled: isRecordModified,
      onCommit: this.handleParentRefNameCommit,
      matchFilter: this.filterMatch
    }), _react.default.createElement(_OptionPickerInputContainer.default, {
      label: intl.formatMessage(messages.parentType),
      name: "parentType",
      source: parentTypeOptionListName,
      value: parentType,
      readOnly: readOnly,
      onCommit: this.handleParentTypeCommit
    }));
  }

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

    if (!showChildren) {
      return undefined;
    }

    const source = [recordType, vocabulary].join('/');
    const children = value.get('children');
    return _react.default.createElement(CompoundInput, {
      label: intl.formatMessage(messages.children),
      value: children,
      readOnly: readOnly
    }, _react.default.createElement(CompoundInput, {
      tabular: true,
      repeating: true,
      reorderable: false,
      onAddInstance: this.handleAddChild,
      onRemoveInstance: this.handleRemoveChild
    }, _react.default.createElement(_MiniViewPopupAutocompleteInputContainer.default, {
      label: intl.formatMessage(messages.childName),
      name: "refName",
      source: source,
      showQuickAddCloneOption: true,
      quickAddCloneOptionDisabled: isRecordModified,
      matchFilter: this.filterMatch,
      onCommit: this.handleChildRefNameCommit
    }), _react.default.createElement(_OptionPickerInputContainer.default, {
      label: intl.formatMessage(messages.childType),
      name: "type",
      source: childTypeOptionListName,
      onCommit: this.handleChildTypeCommit
    })));
  }

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

}

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

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

exports.default = _default;