"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _react = _interopRequireWildcard(require("react"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _domLib = require("dom-lib");

var _utils = require("../utils");

var omitProps = ['placement', 'arrowOffsetLeft', 'arrowOffsetTop', 'positionLeft', 'positionTop', 'getPositionInstance', 'getToggleInstance', 'autoWidth'];
var resizePlacement = ['topStart', 'topEnd', 'leftEnd', 'rightEnd', 'auto', 'autoVerticalStart', 'autoVerticalEnd', 'autoHorizontalEnd'];

var PickerOverlay = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _props$as = props.as,
      Component = _props$as === void 0 ? 'div' : _props$as,
      _props$classPrefix = props.classPrefix,
      classPrefix = _props$classPrefix === void 0 ? 'picker-menu' : _props$classPrefix,
      autoWidth = props.autoWidth,
      className = props.className,
      placement = props.placement,
      target = props.target,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "classPrefix", "autoWidth", "className", "placement", "target"]);
  var overlayRef = (0, _react.useRef)();
  var handleResize = (0, _react.useCallback)(function () {
    var instance = target === null || target === void 0 ? void 0 : target.current;

    if (instance && resizePlacement.includes(placement)) {
      instance.updatePosition();
    }
  }, [target, placement]);
  (0, _utils.useElementResize)(function () {
    return overlayRef.current;
  }, handleResize);
  (0, _react.useEffect)(function () {
    var toggle = target === null || target === void 0 ? void 0 : target.current;

    if (autoWidth && toggle.root) {
      // Get the width value of the button,
      // and then set it to the menu to make their width consistent.
      var width = (0, _domLib.getWidth)((0, _utils.getDOMNode)(toggle.root));
      (0, _domLib.addStyle)(overlayRef.current, 'min-width', width + "px");
    }
  }, [autoWidth, target, overlayRef]);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      withClassPrefix = _useClassNames.withClassPrefix,
      merge = _useClassNames.merge;

  var classes = merge(className, withClassPrefix());
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, (0, _omit.default)(rest, omitProps), {
    ref: (0, _utils.mergeRefs)(overlayRef, ref),
    className: classes
  }));
});

PickerOverlay.displayName = 'PickerOverlay';
var _default = PickerOverlay;
exports.default = _default;