'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _Field = require('../record/Field');

var _Field2 = _interopRequireDefault(_Field);

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

var _SearchOrderIndicator2 = _interopRequireDefault(_SearchOrderIndicator);

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

function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }

const messages = (0, _reactIntl.defineMessages)({
  or: {
    'id': 'searchField.or',
    'defaultMessage': 'or'
  }
});

const propTypes = {
  parentPath: _propTypes2.default.arrayOf(_propTypes2.default.string),
  name: _propTypes2.default.string,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.instanceOf(_immutable2.default.List)]),
  readOnly: _propTypes2.default.bool,
  repeating: _propTypes2.default.bool,
  onCommit: _propTypes2.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) {
    var _props = this.props;
    const repeating = _props.repeating,
          onCommit = _props.onCommit,
          currentValue = _props.value;


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

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

        var _path = _toArray(path);

        const fieldPath = _path.slice(0);

        const index = fieldPath.pop();

        updatedValue = updatedValue.set(index, value);

        onCommit(fieldPath, updatedValue);
      } else {
        onCommit(path, value);
      }
    }
  }

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


    if (onCommit) {
      let updatedValue = value;

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

      updatedValue = updatedValue.push(null);

      onCommit(path, updatedValue);
    }
  }

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


    if (onCommit) {
      var _path2 = _toArray(path);

      const fieldPath = _path2.slice(0);

      const index = fieldPath.pop();

      onCommit(fieldPath, value.delete(index));
    }
  }

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


    if (readOnly) {
      return orderNumber > 1 ? _react2.default.createElement(
        'div',
        null,
        '/'
      ) : null;
    }

    return _react2.default.createElement(
      'div',
      { className: _SearchOrderIndicator2.default.common },
      _react2.default.createElement(
        'div',
        null,
        orderNumber > 1 ? _react2.default.createElement(_reactIntl.FormattedMessage, messages.or) : null
      )
    );
  }

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


    const repeatingProps = {};

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

    return _react2.default.createElement(_Field2.default, _extends({
      label: undefined,
      parentPath: parentPath,
      name: name,
      asText: readOnly,
      value: value,
      viewType: 'search',
      onCommit: this.handleCommit
      // 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;