import { extractTitle } from '@atlaskit/link-extractors';
import { IconType } from '../../../constants';
import { prioritiseIcon } from '../../common/icon';
import { extractorPriorityMap as priorityMap } from '../../common/icon/priority';
import { extractTaskType } from '../../common/lozenge';
import { JIRA_GENERATOR_ID } from '../../constants';
import extractDocumentTypeIcon from './extract-document-type-icon';
import extractFileFormatIcon from './extract-file-formatIcon';
import extractJiraTaskIcon from './extract-jira-task-icon';
import extractProviderIcon from './extract-provider-icon';
import extractUrlIcon from './extract-url-icon';
var extractTask = function extractTask(data, label) {
  var _ref = 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 priorityMap[b] - priorityMap[a];
  })[0] : type;
};
var isJiraProvider = function isJiraProvider(provider) {
  return provider === 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 = extractFileFormatIcon(fileFormat);
  var documentTypeIcon = typeToIconDescriptor({
    type: type,
    label: label,
    providerId: providerId,
    data: data
  }) || extractDocumentTypeIcon(type, label, providerId);
  return 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: IconType.Task,
        label: label || 'Goal'
      };
    case 'atlassian:Project':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: IconType.Project,
        label: label || 'Project'
      };
    case 'atlassian:SourceCodeCommit':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: IconType.Commit,
        label: label || 'Commit'
      };
    case 'atlassian:SourceCodePullRequest':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: IconType.PullRequest,
        label: label || 'Pull request'
      };
    case 'atlassian:SourceCodeReference':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: IconType.Branch,
        label: label || 'Reference'
      };
    case 'atlassian:SourceCodeRepository':
      // eslint-disable-next-line @atlassian/i18n/no-literal-string-in-object
      return {
        icon: IconType.Repo,
        label: label || 'Repository'
      };
    case 'atlassian:Task':
      var taskLabel = label || 'Task';
      var taskIconDescriptor = {
        icon: IconType.Task,
        label: taskLabel
      };
      if (isJiraProvider(providerId)) {
        var _extractTask = extractTask(data),
          taskType = _extractTask.taskType,
          taskIcon = _extractTask.taskIcon;
        return taskType === 'JiraCustomTaskType' ? taskIcon || taskIconDescriptor : extractJiraTaskIcon(taskType, taskLabel);
      }
      return taskIconDescriptor;
    default:
      return undefined;
  }
}

/**
 * Return the icon object given a JSON-LD data object.
 */
var extractJsonldDataIcon = function extractJsonldDataIcon(data) {
  var label = extractTitle(data);
  var type = extractType(data);
  var urlIcon = extractUrlIcon(data.icon, label);
  var providerIcon = extractProviderIcon(data);
  return chooseIcon({
    urlIcon: urlIcon,
    providerIcon: providerIcon,
    type: type,
    label: label,
    data: data
  });
};
export default extractJsonldDataIcon;