"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _linkExtractors = require("@atlaskit/link-extractors");
var _constants = require("../../../constants");
var _icon = require("../../common/icon");
var _priority = require("../../common/icon/priority");
var _lozenge = require("../../common/lozenge");
var _constants2 = require("../../constants");
var _extractDocumentTypeIcon = _interopRequireDefault(require("./extract-document-type-icon"));
var _extractFileFormatIcon = _interopRequireDefault(require("./extract-file-formatIcon"));
var _extractJiraTaskIcon = _interopRequireDefault(require("./extract-jira-task-icon"));
var _extractProviderIcon = _interopRequireDefault(require("./extract-provider-icon"));
var _extractUrlIcon = _interopRequireDefault(require("./extract-url-icon"));
var extractTask = function extractTask(data, label) {
  var _ref = (0, _lozenge.extractTaskType)(data) || {},
    id = _ref.id,
    url = _ref.icon;
  var taskType = id === null || id === void 0 ? void 0 : id.split('#').pop();
  var taskIcon = url ? {
    label: label,
    url: url
  } : undefined;
  return {
    taskType: taskType,
    taskIcon: taskIcon
  };
};
var extractType = function extractType(jsonLd) {
  var type = jsonLd['@type'];
  return Array.isArray(type) ? type.sort(function (a, b) {
    return _priority.extractorPriorityMap[b] - _priority.extractorPriorityMap[a];
  })[0] : type;
};
var isJiraProvider = function isJiraProvider(provider) {
  return provider === _constants2.JIRA_GENERATOR_ID;
};
function chooseIcon(_ref2) {
  var _data$generator;
  var urlIcon = _ref2.urlIcon,
    type = _ref2.type,
    label = _ref2.label,
    data = _ref2.data,
    providerIcon = _ref2.providerIcon;
  var providerId = (_data$generator = data.generator) === null || _data$generator === void 0 ? void 0 : _data$generator['@id'];
  var fileFormat = data === null || data === void 0 ? void 0 : data['schema:fileFormat'];
  var fileFormatIcon = (0, _extractFileFormatIcon.default)(fileFormat);
  var documentTypeIcon = typeToIconDescriptor({
    type: type,
    label: label,
    providerId: providerId,
    data: data
  }) || (0, _extractDocumentTypeIcon.default)(type, label, providerId);
  return (0, _icon.prioritiseIcon)({
    fileFormatIcon: fileFormatIcon,
    documentTypeIcon: documentTypeIcon,
    urlIcon: urlIcon,
    providerIcon: providerIcon
  });
}
function typeToIconDescriptor(_ref3) {
  var type = _ref3.type,
    label = _ref3.label,
    providerId = _ref3.providerId,
    data = _ref3.data;
  switch (type) {
    case 'atlassian:Goal':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.Task,
        label: label || 'Goal'
      };
    case 'atlassian:Project':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.Project,
        label: label || 'Project'
      };
    case 'atlassian:SourceCodeCommit':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.Commit,
        label: label || 'Commit'
      };
    case 'atlassian:SourceCodePullRequest':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.PullRequest,
        label: label || 'Pull request'
      };
    case 'atlassian:SourceCodeReference':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.Branch,
        label: label || 'Reference'
      };
    case 'atlassian:SourceCodeRepository':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: _constants.IconType.Repo,
        label: label || 'Repository'
      };
    case 'atlassian:Task':
      var taskLabel = label || 'Task';
      var taskIconDescriptor = {
        icon: _constants.IconType.Task,
        label: taskLabel
      };
      if (isJiraProvider(providerId)) {
        var _extractTask = extractTask(data),
          taskType = _extractTask.taskType,
          taskIcon = _extractTask.taskIcon;
        return taskType === 'JiraCustomTaskType' ? taskIcon || taskIconDescriptor : (0, _extractJiraTaskIcon.default)(taskType, taskLabel);
      }
      return taskIconDescriptor;
    default:
      return undefined;
  }
}

/**
 * Return the icon object given a JSON-LD data object.
 */
var extractJsonldDataIcon = function extractJsonldDataIcon(data) {
  var label = (0, _linkExtractors.extractTitle)(data);
  var type = extractType(data);
  var urlIcon = (0, _extractUrlIcon.default)(data.icon, label);
  var providerIcon = (0, _extractProviderIcon.default)(data);
  return chooseIcon({
    urlIcon: urlIcon,
    providerIcon: providerIcon,
    type: type,
    label: label,
    data: data
  });
};
var _default = exports.default = extractJsonldDataIcon;