"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 _cspaceInput = require("cspace-input");

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; 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); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const {
  labelable,
  repeatable
} = _cspaceInput.enhancers;
const BaseStructuredDateInput = repeatable(labelable(_cspaceInput.baseComponents.StructuredDateInput));
const fieldMessages = (0, _reactIntl.defineMessages)({
  earliestSingle: {
    "id": "field.structuredDate.earliestSingle",
    "defaultMessage": "Earliest/Single"
  },
  latest: {
    "id": "field.structuredDate.latest",
    "defaultMessage": "Latest"
  },
  datePeriod: {
    "id": "field.structuredDate.datePeriod",
    "defaultMessage": "Period"
  },
  dateAssociation: {
    "id": "field.structuredDate.dateAssociation",
    "defaultMessage": "Association"
  },
  dateNote: {
    "id": "field.structuredDate.dateNote",
    "defaultMessage": "Note"
  },
  dateYear: {
    "id": "field.structuredDate.dateYear",
    "defaultMessage": "Year"
  },
  dateMonth: {
    "id": "field.structuredDate.dateMonth",
    "defaultMessage": "Month"
  },
  dateDay: {
    "id": "field.structuredDate.dateDay",
    "defaultMessage": "Day"
  },
  dateEra: {
    "id": "field.structuredDate.dateEra",
    "defaultMessage": "Era"
  },
  dateCertainty: {
    "id": "field.structuredDate.dateCertainty",
    "defaultMessage": "Certainty"
  },
  dateQualifier: {
    "id": "field.structuredDate.dateQualifier",
    "defaultMessage": "Qualifier"
  },
  dateQualifierValue: {
    "id": "field.structuredDate.dateQualifierValue",
    "defaultMessage": "Value"
  },
  dateQualifierUnit: {
    "id": "field.structuredDate.dateQualifierUnit",
    "defaultMessage": "Unit"
  }
});
const messages = (0, _reactIntl.defineMessages)({
  parseFailed: {
    "id": "structuredDateInput.parseFailed",
    "defaultMessage": "Unrecognized display date format. Try a different format, or enter values in the fields below."
  }
});

const propTypes = _objectSpread({}, BaseStructuredDateInput.propTypes, {
  config: _propTypes.default.object,
  intl: _reactIntl.intlShape,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  readTerms: _propTypes.default.func
});

class StructuredDateInput extends _react.Component {
  constructor() {
    super();
    this.formatFieldLabel = this.formatFieldLabel.bind(this);
    this.formatOptionLabel = this.formatOptionLabel.bind(this);
    this.formatParseFailedMessage = this.formatParseFailedMessage.bind(this);
  }

  componentDidMount() {
    const {
      config,
      readTerms
    } = this.props;

    if (readTerms) {
      config.structDateVocabNames.forEach(vocabName => {
        readTerms(vocabName);
      });
    }
  }

  componentDidUpdate(prevProps) {
    const {
      config,
      perms,
      readTerms
    } = this.props;
    const {
      perms: prevPerms
    } = prevProps;

    if (readTerms && perms !== prevPerms) {
      config.structDateVocabNames.forEach(vocabName => {
        readTerms(vocabName);
      });
    }
  }

  formatFieldLabel(name) {
    const {
      intl
    } = this.props;
    return intl.formatMessage(fieldMessages[name]);
  }

  formatOptionLabel(option) {
    const {
      intl
    } = this.props;
    return option.message ? intl.formatMessage(option.message) : option.value;
  }

  formatParseFailedMessage() {
    const {
      intl
    } = this.props;
    return intl.formatMessage(messages.parseFailed);
  }

  render() {
    const _this$props = this.props,
          {
      /* eslint-disable no-unused-vars */
      config,
      intl,
      perms,
      readTerms
    } = _this$props,
          remainingProps = _objectWithoutProperties(_this$props, ["config", "intl", "perms", "readTerms"]);

    return _react.default.createElement(BaseStructuredDateInput, _extends({
      defaultValue: _immutable.default.Map(),
      formatFieldLabel: this.formatFieldLabel,
      formatOptionLabel: this.formatOptionLabel,
      formatParseFailedMessage: this.formatParseFailedMessage
    }, remainingProps));
  }

}

StructuredDateInput.propTypes = propTypes;
const IntlAwareStructuredDateInput = (0, _reactIntl.injectIntl)(StructuredDateInput);
IntlAwareStructuredDateInput.propTypes = StructuredDateInput.propTypes;
var _default = IntlAwareStructuredDateInput;
exports.default = _default;