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/components/SafeAreaTop/index.tsx
var SafeAreaTop_exports = {};
__export(SafeAreaTop_exports, {
  SafeAreaTop: () => SafeAreaTop,
  default: () => SafeAreaTop_default
});
module.exports = __toCommonJS(SafeAreaTop_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_index = require("./index.less");
var PREFIX_CLS = "pisell-safe-area-top";
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 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 getSafeAreaTopInset = () => {
  if (typeof window === "undefined" || typeof document === "undefined") {
    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 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 normalizeCssLength = (value) => {
  if (typeof value === "number") {
    return `${value}px`;
  }
  return value;
};
var NativeSafeAreaTop = import_react.default.memo((props) => {
  const { backgroundColor = "transparent", className, style } = props;
  const bodyScaleY = useBodyScaleY(true);
  const safeAreaTopInset = useSafeAreaTopInset(true);
  const safeAreaTopHeight = `${safeAreaTopInset / bodyScaleY}px`;
  const styleHeight = normalizeCssLength(style == null ? void 0 : style.height);
  const mergedStyle = {
    "--safe-area-top-background": backgroundColor,
    "--safe-area-top-height": styleHeight || safeAreaTopHeight,
    ...style
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      "aria-hidden": "true",
      className: (0, import_classnames.default)(PREFIX_CLS, className),
      style: mergedStyle
    }
  );
});
NativeSafeAreaTop.displayName = "NativeSafeAreaTop";
var SafeAreaTop = import_react.default.memo((props) => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const { isNativePlatform } = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
  const isNative = typeof isNativePlatform === "function" ? isNativePlatform() : isNativePlatform || false;
  if (!isNative) {
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(PREFIX_CLS, props.className),
        style: { display: "none" }
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(NativeSafeAreaTop, { ...props });
});
SafeAreaTop.displayName = "SafeAreaTop";
var SafeAreaTop_default = SafeAreaTop;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SafeAreaTop
});
