"use strict";

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

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

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

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

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

var _get = _interopRequireDefault(require("lodash/get"));

var _RemoveConditionButton = _interopRequireDefault(require("../RemoveConditionButton"));

var _GroupInput = _interopRequireDefault(require("./GroupInput"));

var _GroupConditionInput = _interopRequireDefault(require("../../../../styles/cspace-ui/GroupConditionInput.css"));

var _searchOperators = require("../../../constants/searchOperators");

var _configHelpers = require("../../../helpers/configHelpers");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const propTypes = {
  condition: _propTypes.default.instanceOf(_immutable.default.Map),
  config: _propTypes.default.object,
  hasChildGroups: _propTypes.default.bool,
  inline: _propTypes.default.bool,
  name: _propTypes.default.string,
  readOnly: _propTypes.default.bool,
  recordType: _propTypes.default.string,
  rootPath: _propTypes.default.string,
  getSearchConditionInputComponent: _propTypes.default.func.isRequired,
  buildRecordFieldOptionLists: _propTypes.default.func,
  deleteOptionList: _propTypes.default.func,
  onCommit: _propTypes.default.func,
  onRemove: _propTypes.default.func
};
const messages = {
  groupInput: (0, _reactIntl.defineMessages)({
    pendingLabel: {
      "id": "GroupConditionInput.groupInput.pendingLabel",
      "defaultMessage": "In a single {groupInput} ..."
    },
    selectedLabel: {
      "id": "GroupConditionInput.groupInput.selectedLabel",
      "defaultMessage": "In a single {groupInput} group:"
    },
    compactLabel: {
      "id": "GroupConditionInput.groupInput.compactLabel",
      "defaultMessage": "In {groupInput}:"
    }
  })
};

class GroupConditionInput extends _react.Component {
  constructor() {
    super();
    this.handleChildConditionCommit = this.handleChildConditionCommit.bind(this);
    this.handleRef = this.handleRef.bind(this);
    this.handleRemoveButtonClick = this.handleRemoveButtonClick.bind(this);
    this.handleGroupCommit = this.handleGroupCommit.bind(this);
  }

  componentDidMount() {
    const {
      buildRecordFieldOptionLists,
      condition,
      config,
      recordType
    } = this.props;
    const path = condition.get('path');

    if (path === null) {
      // The condition was just added, and the group needs to be selected. Focus it.
      if (this.domNode) {
        const input = this.domNode.querySelector('input[data-name="group"]');

        if (input) {
          input.focus();
        }
      }
    } else if (buildRecordFieldOptionLists) {
      buildRecordFieldOptionLists(config, recordType, path, true);
    }
  }

  componentDidUpdate(prevProps) {
    const {
      condition,
      buildRecordFieldOptionLists,
      config,
      recordType
    } = this.props;
    const {
      condition: prevCondition
    } = prevProps;
    const path = condition.get('path');
    const prevPath = prevCondition.get('path');

    if (path !== prevPath) {
      // The group changed. Load the field options for the group.
      if (buildRecordFieldOptionLists) {
        buildRecordFieldOptionLists(config, recordType, path, true);
      }
    }
  }

  componentWillUnmount() {
    const {
      deleteOptionList,
      condition,
      recordType
    } = this.props;
    const path = condition.get('path');

    if (deleteOptionList) {
      deleteOptionList((0, _configHelpers.getRecordFieldOptionListName)(recordType, path));
      deleteOptionList((0, _configHelpers.getRecordGroupOptionListName)(recordType, path));
    }
  }

  handleChildConditionCommit(childName, childCondition) {
    const {
      condition,
      name,
      onCommit
    } = this.props;

    if (onCommit) {
      onCommit(name, condition.set('value', childCondition));
    }
  }

  handleGroupCommit(path, groupPath) {
    const {
      condition,
      name,
      onCommit
    } = this.props;

    if (onCommit) {
      // Reset the current value, since child conditions may not be valid for the new field.
      onCommit(name, condition.set('path', groupPath).set('value', _immutable.default.fromJS({
        op: _searchOperators.OP_AND,
        value: [{
          path: null
        }]
      })));
    }
  }

  handleRef(ref) {
    this.domNode = ref;
  }

  handleRemoveButtonClick() {
    const {
      name,
      onRemove
    } = this.props;

    if (onRemove) {
      onRemove(name);
    }
  }

  renderRemoveButton() {
    const {
      readOnly
    } = this.props;

    if (readOnly) {
      return null;
    }

    return _react.default.createElement(_RemoveConditionButton.default, {
      onClick: this.handleRemoveButtonClick
    });
  }

  renderGroupInput() {
    const {
      condition,
      config,
      inline,
      recordType,
      rootPath
    } = this.props;
    const pathSpec = condition.get('path');

    if (!pathSpec) {
      if (inline) {
        return null;
      }

      return _react.default.createElement(_GroupInput.default, {
        config: config,
        inline: inline,
        name: "group",
        placeholder: "group",
        recordType: recordType,
        rootPath: rootPath,
        onCommit: this.handleGroupCommit
      });
    }

    const path = ['document', ...pathSpec.split('/')];
    const fieldDescriptor = (0, _get.default)(config, ['recordTypes', recordType, 'fields', ...path]);
    return _react.default.createElement(_GroupInput.default, {
      config: config,
      inline: inline,
      readOnly: true,
      recordType: recordType,
      rootPath: rootPath,
      value: pathSpec,
      valueDescriptor: fieldDescriptor,
      onCommit: this.handleGroupCommit
    });
  }

  renderHeader() {
    const {
      condition,
      inline
    } = this.props;

    if (inline) {
      const groupInput = this.renderGroupInput();
      return _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.groupInput.compactLabel, {
        tagName: "div",
        values: {
          groupInput
        }
      }));
    }

    const groupInput = this.renderGroupInput();
    const labelMessage = condition.get('path') === null ? messages.groupInput.pendingLabel : messages.groupInput.selectedLabel;
    return _react.default.createElement("header", null, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, labelMessage, {
      tagName: "div",
      values: {
        groupInput
      }
    })), this.renderRemoveButton());
  }

  renderChildConditions() {
    const {
      condition,
      config,
      hasChildGroups,
      inline,
      readOnly,
      recordType,
      getSearchConditionInputComponent
    } = this.props;
    const childCondition = condition.get('value');

    if (!childCondition) {
      return null;
    }

    const path = condition.get('path');
    const SearchConditionInputComponent = getSearchConditionInputComponent(childCondition);
    return _react.default.createElement(SearchConditionInputComponent, {
      condition: childCondition,
      config: config,
      hasChildGroups: hasChildGroups,
      inline: inline,
      name: "groupConditions",
      readOnly: readOnly,
      recordType: recordType,
      rootPath: path,
      showRemoveButton: false,
      getSearchConditionInputComponent: getSearchConditionInputComponent,
      onCommit: this.handleChildConditionCommit
    });
  }

  render() {
    const {
      inline
    } = this.props;
    const className = inline ? _GroupConditionInput.default.inline : _GroupConditionInput.default.normal;
    return _react.default.createElement("div", {
      className: className,
      ref: this.handleRef
    }, this.renderHeader(), inline ? ' ' : null, this.renderChildConditions());
  }

}

exports.default = GroupConditionInput;
GroupConditionInput.propTypes = propTypes;