"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _reactLoadable = _interopRequireDefault(require("react-loadable"));
var _icon = require("@atlaskit/icon");
var _logo = require("@atlaskit/logo");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _utils = require("../../../../../common/ui/icons/utils");
var _constants = require("../../../../../constants");
var _utils2 = require("../../../../../utils");
var getIconImportFn = function getIconImportFn(icon, size) {
  var item = (0, _utils2.getLazyIcons)()[icon];
  if ((0, _utils2.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 (0, _reactLoadable.default)({
    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 [_constants.IconType.Project, _constants.IconType.Template, _constants.IconType.Forbidden, _constants.IconType.Default, _constants.IconType.Error, _constants.IconType.Attachment, _constants.IconType.CheckItem, _constants.IconType.Component, _constants.IconType.Comment, _constants.IconType.View, _constants.IconType.React, _constants.IconType.Vote, _constants.IconType.PriorityUndefined, _constants.IconType.ProgrammingLanguage, _constants.IconType.Subscriber, _constants.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 ? _constants.SmartLinkSize.Medium : _ref$size,
    isTiledIcon = _ref.isTiledIcon;
  if (!(0, _platformFeatureFlags.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 _constants.IconType.Document:
        {
          var DocumentIcon = getDocumentIcon();
          return /*#__PURE__*/_react.default.createElement(DocumentIcon, {
            label: label !== null && label !== void 0 ? label : 'document',
            testId: testId,
            size: size,
            isTiledIcon: isTiledIcon
          });
        }
      case _constants.IconType.Blog:
        {
          var BlogIcon = getBlogIcon();
          return /*#__PURE__*/_react.default.createElement(BlogIcon, {
            label: label !== null && label !== void 0 ? label : 'blog',
            testId: testId,
            size: size,
            isTiledIcon: isTiledIcon
          });
        }
      case _constants.IconType.LiveDocument:
        {
          var LiveDocumentIcon = getLiveDocumentIcon();
          return /*#__PURE__*/_react.default.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) && (0, _platformFeatureFlags.fg)('platform_sl_icons_refactor')) {
    var color;
    if (icon === _constants.IconType.Error || icon === _constants.IconType.Forbidden) {
      color = "var(--ds-icon-danger, #C9372C)";
    }
    switch (size) {
      case _constants.SmartLinkSize.Small:
      case _constants.SmartLinkSize.Medium:
        return /*#__PURE__*/_react.default.createElement(ImportedIcon, {
          label: label,
          testId: testId,
          color: color
        });
      case _constants.SmartLinkSize.Large:
      case _constants.SmartLinkSize.XLarge:
        var appearance;
        if (icon === _constants.IconType.Error || icon === _constants.IconType.Forbidden) {
          appearance = 'redBold';
        } else {
          appearance = 'grayBold';
        }
        var iconTileSize = (0, _utils.transformSmartLinkSizeToIconTileSize)(size);
        if (iconTileSize) {
          return /*#__PURE__*/_react.default.createElement(_icon.IconTile, {
            appearance: appearance,
            icon: isTiledIcon ? function (iconProps) {
              return /*#__PURE__*/_react.default.createElement(ImportedIcon, (0, _extends2.default)({}, iconProps, {
                spacing: "spacious"
              }));
            } : ImportedIcon,
            size: iconTileSize,
            label: label !== null && label !== void 0 ? label : ''
          });
        } else {
          return /*#__PURE__*/_react.default.createElement(ImportedIcon, {
            label: label,
            testId: testId,
            color: color
          });
        }
    }
  }
  switch (icon) {
    case _constants.IconType.Confluence:
      return /*#__PURE__*/_react.default.createElement(_logo.ConfluenceIcon, (0, _extends2.default)({
        appearance: "brand",
        testId: testId,
        size: size === _constants.SmartLinkSize.Large || size === _constants.SmartLinkSize.XLarge ? 'small' : 'xxsmall'
      }, (0, _platformFeatureFlags.fg)('navx-1895-new-logo-design') ? {
        shouldUseNewLogoDesign: true
      } : undefined));
    case _constants.IconType.Jira:
      return /*#__PURE__*/_react.default.createElement(_logo.JiraIcon, (0, _extends2.default)({
        appearance: "brand",
        testId: testId,
        size: size === _constants.SmartLinkSize.Large || size === _constants.SmartLinkSize.XLarge ? 'small' : 'xxsmall'
      }, (0, _platformFeatureFlags.fg)('navx-1895-new-logo-design') ? {
        shouldUseNewLogoDesign: true
      } : undefined));
    case _constants.IconType.Error:
    case _constants.IconType.Forbidden:
      if ((0, _platformFeatureFlags.fg)('platform_sl_icons_refactor')) {
        return /*#__PURE__*/_react.default.createElement(ImportedIcon, {
          label: label,
          testId: testId,
          size: size,
          isTiledIcon: isTiledIcon
        });
      }
      return /*#__PURE__*/_react.default.createElement(ImportedIcon, {
        label: label,
        testId: testId,
        color: "var(--ds-icon-danger, #C9372C)"
      });
    default:
      return /*#__PURE__*/_react.default.createElement(ImportedIcon, {
        label: label,
        testId: testId,
        size: size,
        isTiledIcon: isTiledIcon
      });
  }
};
var _default = exports.default = AtlaskitIcon;