"use strict";

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

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

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

var _useTheme = _interopRequireDefault(require("../../hooks/useTheme"));

var _Portal = require("../Portal");

var _Flex = require("../Flex");

var _Animated = require("../Animated");

var _Backdrop = require("../Backdrop");

var _useScrollBlock = _interopRequireDefault(require("../../hooks/useScrollBlock"));

var _useKeyPress = _interopRequireDefault(require("../../hooks/useKeyPress"));

var _jsxRuntime = require("react/jsx-runtime");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// Generated with util/create-component.js
// component dependencies
const Modal = props => {
  const theme = (0, _useTheme.default)();
  const {
    id = "modal",
    open = false,
    duration = 200,
    position = "fixed",
    animation = "fadeInScale",
    transition = "ease-in-out",
    onCloseEnd = () => {},
    onCloseStart = () => {},
    onOpenEnd = () => {},
    onOpenStart = () => {},
    backdropColor = "basic",
    onBackdropClick = () => {},
    onEscapeKeyDown = () => {},
    disableScrollLock = false,
    closeKey = "Escape",
    backdropProps = {},
    portalProps = {
      style: {}
    },
    containerProps = null,
    noPortal = false,
    children,
    ...otherProps
  } = props;
  const [, setLocked] = (0, _useScrollBlock.default)();
  const escapePress = (0, _useKeyPress.default)(closeKey);
  const [animationTrigger, setAnimationTrigger] = (0, _react.useState)(open);
  const [isOpen, setIsOpen] = (0, _react.useState)(open);
  (0, _react.useEffect)(() => {
    if (open) {
      setIsOpen(open);
      onOpenStart();
      !disableScrollLock && setLocked(true);
      setTimeout(() => {
        setAnimationTrigger(true);
        onOpenEnd();
      }, 10);
    } else {
      setAnimationTrigger(false);
      onCloseStart();
      setTimeout(() => {
        setIsOpen(open);
        onCloseEnd();
        !disableScrollLock && setLocked(false);
      }, duration);
    }
  }, [open]);
  (0, _react.useEffect)(() => {
    if (escapePress) {
      onEscapeKeyDown();
    }
  }, [escapePress]);
  const Component = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Backdrop.Backdrop, {
      visible: animationTrigger,
      duration: (backdropProps == null ? void 0 : backdropProps.duration) || duration,
      color: (backdropProps == null ? void 0 : backdropProps.color) || backdropColor,
      onClick: (backdropProps == null ? void 0 : backdropProps.onClick) || onBackdropClick,
      transition: (backdropProps == null ? void 0 : backdropProps.transition) || transition,
      noPortal: (backdropProps == null ? void 0 : backdropProps.noPortal) || true,
      type: (backdropProps == null ? void 0 : backdropProps.type) || "fixed",
      ...backdropProps
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Flex.Flex, { ...containerProps,
      align: "center",
      justify: "center",
      fullHeight: true,
      fullWidth: true,
      style: {
        position: position,
        top: 0,
        left: 0,
        pointerEvents: "none",
        ...(containerProps == null ? void 0 : containerProps.style)
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Animated.Animated, {
        visible: animationTrigger,
        animation: animation,
        duration: duration,
        transition: transition,
        style: {
          pointerEvents: "all",
          ...otherProps.style
        },
        ...otherProps,
        children: children
      })
    })]
  });

  if (noPortal) {
    return Component;
  } else {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Portal.Portal, {
      id: id,
      visible: isOpen,
      style: {
        zIndex: theme.layout.modalIndex,
        ...portalProps.style
      },
      ...portalProps,
      children: Component
    });
  }
};

exports.Modal = Modal;