"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 _SearchField = _interopRequireDefault(require("./SearchField"));

var _RangeSearchField = _interopRequireDefault(require("../../../styles/cspace-ui/RangeSearchField.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)({
  fields: {
    "id": "RangeSearchField.fields",
    "defaultMessage": "{startField} and {endField}"
  }
});
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,
  inline: _propTypes.default.bool,
  onCommit: _propTypes.default.func
};
const defaultProps = {
  value: _immutable.default.List()
};

class RangeSearchField extends _react.Component {
  constructor() {
    super();
    this.handleStartFieldCommit = this.handleStartFieldCommit.bind(this);
    this.handleEndFieldCommit = this.handleEndFieldCommit.bind(this);
  }

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

    if (onCommit) {
      const nextValue = _immutable.default.List.isList(value) ? value.set(0, startValue) : _immutable.default.List.of(startValue);
      onCommit(path, nextValue);
    }
  }

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

    if (onCommit) {
      const nextValue = _immutable.default.List.isList(value) ? value.set(1, endValue) : _immutable.default.List.of(value).set(1, endValue);
      onCommit(path, nextValue);
    }
  }

  render() {
    const {
      inline,
      parentPath,
      name,
      value,
      readOnly
    } = this.props;
    const normalizedValue = _immutable.default.List.isList(value) ? value : _immutable.default.List.of(value);

    const startField = _react.default.createElement(_SearchField.default, {
      parentPath: parentPath,
      name: name,
      value: normalizedValue.get(0),
      readOnly: readOnly,
      repeating: false,
      onCommit: this.handleStartFieldCommit
    });

    const endField = _react.default.createElement(_SearchField.default, {
      parentPath: parentPath,
      name: name,
      value: normalizedValue.get(1),
      readOnly: readOnly,
      repeating: false,
      onCommit: this.handleEndFieldCommit
    });

    const className = inline ? _RangeSearchField.default.inline : _RangeSearchField.default.normal;
    return _react.default.createElement("div", {
      className: className
    }, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.fields, {
      tagName: "div",
      values: {
        startField,
        endField
      }
    })));
  }

}

exports.default = RangeSearchField;
RangeSearchField.propTypes = propTypes;
RangeSearchField.defaultProps = defaultProps;