'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 }; }

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

var formatDate = exports.formatDate = function formatDate(date) {
  if (!date) {
    return null;
  }

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

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

  return date;
};

var formatServiceObjectName = exports.formatServiceObjectName = function formatServiceObjectName(serviceObjectName, _ref2) {
  var intl = _ref2.intl,
      config = _ref2.config;

  var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServiceObjectName)(config, serviceObjectName);

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

  return '[ ' + serviceObjectName.toLowerCase() + ' ]';
};

var formatRefName = exports.formatRefName = function formatRefName(refName) {
  return (0, _cspaceRefname.getDisplayName)(refName);
};

var formatRefNameAsRecordType = exports.formatRefNameAsRecordType = function formatRefNameAsRecordType(refName, _ref3) {
  var intl = _ref3.intl,
      config = _ref3.config;

  var recordServicePath = (0, _cspaceRefname.getServicePath)(refName);
  var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, recordServicePath);

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

  return '[ ' + recordServicePath.toLowerCase() + ' ]';
};

var formatRefNameAsVocabularyName = exports.formatRefNameAsVocabularyName = function formatRefNameAsVocabularyName(refName, _ref4) {
  var intl = _ref4.intl,
      config = _ref4.config;

  var recordServicePath = (0, _cspaceRefname.getServicePath)(refName);
  var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, recordServicePath);

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

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

    return '[ ' + vocabularyShortID.toLowerCase() + ' ]';
  }

  return '[ ' + recordServicePath.toLowerCase() + ' ]';
};

var formatRecordTypeSourceField = exports.formatRecordTypeSourceField = function formatRecordTypeSourceField(recordType, sourceField, _ref5) {
  var intl = _ref5.intl,
      config = _ref5.config;

  var recordTypeConfig = config.recordTypes[recordType];

  var _sourceField$split = sourceField.split(':'),
      _sourceField$split2 = _slicedToArray(_sourceField$split, 2),
      partName = _sourceField$split2[0],
      fieldName = _sourceField$split2[1];

  var fieldConfig = (0, _configHelpers.findFieldConfigInPart)(recordTypeConfig, partName, fieldName);

  var message = void 0;

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

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

  return message ? intl.formatMessage(message) : '[ ' + fieldName + ' ]';
};

var formatSourceField = exports.formatSourceField = function formatSourceField(sourceField, formatterContext) {
  return formatRecordTypeSourceField(formatterContext.recordType, sourceField, formatterContext);
};

var formatForeignSourceField = exports.formatForeignSourceField = function formatForeignSourceField(sourceField, formatterContext) {
  var config = formatterContext.config,
      rowData = formatterContext.rowData;


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

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

var formatWorkflowStateIcon = exports.formatWorkflowStateIcon = function formatWorkflowStateIcon(workflowState) {
  return _react2.default.createElement(_WorkflowStateIcon2.default, { value: workflowState });
};

var formatOption = exports.formatOption = function formatOption(optionListName, value, _ref6) {
  var intl = _ref6.intl,
      config = _ref6.config;

  var message = (0, _get2.default)(config, ['optionLists', optionListName, 'messages', value]);

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