import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import Loadable from 'react-loadable';
import { IconTile } from '@atlaskit/icon';
import { ConfluenceIcon, JiraIcon } from '@atlaskit/logo';
import { fg } from '@atlaskit/platform-feature-flags';
import { transformSmartLinkSizeToIconTileSize } from '../../../../../common/ui/icons/utils';
import { IconType, SmartLinkSize } from '../../../../../constants';
import { getLazyIcons, isIconSizeLarge } from '../../../../../utils';
const getIconImportFn = (icon, size) => {
  const item = getLazyIcons()[icon];
  if (isIconSizeLarge(size) && item !== null && item !== void 0 && item.large) {
    return item.large;
  }
  return item === null || item === void 0 ? void 0 : item.default;
};
const importIcon = importFn => {
  return Loadable({
    loader: () => importFn().then(module => module.default),
    loading: () => null
  }); // Because we're using dynamic loading here, TS will not be able to infer the type.
};
const getBlogIcon = () => require('../../../../../common/ui/icons/blog-icon').default;
const getDocumentIcon = () => require('../../../../../common/ui/icons/page-icon').default;
const getLiveDocumentIcon = () => require('../../../../../common/ui/icons/live-document-icon').default;
const isCoreIcon = icon => {
  return [IconType.Project, IconType.Template, IconType.Forbidden, IconType.Default, IconType.Error, IconType.Attachment, IconType.CheckItem, IconType.Component, IconType.Comment, IconType.View, IconType.React, IconType.Vote, IconType.PriorityUndefined, IconType.ProgrammingLanguage, IconType.Subscriber, IconType.SubTasksProgress].includes(icon);
};
const AtlaskitIcon = ({
  icon,
  label,
  testId,
  size = SmartLinkSize.Medium,
  isTiledIcon
}) => {
  if (!fg('platform_sl_icons_refactor')) {
    // Check for synchronously loaded icons first for SSR purposes.
    // Require these icons only inside this branch to avoid evaluating legacy icon
    // wrapper modules when the icon refactor is enabled.
    switch (icon) {
      case IconType.Document:
        {
          const DocumentIcon = getDocumentIcon();
          return /*#__PURE__*/React.createElement(DocumentIcon, {
            label: label !== null && label !== void 0 ? label : 'document',
            testId: testId,
            size: size,
            isTiledIcon: isTiledIcon
          });
        }
      case IconType.Blog:
        {
          const BlogIcon = getBlogIcon();
          return /*#__PURE__*/React.createElement(BlogIcon, {
            label: label !== null && label !== void 0 ? label : 'blog',
            testId: testId,
            size: size,
            isTiledIcon: isTiledIcon
          });
        }
      case IconType.LiveDocument:
        {
          const LiveDocumentIcon = getLiveDocumentIcon();
          return /*#__PURE__*/React.createElement(LiveDocumentIcon, {
            label: label !== null && label !== void 0 ? label : 'live-doc',
            testId: testId,
            size: size,
            isTiledIcon: isTiledIcon
          });
        }
    }
  }
  const importFn = getIconImportFn(icon, size);
  if (!importFn) {
    return null;
  }
  const ImportedIcon = importIcon(importFn);
  if (isCoreIcon(icon) && fg('platform_sl_icons_refactor')) {
    let color;
    if (icon === IconType.Error || icon === IconType.Forbidden) {
      color = "var(--ds-icon-danger, #C9372C)";
    }
    switch (size) {
      case SmartLinkSize.Small:
      case SmartLinkSize.Medium:
        return /*#__PURE__*/React.createElement(ImportedIcon, {
          label: label,
          testId: testId,
          color: color
        });
      case SmartLinkSize.Large:
      case SmartLinkSize.XLarge:
        let appearance;
        if (icon === IconType.Error || icon === IconType.Forbidden) {
          appearance = 'redBold';
        } else {
          appearance = 'grayBold';
        }
        const iconTileSize = transformSmartLinkSizeToIconTileSize(size);
        if (iconTileSize) {
          return /*#__PURE__*/React.createElement(IconTile, {
            appearance: appearance,
            icon: isTiledIcon ? iconProps => /*#__PURE__*/React.createElement(ImportedIcon, _extends({}, iconProps, {
              spacing: "spacious"
            })) : ImportedIcon,
            size: iconTileSize,
            label: label !== null && label !== void 0 ? label : ''
          });
        } else {
          return /*#__PURE__*/React.createElement(ImportedIcon, {
            label: label,
            testId: testId,
            color: color
          });
        }
    }
  }
  switch (icon) {
    case IconType.Confluence:
      return /*#__PURE__*/React.createElement(ConfluenceIcon, _extends({
        appearance: "brand",
        testId: testId,
        size: size === SmartLinkSize.Large || size === SmartLinkSize.XLarge ? 'small' : 'xxsmall'
      }, fg('navx-1895-new-logo-design') ? {
        shouldUseNewLogoDesign: true
      } : undefined));
    case IconType.Jira:
      return /*#__PURE__*/React.createElement(JiraIcon, _extends({
        appearance: "brand",
        testId: testId,
        size: size === SmartLinkSize.Large || size === SmartLinkSize.XLarge ? 'small' : 'xxsmall'
      }, fg('navx-1895-new-logo-design') ? {
        shouldUseNewLogoDesign: true
      } : undefined));
    case IconType.Error:
    case IconType.Forbidden:
      if (fg('platform_sl_icons_refactor')) {
        return /*#__PURE__*/React.createElement(ImportedIcon, {
          label: label,
          testId: testId,
          size: size,
          isTiledIcon: isTiledIcon
        });
      }
      return /*#__PURE__*/React.createElement(ImportedIcon, {
        label: label,
        testId: testId,
        color: "var(--ds-icon-danger, #C9372C)"
      });
    default:
      return /*#__PURE__*/React.createElement(ImportedIcon, {
        label: label,
        testId: testId,
        size: size,
        isTiledIcon: isTiledIcon
      });
  }
};
export default AtlaskitIcon;