function _array_like_to_array(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 _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(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(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { forwardRef, useRef } from "react";
import { Overlay } from "../Overlay";
import { cx } from "../../utils";
import { useOverflow, useSheetSwipe } from "./hooks";
import { classes, tokens } from "./Sheet.tokens";
import { StyledContentWrapper, StyledSheetBody, StyledSheetContent, StyledSheetFooter, StyledSheetHandle, StyledSheetHeader } from "./Sheet.styles";
import { base as viewCSS } from "./variations/_view/base";
import { base as handlePlacementCSS } from "./variations/_handlePlacement/base";
import { DEFAULT_Z_INDEX } from "./utils";
/**
 * Открывает окно-шторку поверх основного экрана.
 */ export var sheetRoot = function sheetRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), opened = _ref2.opened, children = _ref2.children, onClose = _ref2.onClose, _ref_hasHandle = _ref2.hasHandle, hasHandle = _ref_hasHandle === void 0 ? true : _ref_hasHandle, handlePlacement = _ref2.handlePlacement, contentHeader = _ref2.contentHeader, _ref_isHeaderFixed = _ref2.isHeaderFixed, isHeaderFixed = _ref_isHeaderFixed === void 0 ? false : _ref_isHeaderFixed, contentFooter = _ref2.contentFooter, _ref_isFooterFixed = _ref2.isFooterFixed, isFooterFixed = _ref_isFooterFixed === void 0 ? false : _ref_isFooterFixed, _ref_withOverlay = _ref2.withOverlay, withOverlay = _ref_withOverlay === void 0 ? true : _ref_withOverlay, _ref_withBlur = _ref2.withBlur, withBlur = _ref_withBlur === void 0 ? false : _ref_withBlur, _ref_withTransition = _ref2.withTransition, withTransition = _ref_withTransition === void 0 ? true : _ref_withTransition, _ref_hasScrollEvents = _ref2.hasScrollEvents, hasScrollEvents = _ref_hasScrollEvents === void 0 ? true : _ref_hasScrollEvents, throttleMs = _ref2.throttleMs, className = _ref2.className, view = _ref2.view, restProps = _object_without_properties(_ref2, [
            "opened",
            "children",
            "onClose",
            "hasHandle",
            "handlePlacement",
            "contentHeader",
            "isHeaderFixed",
            "contentFooter",
            "isFooterFixed",
            "withOverlay",
            "withBlur",
            "withTransition",
            "hasScrollEvents",
            "throttleMs",
            "className",
            "view"
        ]), _rest1 = _sliced_to_array(_rest, 1), rootRef = _rest1[0];
        var contentWrapperRef = useRef(null);
        var contentRef = useRef(null);
        var handleRef = useRef(null);
        useOverflow({
            opened: opened
        });
        useSheetSwipe({
            contentWrapperRef: contentWrapperRef,
            contentRef: contentRef,
            handleRef: handleRef,
            throttleMs: throttleMs,
            hasScrollEvents: hasScrollEvents,
            onClose: onClose
        });
        var hasHeader = Boolean(contentHeader);
        var hasFooter = Boolean(contentFooter);
        var closedClass = opened ? undefined : "".concat(classes.closed);
        var overlayBackgroundToken = withBlur ? "var(".concat(tokens.sheetOverlayWithBlurColor, ")") : "var(".concat(tokens.sheetOverlayColor, ")");
        return /*#__PURE__*/ React.createElement(Root, {
            opened: opened,
            onClose: onClose,
            view: view,
            handlePlacement: handlePlacement,
            ref: rootRef
        }, /*#__PURE__*/ React.createElement(StyledContentWrapper, _object_spread({
            opened: opened,
            withTransition: withTransition,
            className: cx(closedClass, className),
            ref: contentWrapperRef
        }, restProps), hasHandle && /*#__PURE__*/ React.createElement(StyledSheetHandle, {
            ref: handleRef
        }), /*#__PURE__*/ React.createElement(StyledSheetContent, {
            hasHeader: hasHeader,
            hasFooter: hasFooter,
            ref: contentRef
        }, hasHeader && /*#__PURE__*/ React.createElement(StyledSheetHeader, {
            isHeaderFixed: isHeaderFixed
        }, contentHeader), /*#__PURE__*/ React.createElement(StyledSheetBody, null, children), hasFooter && /*#__PURE__*/ React.createElement(StyledSheetFooter, {
            isFooterFixed: isFooterFixed
        }, contentFooter))), withOverlay && opened && /*#__PURE__*/ React.createElement(Overlay, {
            zIndex: DEFAULT_Z_INDEX,
            backgroundColorProperty: overlayBackgroundToken,
            withBlur: withBlur,
            isClickable: true,
            onOverlayClick: onClose
        }));
    });
};
export var sheetConfig = {
    name: 'Sheet',
    tag: 'div',
    layout: sheetRoot,
    base: '',
    variations: {
        view: {
            css: viewCSS
        },
        handlePlacement: {
            css: handlePlacementCSS
        }
    },
    defaults: {
        view: 'default',
        handlePlacement: 'outer'
    }
};
