"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
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"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 && {}.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 _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 (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
var Backdrop = _styledComponents["default"].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["default"]);
var leftTopAlign = {
  right: "left",
  bottom: "top"
};
var leftBottomAlign = {
  right: "right",
  top: "bottom"
};
var rightTopAlign = {
  left: "right",
  bottom: "top"
};
var rightBottomAlign = {
  left: "left",
  top: "bottom"
};
var getAlign = function getAlign(left, top) {
  if (left && top) return leftTopAlign;
  if (left) return leftBottomAlign;
  if (top) return rightTopAlign;
  return rightBottomAlign;
};
var defaultAlignValue = {
  top: "bottom",
  left: "left"
};
var Drop = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, parentRef) {
  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 ? true : _ref$canHideTarget,
    keepHorizontal = _ref.keepHorizontal,
    _ref$dataDrop = _ref.dataDrop,
    dataDrop = _ref$dataDrop === void 0 ? "drop-content" : _ref$dataDrop,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var _useState = (0, _react.useState)(defaultAlign),
    align = _useState[0],
    setAlign = _useState[1];
  var updatePosition = (0, _useMakeUpdatePosition["default"])(target, ref, align, stretch, canHideTarget, keepHorizontal);
  (0, _react.useLayoutEffect)(function () {
    if (!(target != null && target.getBoundingClientRect) || !ref.current) return;
    var _target$getBoundingCl = target.getBoundingClientRect(),
      targetRight = _target$getBoundingCl.right,
      targetBottom = _target$getBoundingCl.bottom;
    var winHeight = window.innerHeight;
    var winWidth = window.innerWidth;
    var _ref$current$getBound = ref.current.getBoundingClientRect(),
      width = _ref$current$getBound.width,
      height = _ref$current$getBound.height;
    var left = targetRight + width > winWidth;
    var top = targetBottom + height > winHeight;
    setAlign(getAlign(left, top));
  }, [target]);
  (0, _react.useLayoutEffect)(function () {
    updatePosition();
  }, [updatePosition]);
  (0, _useDimensionChange["default"])(target, updatePosition);
  (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, {
    onClick: onClickOutside
  })) : /*#__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;