import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import { withReactEditorViewOuterListeners } from '../../ui-react';
import DropdownList from '../../ui/DropList';
import Popup from '../../ui/Popup';
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.
 */
export class Dropdown extends PureComponent {
  constructor(props) {
    super(props);
    _defineProperty(this, "handleRef", 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']
    };
  }
  renderDropdown() {
    const {
      target,
      popupPlacement
    } = this.state;
    const {
      children,
      mountTo,
      boundariesElement,
      scrollableElement,
      onOpenChange,
      fitHeight,
      fitWidth,
      zIndex,
      arrowKeyNavigationProviderOptions,
      dropdownListId,
      alignDropdownWithParentElement
    } = this.props;
    return /*#__PURE__*/React.createElement(Popup, {
      target: alignDropdownWithParentElement ? target === null || target === void 0 ? void 0 : target.closest("[data-testid='editor-floating-toolbar']") : target,
      mountTo: mountTo,
      boundariesElement: boundariesElement,
      scrollableElement: scrollableElement,
      onPlacementChanged: this.updatePopupPlacement,
      fitHeight: fitHeight,
      fitWidth: fitWidth,
      zIndex: zIndex,
      allowOutOfBounds: alignDropdownWithParentElement
    }, /*#__PURE__*/React.createElement(ArrowKeyNavigationProvider, _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: true,
      id: dropdownListId
    }, children))));
  }
  render() {
    const {
      trigger,
      isOpen
    } = this.props;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      ref: this.handleRef
    }, trigger), isOpen ? this.renderDropdown() : null);
  }
}
const DropdownWithOuterListeners = withReactEditorViewOuterListeners(Dropdown);
export default DropdownWithOuterListeners;