function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import cx from 'classnames';
import "./index.less";
var DEFAULT_MAX_HEIGHT = '95dvh';
var TRANSITION_MS = 240;
var getBodyScaleY = function getBodyScaleY() {
  if (typeof window === 'undefined' || typeof document === 'undefined') {
    return 1;
  }
  var transform = window.getComputedStyle(document.body).transform;
  if (!transform || transform === 'none') {
    return 1;
  }
  var matrix = new DOMMatrixReadOnly(transform);
  return matrix.d || 1;
};
var getViewportHeight = function getViewportHeight() {
  var _window$visualViewpor;
  if (typeof window === 'undefined') {
    return 0;
  }
  return (((_window$visualViewpor = window.visualViewport) === null || _window$visualViewpor === void 0 ? void 0 : _window$visualViewpor.height) || window.innerHeight) / getBodyScaleY();
};
var resolveHeight = function resolveHeight(height, viewportHeight) {
  if (typeof height === 'number') {
    return height;
  }
  if (!height) {
    return Math.floor(viewportHeight * 0.95);
  }
  var normalizedHeight = height.trim();
  var 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;
};

/**
 * A bottom-aligned sheet that keeps header and footer visible while collapsing
 * only the body area. Header/footer heights are measured so footer growth
 * automatically reduces the available body height.
 */
