'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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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

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

var defaultProps = {
  repeating: true
};

var SearchField = function (_Component) {
  _inherits(SearchField, _Component);

  function SearchField() {
    _classCallCheck(this, SearchField);

    var _this = _possibleConstructorReturn(this, (SearchField.__proto__ || Object.getPrototypeOf(SearchField)).call(this));

    _this.handleCommit = _this.handleCommit.bind(_this);
    _this.handleAddInstance = _this.handleAddInstance.bind(_this);
    _this.handleRemoveInstance = _this.handleRemoveInstance.bind(_this);
    _this.renderOrderIndicator = _this.renderOrderIndicator.bind(_this);
    return _this;
  }

  _createClass(SearchField, [{
    key: 'handleCommit',
    value: function handleCommit(path, value) {
      var _props = this.props,
          repeating = _props.repeating,
          onCommit = _props.onCommit,
          currentValue = _props.value;


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

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

          var _path = _toArray(path),
              fieldPath = _path.slice(0);

          var index = fieldPath.pop();

          updatedValue = updatedValue.set(index, value);

          onCommit(fieldPath, updatedValue);
        } else {
          onCommit(path, value);
        }
      }
    }
  }, {
    key: 'handleAddInstance',
    value: function handleAddInstance(path) {
      var _props2 = this.props,
          value = _props2.value,
          onCommit = _props2.onCommit;


      if (onCommit) {
        var updatedValue = value;

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

        updatedValue = updatedValue.push(null);

        onCommit(path, updatedValue);
      }
    }
  }, {
    key: 'handleRemoveInstance',
    value: function handleRemoveInstance(path) {
      var _props3 = this.props,
          value = _props3.value,
          onCommit = _props3.onCommit;


      if (onCommit) {
        var _path2 = _toArray(path),
            fieldPath = _path2.slice(0);

        var index = fieldPath.pop();

        onCommit(fieldPath, value.delete(index));
      }
    }
  }, {
    key: 'renderOrderIndicator',
    value: function renderOrderIndicator(orderNumber) {
      var 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
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props4 = this.props,
          parentPath = _props4.parentPath,
          name = _props4.name,
          value = _props4.value,
          readOnly = _props4.readOnly,
          repeating = _props4.repeating;


      var 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));
    }
  }]);

  return SearchField;
}(_react.Component);

exports.default = SearchField;


SearchField.propTypes = propTypes;
SearchField.defaultProps = defaultProps;