import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useCallback, useRef, useState } from 'react';
import ModalDialog, { ModalBody, ModalTransition } from '@atlaskit/modal-dialog';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
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';
var toSize = function toSize(width) {
  return width === MAX_MODAL_SIZE ? EmbedModalSize.Large : EmbedModalSize.Small;
};
var toWidth = function toWidth(size) {
  return size === EmbedModalSize.Large ? MAX_MODAL_SIZE : MIN_MODAL_SIZE;
};
var EmbedModal = function EmbedModal(_ref) {
  var invokeDownloadAction = _ref.invokeDownloadAction,
    fireEvent = _ref.fireEvent,
    iframeName = _ref.iframeName,
    isSupportTheming = _ref.isSupportTheming,
    _ref$isTrusted = _ref.isTrusted,
    isTrusted = _ref$isTrusted === void 0 ? false : _ref$isTrusted,
    linkIcon = _ref.linkIcon,
    onClose = _ref.onClose,
    onOpen = _ref.onOpen,
    onResize = _ref.onResize,
    providerName = _ref.providerName,
    showModal = _ref.showModal,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? EmbedModalSize.Large : _ref$size,
    src = _ref.src,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-embed-preview-modal' : _ref$testId,
    title = _ref.title,
    invokeViewAction = _ref.invokeViewAction,
    extensionKey = _ref.extensionKey,
    isBlanketHidden = _ref.isBlanketHidden;
  var defaultWidth = toWidth(size);
  var _useState = useState(showModal),
    _useState2 = _slicedToArray(_useState, 2),
    isOpen = _useState2[0],
    setIsOpen = _useState2[1];
  var _useState3 = useState(defaultWidth),
    _useState4 = _slicedToArray(_useState3, 2),
    width = _useState4[0],
    setWidth = _useState4[1];
  var openAt = useRef();
  var invoke = useInvokeClientAction({
    fireEvent: fireEvent
  });
  var handleOnOpenComplete = useCallback(function () {
    openAt.current = Date.now();
    if (onOpen) {
      onOpen({
        size: size
      });
    }
  }, [onOpen, size]);
  var handleOnClose = useCallback(function () {
    return setIsOpen(false);
  }, []);
  var handleOnCloseComplete = useCallback(function () {
    if (onClose) {
      var duration = openAt.current ? Date.now() - openAt.current : undefined;
      onClose({
        duration: duration,
        size: toSize(width)
      });
    }
  }, [onClose, width]);
  var handleOnResizeClick = useCallback(function () {
    var newWidth = width === MIN_MODAL_SIZE ? MAX_MODAL_SIZE : MIN_MODAL_SIZE;
    setWidth(newWidth);
    if (onResize) {
      onResize({
        size: toSize(newWidth)
      });
    }
    if (invokeViewAction) {
      var visitedDisplay = invokeViewAction.display && invokeViewAction.display !== 'url' ? invokeViewAction.display : null;
      if (visitedDisplay && expValEquals('cc_integrations_editor_open_link_click_analytics', 'isEnabled', true)) {
        var _invokeViewAction$id, _invokeViewAction$def;
        fireEvent === null || fireEvent === void 0 || fireEvent('track.smartLink.visited', {
          id: (_invokeViewAction$id = invokeViewAction.id) !== null && _invokeViewAction$id !== void 0 ? _invokeViewAction$id : iframeName,
          display: visitedDisplay,
          definitionId: (_invokeViewAction$def = invokeViewAction.definitionId) !== null && _invokeViewAction$def !== void 0 ? _invokeViewAction$def : null
        });
      }
    }
  }, [fireEvent, iframeName, invokeViewAction, onResize, width]);
  var themeState = useThemeObserver();
  var previewUrl = src;
  var handleOnViewActionClick = useCallback(function () {
    if (invokeViewAction) {
      invoke(invokeViewAction);
      var visitedDisplay = invokeViewAction.display && invokeViewAction.display !== 'url' ? invokeViewAction.display : null;
      if (visitedDisplay && expValEquals('cc_integrations_editor_open_link_click_analytics', 'isEnabled', true)) {
        var _invokeViewAction$id2, _invokeViewAction$def2;
        fireEvent === null || fireEvent === void 0 || fireEvent('track.smartLink.visited', {
          id: (_invokeViewAction$id2 = invokeViewAction.id) !== null && _invokeViewAction$id2 !== void 0 ? _invokeViewAction$id2 : iframeName,
          display: visitedDisplay,
          definitionId: (_invokeViewAction$def2 = invokeViewAction.definitionId) !== null && _invokeViewAction$def2 !== void 0 ? _invokeViewAction$def2 : null
        });
      }
    }
  }, [fireEvent, iframeName, invoke, invokeViewAction]);
  var handleOnDownloadActionClick = useCallback(function () {
    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
  }))));
};
var _default_1 = withAnalytics(withErrorBoundary(EmbedModal));
export default _default_1;