"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractIcon = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _ = _interopRequireDefault(require("@atlaskit/icon-object/glyph/branch/16"));
var _2 = _interopRequireDefault(require("@atlaskit/icon-object/glyph/code/16"));
var _3 = _interopRequireDefault(require("@atlaskit/icon-object/glyph/commit/16"));
var _4 = _interopRequireDefault(require("@atlaskit/icon-object/glyph/pull-request/16"));
var _5 = _interopRequireDefault(require("@atlaskit/icon-object/glyph/task/16"));
var _peopleGroup = _interopRequireDefault(require("@atlaskit/icon/core/people-group"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _utils = require("../../../utils");
var _extractTaskType = require("../lozenge/extractTaskType");
var _extractFileFormat = require("./extractFileFormat");
var _extractIconFromDocument = require("./extractIconFromDocument");
var _extractIconFromTask = require("./extractIconFromTask");
var _prioritiseIcon = require("./prioritiseIcon");
var _priority = require("./priority");
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) { (0, _defineProperty2.default)(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; }
var extractIcon = exports.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({}, (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? {} : {
    title: (0, _linkExtractors.extractTitle)(jsonLd)
  }), {}, {
    provider: (0, _linkExtractors.extractProvider)(jsonLd),
    fileFormat: (0, _extractFileFormat.extractFileFormat)(jsonLd),
    taskType: (0, _extractTaskType.extractTaskType)(jsonLd),
    icon: jsonLd.icon && (0, _linkExtractors.extractUrlFromIconJsonLd)(jsonLd.icon),
    priority: priority,
    showIconLabel: showIconLabel
  });
  if (Array.isArray(type)) {
    var highestPriorityType = type.sort(function (a, b) {
      return _priority.extractorPriorityMap[b] - _priority.extractorPriorityMap[a];
    })[0];
    return extractIconByType(highestPriorityType, opts);
  } else {
    return extractIconByType(type, opts);
  }
};
function typeToIcon(type, opts) {
  var getLabel = function getLabel(title) {
    return (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? title : opts.title || title;
  };
  switch (type) {
    case 'atlassian:SourceCodeCommit':
      return /*#__PURE__*/_react.default.createElement(_3.default, {
        label: getLabel('commit'),
        testId: "commit-icon"
      });
    case 'atlassian:Project':
      return /*#__PURE__*/_react.default.createElement(_peopleGroup.default, {
        label: getLabel('project'),
        testId: "project-icon",
        color: "currentColor"
      });
    case 'atlassian:SourceCodePullRequest':
      return /*#__PURE__*/_react.default.createElement(_4.default, {
        label: getLabel('pull request'),
        testId: "pull-request-icon"
      });
    case 'atlassian:SourceCodeReference':
      return /*#__PURE__*/_react.default.createElement(_.default, {
        label: getLabel('reference'),
        testId: "branch-icon"
      });
    case 'atlassian:SourceCodeRepository':
      return /*#__PURE__*/_react.default.createElement(_2.default, {
        label: getLabel('repository'),
        testId: "repo-icon"
      });
    case 'atlassian:Goal':
      return /*#__PURE__*/_react.default.createElement(_5.default, {
        label: getLabel('goal'),
        testId: "task-icon"
      });
    case 'atlassian:Task':
      return (0, _extractIconFromTask.extractIconFromTask)(opts);
    default:
      return undefined;
  }
}
function standardisedExtractIcon(type, opts) {
  var iconFromType = typeToIcon(type, opts);
  var iconFromFileFormat = opts.fileFormat ? (0, _utils.getIconForFileType)(opts.fileFormat, (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? opts.showIconLabel : undefined) : undefined;
  var iconFromProvider = opts.provider && opts.provider.icon;
  return (0, _prioritiseIcon.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 (0, _extractIconFromDocument.extractIconFromDocument)(type, opts);
    default:
      return standardisedExtractIcon(type, opts);
  }
};