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';
var getIconImportFn = function getIconImportFn(icon, size) {
  var 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;
};
var importIcon = function importIcon(importFn) {
  return Loadable({
    loader: function loader() {
      return importFn().then(function (module) {
        return module.default;
      });
    },
    loading: function loading() {
      return null;
    }
  }); // Because we're using dynamic loading here, TS will not be able to infer the type.
};
var getBlogIcon = function getBlogIcon() {
  return require('../../../../../common/ui/icons/blog-icon').default;
};
var getDocumentIcon = function getDocumentIcon() {
  return require('../../../../../common/ui/icons/page-icon').default;
};
var getLiveDocumentIcon = function getLiveDocumentIcon() {
  return require('../../../../../common/ui/icons/live-document-icon').default;
};
var isCoreIcon = function 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);
};
var AtlaskitIcon = function AtlaskitIcon(_ref) {
  var icon = _ref.icon,
    label = _ref.label,
    testId = _ref.testId,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? SmartLinkSize.Medium : _ref$size,
    isTiledIcon = _ref.isTiledIcon;
  // Intentionally keep these three icon types synchronous and outside lazy imports.
  // These icons should be present in SSR markup so hydration does not cause a
  // visible empty-state-then-pop-in transition in Smart Link consumers.
  switch (icon) {
    case IconType.Document:
      {
        var DocumentIcon = getDocumentIcon();
        return /*#__PURE__*/React.createElement(DocumentIcon, {
          label: label !== null && label !== void 0 ? label : 'document',
          testId: testId,
          size: size,
          isTiledIcon: isTiledIcon
        });
      }
    case IconType.Blog:
      {
        var BlogIcon = getBlogIcon();
        return /*#__PURE__*/React.createElement(BlogIcon, {
          label: label !== null && label !== void 0 ? label : 'blog',
          testId: testId,
          size: size,
          isTiledIcon: isTiledIcon
        });
      }
    case IconType.LiveDocument:
      {
        var LiveDocumentIcon = getLiveDocumentIcon();
        return /*#__PURE__*/React.createElement(LiveDocumentIcon, {
          label: label !== null && label !== void 0 ? label : 'live-doc',
          testId: testId,
          size: size,
          isTiledIcon: isTiledIcon
        });
      }
  }
  var importFn = getIconImportFn(icon, size);
  if (!importFn) {
    return null;
  }
  var ImportedIcon = importIcon(importFn);
  if (isCoreIcon(icon) && fg('platform_sl_icons_refactor')) {
    var 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:
        var appearance;
        if (icon === IconType.Error || icon === IconType.Forbidden) {
          appearance = 'redBold';
        } else {
          appearance = 'grayBold';
        }
        var iconTileSize = transformSmartLinkSizeToIconTileSize(size);
        if (iconTileSize) {
          return /*#__PURE__*/React.createElement(IconTile, {
            appearance: appearance,
            icon: isTiledIcon ? function (iconProps) {
              return /*#__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
      }, fg('billplat_a11y_icon_label_fix') ? {
        label: ''
      } : undefined, {
        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
      }, fg('billplat_a11y_icon_label_fix') ? {
        label: ''
      } : undefined, {
        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;