'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.formatForeignSourceField = exports.formatSourceField = exports.formatRecordTypeSourceField = exports.formatRefNameAsVocabularyName = exports.formatRefNameAsRecordType = exports.formatServiceObjectName = exports.formatTimestamp = undefined;

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

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

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

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

  // FIXME: This should use a full name message, distinct from the label used in the record editor,
  // because the record editor label may not have enough context. This requires refactoring of
  // messages and reworking how record editor fields are labeled.

  // FIXME: This should take the part name into account, since a field name is unique only within a
  // part. This requires refactoring of messages.

  var fieldName = sourceField.split(':')[1]; // partName:fieldName
  var recordTypeConfig = config.recordTypes[recordType];
  var message = recordTypeConfig.messages.field[fieldName];

  if (message) {
    return intl.formatMessage(message);
  }

  return '[ ' + 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);
};