"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 _utils = require("../../../utils");
var _prioritiseIcon = require("./prioritiseIcon");
var getBlogIconWrapped = function getBlogIconWrapped() {
  return require('../../../common/ui/icons/blog-icon').default;
};
var getDocumentIcon = function getDocumentIcon() {
  return require('../../../common/ui/icons/page-icon').default;
};
var getFileIcon = function getFileIcon() {
  return require('../../../common/ui/icons/file-icon').default;
};
var getLiveDocumentIconWrapped = function getLiveDocumentIconWrapped() {
  return require('../../../common/ui/icons/live-document-icon').default;
};
var getPresentationIcon = function getPresentationIcon() {
  return require('../../../common/ui/icons/chart-bar-icon').default;
};
var getSpreadsheetIcon = function getSpreadsheetIcon() {
  return require('../../../common/ui/icons/list-bullet-icon').default;
};

/**
 * 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':
      {
        // Intentionally keep this icon on a synchronous require()-based path.
        // This icon must be present in SSR markup so hydration does not cause a
        // visible empty-state-then-pop-in transition for Smart Links.
        var BlogIconWrapped = getBlogIconWrapped();
        return /*#__PURE__*/_react.default.createElement(BlogIconWrapped, {
          label: documentLabel(opts, 'blog'),
          testId: "blog-icon"
        });
      }
    case 'schema:DigitalDocument':
      return digitalDocumentToIcon(opts);
    case 'schema:TextDigitalDocument':
      {
        var DocumentIcon = getDocumentIcon();
        return /*#__PURE__*/_react.default.createElement(DocumentIcon, {
          label: documentLabel(opts, 'document'),
          testId: "document-icon"
        });
      }
    case 'schema:PresentationDigitalDocument':
      {
        var PresentationIcon = getPresentationIcon();
        return /*#__PURE__*/_react.default.createElement(PresentationIcon, {
          label: documentLabel(opts, 'presentation'),
          testId: "presentation-icon"
        });
      }
    case 'schema:SpreadsheetDigitalDocument':
      {
        var SpreadsheetIcon = getSpreadsheetIcon();
        return /*#__PURE__*/_react.default.createElement(SpreadsheetIcon, {
          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':
      {
        var _DocumentIcon = getDocumentIcon();
        return /*#__PURE__*/_react.default.createElement(_DocumentIcon, {
          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)) {
    // Intentionally keep this icon on a synchronous require()-based path.
    // This icon must be present in SSR markup so hydration does not cause a
    // visible empty-state-then-pop-in transition for Smart Links.
    var LiveDocumentIconWrapped = getLiveDocumentIconWrapped();
    return /*#__PURE__*/_react.default.createElement(LiveDocumentIconWrapped, {
      label: documentLabel(opts, 'live document'),
      testId: "live-doc-icon"
    });
  } else {
    var FileIcon = getFileIcon();
    return /*#__PURE__*/_react.default.createElement(FileIcon, {
      label: documentLabel(opts, 'file'),
      testId: "file-icon"
    });
  }
};