"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 _blogIcon = _interopRequireDefault(require("../../../../../common/ui/icons/blog-icon"));
var _liveDocumentIcon = _interopRequireDefault(require("../../../../../common/ui/icons/live-document-icon"));
var _pageIcon = _interopRequireDefault(require("../../../../../common/ui/icons/page-icon"));
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 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;
  if (!(0, _platformFeatureFlags.fg)('platform_sl_icons_refactor')) {
    // Check for synchonously loaded icons first for SSR purposes
    switch (icon) {
      case _constants.IconType.Document:
        return /*#__PURE__*/_react.default.createElement(_pageIcon.default, {
          label: label !== null && label !== void 0 ? label : 'document',
          testId: testId,
          size: size
        });
      case _constants.IconType.Blog:
        return /*#__PURE__*/_react.default.createElement(_blogIcon.default, {
          label: label !== null && label !== void 0 ? label : 'blog',
          testId: testId,
          size: size
        });
      case _constants.IconType.LiveDocument:
        return /*#__PURE__*/_react.default.createElement(_liveDocumentIcon.default, {
          label: label !== null && label !== void 0 ? label : 'live-doc',
          testId: testId,
          size: size
        });
    }
  }
  var importFn = getIconImportFn(icon, size);
  if (!importFn) {
    return null;
  }
  var ImportedIcon = importIcon(importFn);
  if (isCoreIcon(icon) && (0, _platformFeatureFlags.fg)('platform_sl_icons_refactor')) {
    switch (size) {
      case _constants.SmartLinkSize.Small:
      case _constants.SmartLinkSize.Medium:
        var color;
        if (icon === _constants.IconType.Error || icon === _constants.IconType.Forbidden) {
          color = "var(--ds-icon-danger, #C9372C)";
        }
        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';
        }
        return /*#__PURE__*/_react.default.createElement(_icon.IconTile, {
          appearance: appearance,
          icon: ImportedIcon,
          size: (0, _utils.transformSmartLinkSizeToIconTileSize)(size),
          label: label !== null && label !== void 0 ? label : ''
        });
    }
  }
  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
        });
      }
      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
      });
  }
};
var _default = exports.default = AtlaskitIcon;