"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _box = _interopRequireDefault(require("../../templates/box"));
var _useDropElement = _interopRequireDefault(require("../../../hooks/useDropElement"));
var _useKeyboardEsc = _interopRequireDefault(require("../../../hooks/useKeyboardEsc"));
var _useOutsideClick = _interopRequireDefault(require("../../../hooks/useOutsideClick"));
var _useForwardRef2 = _interopRequireDefault(require("../../../hooks/useForwardRef"));
var _useDimensionChange = _interopRequireDefault(require("./useDimensionChange"));
var _useMakeUpdatePosition = _interopRequireDefault(require("./useMakeUpdatePosition"));
var _container = _interopRequireDefault(require("./container"));
var _backdropBlur = _interopRequireDefault(require("../../templates/layer/mixins/backdropBlur"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _excluded = ["backdrop", "target", "align", "stretch", "onClickOutside", "onEsc", "children", "canHideTarget", "keepHorizontal", "dataDrop", "ref", "backdropProps"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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; }
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); }
var Backdrop = (0, _styledComponents["default"])(_box["default"]).attrs(function (props) {
  var _extends2;
  return _extends((_extends2 = {
    background: "rgba(0, 0, 0, 0.3)",
    zIndex: 60
  }, _extends2["data-drop"] = "drop-backdrop", _extends2), props);
}).withConfig({
  displayName: "drop__Backdrop",
  componentId: "sc-1lp2o35-0"
})(["position:absolute;inset:0;pointer-events:all;", ";}"], _backdropBlur["default"]);
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,
    _ref$backdropProps = _ref.backdropProps,
    backdropProps = _ref$backdropProps === void 0 ? {} : _ref$backdropProps,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var updatePosition = (0, _useMakeUpdatePosition["default"])(target, ref, defaultAlign, stretch, canHideTarget, keepHorizontal);
  (0, _react.useLayoutEffect)(function () {
    updatePosition();
  }, [updatePosition]);
  (0, _useDimensionChange["default"])(target, updatePosition, ref);
  (0, _useOutsideClick["default"])(ref, onClickOutside, target, backdrop, dataDrop);
  (0, _useKeyboardEsc["default"])(onEsc);
  var el = (0, _useDropElement["default"])();
  return /*#__PURE__*/_reactDom["default"].createPortal(backdrop ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_container["default"], _extends({
    ref: setRef,
    width: {
      max: "100%"
    },
    column: true,
    "data-testid": "drop",
    "data-drop": dataDrop
  }, rest), children), /*#__PURE__*/_react["default"].createElement(Backdrop, _extends({
    onClick: onClickOutside
  }, backdropProps))) : /*#__PURE__*/_react["default"].createElement(_container["default"], _extends({
    ref: setRef,
    width: {
      max: "100%"
    },
    column: true,
    "data-testid": "drop",
    "data-drop": dataDrop
  }, rest), children), el);
};
var _default = exports["default"] = Drop;