"use strict";

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

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

var _style = _interopRequireDefault(require("styled-jsx/style"));

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

var _classnames = _interopRequireDefault(require("classnames"));

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

var _isThemePalette = _interopRequireDefault(require("../../utils/isThemePalette"));

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

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
const Backdrop = props => {
  const theme = (0, _useTheme.default)();
  const {
    children,
    duration = 200,
    transition = "ease-in-out",
    visible = true,
    color = "basic",
    noPortal = false,
    type = "absolute",
    portalProps = {},
    className,
    ...otherProps
  } = props;
  const [animationTrigger, setAnimationTrigger] = (0, _react.useState)(visible);
  const [isOpen, setIsOpen] = (0, _react.useState)(visible);
  (0, _react.useEffect)(() => {
    if (visible) {
      setIsOpen(visible);
      setTimeout(() => {
        setAnimationTrigger(true);
      }, 10);
    } else {
      setAnimationTrigger(false);
      setTimeout(() => {
        setIsOpen(visible);
      }, duration);
    }
  }, [visible]);

  function handleBackgroundColor() {
    switch (color) {
      case "text":
        return theme.palette.text;

      case "subtext":
        return theme.palette.subtext;

      case "highlight":
        return theme.palette.highlight;

      case "background":
        return theme.palette.background;

      case "shade":
        return theme.palette.shade;

      default:
        return (0, _isThemePalette.default)(color) ? theme.palette[color].shadow : color;
    }
  }

  const Component = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    "data-testid": "Wps-Backdrop",
    ...otherProps,
    className: _style.default.dynamic([["2710836584", [type, duration, transition, handleBackgroundColor()]]]) + " " + (otherProps && otherProps.className != null && otherProps.className || (0, _classnames.default)("Wps-Backdrop", animationTrigger ? "visible" : "hidden", className) || ""),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_style.default, {
      id: "2710836584",
      dynamic: [type, duration, transition, handleBackgroundColor()],
      children: `.Wps-Backdrop.__jsx-style-dynamic-selector{position:${type};top:0;left:0;width:100%;height:100%;visibility:hidden;-webkit-transition:all ${duration}ms ${transition};transition:all ${duration}ms ${transition};background-color:${handleBackgroundColor()};}.Wps-Backdrop.hidden.__jsx-style-dynamic-selector{visibility:hidden;opacity:0;}.Wps-Backdrop.visible.__jsx-style-dynamic-selector{visibility:visible;opacity:1;}`
    }), children]
  });

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

exports.Backdrop = Backdrop;