import React from 'react';
import DocumentFilledIcon from '@atlaskit/icon/core/file';
import { isConfluenceGenerator } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { getIconForFileType } from '../../../utils';
import { prioritiseIcon } from './prioritiseIcon';
const getBlogIconWrapped = () => require('../../../common/ui/icons/blog-icon').default;
const getDocumentIcon = () => require('../../../common/ui/icons/page-icon').default;
const getFileIcon = () => require('../../../common/ui/icons/file-icon').default;
const getLiveDocumentIconWrapped = () => require('../../../common/ui/icons/live-document-icon').default;
const getPresentationIcon = () => require('../../../common/ui/icons/chart-bar-icon').default;
const getSpreadsheetIcon = () => 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.
 */
export const extractIconFromDocument = (type, opts) => {
  const iconFromType = documentTypeToIcon(type, opts);
  const iconFromFileFormat = documentFileFormatToIcon(opts);
  const iconFromProvider = opts.provider && opts.provider.icon;
  return prioritiseIcon({
    fileFormatIcon: iconFromFileFormat,
    documentTypeIcon: iconFromType,
    urlIcon: opts.icon,
    providerIcon: iconFromProvider
  });
};
const documentFileFormatToIcon = opts => {
  if (opts.fileFormat) {
    return getIconForFileType(opts.fileFormat, fg('platform_navx_smart_link_icon_label_a11y') ? opts.showIconLabel : undefined);
  }
};
const documentLabel = (opts, label) => {
  // NAVX-4354 can be inlined during cleanup.
  if (!opts.showIconLabel) {
    return '';
  }
  return fg('platform_navx_smart_link_icon_label_a11y') ? label : opts.title || label;
};
const 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.
        const BlogIconWrapped = getBlogIconWrapped();
        return /*#__PURE__*/React.createElement(BlogIconWrapped, {
          label: documentLabel(opts, 'blog'),
          testId: "blog-icon"
        });
      }
    case 'schema:DigitalDocument':
      return digitalDocumentToIcon(opts);
    case 'schema:TextDigitalDocument':
      {
        const DocumentIcon = getDocumentIcon();
        return /*#__PURE__*/React.createElement(DocumentIcon, {
          label: documentLabel(opts, 'document'),
          testId: "document-icon"
        });
      }
    case 'schema:PresentationDigitalDocument':
      {
        const PresentationIcon = getPresentationIcon();
        return /*#__PURE__*/React.createElement(PresentationIcon, {
          label: documentLabel(opts, 'presentation'),
          testId: "presentation-icon"
        });
      }
    case 'schema:SpreadsheetDigitalDocument':
      {
        const SpreadsheetIcon = getSpreadsheetIcon();
        return /*#__PURE__*/React.createElement(SpreadsheetIcon, {
          label: documentLabel(opts, 'spreadsheet'),
          testId: "spreadsheet-icon"
        });
      }
    case 'atlassian:Template':
      return /*#__PURE__*/React.createElement(DocumentFilledIcon, {
        color: "currentColor",
        label: documentLabel(opts, 'template'),
        testId: "document-filled-icon"
      });
    case 'atlassian:UndefinedLink':
      {
        const DocumentIcon = getDocumentIcon();
        return /*#__PURE__*/React.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!
 */
const digitalDocumentToIcon = opts => {
  var _opts$provider;
  if ((_opts$provider = opts.provider) !== null && _opts$provider !== void 0 && _opts$provider.id && 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.
    const LiveDocumentIconWrapped = getLiveDocumentIconWrapped();
    return /*#__PURE__*/React.createElement(LiveDocumentIconWrapped, {
      label: documentLabel(opts, 'live document'),
      testId: "live-doc-icon"
    });
  } else {
    const FileIcon = getFileIcon();
    return /*#__PURE__*/React.createElement(FileIcon, {
      label: documentLabel(opts, 'file'),
      testId: "file-icon"
    });
  }
};