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';
const extractTask = (data, label) => {
  const {
    id,
    icon: url
  } = extractTaskType(data) || {};
  const taskType = id === null || id === void 0 ? void 0 : id.split('#').pop();
  const taskIcon = url ? {
    label,
    url
  } : undefined;
  return {
    taskType,
    taskIcon
  };
};
const extractType = jsonLd => {
  const type = jsonLd['@type'];
  return Array.isArray(type) ? type.sort((a, b) => priorityMap[b] - priorityMap[a])[0] : type;
};
const isJiraProvider = provider => provider === JIRA_GENERATOR_ID;
function chooseIcon({
  urlIcon,
  type,
  label,
  data,
  providerIcon
}) {
  var _data$generator;
  const providerId = (_data$generator = data.generator) === null || _data$generator === void 0 ? void 0 : _data$generator['@id'];
  const fileFormat = data === null || data === void 0 ? void 0 : data['schema:fileFormat'];
  const fileFormatIcon = extractFileFormatIcon(fileFormat);
  const documentTypeIcon = typeToIconDescriptor({
    type,
    label,
    providerId,
    data
  }) || extractDocumentTypeIcon(type, label, providerId);
  return prioritiseIcon({
    fileFormatIcon,
    documentTypeIcon,
    urlIcon,
    providerIcon
  });
}
function typeToIconDescriptor({
  type,
  label,
  providerId,
  data
}) {
  switch (type) {
    case 'atlassian:Goal':
      return {
        icon: IconType.Task,
        label
      };
    case 'atlassian:Project':
      return {
        icon: IconType.Project,
        label
      };
    case 'atlassian:SourceCodeCommit':
      return {
        icon: IconType.Commit,
        label
      };
    case 'atlassian:SourceCodePullRequest':
      return {
        icon: IconType.PullRequest,
        label
      };
    case 'atlassian:SourceCodeReference':
      return {
        icon: IconType.Branch,
        label
      };
    case 'atlassian:SourceCodeRepository':
      return {
        icon: IconType.Repo,
        label
      };
    case 'atlassian:Task':
      const taskLabel = label || 'Task';
      const taskIconDescriptor = {
        icon: IconType.Task,
        label: taskLabel
      };
      if (isJiraProvider(providerId)) {
        const {
          taskType,
          taskIcon
        } = extractTask(data);
        return taskType === 'JiraCustomTaskType' ? taskIcon || taskIconDescriptor : extractJiraTaskIcon(taskType, taskLabel);
      }
      return taskIconDescriptor;
    default:
      return undefined;
  }
}

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