import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import Loadable from 'react-loadable';
import { fg } from '@atlaskit/platform-feature-flags';
import { themeObjectToString } from '@atlaskit/tokens/theme-state-transformer';
import { IconType, SmartLinkSize } from '../constants';
import extractFileFormatIcon from '../extractors/flexible/icon/extract-file-formatIcon';
export const isSpecialEvent = evt => evt.isDefaultPrevented() && (isIframe() || isSpecialKey(evt) || isSpecialClick(evt));
export const isIframe = () => window.parent !== parent;

/**
 * Meta key = cmd on mac, windows key on windows
 * Ctrl key on mac by default triggers a right click instead of left click
 * Ctrl key on Windows has the same behaviour of cmd key of mac (open in new tab)
 * Shift key is also a "special" key because the default behavior of Chromium-based browsers is to open the
 * link in a new window; Arc browser has custom logic to show links in its "peek" window when shift is held.
 *
 * `isSpecialKey` on a mouse event on mac with default behaviour should be equivalent to opening in new tab
 * On Windows it will be equivalent to opening a new tab, unless its the Window key that is held
 * in which case typically only a standard clickthrough will occur, this is likely a small portion of events
 */
export const isSpecialKey = event => {
  return fg('platform-smart-card-shift-key') ? event.metaKey || event.ctrlKey || event.shiftKey : event.metaKey || event.ctrlKey;
};
export const isSpecialClick = event => event.button === 1;
export const getIconForFileType = (fileMimeType, showIconLabel) => {
  if (!fileMimeType) {
    return;
  }
  let icon = getTypeToIconMap(fileMimeType.toLowerCase());
  if (!icon) {
    return;
  }
  const [importCb] = icon;
  if (!importCb) {
    return;
  }
  const Icon = Loadable({
    loader: () => importCb().then(module => module.default),
    loading: () => null
  }); // because we're using dynamic loading here, TS will not be able to infer the type

  if (fg('platform_navx_smart_link_icon_label_a11y')) {
    const descriptorLabel = icon[1] || ''; // NAVX-4354: Combine into const [importCb] = icon; above
    const label = (showIconLabel !== null && showIconLabel !== void 0 ? showIconLabel : true) ? descriptorLabel : '';
    return /*#__PURE__*/React.createElement(Icon, {
      testId: "document-file-format-icon",
      label: label
    });
  } else {
    return /*#__PURE__*/React.createElement(Icon, {
      testId: "document-file-format-icon"
    });
  }
};
// prettier-ignore
export const getLazyIcons = () => {
  return {
    // IconTile wrappers (@atlaskit/icon/core/* wrapped in <IconTile> with color)
    [IconType.Document]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_page-icon" */'../common/ui/icons/page-icon')
    },
    [IconType.Audio]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_audio-icon" */'../common/ui/icons/audio-icon')
    },
    [IconType.Code]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_angle-brackets-icon" */'../common/ui/icons/angle-brackets-icon')
    },
    [IconType.File]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_file-icon" */'../common/ui/icons/file-icon')
    },
    [IconType.Folder]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_folder-icon" */'../common/ui/icons/folder-icon')
    },
    [IconType.Generic]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_file-icon" */'../common/ui/icons/file-icon')
    },
    [IconType.Image]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_image-icon" */'../common/ui/icons/image-icon')
    },
    [IconType.Presentation]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_chart-bar-icon" */'../common/ui/icons/chart-bar-icon')
    },
    [IconType.Spreadsheet]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_list-bullet-icon" */'../common/ui/icons/list-bullet-icon')
    },
    [IconType.Video]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_video-icon" */'../common/ui/icons/video-icon')
    },
    // @atlaskit/icon/core/* (plain monochrome icons)
    [IconType.Project]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphProject" */'@atlaskit/icon/core/people-group')
    },
    [IconType.Template]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphTemplate" */'@atlaskit/icon/core/file')
    },
    [IconType.Forbidden]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphForbidden" */'@atlaskit/icon/core/lock-locked')
    },
    [IconType.Default]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphDefault" */'@atlaskit/icon/core/link')
    },
    [IconType.Error]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphDefault" */'@atlaskit/icon/core/status-error')
    },
    // @atlaskit/icon/core/* (badge icons)
    [IconType.Attachment]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphAttachment" */'@atlaskit/icon/core/attachment')
    },
    [IconType.CheckItem]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphAttachment" */'@atlaskit/icon/core/task')
    },
    [IconType.Component]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphComponent" */'@atlaskit/icon/core/component')
    },
    [IconType.Comment]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphComment" */'@atlaskit/icon/core/comment')
    },
    [IconType.View]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphComment" */'@atlaskit/icon/core/eye-open')
    },
    [IconType.React]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphComment" */'@atlaskit/icon/core/thumbs-up')
    },
    [IconType.Vote]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphComment" */'@atlaskit/icon/core/arrow-up')
    },
    [IconType.PriorityUndefined]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphUndefined" */'@atlaskit/icon/core/question-circle')
    },
    [IconType.ProgrammingLanguage]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphProgrammingLanguage" */'@atlaskit/icon/core/angle-brackets')
    },
    [IconType.Subscriber]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSubscriber" */'@atlaskit/icon/core/people-group')
    },
    [IconType.SubTasksProgress]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSubtaskProgress" */'@atlaskit/icon/core/subtasks')
    },
    // @atlaskit/icon-file-type/glyph/* (file type icons with 16/24 size variants)
    [IconType.Archive]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphArchive" */'@atlaskit/icon-file-type/glyph/archive/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphArchive" */'@atlaskit/icon-file-type/glyph/archive/24')
    },
    [IconType.Executable]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphExecutable" */'@atlaskit/icon-file-type/glyph/executable/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphExecutable" */'@atlaskit/icon-file-type/glyph/executable/24')
    },
    [IconType.GIF]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGIF" */'@atlaskit/icon-file-type/glyph/gif/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGIF" */'@atlaskit/icon-file-type/glyph/gif/24')
    },
    [IconType.GoogleDocs]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleDocs" */'@atlaskit/icon-file-type/glyph/google-doc/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleDocs" */'@atlaskit/icon-file-type/glyph/google-doc/24')
    },
    [IconType.GoogleForms]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleForms" */'@atlaskit/icon-file-type/glyph/google-form/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleForms" */'@atlaskit/icon-file-type/glyph/google-form/24')
    },
    [IconType.GoogleSheets]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleSheets" */'@atlaskit/icon-file-type/glyph/google-sheet/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleSheets" */'@atlaskit/icon-file-type/glyph/google-sheet/24')
    },
    [IconType.GoogleSlides]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleSlides" */'@atlaskit/icon-file-type/glyph/google-slide/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphGoogleSlides" */'@atlaskit/icon-file-type/glyph/google-slide/24')
    },
    [IconType.MSExcel]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSExcel" */'@atlaskit/icon-file-type/glyph/excel-spreadsheet/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSExcel" */'@atlaskit/icon-file-type/glyph/excel-spreadsheet/24')
    },
    [IconType.MSPowerpoint]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSPowerpoint" */'@atlaskit/icon-file-type/glyph/powerpoint-presentation/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSPowerpoint" */'@atlaskit/icon-file-type/glyph/powerpoint-presentation/24')
    },
    [IconType.MSWord]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSWord" */'@atlaskit/icon-file-type/glyph/word-document/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMSWord" */'@atlaskit/icon-file-type/glyph/word-document/24')
    },
    [IconType.PDF]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphPDF" */'@atlaskit/icon-file-type/glyph/pdf-document/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphPDF" */'@atlaskit/icon-file-type/glyph/pdf-document/24')
    },
    [IconType.Sketch]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSketch" */'@atlaskit/icon-file-type/glyph/sketch/16'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSketch" */'@atlaskit/icon-file-type/glyph/sketch/24')
    },
    // Keep blog/live-document out of the lazy map on purpose.
    // 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.
    [IconType.Branch]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphBranch" */'@atlaskit/object/branch'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphBranch" */'@atlaskit/object/tile/branch').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Commit]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphCommit" */'@atlaskit/object/commit'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphCommit" */'@atlaskit/object/tile/commit').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.PullRequest]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphPullRequest" */'@atlaskit/object/pull-request'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphPullRequest" */'@atlaskit/object/tile/pull-request').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Repo]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphRepo" */'@atlaskit/object/code'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphRepo" */'@atlaskit/object/tile/code').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Bug]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphBug" */'@atlaskit/object/bug'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphBug" */'@atlaskit/object/tile/bug').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Change]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphChange" */'@atlaskit/object/changes'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphChange" */'@atlaskit/object/tile/changes').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Epic]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphEpic" */'@atlaskit/object/epic'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphEpic" */'@atlaskit/object/tile/epic').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Incident]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphIncident" */'@atlaskit/object/incident'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphIncident" */'@atlaskit/object/tile/incident').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Problem]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphProblem" */'@atlaskit/object/problem'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphProblem" */'@atlaskit/object/tile/problem').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.ServiceRequest]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphServiceRequest" */'@atlaskit/object/work-item'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphServiceRequest" */'@atlaskit/object/tile/work-item').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Story]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphStory" */'@atlaskit/object/story'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphStory" */'@atlaskit/object/tile/story').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.SubTask]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSubTask" */'@atlaskit/object/subtask'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphSubTask" */'@atlaskit/object/tile/subtask').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    [IconType.Task]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphTask" */'@atlaskit/object/task'),
      large: () => import( /* webpackChunkName: "@atlaskit-internal_glyphTask" */'@atlaskit/object/tile/task').then(module => ({
        default: props => /*#__PURE__*/React.createElement(module.default, _extends({}, props, {
          size: "small"
        }))
      }))
    },
    // @atlaskit/logo/* (product logos)
    [IconType.Confluence]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphConfluence" */'@atlaskit/logo/confluence-icon').then(({
        ConfluenceIcon
      }) => ({
        default: ConfluenceIcon
      }))
    },
    [IconType.Jira]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphJira" */'@atlaskit/logo/jira-icon').then(({
        JiraIcon
      }) => ({
        default: JiraIcon
      }))
    },
    // Local priority icons (custom SVG components)
    [IconType.PriorityBlocker]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphBlocker" */'../common/ui/icons/priority-blocker-icon')
    },
    [IconType.PriorityCritical]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphCritical" */'../common/ui/icons/priority-critical-icon')
    },
    [IconType.PriorityHigh]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphHigh" */'../common/ui/icons/priority-high-icon')
    },
    [IconType.PriorityHighest]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphHighest" */'../common/ui/icons/priority-highest-icon')
    },
    [IconType.PriorityLow]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphLow" */'../common/ui/icons/priority-low-icon')
    },
    [IconType.PriorityLowest]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphLowest" */'../common/ui/icons/priority-lowest-icon')
    },
    [IconType.PriorityMajor]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMajor" */'../common/ui/icons/priority-major-icon')
    },
    [IconType.PriorityMedium]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMedium" */'../common/ui/icons/priority-medium-icon')
    },
    [IconType.PriorityMinor]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphMinor" */'../common/ui/icons/priority-minor-icon')
    },
    [IconType.PriorityTrivial]: {
      default: () => import( /* webpackChunkName: "@atlaskit-internal_glyphTrivial" */'../common/ui/icons/priority-trivial-icon')
    }
  };
};
const getTypeToIconMap = fileFormat => {
  const iconDescriptor = extractFileFormatIcon(fileFormat);
  if (!(iconDescriptor !== null && iconDescriptor !== void 0 && iconDescriptor.icon)) {
    return null;
  }
  const lazyIcons = getLazyIcons();
  if (fg('platform_navx_smart_link_icon_label_a11y')) {
    var _lazyIcons$iconDescri, _iconDescriptor$label;
    return [(_lazyIcons$iconDescri = lazyIcons[iconDescriptor.icon]) === null || _lazyIcons$iconDescri === void 0 ? void 0 : _lazyIcons$iconDescri.default, (_iconDescriptor$label = iconDescriptor.label) !== null && _iconDescriptor$label !== void 0 ? _iconDescriptor$label : ''];
  } else {
    var _lazyIcons$iconDescri2;
    return [(_lazyIcons$iconDescri2 = lazyIcons[iconDescriptor.icon]) === null || _lazyIcons$iconDescri2 === void 0 ? void 0 : _lazyIcons$iconDescri2.default];
  }
};
export const getIframeSandboxAttribute = isTrusted => {
  if (isTrusted) {
    return undefined;
  }
  const sandboxPermissions = 'allow-downloads allow-forms allow-popups allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-scripts';
  return sandboxPermissions;
};
export const handleOnClick = handler => e => {
  e.preventDefault();
  e.stopPropagation();
  handler();
};
export const isIntersectionObserverSupported = () => typeof IntersectionObserver !== 'undefined';
export const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms));
};
export const importWithRetry = async (importFn, retries = 2, interval = 500) => {
  try {
    return await importFn();
  } catch (error) {
    if (error.name === 'ChunkLoadError' && retries > 0) {
      await sleep(interval);
      return importWithRetry(importFn, retries - 1, interval);
    } else {
      throw error;
    }
  }
};
export const downloadUrl = async url => {
  if (!url) {
    return;
  }
  const isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
  const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  const iframeName = 'media-download-iframe';
  const link = document.createElement('a');
  let iframe = document.getElementById(iframeName);
  if (!iframe) {
    iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    iframe.id = iframeName;
    iframe.name = iframeName;
    document.body.appendChild(iframe);
  }
  link.href = url;
  link.download = url;
  link.target = isIE11 || isSafari ? '_blank' : iframeName;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};
export const openUrl = async url => {
  if (!url) {
    return;
  }
  window.open(url, '_blank', 'noopener=yes');
};

/**
 * Append a theme to the URL if it exists
 * @param previewUrl
 * @param themeState
 */
export const getPreviewUrlWithTheme = (previewUrl, themeState) => {
  try {
    const url = new URL(previewUrl);
    url.searchParams.append('themeState', themeObjectToString(themeState));
    return url.href;
  } catch {
    return previewUrl;
  }
};
export const isIconSizeLarge = size => size && [SmartLinkSize.Large, SmartLinkSize.XLarge].includes(size);
export const isProfileType = type => !!(type && type.includes('Profile'));
export const noop = () => undefined;