import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import PeopleGroupIcon from '@atlaskit/icon/core/people-group';
import { extractProvider, extractTitle, extractUrlFromIconJsonLd } from '@atlaskit/link-extractors';
import BranchObject from '@atlaskit/object/branch';
import CodeObject from '@atlaskit/object/code';
import CommitObject from '@atlaskit/object/commit';
import PullRequestObject from '@atlaskit/object/pull-request';
import TaskObject from '@atlaskit/object/task';
import { fg } from '@atlaskit/platform-feature-flags';
import { getIconForFileType } from '../../../utils';
import { extractTaskType } from '../lozenge/extractTaskType';
import { extractFileFormat } from './extractFileFormat';
import { extractIconFromDocument } from './extractIconFromDocument';
import { extractIconFromTask } from './extractIconFromTask';
import { prioritiseIcon } from './prioritiseIcon';
import { extractorPriorityMap } from './priority';
export var extractIcon = function extractIcon(jsonLd) {
  var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'type';
  var showIconLabel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var type = jsonLd['@type'];
  var opts = _objectSpread(_objectSpread({}, fg('platform_navx_smart_link_icon_label_a11y') ? {} : {
    title: extractTitle(jsonLd)
  }), {}, {
    provider: extractProvider(jsonLd),
    fileFormat: extractFileFormat(jsonLd),
    taskType: extractTaskType(jsonLd),
    icon: jsonLd.icon && extractUrlFromIconJsonLd(jsonLd.icon),
    priority: priority,
    showIconLabel: showIconLabel
  });
  if (Array.isArray(type)) {
    var highestPriorityType = type.sort(function (a, b) {
      return extractorPriorityMap[b] - extractorPriorityMap[a];
    })[0];
    return extractIconByType(highestPriorityType, opts);
  } else {
    return extractIconByType(type, opts);
  }
};
function typeToIcon(type, opts) {
  var getLabel = function getLabel(title) {
    return fg('platform_navx_smart_link_icon_label_a11y') ? title : opts.title || title;
  };
  switch (type) {
    case 'atlassian:SourceCodeCommit':
      return /*#__PURE__*/React.createElement(CommitObject, {
        label: getLabel('commit'),
        testId: "commit-icon"
      });
    case 'atlassian:Project':
      return /*#__PURE__*/React.createElement(PeopleGroupIcon, {
        label: getLabel('project'),
        testId: "project-icon",
        color: "currentColor"
      });
    case 'atlassian:SourceCodePullRequest':
      return /*#__PURE__*/React.createElement(PullRequestObject, {
        label: getLabel('pull request'),
        testId: "pull-request-icon"
      });
    case 'atlassian:SourceCodeReference':
      return /*#__PURE__*/React.createElement(BranchObject, {
        label: getLabel('reference'),
        testId: "branch-icon"
      });
    case 'atlassian:SourceCodeRepository':
      return /*#__PURE__*/React.createElement(CodeObject, {
        label: getLabel('repository'),
        testId: "repo-icon"
      });
    case 'atlassian:Goal':
      return /*#__PURE__*/React.createElement(TaskObject, {
        label: getLabel('goal'),
        testId: "task-icon"
      });
    case 'atlassian:Task':
      return extractIconFromTask(opts);
    default:
      return undefined;
  }
}
function standardisedExtractIcon(type, opts) {
  var iconFromType = typeToIcon(type, opts);
  var iconFromFileFormat = opts.fileFormat ? getIconForFileType(opts.fileFormat, fg('platform_navx_smart_link_icon_label_a11y') ? opts.showIconLabel : undefined) : undefined;
  var iconFromProvider = opts.provider && opts.provider.icon;
  return prioritiseIcon({
    fileFormatIcon: iconFromFileFormat,
    documentTypeIcon: iconFromType,
    urlIcon: opts.icon,
    providerIcon: iconFromProvider
  });
}

/**
 * Extracts an icon based on the given type.
 *
 * Some types return hardcoded icons, while others follow a priority list on how to pick the icon.
 * If type is not recognized, it will return the icon from the provider, or `undefined` if no icon is found.
 *
 * @param type - The type of the object.
 * @param opts - Options for the icon extraction.
 * @returns The extracted icon as a React node, or `undefined` if no icon is found.
 */
var extractIconByType = function extractIconByType(type, opts) {
  switch (type) {
    case 'Document':
    case 'schema:BlogPosting':
    case 'schema:DigitalDocument':
    case 'schema:TextDigitalDocument':
    case 'schema:PresentationDigitalDocument':
    case 'schema:SpreadsheetDigitalDocument':
    case 'atlassian:Template':
    case 'atlassian:UndefinedLink':
      return extractIconFromDocument(type, opts);
    default:
      return standardisedExtractIcon(type, opts);
  }
};