"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BaseFieldInput = BaseFieldInput;
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _get = _interopRequireDefault(require("lodash/get"));

var _dataTypes = require("../../../constants/dataTypes");

var _formatHelpers = require("../../../helpers/formatHelpers");

var _configContextInputs = require("../../../helpers/configContextInputs");

var _configHelpers = require("../../../helpers/configHelpers");

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

const propTypes = {
  config: _propTypes.default.shape({
    locale: _propTypes.default.string
  }).isRequired,
  embedded: _propTypes.default.bool,
  intl: _reactIntl.intlShape.isRequired,
  name: _propTypes.default.string,
  placeholder: _propTypes.default.string,
  readOnly: _propTypes.default.bool,
  recordType: _propTypes.default.string,
  rootPath: _propTypes.default.string,
  value: _propTypes.default.string,
  valueDescriptor: _propTypes.default.shape({
    [_configHelpers.configKey]: _propTypes.default.object
  }),
  onCommit: _propTypes.default.func
};

function BaseFieldInput(props) {
  const {
    config,
    embedded,
    intl,
    name,
    placeholder,
    readOnly,
    recordType,
    rootPath,
    value,
    valueDescriptor,
    onCommit
  } = props;

  if (readOnly) {
    let label;

    if (valueDescriptor) {
      const valueConfig = valueDescriptor[_configHelpers.configKey];
      const extensionParentConfig = (0, _get.default)(valueConfig, 'extensionParentConfig');
      const messages = (0, _get.default)(valueConfig, 'messages');
      const rootPathParts = rootPath ? rootPath.split('/') : [];
      const pathParts = value ? value.split('/') : [];

      if (messages) {
        let message;
        const level = pathParts.length - rootPathParts.length;

        if (level > 1) {
          if (extensionParentConfig && extensionParentConfig.dataType === _dataTypes.DATA_TYPE_STRUCTURED_DATE) {
            // Construct the full label for a field inside a structured date.
            // If the level is 2, the structured date group (this field's parent) is at the top
            // level, so use the groupName message instead of the fullName.
            const messageName = level === 2 ? 'groupName' : 'fullName';
            label = (0, _formatHelpers.formatExtensionFieldName)(intl, valueConfig, messageName);
          } else {
            // Prefer the fullName message.
            message = messages.fullName;
          }
        } else {
          // This is a top-level field in a group. Prefer the groupName message.
          message = messages.groupName;
        }

        if (!label) {
          if (!message) {
            message = messages.name || messages.fullName;
          }

          if (message) {
            label = intl.formatMessage(message);
          }
        }
      }
    }

    if (!label) {
      label = value;
    }

    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("span", null, label));
  }

  const sortComparator = (optionA, optionB) => {
    const labelA = optionA.label;
    const labelB = optionB.label;
    return labelA.localeCompare(labelB, config.locale, {
      sensitivity: 'base'
    });
  };

  return /*#__PURE__*/_react.default.createElement(_configContextInputs.OptionPickerInput, {
    blankable: false,
    embedded: embedded,
    name: name,
    placeholder: placeholder,
    source: (0, _configHelpers.getRecordFieldOptionListName)(recordType, rootPath),
    value: value,
    sortComparator: sortComparator,
    onCommit: onCommit
  });
}

BaseFieldInput.propTypes = propTypes;

var _default = (0, _reactIntl.injectIntl)(BaseFieldInput);

exports.default = _default;