"use strict";

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

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

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

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

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

var _Fixed = require("../Fixed");

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

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

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 Snackbar = props => {
  const theme = (0, _useTheme.default)();
  const {
    id = "modal",
    type = "fixed",
    open = false,
    spacing = 2,
    position = "bottom center",
    duration = 200,
    animation = "fadeInBottom",
    fullWidth,
    fullHeight,
    backdrop = false,
    backdropColor = "basic",
    onBackdropClick = () => {},
    backdropProps = {},
    portalProps = {},
    noPortal = false,
    style = {},
    children,
    ...otherProps
  } = props;
  const [animationTrigger, setAnimationTrigger] = (0, _react.useState)(open);
  const [isOpen, setIsOpen] = (0, _react.useState)(open);
  (0, _react.useEffect)(() => {
    if (open) {
      setIsOpen(open);
      setTimeout(() => {
        setAnimationTrigger(true);
      }, 10);
    } else {
      setAnimationTrigger(false);
      setTimeout(() => {
        setIsOpen(open);
      }, duration);
    }
  }, [open]);

  function handleValue(value) {
    return value ? typeof value === "number" ? `${value * theme.layout.spacingUnit}px` : value : "0px";
  }

  const Component = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [backdrop && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Backdrop.Backdrop, {
      visible: animationTrigger,
      duration: (backdropProps == null ? void 0 : backdropProps.duration) || duration || 300,
      color: (backdropProps == null ? void 0 : backdropProps.color) || backdropColor || "basic",
      onClick: (backdropProps == null ? void 0 : backdropProps.onClick) || onBackdropClick,
      noPortal: (backdropProps == null ? void 0 : backdropProps.noPortal) || true,
      type: (backdropProps == null ? void 0 : backdropProps.type) || "fixed",
      ...backdropProps
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Fixed.Fixed, {
      type: type,
      position: position,
      spacing: 0,
      style: {
        width: fullWidth ? "100%" : "auto",
        height: fullHeight ? "100%" : "auto",
        margin: handleValue(spacing)
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Animated.Animated, {
        visible: animationTrigger,
        animation: animation,
        duration: duration,
        style: {
          pointerEvents: "all",
          zIndex: theme.layout.modalIndex,
          width: fullWidth ? "100%" : "auto",
          height: fullHeight ? "100%" : "auto",
          ...style
        },
        ...otherProps,
        children: children
      })
    })]
  });

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

exports.Snackbar = Snackbar;