var PersistentBottomSheet = function PersistentBottomSheet(props) {
  var open = props.open,
    _props$defaultOpen = props.defaultOpen,
    defaultOpen = _props$defaultOpen === void 0 ? false : _props$defaultOpen,
    onOpenChange = props.onOpenChange,
    afterOpenChange = props.afterOpenChange,
    onClose = props.onClose,
    className = props.className,
    style = props.style,
    header = props.header,
    footer = props.footer,
    children = props.children,
    classNames = props.classNames,
    styles = props.styles,
    _props$maxHeight = props.maxHeight,
    maxHeight = _props$maxHeight === void 0 ? DEFAULT_MAX_HEIGHT : _props$maxHeight,
    _props$mask = props.mask,
    mask = _props$mask === void 0 ? false : _props$mask,
    _props$maskClosable = props.maskClosable,
    maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable,
    _props$destroyOnHidde = props.destroyOnHidden,
    destroyOnHidden = _props$destroyOnHidde === void 0 ? false : _props$destroyOnHidde,
    _props$zIndex = props.zIndex,
    zIndex = _props$zIndex === void 0 ? 101 : _props$zIndex;
  var isControlled = open !== undefined;
  var _useState = useState(defaultOpen),
    _useState2 = _slicedToArray(_useState, 2),
    innerOpen = _useState2[0],
    setInnerOpen = _useState2[1];
  var mergedOpen = isControlled ? !!open : innerOpen;
  var _useState3 = useState(function () {
      return !destroyOnHidden || mergedOpen;
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    bodyRendered = _useState4[0],
    setBodyRendered = _useState4[1];
  var lastAfterOpenRef = useRef(mergedOpen);
  var headerRef = useRef(null);
  var footerRef = useRef(null);
  var _useState5 = useState(getViewportHeight),
    _useState6 = _slicedToArray(_useState5, 2),
    viewportHeight = _useState6[0],
    setViewportHeight = _useState6[1];
  var _useState7 = useState(0),
    _useState8 = _slicedToArray(_useState7, 2),
    headerHeight = _useState8[0],
    setHeaderHeight = _useState8[1];
  var _useState9 = useState(0),
    _useState10 = _slicedToArray(_useState9, 2),
    footerHeight = _useState10[0],
    setFooterHeight = _useState10[1];
  var updateOpen = useCallback(function (nextOpen) {
    if (!isControlled) {
      setInnerOpen(nextOpen);
    }
    onOpenChange === null || onOpenChange === void 0 || onOpenChange(nextOpen);
  }, [isControlled, onOpenChange]);
  useEffect(function () {
    var _window$visualViewpor2;
    var updateViewportHeight = function updateViewportHeight() {
      setViewportHeight(getViewportHeight());
    };
    updateViewportHeight();
    window.addEventListener('resize', updateViewportHeight);
    (_window$visualViewpor2 = window.visualViewport) === null || _window$visualViewpor2 === void 0 || _window$visualViewpor2.addEventListener('resize', updateViewportHeight);
    return function () {
      var _window$visualViewpor3;
      window.removeEventListener('resize', updateViewportHeight);
      (_window$visualViewpor3 = window.visualViewport) === null || _window$visualViewpor3 === void 0 || _window$visualViewpor3.removeEventListener('resize', updateViewportHeight);
    };
  }, []);
  useEffect(function () {
    if (!destroyOnHidden || mergedOpen) {
      setBodyRendered(true);
    }
  }, [destroyOnHidden, mergedOpen]);
  useEffect(function () {
    if (typeof ResizeObserver === 'undefined') {
      var _headerRef$current, _footerRef$current;
      setHeaderHeight(((_headerRef$current = headerRef.current) === null || _headerRef$current === void 0 ? void 0 : _headerRef$current.offsetHeight) || 0);
      setFooterHeight(((_footerRef$current = footerRef.current) === null || _footerRef$current === void 0 ? void 0 : _footerRef$current.offsetHeight) || 0);
      return;
    }
    var updateFixedHeight = function updateFixedHeight() {
      var _headerRef$current2, _footerRef$current2;
      setHeaderHeight(((_headerRef$current2 = headerRef.current) === null || _headerRef$current2 === void 0 ? void 0 : _headerRef$current2.offsetHeight) || 0);
      setFooterHeight(((_footerRef$current2 = footerRef.current) === null || _footerRef$current2 === void 0 ? void 0 : _footerRef$current2.offsetHeight) || 0);
    };
    var resizeObserver = new ResizeObserver(function () {
      updateFixedHeight();
    });
    if (headerRef.current) {
      resizeObserver.observe(headerRef.current);
    }
    if (footerRef.current) {
      resizeObserver.observe(footerRef.current);
    }
    updateFixedHeight();
    return function () {
      resizeObserver.disconnect();
    };
  }, [footer, header]);
  var maxHeightValue = useMemo(function () {
    return resolveHeight(maxHeight, viewportHeight);
  }, [maxHeight, viewportHeight]);
  var fixedHeight = headerHeight + footerHeight;
  var availableBodyHeight = Math.max(0, maxHeightValue - fixedHeight);
  var bodyHeight = mergedOpen ? availableBodyHeight : 0;
  var sheetStyle = useMemo(function () {
    return _objectSpread(_objectSpread({}, style), {}, {
      zIndex: zIndex,
      maxHeight: 'var(--pisell-bottom-sheet-max-height)',
      '--pisell-bottom-sheet-max-height': "".concat(maxHeightValue, "px"),
      '--pisell-bottom-sheet-body-height': "".concat(bodyHeight, "px"),
      '--pisell-bottom-sheet-transition-duration': "".concat(TRANSITION_MS, "ms")
    });
  }, [bodyHeight, maxHeightValue, style, zIndex]);
  var handleMaskClick = function handleMaskClick(event) {
    if (!maskClosable) {
      return;
    }
    onClose === null || onClose === void 0 || onClose(event);
    updateOpen(false);
  };
  var handleBodyTransitionEnd = function 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 || afterOpenChange === void 0 || afterOpenChange(mergedOpen);
    }
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, mask && mergedOpen ? /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: cx('pisell-persistent-bottom-sheet-mask', classNames === null || classNames === void 0 ? void 0 : classNames.mask),
    style: _objectSpread(_objectSpread({}, styles === null || styles === void 0 ? void 0 : styles.mask), {}, {
      zIndex: zIndex - 1
    }),
    "aria-label": "Close",
    onClick: handleMaskClick
  }) : null, /*#__PURE__*/React.createElement("div", {
    className: cx('pisell-persistent-bottom-sheet', className, {
      'pisell-persistent-bottom-sheet-open': mergedOpen
    }),
    style: sheetStyle
  }, header ? /*#__PURE__*/React.createElement("div", {
    ref: headerRef,
    className: cx('pisell-persistent-bottom-sheet-header', classNames === null || classNames === void 0 ? void 0 : classNames.header),
    style: styles === null || styles === void 0 ? void 0 : styles.header
  }, header) : /*#__PURE__*/React.createElement("div", {
    ref: headerRef
  }), /*#__PURE__*/React.createElement("div", {
    className: cx('pisell-persistent-bottom-sheet-body', classNames === null || classNames === void 0 ? void 0 : classNames.body),
    style: styles === null || styles === void 0 ? void 0 : styles.body,
    "aria-hidden": !mergedOpen,
    onTransitionEnd: handleBodyTransitionEnd
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-persistent-bottom-sheet-body-inner"
  }, bodyRendered ? children : null)), footer ? /*#__PURE__*/React.createElement("div", {
    ref: footerRef,
    className: cx('pisell-persistent-bottom-sheet-footer', classNames === null || classNames === void 0 ? void 0 : classNames.footer),
    style: styles === null || styles === void 0 ? void 0 : styles.footer
  }, footer) : /*#__PURE__*/React.createElement("div", {
    ref: footerRef
  })));
};
export default PersistentBottomSheet;