var _excluded = ["backdrop", "target", "align", "stretch", "onClickOutside", "onEsc", "children", "canHideTarget", "keepHorizontal", "dataDrop", "ref"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useLayoutEffect } from "react";
import ReactDOM from "react-dom";
import useDropElement from "../../../hooks/useDropElement";
import useKeyboardEsc from "../../../hooks/useKeyboardEsc";
import useOutsideClick from "../../../hooks/useOutsideClick";
import useForwardRef from "../../../hooks/useForwardRef";
import useDimensionChange from "./useDimensionChange";
import useMakeUpdatePosition from "./useMakeUpdatePosition";
import Container from "./container";
import backdropBlur from "../../templates/layer/mixins/backdropBlur";
import styled from "styled-components";
var Backdrop = styled.div.withConfig({
  displayName: "drop__Backdrop",
  componentId: "sc-1lp2o35-0"
})(["position:absolute;inset:0;pointer-events:all;background-color:rgba(0,0,0,0.3);z-index:60;", ";}"], backdropBlur);
var defaultAlignValue = {
  top: "bottom",
  left: "left"
};
var Drop = function Drop(_ref) {
  var _ref$backdrop = _ref.backdrop,
    backdrop = _ref$backdrop === void 0 ? false : _ref$backdrop,
    target = _ref.target,
    _ref$align = _ref.align,
    defaultAlign = _ref$align === void 0 ? defaultAlignValue : _ref$align,
    _ref$stretch = _ref.stretch,
    stretch = _ref$stretch === void 0 ? "width" : _ref$stretch,
    onClickOutside = _ref.onClickOutside,
    onEsc = _ref.onEsc,
    children = _ref.children,
    _ref$canHideTarget = _ref.canHideTarget,
    canHideTarget = _ref$canHideTarget === void 0 ? false : _ref$canHideTarget,
    keepHorizontal = _ref.keepHorizontal,
    _ref$dataDrop = _ref.dataDrop,
    dataDrop = _ref$dataDrop === void 0 ? "drop-content" : _ref$dataDrop,
    parentRef = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useForwardRef = useForwardRef(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var updatePosition = useMakeUpdatePosition(target, ref, defaultAlign, stretch, canHideTarget, keepHorizontal);
  useLayoutEffect(function () {
    updatePosition();
  }, [updatePosition]);
  useDimensionChange(target, updatePosition);
  useOutsideClick(ref, onClickOutside, target, backdrop, dataDrop);
  useKeyboardEsc(onEsc);
  var el = useDropElement();
  return /*#__PURE__*/ReactDOM.createPortal(backdrop ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Container, _extends({
    ref: setRef,
    width: {
      max: "100%"
    },
    column: true,
    "data-testid": "drop",
    "data-drop": dataDrop
  }, rest), children), /*#__PURE__*/React.createElement(Backdrop, {
    onClick: onClickOutside
  })) : /*#__PURE__*/React.createElement(Container, _extends({
    ref: setRef,
    width: {
      max: "100%"
    },
    column: true,
    "data-testid": "drop",
    "data-drop": dataDrop
  }, rest), children), el);
};
export default Drop;