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/PersistentBottomSheet/index.tsx
var PersistentBottomSheet_exports = {};
__export(PersistentBottomSheet_exports, {
  default: () => PersistentBottomSheet_default
});
module.exports = __toCommonJS(PersistentBottomSheet_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_index = require("./index.less");
var DEFAULT_MAX_HEIGHT = "95dvh";
var TRANSITION_MS = 240;
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 matrix.d || 1;
};
var getViewportHeight = () => {
  var _a;
  if (typeof window === "undefined") {
    return 0;
  }
  return (((_a = window.visualViewport) == null ? void 0 : _a.height) || window.innerHeight) / getBodyScaleY();
};
var resolveHeight = (height, viewportHeight) => {
  if (typeof height === "number") {
    return height;
  }
  if (!height) {
    return Math.floor(viewportHeight * 0.95);
  }
  const normalizedHeight = height.trim();
  const value = Number.parseFloat(normalizedHeight);
  if (!Number.isFinite(value)) {
    return Math.floor(viewportHeight * 0.95);
  }
  if (normalizedHeight.endsWith("px")) {
    return value;
  }
  if (normalizedHeight.endsWith("%") || normalizedHeight.endsWith("vh") || normalizedHeight.endsWith("dvh")) {
    return Math.floor(viewportHeight * value / 100);
  }
  return value;
};
var PersistentBottomSheet = (props) => {
  const {
    open,
    defaultOpen = false,
    onOpenChange,
    afterOpenChange,
    onClose,
    className,
    style,
    header,
    footer,
    children,
    classNames,
    styles,
    maxHeight = DEFAULT_MAX_HEIGHT,
    mask = false,
    maskClosable = true,
    destroyOnHidden = false,
    zIndex = 101
  } = props;
  const isControlled = open !== void 0;
  const [innerOpen, setInnerOpen] = (0, import_react.useState)(defaultOpen);
  const mergedOpen = isControlled ? !!open : innerOpen;
  const [bodyRendered, setBodyRendered] = (0, import_react.useState)(
    () => !destroyOnHidden || mergedOpen
  );
  const lastAfterOpenRef = (0, import_react.useRef)(mergedOpen);
  const headerRef = (0, import_react.useRef)(null);
  const footerRef = (0, import_react.useRef)(null);
  const [viewportHeight, setViewportHeight] = (0, import_react.useState)(getViewportHeight);
  const [headerHeight, setHeaderHeight] = (0, import_react.useState)(0);
  const [footerHeight, setFooterHeight] = (0, import_react.useState)(0);
  const updateOpen = (0, import_react.useCallback)(
    (nextOpen) => {
      if (!isControlled) {
        setInnerOpen(nextOpen);
      }
      onOpenChange == null ? void 0 : onOpenChange(nextOpen);
    },
    [isControlled, onOpenChange]
  );
  (0, import_react.useEffect)(() => {
    var _a;
    const updateViewportHeight = () => {
      setViewportHeight(getViewportHeight());
    };
    updateViewportHeight();
    window.addEventListener("resize", updateViewportHeight);
    (_a = window.visualViewport) == null ? void 0 : _a.addEventListener("resize", updateViewportHeight);
    return () => {
      var _a2;
      window.removeEventListener("resize", updateViewportHeight);
      (_a2 = window.visualViewport) == null ? void 0 : _a2.removeEventListener("resize", updateViewportHeight);
    };
  }, []);
  (0, import_react.useEffect)(() => {
    if (!destroyOnHidden || mergedOpen) {
      setBodyRendered(true);
    }
  }, [destroyOnHidden, mergedOpen]);
  (0, import_react.useEffect)(() => {
    var _a, _b;
    if (typeof ResizeObserver === "undefined") {
      setHeaderHeight(((_a = headerRef.current) == null ? void 0 : _a.offsetHeight) || 0);
      setFooterHeight(((_b = footerRef.current) == null ? void 0 : _b.offsetHeight) || 0);
      return;
    }
    const updateFixedHeight = () => {
      var _a2, _b2;
      setHeaderHeight(((_a2 = headerRef.current) == null ? void 0 : _a2.offsetHeight) || 0);
      setFooterHeight(((_b2 = footerRef.current) == null ? void 0 : _b2.offsetHeight) || 0);
    };
    const resizeObserver = new ResizeObserver(() => {
      updateFixedHeight();
    });
    if (headerRef.current) {
      resizeObserver.observe(headerRef.current);
    }
    if (footerRef.current) {
      resizeObserver.observe(footerRef.current);
    }
    updateFixedHeight();
    return () => {
      resizeObserver.disconnect();
    };
  }, [footer, header]);
  const maxHeightValue = (0, import_react.useMemo)(
    () => resolveHeight(maxHeight, viewportHeight),
    [maxHeight, viewportHeight]
  );
  const fixedHeight = headerHeight + footerHeight;
  const availableBodyHeight = Math.max(0, maxHeightValue - fixedHeight);
  const bodyHeight = mergedOpen ? availableBodyHeight : 0;
  const sheetStyle = (0, import_react.useMemo)(
    () => ({
      ...style,
      zIndex,
      maxHeight: "var(--pisell-bottom-sheet-max-height)",
      "--pisell-bottom-sheet-max-height": `${maxHeightValue}px`,
      "--pisell-bottom-sheet-body-height": `${bodyHeight}px`,
      "--pisell-bottom-sheet-transition-duration": `${TRANSITION_MS}ms`
    }),
    [bodyHeight, maxHeightValue, style, zIndex]
  );
  const handleMaskClick = (event) => {
    if (!maskClosable) {
      return;
    }
    onClose == null ? void 0 : onClose(event);
    updateOpen(false);
  };
  const handleBodyTransitionEnd = (event) => {
    if (event.currentTarget !== event.target || event.propertyName !== "height") {
      return;
    }
    if (destroyOnHidden && !mergedOpen) {
      setBodyRendered(false);
    }
    if (lastAfterOpenRef.current !== mergedOpen) {
      lastAfterOpenRef.current = mergedOpen;
      afterOpenChange == null ? void 0 : afterOpenChange(mergedOpen);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, mask && mergedOpen ? /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: (0, import_classnames.default)(
        "pisell-persistent-bottom-sheet-mask",
        classNames == null ? void 0 : classNames.mask
      ),
      style: { ...styles == null ? void 0 : styles.mask, zIndex: zIndex - 1 },
      "aria-label": "Close",
      onClick: handleMaskClick
    }
  ) : null, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-persistent-bottom-sheet", className, {
        "pisell-persistent-bottom-sheet-open": mergedOpen
      }),
      style: sheetStyle
    },
    header ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: headerRef,
        className: (0, import_classnames.default)(
          "pisell-persistent-bottom-sheet-header",
          classNames == null ? void 0 : classNames.header
        ),
        style: styles == null ? void 0 : styles.header
      },
      header
    ) : /* @__PURE__ */ import_react.default.createElement("div", { ref: headerRef }),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          "pisell-persistent-bottom-sheet-body",
          classNames == null ? void 0 : classNames.body
        ),
        style: styles == null ? void 0 : styles.body,
        "aria-hidden": !mergedOpen,
        onTransitionEnd: handleBodyTransitionEnd
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-persistent-bottom-sheet-body-inner" }, bodyRendered ? children : null)
    ),
    footer ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: footerRef,
        className: (0, import_classnames.default)(
          "pisell-persistent-bottom-sheet-footer",
          classNames == null ? void 0 : classNames.footer
        ),
        style: styles == null ? void 0 : styles.footer
      },
      footer
    ) : /* @__PURE__ */ import_react.default.createElement("div", { ref: footerRef })
  ));
};
var PersistentBottomSheet_default = PersistentBottomSheet;
