"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.formatOption = exports.formatWorkflowStateIcon = exports.formatForeignSourceField = exports.formatSourceField = exports.formatRecordTypeSourceField = exports.formatRefNameAsVocabularyName = exports.formatRefNameAsRecordType = exports.formatRefName = exports.formatServiceObjectName = exports.formatDate = exports.formatTimestamp = void 0;

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

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

var _cspaceRefname = require("cspace-refname");

var _WorkflowStateIcon = _interopRequireDefault(require("../components/record/WorkflowStateIcon"));

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

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

const formatTimestamp = (timestamp, _ref) => {
  let {
    intl
  } = _ref;
  return intl.formatDate(timestamp, {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric' // timeZoneName: 'short',

  });
};

exports.formatTimestamp = formatTimestamp;

const formatDate = date => {
  if (!date) {
    return null;
  }

  const index = date.indexOf('T');

  if (index >= 0) {
    return date.substring(0, index);
  }

  return date;
};

exports.formatDate = formatDate;

const formatServiceObjectName = (serviceObjectName, _ref2) => {
  let {
    intl,
    config
  } = _ref2;
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServiceObjectName)(config, serviceObjectName);

  if (recordTypeConfig) {
    return intl.formatMessage(recordTypeConfig.messages.record.name);
  }

  return "[ ".concat(serviceObjectName.toLowerCase(), " ]");
};

exports.formatServiceObjectName = formatServiceObjectName;

const formatRefName = refName => (0, _cspaceRefname.getDisplayName)(refName);

exports.formatRefName = formatRefName;

const formatRefNameAsRecordType = (refName, _ref3) => {
  let {
    intl,
    config
  } = _ref3;
  const recordServicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, recordServicePath);

  if (recordTypeConfig) {
    return intl.formatMessage(recordTypeConfig.messages.record.name);
  }

  return "[ ".concat(recordServicePath.toLowerCase(), " ]");
};

exports.formatRefNameAsRecordType = formatRefNameAsRecordType;

const formatRefNameAsVocabularyName = (refName, _ref4) => {
  let {
    intl,
    config
  } = _ref4;
  const recordServicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, recordServicePath);

  if (recordTypeConfig) {
    const vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);
    const vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

    if (vocabularyConfig) {
      return intl.formatMessage(vocabularyConfig.messages.name);
    }

    const fallback = vocabularyShortID ? vocabularyShortID.toLowerCase() : '';
    return "[ ".concat(fallback, " ]");
  }

  return "[ ".concat(recordServicePath.toLowerCase(), " ]");
};

exports.formatRefNameAsVocabularyName = formatRefNameAsVocabularyName;

const formatRecordTypeSourceField = (recordType, sourceField, _ref5) => {
  let {
    intl,
    config
  } = _ref5;
  const recordTypeConfig = config.recordTypes[recordType];
  const [partName, fieldName] = sourceField.split(':');
  const fieldConfig = (0, _configHelpers.findFieldConfigInPart)(recordTypeConfig, partName, fieldName);
  let message;

  if (fieldConfig) {
    const messages = fieldConfig.messages;

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

  return message ? intl.formatMessage(message) : "[ ".concat(fieldName, " ]");
};

exports.formatRecordTypeSourceField = formatRecordTypeSourceField;

const formatSourceField = (sourceField, formatterContext) => formatRecordTypeSourceField(formatterContext.recordType, sourceField, formatterContext);

exports.formatSourceField = formatSourceField;

const formatForeignSourceField = (sourceField, formatterContext) => {
  const {
    config,
    rowData
  } = formatterContext;
  const serviceObjectName = rowData.get('docType');
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServiceObjectName)(config, serviceObjectName);
  const recordType = recordTypeConfig ? recordTypeConfig.name : null;
  return formatRecordTypeSourceField(recordType, sourceField, formatterContext);
};

exports.formatForeignSourceField = formatForeignSourceField;

const formatWorkflowStateIcon = workflowState => _react.default.createElement(_WorkflowStateIcon.default, {
  value: workflowState
});

exports.formatWorkflowStateIcon = formatWorkflowStateIcon;

const formatOption = (optionListName, value, _ref6) => {
  let {
    intl,
    config
  } = _ref6;
  const message = (0, _get.default)(config, ['optionLists', optionListName, 'messages', value]);
  return message ? intl.formatMessage(message) : value;
};

exports.formatOption = formatOption;