import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _class, _DropdownMenu;
import React from 'react';
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { PopupMenu } from "../../../internal/PopupMenu";
import { isProductionEnv, isTestEnv } from "../../../lib/currentEnvironment";
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { rootNode } from "../../../lib/rootNode";
import { createPropsGetter } from "../../../lib/createPropsGetter";
import { getDropdownMenuTheme } from "../getDropdownMenuTheme";

/**
 * Выпадающее меню `DropdownMenu` раскрывается по клику на переданный в `caption` элемент.
 */
export var DropdownMenu = rootNode(_class = (_DropdownMenu = /*#__PURE__*/function (_React$Component) {
  function DropdownMenu(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.getProps = createPropsGetter(DropdownMenu.defaultProps);
    _this.popupMenu = null;
    _this.open = function () {
      if (_this.popupMenu) {
        _this.popupMenu.open();
      }
    };
    _this.close = function () {
      if (_this.popupMenu) {
        _this.popupMenu.close();
      }
    };
    _this.refPopupMenu = function (ref) {
      return _this.popupMenu = ref;
    };
    if (!props.caption && !isProductionEnv) {
      throw new Error('Prop "caption" is required!!!');
    }
    return _this;
  }
  _inheritsLoose(DropdownMenu, _React$Component);
  var _proto = DropdownMenu.prototype;
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, function (theme) {
      return /*#__PURE__*/React.createElement(ThemeContext.Provider, {
        value: getDropdownMenuTheme(theme)
      }, _this2.renderMain());
    });
  };
  _proto.renderMain = function renderMain() {
    if (!this.props.caption) {
      return null;
    }
    var _this$getProps = this.getProps(),
      positions = _this$getProps.positions,
      disableAnimations = _this$getProps.disableAnimations;
    return /*#__PURE__*/React.createElement(CommonWrapper, _extends({
      rootNodeRef: this.setRootNode
    }, this.props), /*#__PURE__*/React.createElement(PopupMenu, {
      id: this.props.id,
      "aria-label": this.props['aria-label'],
      ref: this.refPopupMenu,
      caption: this.props.caption,
      menuMaxHeight: this.props.menuMaxHeight,
      menuWidth: this.props.menuWidth,
      preventIconsOffset: this.props.preventIconsOffset,
      popupHasPin: false,
      positions: positions,
      disableAnimations: disableAnimations,
      header: this.props.header,
      footer: this.props.footer,
      width: this.props.width,
      corners: this.props.corners,
      onClose: this.props.onClose,
      onOpen: this.props.onOpen,
      popupMenuId: this.props.popupMenuId
    }, this.props.children));
  };
  return DropdownMenu;
}(React.Component), _DropdownMenu.__KONTUR_REACT_UI__ = 'DropdownMenu', _DropdownMenu.displayName = 'DropdownMenu', _DropdownMenu.defaultProps = {
  disableAnimations: isTestEnv,
  positions: ['bottom left', 'bottom right', 'top left', 'top right']
}, _DropdownMenu)) || _class;