const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/pro/compensatedPisellContainer/index.tsx
const _excluded = [
	"className",
	"showHandlerBar",
	"onHandlerBarClick",
	"handlerBarAriaLabel",
	"handlerBarClassName",
	"children"
], _excluded2 = ["style"];
const 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;
};
const 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]}`;
};
const hasLengthValue = (value) => {
	return value !== void 0 && value !== null && value !== "";
};
const 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;
};
const 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();
	return /^-?\d+(?:\.\d+)?(px|%|vh|dvh|svh|lvh|rem|em)$/.test(trimmedValue) || /^(calc|min|max|clamp)\(.+\)$/.test(trimmedValue) ? `calc(${trimmedValue} - ${safeAreaTopHeight}px)` : value;
};
const getIsNativePlatform = (isNativePlatform) => {
	if (typeof isNativePlatform === "function") return isNativePlatform();
	return Boolean(isNativePlatform);
};
const useBodyScaleY = (enabled) => {
	const [bodyScaleY, setBodyScaleY] = (0, react.useState)(getBodyScaleY);
	(0, react.useEffect)(() => {
		var _window$visualViewpor;
		if (!enabled || typeof window === "undefined" || typeof document === "undefined") return;
		const updateBodyScaleY = () => {
			setBodyScaleY(getBodyScaleY());
		};
		updateBodyScaleY();
		window.addEventListener("resize", updateBodyScaleY);
		(_window$visualViewpor = window.visualViewport) === null || _window$visualViewpor === void 0 || _window$visualViewpor.addEventListener("resize", updateBodyScaleY);
		const bodyObserver = new MutationObserver(updateBodyScaleY);
		bodyObserver.observe(document.body, {
			attributes: true,
			attributeFilter: ["style", "class"]
		});
		return () => {
			var _window$visualViewpor2;
			window.removeEventListener("resize", updateBodyScaleY);
			(_window$visualViewpor2 = window.visualViewport) === null || _window$visualViewpor2 === void 0 || _window$visualViewpor2.removeEventListener("resize", updateBodyScaleY);
			bodyObserver.disconnect();
		};
	}, [enabled]);
	return enabled ? bodyScaleY : 1;
};
const useSafeAreaTopInset = (enabled) => {
	const [safeAreaTopInset, setSafeAreaTopInset] = (0, react.useState)(0);
	(0, react.useEffect)(() => {
		var _window$visualViewpor3;
		if (!enabled || typeof window === "undefined" || typeof document === "undefined") {
			setSafeAreaTopInset(0);
			return;
		}
		const updateSafeAreaTopInset = () => {
			setSafeAreaTopInset(getSafeAreaTopInset());
		};
		updateSafeAreaTopInset();
		window.addEventListener("resize", updateSafeAreaTopInset);
		(_window$visualViewpor3 = window.visualViewport) === null || _window$visualViewpor3 === void 0 || _window$visualViewpor3.addEventListener("resize", updateSafeAreaTopInset);
		return () => {
			var _window$visualViewpor4;
			window.removeEventListener("resize", updateSafeAreaTopInset);
			(_window$visualViewpor4 = window.visualViewport) === null || _window$visualViewpor4 === void 0 || _window$visualViewpor4.removeEventListener("resize", updateSafeAreaTopInset);
		};
	}, [enabled]);
	return enabled ? safeAreaTopInset : 0;
};
const CompensatedPisellContainer = (_ref) => {
	var _context$appHelper, _drawerProps$style;
	let { className, showHandlerBar, onHandlerBarClick, handlerBarAriaLabel = "Close", handlerBarClassName, children } = _ref, props = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const isDrawer = props.renderType === "drawer";
	const drawerProps = props.props || {};
	const context = require_useEngineContext.default();
	const { isNativePlatform } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const isNative = getIsNativePlatform(isNativePlatform);
	const shouldDeductSafeAreaTop = isDrawer && isNative && hasLengthValue((_drawerProps$style = drawerProps.style) === null || _drawerProps$style === void 0 ? void 0 : _drawerProps$style.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__ */ react.default.createElement("button", {
		type: "button",
		className: (0, 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__ */ react.default.createElement("span", { className: "pisell-compensated-container-handler-icon" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-chevron-down-double" }))) : null;
	const compensatedProps = (0, react.useMemo)(() => {
		if (!isDrawer) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, { children });
		const drawerTitle = showHandlerBar ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, handlerBarNode, drawerProps.title ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-compensated-container-title" }, drawerProps.title) : null) : drawerProps.title;
		const { style: drawerStyle } = drawerProps, restDrawerProps = require_objectWithoutProperties._objectWithoutProperties(drawerProps, _excluded2);
		delete restDrawerProps.maxHeight;
		const compensatedMaxHeight = subtractSafeAreaTop(compensateLength(drawerStyle === null || drawerStyle === void 0 ? void 0 : drawerStyle.maxHeight, bodyScaleY), safeAreaTopInset, bodyScaleY);
		const nextDrawerStyle = hasLengthValue(compensatedMaxHeight) ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, drawerStyle), {}, { maxHeight: compensatedMaxHeight }) : drawerStyle;
		const nextDrawerProps = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, restDrawerProps), {}, {
			rootClassName: (0, classnames.default)("pisell-compensated-container", drawerProps.rootClassName),
			className: (0, 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 require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
			props: nextDrawerProps,
			children
		});
	}, [
		bodyScaleY,
		children,
		drawerProps,
		handlerBarNode,
		isDrawer,
		props,
		safeAreaTopInset,
		showHandlerBar
	]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellContainer, compensatedProps, children);
};
//#endregion
exports.default = CompensatedPisellContainer;
