'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 = undefined;

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

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

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

var _WorkflowStateIcon2 = _interopRequireDefault(_WorkflowStateIcon);

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

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

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

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

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

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

  return date;
};

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

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

  return `[ ${serviceObjectName.toLowerCase()} ]`;
};

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

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

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

  return `[ ${recordServicePath.toLowerCase()} ]`;
};

const formatRefNameAsVocabularyName = exports.formatRefNameAsVocabularyName = (refName, { intl, config }) => {
  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);
    }

    return `[ ${vocabularyShortID.toLowerCase()} ]`;
  }

  return `[ ${recordServicePath.toLowerCase()} ]`;
};

const formatRecordTypeSourceField = exports.formatRecordTypeSourceField = (recordType, sourceField, { intl, config }) => {
  const recordTypeConfig = config.recordTypes[recordType];

  var _sourceField$split = sourceField.split(':'),
      _sourceField$split2 = _slicedToArray(_sourceField$split, 2);

  const partName = _sourceField$split2[0],
        fieldName = _sourceField$split2[1];


  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) : `[ ${fieldName} ]`;
};

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

const formatForeignSourceField = exports.formatForeignSourceField = (sourceField, formatterContext) => {
  const config = formatterContext.config,
        rowData = formatterContext.rowData;


  const serviceObjectName = rowData.get('docType');
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServiceObjectName)(config, serviceObjectName);
  const recordType = recordTypeConfig ? recordTypeConfig.name : null;

  return formatRecordTypeSourceField(recordType, sourceField, formatterContext);
};

const formatWorkflowStateIcon = exports.formatWorkflowStateIcon = workflowState => _react2.default.createElement(_WorkflowStateIcon2.default, { value: workflowState });

const formatOption = exports.formatOption = (optionListName, value, { intl, config }) => {
  const message = (0, _get2.default)(config, ['optionLists', optionListName, 'messages', value]);

  return message ? intl.formatMessage(message) : value;
};