"use strict";

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

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

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

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

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

var _Field = _interopRequireDefault(require("../record/Field"));

var _SearchOrderIndicator = _interopRequireDefault(require("../../../styles/cspace-ui/SearchOrderIndicator.css"));

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 messages = (0, _reactIntl.defineMessages)({
  or: {
    "id": "searchField.or",
    "defaultMessage": "or"
  }
});
const propTypes = {
  parentPath: _propTypes.default.arrayOf(_propTypes.default.string),
  name: _propTypes.default.string,
  value: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.instanceOf(_immutable.default.List)]),
  readOnly: _propTypes.default.bool,
  repeating: _propTypes.default.bool,
  onCommit: _propTypes.default.func
};
const defaultProps = {
  repeating: true
};

class SearchField extends _react.Component {
  constructor() {
    super();
    this.handleCommit = this.handleCommit.bind(this);
    this.handleAddInstance = this.handleAddInstance.bind(this);
    this.handleRemoveInstance = this.handleRemoveInstance.bind(this);
    this.renderOrderIndicator = this.renderOrderIndicator.bind(this);
  }

  handleCommit(path, value) {
    const {
      repeating,
      onCommit,
      value: currentValue
    } = this.props;

    if (onCommit) {
      if (repeating) {
        let updatedValue = currentValue;

        if (!_immutable.default.List.isList(updatedValue)) {
          updatedValue = _immutable.default.List.of(updatedValue);
        }

        const [...fieldPath] = path;
        const index = fieldPath.pop();
        updatedValue = updatedValue.set(index, value);
        onCommit(fieldPath, updatedValue);
      } else if (typeof value === 'boolean') {
        let stringValue;

        if (value === true) {
          stringValue = 'true';
        } else if (value === false) {
          stringValue = 'false';
        }

        onCommit(path, stringValue);
      } else {
        onCommit(path, value);
      }
    }
  }

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

    if (onCommit) {
      let updatedValue = value;

      if (!_immutable.default.List.isList(updatedValue)) {
        updatedValue = _immutable.default.List.of(updatedValue);
      }

      updatedValue = updatedValue.push(null);
      onCommit(path, updatedValue);
    }
  }

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

    if (onCommit) {
      const [...fieldPath] = path;
      const index = fieldPath.pop();
      onCommit(fieldPath, value.delete(index));
    }
  }

  renderOrderIndicator(orderNumber) {
    const {
      readOnly
    } = this.props;

    if (readOnly) {
      return orderNumber > 1 ? _react.default.createElement("div", null, ", ") : null;
    }

    return _react.default.createElement("div", {
      className: _SearchOrderIndicator.default.common
    }, _react.default.createElement("div", null, orderNumber > 1 ? _react.default.createElement(_reactIntl.FormattedMessage, messages.or) : null));
  }

  render() {
    const {
      parentPath,
      name,
      value,
      readOnly,
      repeating
    } = this.props;
    const repeatingProps = {};

    if (repeating) {
      repeatingProps.repeating = true;
      repeatingProps.reorderable = false;
      repeatingProps.renderOrderIndicator = this.renderOrderIndicator;
      repeatingProps.onAddInstance = this.handleAddInstance;
      repeatingProps.onRemoveInstance = this.handleRemoveInstance;
    }

    return _react.default.createElement(_Field.default, _extends({
      label: undefined,
      parentPath: parentPath,
      name: name,
      asText: readOnly,
      value: value,
      viewType: "search",
      onCommit: this.handleCommit // Do not allow multiline text fields.
      ,
      multiline: false // Do not disable options in menus.
      ,
      ignoreDisabledOptions: true // Do not show quick add on autocomplete inputs.
      ,
      showQuickAdd: false
    }, repeatingProps));
  }

}

exports.default = SearchField;
SearchField.propTypes = propTypes;
SearchField.defaultProps = defaultProps;