"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSearchConditionInputComponent = 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 _PanelContainer = require("../../containers/layout/PanelContainer");
var _searchOperators = require("../../constants/searchOperators");
var _GroupConditionInputContainer = _interopRequireDefault(require("../../containers/search/input/GroupConditionInputContainer"));
var _BooleanConditionInput = _interopRequireDefault(require("./input/BooleanConditionInput"));
var _FieldConditionInput = _interopRequireDefault(require("./input/FieldConditionInput"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
const propTypes = {
  condition: _propTypes.default.instanceOf(_immutable.default.Map),
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  hasChildGroups: _propTypes.default.bool,
  inline: _propTypes.default.bool,
  preferredBooleanOp: _propTypes.default.string,
  preferredCondition: _propTypes.default.instanceOf(_immutable.default.Map),
  readOnly: _propTypes.default.bool,
  recordType: _propTypes.default.string,
  onConditionCommit: _propTypes.default.func
};
const defaultProps = {
  preferredBooleanOp: 'or'
};
const childContextTypes = {
  recordType: _propTypes.default.string
};
const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "advancedSearchBuilder.title",
    "defaultMessage": "Advanced Search"
  }
});
const ensureRootBooleanOp = (condition, preferredBooleanOp) => {
  const op = condition && condition.get('op');
  if (op === _searchOperators.OP_AND || op === _searchOperators.OP_OR) {
    return condition;
  }
  return _immutable.default.Map().set('op', preferredBooleanOp).set('value', condition ? _immutable.default.List.of(condition) : _immutable.default.List());
};
const getSearchConditionInputComponent = condition => {
  const operator = condition.get('op');
  if (operator === _searchOperators.OP_AND || operator === _searchOperators.OP_OR) {
    return _BooleanConditionInput.default;
  }
  if (operator === _searchOperators.OP_GROUP) {
    return _GroupConditionInputContainer.default;
  }
  return _FieldConditionInput.default;
};
exports.getSearchConditionInputComponent = getSearchConditionInputComponent;
class AdvancedSearchBuilder extends _react.Component {
  constructor() {
    super();
    this.handleConditionCommit = this.handleConditionCommit.bind(this);
  }
  getChildContext() {
    const {
      recordType
    } = this.props;
    return {
      recordType
    };
  }
  componentDidMount() {
    this.normalizeCondition();
  }
  componentDidUpdate() {
    this.normalizeCondition();
  }
  handleConditionCommit(name, condition) {
    const {
      onConditionCommit
    } = this.props;
    if (onConditionCommit) {
      onConditionCommit(condition);
    }
  }
  normalizeCondition() {
    const {
      condition,
      config,
      preferredBooleanOp,
      preferredCondition,
      recordType,
      onConditionCommit
    } = this.props;
    if (recordType && onConditionCommit) {
      let normalizedCondition;
      if (condition) {
        normalizedCondition = ensureRootBooleanOp(condition, preferredBooleanOp);
      } else {
        let initialCondition = preferredCondition;
        if (!initialCondition) {
          initialCondition = _immutable.default.fromJS((0, _get.default)(config, ['recordTypes', recordType, 'advancedSearch']));
        }
        normalizedCondition = ensureRootBooleanOp(initialCondition, preferredBooleanOp);
        if (normalizedCondition.get('op') !== preferredBooleanOp) {
          normalizedCondition = normalizedCondition.set('op', preferredBooleanOp);
        }
      }
      if (normalizedCondition !== condition) {
        onConditionCommit(normalizedCondition);
      }
    }
  }
  render() {
    const {
      condition,
      config,
      hasChildGroups,
      inline,
      readOnly,
      recordType
    } = this.props;
    if (!condition) {
      return null;
    }
    const SearchConditionInputComponent = getSearchConditionInputComponent(condition);
    const searchConditionInput = /*#__PURE__*/_react.default.createElement(SearchConditionInputComponent, {
      condition: condition,
      config: config,
      hasChildGroups: hasChildGroups,
      inline: inline,
      name: "advancedSearch",
      readOnly: readOnly,
      recordType: recordType,
      showInlineParens: false,
      showRemoveButton: false,
      getSearchConditionInputComponent: getSearchConditionInputComponent,
      onCommit: this.handleConditionCommit
    });
    if (inline) {
      return searchConditionInput;
    }
    const panelHeader = /*#__PURE__*/_react.default.createElement("h3", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title));
    return /*#__PURE__*/_react.default.createElement(_PanelContainer.ConnectedPanel, {
      collapsible: true,
      header: panelHeader,
      name: "advancedSearch",
      recordType: recordType
    }, searchConditionInput);
  }
}
exports.default = AdvancedSearchBuilder;
AdvancedSearchBuilder.propTypes = propTypes;
AdvancedSearchBuilder.defaultProps = defaultProps;
AdvancedSearchBuilder.childContextTypes = childContextTypes;