import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useRef, useState } from 'react';
import ModalDialog, { ModalBody, ModalTransition } from '@atlaskit/modal-dialog';
import { useThemeObserver } from '@atlaskit/tokens';
import { SmartLinkSize } from '../../constants';
import useInvokeClientAction from '../../state/hooks/use-invoke-client-action';
import { getPreviewUrlWithTheme } from '../../utils';
import { BaseIconElement } from '../FlexibleCard/components/elements/common';
import withAnalytics from './components/analytics';
import EmbedContent from './components/embed-content';
import withErrorBoundary from './components/error-boundary';
import LinkInfo from './components/link-info';
import { MAX_MODAL_SIZE, MIN_MODAL_SIZE } from './constants';
import { EmbedModalSize } from './types';
const toSize = width => width === MAX_MODAL_SIZE ? EmbedModalSize.Large : EmbedModalSize.Small;
const toWidth = size => size === EmbedModalSize.Large ? MAX_MODAL_SIZE : MIN_MODAL_SIZE;
const EmbedModal = ({
  invokeDownloadAction,
  fireEvent,
  iframeName,
  isSupportTheming,
  isTrusted = false,
  linkIcon,
  onClose,
  onOpen,
  onResize,
  providerName,
  showModal,
  size = EmbedModalSize.Large,
  src,
  testId = 'smart-embed-preview-modal',
  title,
  invokeViewAction,
  extensionKey,
  isBlanketHidden
}) => {
  const defaultWidth = toWidth(size);
  const [isOpen, setIsOpen] = useState(showModal);
  const [width, setWidth] = useState(defaultWidth);
  const openAt = useRef();
  const invoke = useInvokeClientAction({
    fireEvent
  });
  const handleOnOpenComplete = useCallback(() => {
    openAt.current = Date.now();
    if (onOpen) {
      onOpen({
        size
      });
    }
  }, [onOpen, size]);
  const handleOnClose = useCallback(() => setIsOpen(false), []);
  const handleOnCloseComplete = useCallback(() => {
    if (onClose) {
      const duration = openAt.current ? Date.now() - openAt.current : undefined;
      onClose({
        duration,
        size: toSize(width)
      });
    }
  }, [onClose, width]);
  const handleOnResizeClick = useCallback(() => {
    const newWidth = width === MIN_MODAL_SIZE ? MAX_MODAL_SIZE : MIN_MODAL_SIZE;
    setWidth(newWidth);
    if (onResize) {
      onResize({
        size: toSize(newWidth)
      });
    }
  }, [onResize, width]);
  const themeState = useThemeObserver();
  let previewUrl = src;
  const handleOnViewActionClick = useCallback(() => {
    invokeViewAction && invoke(invokeViewAction);
  }, [invoke, invokeViewAction]);
  const handleOnDownloadActionClick = useCallback(() => {
    invokeDownloadAction && invoke(invokeDownloadAction);
  }, [invokeDownloadAction, invoke]);
  if (previewUrl && isSupportTheming) {
    previewUrl = getPreviewUrlWithTheme(previewUrl, themeState);
  }
  return /*#__PURE__*/React.createElement(ModalTransition, null, isOpen && /*#__PURE__*/React.createElement(ModalDialog, {
    height: "100%",
    onClose: handleOnClose,
    onCloseComplete: handleOnCloseComplete,
    onOpenComplete: handleOnOpenComplete,
    testId: testId,
    width: width,
    label: title,
    isBlanketHidden: isBlanketHidden
  }, /*#__PURE__*/React.createElement(LinkInfo, {
    icon: linkIcon && {
      icon: /*#__PURE__*/React.createElement(BaseIconElement, _extends({}, linkIcon, {
        size: SmartLinkSize.Large
      })),
      isFlexibleUi: true
    },
    providerName: providerName,
    onViewButtonClick: invokeViewAction ? handleOnViewActionClick : undefined,
    onDownloadButtonClick: invokeDownloadAction ? handleOnDownloadActionClick : undefined,
    onResizeButtonClick: handleOnResizeClick,
    size: width,
    title: title,
    testId: testId
  }), /*#__PURE__*/React.createElement(ModalBody, null, /*#__PURE__*/React.createElement(EmbedContent, {
    isTrusted: isTrusted,
    name: iframeName,
    src: previewUrl,
    testId: testId,
    ariaLabel: title,
    extensionKey: extensionKey
  }))));
};
const _default_1 = withAnalytics(withErrorBoundary(EmbedModal));
export default _default_1;