"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 _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
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 extractTaskNew = function extractTaskNew(data) {
  var taskTypeInfo = (0, _lozenge.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 _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);
  if ((0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y')) {
    var iconDescriptor = typeToIconDescriptor({
      type: type,
      providerId: providerId,
      data: data
    });
    var documentTitle = (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? undefined : (0, _linkExtractors.extractTitle)(data);
    var extractedDocumentTypeIcon = (0, _extractDocumentTypeIcon.default)(type, documentTitle, providerId);
    var documentTypeIcon = iconDescriptor || extractedDocumentTypeIcon;
    var urlIconNew = (0, _extractUrlIcon.default)(data.icon, documentTypeIcon === null || documentTypeIcon === void 0 ? void 0 : documentTypeIcon.label);
    return (0, _icon.prioritiseIcon)({
      fileFormatIcon: fileFormatIcon,
      documentTypeIcon: documentTypeIcon,
      urlIcon: urlIconNew,
      providerIcon: providerIcon
    });
  } else {
    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;
  var descriptorLabel = function descriptorLabel(semantic) {
    return (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? semantic : label;
  };
  switch (type) {
    case 'atlassian:Goal':
      return {
        icon: _constants.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: _constants.IconType.Project,
        label: descriptorLabel('project')
      };
    case 'atlassian:SourceCodeCommit':
      return {
        icon: _constants.IconType.Commit,
        label: descriptorLabel('commit')
      };
    case 'atlassian:SourceCodePullRequest':
      return {
        icon: _constants.IconType.PullRequest,
        label: descriptorLabel('pull request')
      };
    case 'atlassian:SourceCodeReference':
      return {
        icon: _constants.IconType.Branch,
        label: descriptorLabel('branch')
      };
    case 'atlassian:SourceCodeRepository':
      return {
        icon: _constants.IconType.Repo,
        label: descriptorLabel('repository')
      };
    case 'atlassian:Task':
      if ((0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y')) {
        var taskIconDescriptor = {
          icon: _constants.IconType.Task,
          label: descriptorLabel('task')
        };
        if (isJiraProvider(providerId)) {
          var _extractTaskNew = extractTaskNew(data),
            taskType = _extractTaskNew.taskType,
            taskIcon = _extractTaskNew.taskIcon;
          return taskType === 'JiraCustomTaskType' ? taskIcon || taskIconDescriptor : (0, _extractJiraTaskIcon.default)(taskType);
        }
        return taskIconDescriptor;
      } else {
        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) {
  if ((0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y')) {
    var type = extractType(data);
    var providerIcon = (0, _extractProviderIcon.default)(data);
    return chooseIcon({
      type: type,
      data: data,
      providerIcon: providerIcon
    });
  } else {
    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;