"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Dropdown = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _uiReact = require("../../ui-react");
var _DropList = _interopRequireDefault(require("../../ui/DropList"));
var _Popup = _interopRequireDefault(require("../../ui/Popup"));
var _ArrowKeyNavigationProvider = require("../ArrowKeyNavigationProvider");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
/**
 * 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.
 */
var Dropdown = exports.Dropdown = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2.default)(Dropdown, _PureComponent);
  var _super = _createSuper(Dropdown);
  function Dropdown(props) {
    var _this;
    (0, _classCallCheck2.default)(this, Dropdown);
    _this = _super.call(this, props);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleRef", function (target) {
      _this.setState({
        target: target || undefined
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "updatePopupPlacement", function (placement) {
      _this.setState({
        popupPlacement: placement
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleCloseAndFocus", function (event) {
      var _this$state$target;
      (_this$state$target = _this.state.target) === null || _this$state$target === void 0 || (_this$state$target = _this$state$target.querySelector('button')) === null || _this$state$target === void 0 || _this$state$target.focus();
      _this.handleClose(event);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleClose", function (event) {
      if (_this.props.onOpenChange) {
        _this.props.onOpenChange({
          isOpen: false,
          event: event
        });
      }
    });
    _this.state = {
      popupPlacement: ['bottom', 'left']
    };
    return _this;
  }
  (0, _createClass2.default)(Dropdown, [{
    key: "renderDropdown",
    value: function renderDropdown() {
      var _this$state = this.state,
        target = _this$state.target,
        popupPlacement = _this$state.popupPlacement;
      var _this$props = this.props,
        children = _this$props.children,
        mountTo = _this$props.mountTo,
        boundariesElement = _this$props.boundariesElement,
        scrollableElement = _this$props.scrollableElement,
        onOpenChange = _this$props.onOpenChange,
        fitHeight = _this$props.fitHeight,
        fitWidth = _this$props.fitWidth,
        zIndex = _this$props.zIndex,
        arrowKeyNavigationProviderOptions = _this$props.arrowKeyNavigationProviderOptions,
        dropdownListId = _this$props.dropdownListId,
        alignDropdownWithParentElement = _this$props.alignDropdownWithParentElement;
      return /*#__PURE__*/_react.default.createElement(_Popup.default, {
        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.default.createElement(_ArrowKeyNavigationProvider.ArrowKeyNavigationProvider, (0, _extends2.default)({}, arrowKeyNavigationProviderOptions, {
        closeOnTab: true,
        handleClose: this.handleCloseAndFocus
      }), /*#__PURE__*/_react.default.createElement("div", {
        style: {
          height: 0,
          minWidth: fitWidth || 0
        }
      }, /*#__PURE__*/_react.default.createElement(_DropList.default, {
        isOpen: true,
        onOpenChange: onOpenChange,
        position: popupPlacement.join(' '),
        shouldFitContainer: true,
        id: dropdownListId
      }, children))));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        trigger = _this$props2.trigger,
        isOpen = _this$props2.isOpen;
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
        ref: this.handleRef
      }, trigger), isOpen ? this.renderDropdown() : null);
    }
  }]);
  return Dropdown;
}(_react.PureComponent);
var DropdownWithOuterListeners = (0, _uiReact.withReactEditorViewOuterListeners)(Dropdown);
var _default = exports.default = DropdownWithOuterListeners;