"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.PortalContext = void 0;
var _react = require("react");
var _reactDom = require("react-dom");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const LevelContext = /*#__PURE__*/(0, _react.createContext)(0);
const PortalContext = exports.PortalContext = /*#__PURE__*/(0, _react.createContext)(undefined);
/**
 * `Layer` component.
 */
const Layer = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}layer`,
    className,
    children,
    innerRef,
    level = 0,
    ...props
  } = _ref;
  const parentLevel = (0, _react.useContext)(LevelContext);
  const container = (0, _react.useContext)(PortalContext) || document.body;
  const renderChildren = () => {
    if ( /*#__PURE__*/(0, _react.isValidElement)(children)) {
      return /*#__PURE__*/(0, _react.cloneElement)(children, {
        style: {
          ...children.props.style,
          zIndex: parentLevel + level
        }
      });
    }
    return children;
  };
  return /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/(0, _jsxRuntime.jsx)(LevelContext.Provider, {
    value: parentLevel + level,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(baseClassName, className),
      ref: innerRef,
      ...props,
      children: renderChildren()
    })
  }), container);
};
var _default = exports.default = Layer;