import { extractTitle } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
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 extractTaskNew = function extractTaskNew(data) {
  var taskTypeInfo = extractTaskType(data) || {};
  var id = taskTypeInfo.id,
    url = taskTypeInfo.icon,
    name = taskTypeInfo.name;
  var taskType = id === null || id === void 0 ? void 0 : id.split('#').pop();
  var taskIconLabel = (name === null || name === void 0 ? void 0 : name.trim()) || 'Task';
  var taskIcon = url ? {
    label: taskIconLabel,
    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);
  if (fg('platform_navx_smart_link_icon_label_a11y')) {
    var iconDescriptor = typeToIconDescriptor({
      type: type,
      providerId: providerId,
      data: data
    });
    var documentTitle = fg('platform_navx_smart_link_icon_label_a11y') ? undefined : extractTitle(data);
    var extractedDocumentTypeIcon = extractDocumentTypeIcon(type, documentTitle, providerId);
    var documentTypeIcon = iconDescriptor || extractedDocumentTypeIcon;
    var urlIconNew = extractUrlIcon(data.icon, documentTypeIcon === null || documentTypeIcon === void 0 ? void 0 : documentTypeIcon.label);
    return prioritiseIcon({
      fileFormatIcon: fileFormatIcon,
      documentTypeIcon: documentTypeIcon,
      urlIcon: urlIconNew,
      providerIcon: providerIcon
    });
  } else {
    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;
  var descriptorLabel = function descriptorLabel(semantic) {
    return fg('platform_navx_smart_link_icon_label_a11y') ? semantic : label;
  };
  switch (type) {
    case 'atlassian:Goal':
      return {
        icon: IconType.Task,
        label: descriptorLabel('goal')
      };
    case 'atlassian:Project':
      return {
        // FIXME: atlassian:Project seem to be returned for many things, including Confluence space or Trello board,
        // But `IconType.Project` actual value is `BitBucket:Project`!
        icon: IconType.Project,
        label: descriptorLabel('project')
      };
    case 'atlassian:SourceCodeCommit':
      return {
        icon: IconType.Commit,
        label: descriptorLabel('commit')
      };
    case 'atlassian:SourceCodePullRequest':
      return {
        icon: IconType.PullRequest,
        label: descriptorLabel('pull request')
      };
    case 'atlassian:SourceCodeReference':
      return {
        icon: IconType.Branch,
        label: descriptorLabel('branch')
      };
    case 'atlassian:SourceCodeRepository':
      return {
        icon: IconType.Repo,
        label: descriptorLabel('repository')
      };
    case 'atlassian:Task':
      if (fg('platform_navx_smart_link_icon_label_a11y')) {
        var taskIconDescriptor = {
          icon: IconType.Task,
          label: descriptorLabel('task')
        };
        if (isJiraProvider(providerId)) {
          var _extractTaskNew = extractTaskNew(data),
            taskType = _extractTaskNew.taskType,
            taskIcon = _extractTaskNew.taskIcon;
          return taskType === 'JiraCustomTaskType' ? taskIcon || taskIconDescriptor : extractJiraTaskIcon(taskType);
        }
        return taskIconDescriptor;
      } else {
        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) {
  if (fg('platform_navx_smart_link_icon_label_a11y')) {
    var type = extractType(data);
    var providerIcon = extractProviderIcon(data);
    return chooseIcon({
      type: type,
      data: data,
      providerIcon: providerIcon
    });
  } else {
    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;