import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import { OutsideClickTargetRefContext, withReactEditorViewOuterListeners } from '../../ui-react';
import DropdownList from '../../ui/DropList';
import Popup from '../../ui/Popup';
import { calculatePlacement } from '../../ui/Popup/utils';
import { ArrowKeyNavigationProvider } from '../ArrowKeyNavigationProvider';
/**
 * Wrapper around @atlaskit/droplist which uses Popup and Portal to render
 * droplist outside of "overflow: hidden" containers when needed.
 *
 * Also it controls popper's placement.
 */
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
export class Dropdown extends PureComponent {
  constructor(props) {
    super(props);
    _defineProperty(this, "handleRef", setOutsideClickTargetRef => target => {
      setOutsideClickTargetRef(target);
      this.setState({
        target: target || undefined
      });
    });
    _defineProperty(this, "updatePopupPlacement", placement => {
      this.setState({
        popupPlacement: placement
      });
    });
    _defineProperty(this, "handleCloseAndFocus", event => {
      var _this$state$target, _this$state$target$qu;
      (_this$state$target = this.state.target) === null || _this$state$target === void 0 ? void 0 : (_this$state$target$qu = _this$state$target.querySelector('button')) === null || _this$state$target$qu === void 0 ? void 0 : _this$state$target$qu.focus();
      this.handleClose(event);
    });
    _defineProperty(this, "handleClose", event => {
      if (this.props.onOpenChange) {
        this.props.onOpenChange({
          isOpen: false,
          event
        });
      }
    });
    this.state = {
      popupPlacement: ['bottom', 'left']
    };
  }
  componentDidUpdate(prevProps) {
    if (!prevProps.isOpen && this.props.isOpen && this.state.target) {
      // Dropdown flickers when opens as placement is calculated in Popup component and updated after the first render.
      // popupPlacement is set to ['bottom', 'left'] by default, but it may not be the correct placement and is required in DropdownList.
      // To avoid flicker we calculate placement here and set it to state when the dropdown opens.
      const initialPlacement = calculatePlacement(this.state.target, this.props.boundariesElement || document.body, this.props.fitWidth, this.props.fitHeight, this.props.alignX, this.props.alignY, this.props.forcePlacement);
      this.setState({
        popupPlacement: initialPlacement
      });
    }
  }
  renderDropdown() {
    const {
      target,
      popupPlacement
    } = this.state;
    const {
      children,
      mountTo,
      boundariesElement,
      scrollableElement,
      onOpenChange,
      fitHeight,
      fitWidth,
      zIndex,
      arrowKeyNavigationProviderOptions,
      dropdownListId,
      alignDropdownWithParentElement,
      target: targetProp,
      forcePlacement,
      alignX,
      alignY,
      offset,
      shouldFitContainer = true
    } = this.props;
    return /*#__PURE__*/React.createElement(Popup, {
      target: targetProp !== null && targetProp !== void 0 ? targetProp : alignDropdownWithParentElement ? // Ignored via go/ees005
      // eslint-disable-next-line @atlaskit/editor/no-as-casting
      target === null || target === void 0 ? void 0 : target.closest("[data-testid='editor-floating-toolbar']") : target,
      mountTo: mountTo,
      boundariesElement: boundariesElement,
      scrollableElement: scrollableElement,
      onPlacementChanged: forcePlacement ? undefined : this.updatePopupPlacement,
      fitHeight: fitHeight,
      fitWidth: fitWidth,
      zIndex: zIndex,
      allowOutOfBounds: alignDropdownWithParentElement,
      alignX: alignX,
      alignY: alignY,
      forcePlacement: forcePlacement,
      offset: offset
    }, /*#__PURE__*/React.createElement(ArrowKeyNavigationProvider
    // Ignored via go/ees005
    // eslint-disable-next-line react/jsx-props-no-spreading
    , _extends({}, arrowKeyNavigationProviderOptions, {
      closeOnTab: true,
      handleClose: this.handleCloseAndFocus
    }), /*#__PURE__*/React.createElement("div", {
      style: {
        height: 0,
        minWidth: fitWidth || 0
      }
    }, /*#__PURE__*/React.createElement(DropdownList, {
      isOpen: true,
      onOpenChange: onOpenChange,
      position: popupPlacement.join(' '),
      shouldFitContainer: shouldFitContainer,
      id: dropdownListId
    }, children))));
  }
  render() {
    const {
      trigger,
      isOpen
    } = this.props;
    return trigger ? /*#__PURE__*/React.createElement(OutsideClickTargetRefContext.Consumer, null, setOutsideClickTargetRef => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      ref: this.handleRef(setOutsideClickTargetRef)
    }, trigger), isOpen ? this.renderDropdown() : null)) : /*#__PURE__*/React.createElement(React.Fragment, null, isOpen ? this.renderDropdown() : null);
  }
}
const DropdownWithOuterListeners = withReactEditorViewOuterListeners(Dropdown);
export default DropdownWithOuterListeners;