"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useBottomSheet", {
    enumerable: true,
    get: function() {
        return useBottomSheet;
    }
});
const _react = require("react");
const _useBottomSheetDrag = require("./useBottomSheetDrag");
const useBottomSheet = (args)=>{
    const { opened, content, onClose, hasHandle } = args;
    const panelRef = (0, _react.useRef)(null);
    const bodyRef = (0, _react.useRef)(null);
    const handleRef = (0, _react.useRef)(null);
    const [overflows, setOverflows] = (0, _react.useState)(false);
    const [headerDivider, setHeaderDivider] = (0, _react.useState)(false);
    const [footerDivider, setFooterDivider] = (0, _react.useState)(false);
    // Блокировка скролла страницы
    (0, _react.useEffect)(()=>{
        if (!opened) {
            return undefined;
        }
        const { body } = document;
        const { scrollY } = window;
        const prev = {
            position: body.style.position,
            top: body.style.top,
            left: body.style.left,
            right: body.style.right,
            width: body.style.width,
            overflow: body.style.overflow
        };
        body.style.position = 'fixed';
        body.style.top = `-${scrollY}px`;
        body.style.left = '0';
        body.style.right = '0';
        body.style.width = '100%';
        body.style.overflow = 'hidden';
        return ()=>{
            body.style.position = prev.position;
            body.style.top = prev.top;
            body.style.left = prev.left;
            body.style.right = prev.right;
            body.style.width = prev.width;
            body.style.overflow = prev.overflow;
            window.scrollTo(0, scrollY);
        };
    }, [
        opened
    ]);
    // Overflow и разделители body.
    (0, _react.useLayoutEffect)(()=>{
        const panel = panelRef.current;
        const body = bodyRef.current;
        if (body) {
            body.scrollTop = 0;
        }
        if (!opened) {
            setHeaderDivider(false);
            setFooterDivider(false);
            setOverflows(false);
            return;
        }
        if (!panel || !body) {
            return;
        }
        const maxPx = parseFloat(getComputedStyle(panel).maxHeight);
        const isOverflow = Number.isFinite(maxPx) && maxPx > 0 && panel.scrollHeight > maxPx;
        const update = ()=>{
            setHeaderDivider(body.scrollTop > 0);
            setFooterDivider(body.scrollHeight - body.scrollTop - body.clientHeight > 1);
        };
        setOverflows(isOverflow);
        update();
        body.addEventListener('scroll', update);
        return ()=>body.removeEventListener('scroll', update);
    }, [
        opened,
        content
    ]);
    (0, _useBottomSheetDrag.useBottomSheetDrag)({
        panelRef,
        handleRef,
        enabled: hasHandle !== false,
        onClose
    });
    return {
        panelRef,
        bodyRef,
        handleRef,
        overflows,
        headerDivider,
        footerDivider
    };
};
