"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get drawerConfig () {
        return drawerConfig;
    },
    get drawerRoot () {
        return drawerRoot;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _hooks = require("../../hooks");
var _utils = require("../../utils");
var _Popup = require("../Popup");
var _Overlay = require("../Overlay");
var _utils1 = require("../Popup/utils");
var _Drawertokens = require("./Drawer.tokens");
var _base = require("./variations/_view/base");
var _base1 = require("./variations/_size/base");
var _base2 = require("./variations/_borderRadius/base");
var _Drawerstyles = require("./Drawer.styles");
var _DrawerContext = require("./DrawerContext");
var _hooks1 = require("./hooks");
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 _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
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);
}
var drawerRoot = function drawerRoot(Root) {
    return /*#__PURE__*/ (0, _react.forwardRef)(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), id = _ref2.id, zIndex = _ref2.zIndex, popupInfo = _ref2.popupInfo, withBlur = _ref2.withBlur, children = _ref2.children, view = _ref2.view, size = _ref2.size, width = _ref2.width, height = _ref2.height, isOpen = _ref2.isOpen, opened = _ref2.opened, initialFocusRef = _ref2.initialFocusRef, focusAfterRef = _ref2.focusAfterRef, animationInfo = _ref2.animationInfo, className = _ref2.className, customBackgroundColor = _ref2.customBackgroundColor, customContentBackgroundColor = _ref2.customContentBackgroundColor, onClose = _ref2.onClose, onOverlayClick = _ref2.onOverlayClick, onEscKeyDown = _ref2.onEscKeyDown, offset = _ref2.offset, frame = _ref2.frame, _ref_positionFixed = _ref2.positionFixed, positionFixed = _ref_positionFixed === void 0 ? false : _ref_positionFixed, _ref_borderRadius = _ref2.borderRadius, borderRadius = _ref_borderRadius === void 0 ? 'none' : _ref_borderRadius, _ref_placement = _ref2.placement, placement = _ref_placement === void 0 ? 'right' : _ref_placement, _ref_closeOnEsc = _ref2.closeOnEsc, closeOnEsc = _ref_closeOnEsc === void 0 ? true : _ref_closeOnEsc, _ref_closeOnOverlayClick = _ref2.closeOnOverlayClick, closeOnOverlayClick = _ref_closeOnOverlayClick === void 0 ? true : _ref_closeOnOverlayClick, _ref_asModal = _ref2.asModal, asModal = _ref_asModal === void 0 ? true : _ref_asModal, draggable = _ref2.draggable, rest = _object_without_properties(_ref2, [
            "id",
            "zIndex",
            "popupInfo",
            "withBlur",
            "children",
            "view",
            "size",
            "width",
            "height",
            "isOpen",
            "opened",
            "initialFocusRef",
            "focusAfterRef",
            "animationInfo",
            "className",
            "customBackgroundColor",
            "customContentBackgroundColor",
            "onClose",
            "onOverlayClick",
            "onEscKeyDown",
            "offset",
            "frame",
            "positionFixed",
            "borderRadius",
            "placement",
            "closeOnEsc",
            "closeOnOverlayClick",
            "asModal",
            "draggable"
        ]), _rest1 = _sliced_to_array(_rest, 1), outerRef = _rest1[0];
        var innerIsOpen = Boolean(isOpen || opened);
        var trapRef = (0, _hooks.useFocusTrap)(true, initialFocusRef, focusAfterRef);
        var popupController = (0, _Popup.usePopupContext)();
        var innerRef = (0, _hooks.useForkRef)(trapRef, outerRef);
        var uniqId = (0, _utils.safeUseId)();
        var innerId = id || uniqId;
        var overlayBackgroundToken = withBlur ? "var(".concat(_Drawertokens.tokens.drawerOverlayWithBlurColor, ")") : "var(".concat(_Drawertokens.tokens.drawerOverlayColor, ")");
        var innerWidth = width ? (0, _utils.getSizeValueFromProp)(width) : '100%';
        var innerHeight = height ? (0, _utils.getSizeValueFromProp)(height) : '100%';
        var placementClass = placement ? _Drawertokens.classes["".concat(placement, "Placement")] : undefined;
        var drawerInfo = (0, _hooks1.useDrawer)({
            id: innerId,
            isOpen: innerIsOpen,
            closeOnEsc: closeOnEsc,
            onEscKeyDown: onEscKeyDown,
            onClose: onClose,
            popupInfo: popupInfo,
            disableScroll: asModal
        }).drawerInfo;
        var transparent = (0, _react.useMemo)(function() {
            return (0, _DrawerContext.getIdLastDrawer)(Array.from(popupController.items.values())) !== innerId;
        }, [
            innerId,
            popupController.items
        ]);
        var onDrawerOverlayKeyDown = function onDrawerOverlayKeyDown(event) {
            if (onOverlayClick) {
                onOverlayClick(event);
                return;
            }
            if (onClose) {
                onClose();
            }
        };
        var _obj;
        return /*#__PURE__*/ _react.default.createElement(_Drawerstyles.StyledPopup, _object_spread({
            id: innerId,
            ref: asModal ? innerRef : outerRef,
            className: (0, _classnames.default)(placementClass, className, (_obj = {}, _define_property(_obj, _Drawertokens.classes.enterCustomAnimation, animationInfo === null || animationInfo === void 0 ? void 0 : animationInfo.enter), _define_property(_obj, _Drawertokens.classes.exitCustomAnimation, animationInfo === null || animationInfo === void 0 ? void 0 : animationInfo.exit), _obj)),
            opened: innerIsOpen,
            zIndex: zIndex,
            placement: placement,
            popupInfo: drawerInfo,
            frame: frame,
            positionFixed: positionFixed,
            width: innerWidth,
            height: innerHeight,
            offset: offset,
            withAnimation: true,
            drawerAnimationInfo: animationInfo,
            overlay: asModal && /*#__PURE__*/ _react.default.createElement(Root, {
                view: view
            }, /*#__PURE__*/ _react.default.createElement(_Overlay.Overlay, {
                className: _Drawertokens.classes.overlay,
                zIndex: zIndex || _utils1.DEFAULT_Z_INDEX,
                backgroundColorProperty: overlayBackgroundToken,
                withBlur: withBlur,
                transparent: transparent,
                isClickable: closeOnOverlayClick,
                onOverlayClick: onDrawerOverlayKeyDown
            }))
        }, rest), /*#__PURE__*/ _react.default.createElement(Root, {
            className: _Drawertokens.classes.panel,
            view: view,
            size: size,
            style: {
                width: innerWidth,
                height: innerHeight
            },
            borderRadius: borderRadius
        }, /*#__PURE__*/ _react.default.createElement(_Drawerstyles.StyledPanel, {
            width: innerWidth,
            height: innerHeight,
            customBackgroundColor: customBackgroundColor,
            customContentBackgroundColor: customContentBackgroundColor
        }, children)));
    });
};
var drawerConfig = {
    name: 'Drawer',
    tag: 'div',
    layout: drawerRoot,
    base: '',
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        },
        borderRadius: {
            css: _base2.base
        }
    },
    defaults: {
        view: 'default',
        size: 'm',
        borderRadius: 'none'
    }
};
