"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractIcon = void 0;
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 _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");
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 = {
    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) {
  switch (type) {
    case 'atlassian:SourceCodeCommit':
      return /*#__PURE__*/_react.default.createElement(_3.default, {
        label: opts.title || 'commit',
        testId: "commit-icon"
      });
    case 'atlassian:Project':
      return /*#__PURE__*/_react.default.createElement(_peopleGroup.default, {
        label: opts.title || 'project',
        testId: "project-icon",
        color: "currentColor"
      });
    case 'atlassian:SourceCodePullRequest':
      return /*#__PURE__*/_react.default.createElement(_4.default, {
        label: opts.title || 'pullRequest',
        testId: "pull-request-icon"
      });
    case 'atlassian:SourceCodeReference':
      return /*#__PURE__*/_react.default.createElement(_.default, {
        label: opts.title || 'reference',
        testId: "branch-icon"
      });
    case 'atlassian:SourceCodeRepository':
      return /*#__PURE__*/_react.default.createElement(_2.default, {
        label: opts.title || 'repository',
        testId: "repo-icon"
      });
    case 'atlassian:Goal':
      return /*#__PURE__*/_react.default.createElement(_5.default, {
        label: opts.title || '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) : 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);
  }
};