"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 _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { 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 {
  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 = { // eslint-disable-next-line react/forbid-foreign-prop-types
  ...BaseStructuredDateInput.propTypes,
  config: _propTypes.default.shape({
    structDateVocabNames: _propTypes.default.arrayOf(_propTypes.default.string)
  }),
  intl: _reactIntl.intlShape,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  readTerms: _propTypes.default.func,
  structDateVocabNames: _propTypes.default.arrayOf(_propTypes.default.string)
};

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,
      structDateVocabNames: propStructDateVocabNames
    } = this.props;

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

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

    if (readTerms && perms !== prevPerms) {
      const structDateVocabNames = propStructDateVocabNames || config.structDateVocabNames;
      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 {
      config,
      intl,
      perms,
      readTerms,
      structDateVocabNames,
      ...remainingProps
    } = this.props;
    return /*#__PURE__*/_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;