var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/compensatedPisellContainer/index.tsx
var compensatedPisellContainer_exports = {};
__export(compensatedPisellContainer_exports, {
  CompensatedPisellContainer: () => CompensatedPisellContainer,
  default: () => compensatedPisellContainer_default
});
module.exports = __toCommonJS(compensatedPisellContainer_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_index = require("./index.less");
var getBodyScaleY = () => {
  if (typeof window === "undefined" || typeof document === "undefined") {
    return 1;
  }
  const transform = window.getComputedStyle(document.body).transform;
  if (!transform || transform === "none") {
    return 1;
  }
  const matrix = new DOMMatrixReadOnly(transform);
  return Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d) || 1;
};
var compensateLength = (value, scaleY) => {
  if (!value || scaleY === 1) {
    return value;
  }
  if (typeof value === "number") {
    return value / scaleY;
  }
  if (typeof value !== "string") {
    return value;
  }
  const match = value.trim().match(/^(-?\d+(?:\.\d+)?)(px|vh|dvh|svh|lvh)$/);
  if (!match) {
    return value;
  }
  return `${Number(match[1]) / scaleY}${match[2]}`;
};
var hasLengthValue = (value) => {
  return value !== void 0 && value !== null && value !== "";
};
var getSafeAreaTopInset = () => {
  if (typeof window === "undefined" || typeof document === "undefined" || !document.body) {
    return 0;
  }
  const probe = document.createElement("div");
  probe.style.cssText = [
    "position:absolute",
    "visibility:hidden",
    "pointer-events:none",
    "height:constant(safe-area-inset-top)",
    "height:env(safe-area-inset-top)"
  ].join(";");
  document.body.appendChild(probe);
  const height = Number.parseFloat(window.getComputedStyle(probe).height) || 0;
  document.body.removeChild(probe);
  return height;
};
var subtractSafeAreaTop = (value, safeAreaTopInset, scaleY) => {
  if (!value || !safeAreaTopInset) {
    return value;
  }
  const safeAreaTopHeight = safeAreaTopInset / scaleY;
  if (typeof value === "number") {
    return Math.max(value - safeAreaTopHeight, 0);
  }
  if (typeof value !== "string") {
    return value;
  }
  const trimmedValue = value.trim();
  const canUseCalc = /^-?\d+(?:\.\d+)?(px|%|vh|dvh|svh|lvh|rem|em)$/.test(trimmedValue) || /^(calc|min|max|clamp)\(.+\)$/.test(trimmedValue);
  return canUseCalc ? `calc(${trimmedValue} - ${safeAreaTopHeight}px)` : value;
};
var getIsNativePlatform = (isNativePlatform) => {
  if (typeof isNativePlatform === "function") {
    return isNativePlatform();
  }
  return Boolean(isNativePlatform);
};
var useBodyScaleY = (enabled) => {
  const [bodyScaleY, setBodyScaleY] = (0, import_react.useState)(getBodyScaleY);
  (0, import_react.useEffect)(() => {
    var _a;
    if (!enabled || typeof window === "undefined" || typeof document === "undefined") {
      return void 0;
    }
    const updateBodyScaleY = () => {
      setBodyScaleY(getBodyScaleY());
    };
    updateBodyScaleY();
    window.addEventListener("resize", updateBodyScaleY);
    (_a = window.visualViewport) == null ? void 0 : _a.addEventListener("resize", updateBodyScaleY);
    const bodyObserver = new MutationObserver(updateBodyScaleY);
    bodyObserver.observe(document.body, {
      attributes: true,
      attributeFilter: ["style", "class"]
    });
    return () => {
      var _a2;
      window.removeEventListener("resize", updateBodyScaleY);
      (_a2 = window.visualViewport) == null ? void 0 : _a2.removeEventListener("resize", updateBodyScaleY);
      bodyObserver.disconnect();
    };
  }, [enabled]);
  return enabled ? bodyScaleY : 1;
};
var useSafeAreaTopInset = (enabled) => {
  const [safeAreaTopInset, setSafeAreaTopInset] = (0, import_react.useState)(0);
  (0, import_react.useEffect)(() => {
    var _a;
    if (!enabled || typeof window === "undefined" || typeof document === "undefined") {
      setSafeAreaTopInset(0);
      return void 0;
    }
    const updateSafeAreaTopInset = () => {
      setSafeAreaTopInset(getSafeAreaTopInset());
    };
    updateSafeAreaTopInset();
    window.addEventListener("resize", updateSafeAreaTopInset);
    (_a = window.visualViewport) == null ? void 0 : _a.addEventListener("resize", updateSafeAreaTopInset);
    return () => {
      var _a2;
      window.removeEventListener("resize", updateSafeAreaTopInset);
      (_a2 = window.visualViewport) == null ? void 0 : _a2.removeEventListener("resize", updateSafeAreaTopInset);
    };
  }, [enabled]);
  return enabled ? safeAreaTopInset : 0;
};
var CompensatedPisellContainer = ({
  className,
  showHandlerBar,
  onHandlerBarClick,
  handlerBarAriaLabel = "Close",
  handlerBarClassName,
  children,
  ...props
}) => {
  var _a, _b;
  const isDrawer = props.renderType === "drawer";
  const drawerProps = props.props || {};
  const context = (0, import_useEngineContext.default)();
  const { isNativePlatform } = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
  const isNative = getIsNativePlatform(isNativePlatform);
  const shouldDeductSafeAreaTop = isDrawer && isNative && hasLengthValue((_b = drawerProps.style) == null ? void 0 : _b.maxHeight);
  const bodyScaleY = useBodyScaleY(isDrawer);
  const safeAreaTopInset = useSafeAreaTopInset(shouldDeductSafeAreaTop);
  const isDrawerOpen = drawerProps.open !== false;
  const handleHandlerBarClick = onHandlerBarClick || drawerProps.onClose || drawerProps.onCancel;
  const handlerBarNode = showHandlerBar ? /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: (0, import_classnames.default)(
        "pisell-compensated-container-handler",
        {
          "pisell-compensated-container-handler-open": isDrawerOpen,
          "pisell-compensated-container-handler-closed": !isDrawerOpen
        },
        handlerBarClassName
      ),
      "aria-label": handlerBarAriaLabel,
      onClick: handleHandlerBarClick
    },
    /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-compensated-container-handler-icon" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-chevron-down-double" }))
  ) : null;
  const compensatedProps = (0, import_react.useMemo)(() => {
    if (!isDrawer) {
      return {
        ...props,
        children
      };
    }
    const drawerTitle = showHandlerBar ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, handlerBarNode, drawerProps.title ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-compensated-container-title" }, drawerProps.title) : null) : drawerProps.title;
    const { style: drawerStyle, ...restDrawerProps } = drawerProps;
    delete restDrawerProps.maxHeight;
    const rawMaxHeight = drawerStyle == null ? void 0 : drawerStyle.maxHeight;
    const compensatedMaxHeight = subtractSafeAreaTop(
      compensateLength(rawMaxHeight, bodyScaleY),
      safeAreaTopInset,
      bodyScaleY
    );
    const nextDrawerStyle = hasLengthValue(compensatedMaxHeight) ? {
      ...drawerStyle,
      maxHeight: compensatedMaxHeight
    } : drawerStyle;
    const nextDrawerProps = {
      ...restDrawerProps,
      rootClassName: (0, import_classnames.default)(
        "pisell-compensated-container",
        drawerProps.rootClassName
      ),
      className: (0, import_classnames.default)(
        drawerProps.className,
        {
          "pisell-compensated-drawer-content-with-handler": showHandlerBar
        }
      ),
      closable: showHandlerBar ? false : drawerProps.closable,
      title: drawerTitle,
      height: compensateLength(drawerProps.height, bodyScaleY),
      style: nextDrawerStyle
    };
    return {
      ...props,
      props: nextDrawerProps,
      children
    };
  }, [bodyScaleY, children, drawerProps, handlerBarNode, isDrawer, props, safeAreaTopInset, showHandlerBar]);
  return /* @__PURE__ */ import_react.default.createElement(import_materials.PisellContainer, { ...compensatedProps }, children);
};
var compensatedPisellContainer_default = CompensatedPisellContainer;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CompensatedPisellContainer
});
