"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractIconFromDocument = void 0;
var _react = _interopRequireDefault(require("react"));
var _file = _interopRequireDefault(require("@atlaskit/icon/core/file"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _blogIcon = _interopRequireDefault(require("../../../common/ui/icons/blog-icon"));
var _chartBarIcon = _interopRequireDefault(require("../../../common/ui/icons/chart-bar-icon"));
var _fileIcon = _interopRequireDefault(require("../../../common/ui/icons/file-icon"));
var _listBulletIcon = _interopRequireDefault(require("../../../common/ui/icons/list-bullet-icon"));
var _liveDocumentIcon = _interopRequireDefault(require("../../../common/ui/icons/live-document-icon"));
var _pageIcon = _interopRequireDefault(require("../../../common/ui/icons/page-icon"));
var _utils = require("../../../utils");
var _prioritiseIcon = require("./prioritiseIcon");
/**
 * Extracts an icon for a document pbject
 *
 * @param type - The type of the document.
 * @param opts - The options for extracting the icon.
 * @returns The React node representing the extracted icon, or `undefined` if no icon is found.
 */
var extractIconFromDocument = exports.extractIconFromDocument = function extractIconFromDocument(type, opts) {
  var iconFromType = documentTypeToIcon(type, opts);
  var iconFromFileFormat = documentFileFormatToIcon(opts);
  var iconFromProvider = opts.provider && opts.provider.icon;
  return (0, _prioritiseIcon.prioritiseIcon)({
    fileFormatIcon: iconFromFileFormat,
    documentTypeIcon: iconFromType,
    urlIcon: opts.icon,
    providerIcon: iconFromProvider
  });
};
var documentFileFormatToIcon = function documentFileFormatToIcon(opts) {
  if (opts.fileFormat) {
    return (0, _utils.getIconForFileType)(opts.fileFormat, (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? opts.showIconLabel : undefined);
  }
};
var documentLabel = function documentLabel(opts, label) {
  // NAVX-4354 can be inlined during cleanup.
  if (!opts.showIconLabel) {
    return '';
  }
  return (0, _platformFeatureFlags.fg)('platform_navx_smart_link_icon_label_a11y') ? label : opts.title || label;
};
var documentTypeToIcon = function documentTypeToIcon(type, opts) {
  switch (type) {
    case 'schema:BlogPosting':
      return /*#__PURE__*/_react.default.createElement(_blogIcon.default, {
        label: documentLabel(opts, 'blog'),
        testId: "blog-icon"
      });
    case 'schema:DigitalDocument':
      return digitalDocumentToIcon(opts);
    case 'schema:TextDigitalDocument':
      return /*#__PURE__*/_react.default.createElement(_pageIcon.default, {
        label: documentLabel(opts, 'document'),
        testId: "document-icon"
      });
    case 'schema:PresentationDigitalDocument':
      return /*#__PURE__*/_react.default.createElement(_chartBarIcon.default, {
        label: documentLabel(opts, 'presentation'),
        testId: "presentation-icon"
      });
    case 'schema:SpreadsheetDigitalDocument':
      return /*#__PURE__*/_react.default.createElement(_listBulletIcon.default, {
        label: documentLabel(opts, 'spreadsheet'),
        testId: "spreadsheet-icon"
      });
    case 'atlassian:Template':
      return /*#__PURE__*/_react.default.createElement(_file.default, {
        color: "currentColor",
        label: documentLabel(opts, 'template'),
        testId: "document-filled-icon"
      });
    case 'atlassian:UndefinedLink':
      return /*#__PURE__*/_react.default.createElement(_pageIcon.default, {
        label: documentLabel(opts, 'document'),
        testId: "document-icon"
      });
  }
};

/**
 * Enables providers to represent `schema:DigitalDocument` in a manner which
 * aligns with their customers when representing provider-specific types, which
 * do not apply across multiple providers.
 * @example Confluence digital documents represent 'live documents', specific to Confluence.
 * @remark This mechanism will be superseded by backend-driven icon URLs as part
 * of go/j/MODES-5864. Do not add more!
 */
var digitalDocumentToIcon = function digitalDocumentToIcon(opts) {
  var _opts$provider;
  if ((_opts$provider = opts.provider) !== null && _opts$provider !== void 0 && _opts$provider.id && (0, _linkExtractors.isConfluenceGenerator)(opts.provider.id)) {
    return /*#__PURE__*/_react.default.createElement(_liveDocumentIcon.default, {
      label: documentLabel(opts, 'live document'),
      testId: "live-doc-icon"
    });
  } else {
    return /*#__PURE__*/_react.default.createElement(_fileIcon.default, {
      label: documentLabel(opts, 'file'),
      testId: "file-icon"
    });
  }
};