import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
/**
 * @jsxRuntime classic
 * @jsx jsx
 */
import { useCallback, useLayoutEffect, useState } from 'react';
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { css, jsx } from '@emotion/react';
import { useIntl } from 'react-intl';
import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext';
import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import PreferencesIcon from '@atlaskit/icon/core/customize';
import Tooltip from '@atlaskit/tooltip';
import { cardMessages } from '../../messages';
import Dropdown from './Dropdown';
import { StyledButton } from './StyledButton';
import { useLinkOverlayAnalyticsEvents } from './useLinkOverlayAnalyticsEvents';
var buttonWrapperStyles = css({
  position: 'absolute',
  zIndex: 100,
  display: 'inline-flex',
  top: '50%',
  transform: 'translateY(-50%)',
  background: "var(--ds-surface-raised, #FFFFFF)",
  borderRadius: "var(--ds-radius-small, 3px)"
});
var showDropdownThresholdPx = 50;
export var OverlayButton = withAnalyticsContext()(function (_ref) {
  var _docNode$nodeSize;
  var editorView = _ref.editorView,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'link-configure-overlay-button' : _ref$testId,
    _ref$targetElementPos = _ref.targetElementPos,
    targetElementPos = _ref$targetElementPos === void 0 ? 0 : _ref$targetElementPos,
    onDropdownChange = _ref.onDropdownChange,
    onOpenLinkClick = _ref.onOpenLinkClick;
  var _useIntl = useIntl(),
    formatMessage = _useIntl.formatMessage;
  var configureLinkLabel = formatMessage(cardMessages.inlineConfigureLink);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showDropdown = _useState2[0],
    setShowDropdown = _useState2[1];
  var _useLinkOverlayAnalyt = useLinkOverlayAnalyticsEvents(),
    fireActionClickEvent = _useLinkOverlayAnalyt.fireActionClickEvent,
    fireLinkClickEvent = _useLinkOverlayAnalyt.fireLinkClickEvent;
  useLayoutEffect(function () {
    var _domNode;
    var domNode = editorView.nodeDOM(targetElementPos);
    if (((_domNode = domNode) === null || _domNode === void 0 ? void 0 : _domNode.nodeType) === Node.TEXT_NODE) {
      domNode = domNode.parentElement;
    }
    if (domNode instanceof HTMLElement) {
      var _domNode$getBoundingC = domNode.getBoundingClientRect(),
        width = _domNode$getBoundingC.width;
      if (width < showDropdownThresholdPx) {
        setShowDropdown(true);
      }
    }
  }, [editorView, targetElementPos]);
  var docNode = editorView.state.doc.nodeAt(targetElementPos);
  var nodeEnd = targetElementPos + ((_docNode$nodeSize = docNode === null || docNode === void 0 ? void 0 : docNode.nodeSize) !== null && _docNode$nodeSize !== void 0 ? _docNode$nodeSize : 0);
  var isText = docNode === null || docNode === void 0 ? void 0 : docNode.isText;
  var handleConfigureClick = useCallback(function () {
    var tr = editorView.state.tr;
    if (isText) {
      tr.setSelection(TextSelection.create(tr.doc, targetElementPos, Math.min(nodeEnd, tr.doc.nodeSize)));
    } else {
      tr.setSelection(NodeSelection.create(tr.doc, targetElementPos));
    }
    editorView.dispatch(tr);
  }, [editorView, isText, targetElementPos, nodeEnd]);
  var handleConfigureClickWithAnalytics = useCallback(function () {
    fireActionClickEvent('configureLink');
    fireLinkClickEvent();
    handleConfigureClick();
  }, [fireLinkClickEvent, handleConfigureClick, fireActionClickEvent]);
  var _editorView$state$sel = editorView.state.selection,
    from = _editorView$state$sel.from,
    to = _editorView$state$sel.to;
  var isSelected = from === targetElementPos && to === nodeEnd;
  if (!targetElementPos || isSelected) {
    return null;
  }
  return jsx("span", {
    css: buttonWrapperStyles,
    "data-testid": testId
  }, showDropdown ? jsx(Dropdown, {
    testId: testId,
    onConfigureClick: handleConfigureClick,
    onOpenLinkClick: onOpenLinkClick,
    onDropdownChange: onDropdownChange,
    editorView: editorView
  }) : jsx(Tooltip, {
    content: configureLinkLabel,
    hideTooltipOnClick: true,
    testId: "".concat(testId, "-tooltip")
  }, jsx(StyledButton, {
    onClick: handleConfigureClickWithAnalytics,
    iconBefore: jsx(PreferencesIcon, {
      label: configureLinkLabel,
      testId: "".concat(testId, "-configure-icon")
    })
  })));
});