import React__default, { createContext, useContext, useEffect, useState, useId } from 'react';
import { createPortal } from 'react-dom';
import { AnimatePresence, motion } from 'framer-motion';
import { useRefocusOnActivator, useIsMounted, useFocusTrap, useInView } from '@jobber/hooks';
import classnames from 'classnames';
import { tokens } from '@jobber/design';
import { H as Heading } from './Heading-es.js';
import { B as Button } from './Button-es.js';
import { F as Flex } from './Flex-es.js';

const SideDrawerContext = createContext({
    components: {
        backButton: false,
    },
    registerComponent: () => undefined,
    unRegisterComponent: () => undefined,
});
function useSideDrawerContext() {
    const context = useContext(SideDrawerContext);
    return context;
}

function SideDrawerActions({ children }) {
    const { actionPortal } = useSideDrawerContext();
    if (!actionPortal)
        return null;
    return createPortal(React__default.createElement(React__default.Fragment, null, children), actionPortal);
}

function SideDrawerTitle({ children }) {
    const { titlePortal } = useSideDrawerContext();
    if (!titlePortal)
        return null;
    if (typeof children === "string") {
        return createPortal(React__default.createElement(Heading, { level: 2 }, children), titlePortal);
    }
    return createPortal(children, titlePortal);
}

function SideDrawerToolbar({ children }) {
    const { toolbarPortal } = useSideDrawerContext();
    if (!toolbarPortal)
        return null;
    return createPortal(children, toolbarPortal);
}

var styles = {"drawer":"cCwRgRAz4qk-","inline":"ONUfsWMLKdo-","container":"XipkpXgyzyg-","header":"LuqC0RF-R1U-","content":"vArEcEUxGxQ-","footer":"_4HzuRZKD9u8-","subtle":"v5Db-FCF2kU-","heading":"Eti1JL3rghE-","hasShadow":"PtbV1GtWwzY-","headerActions":"YCdqkhcLHfI-","hideWhenEmpty":"ZH2Mp84HhX8-","overlay":"Ro-pE7hx0wY-","reverseScroll":"gSN5nWDwGm4-","headerShadowListener":"tMeBAjf4vEA-","footerShadowListener":"-lPSZVGIIAs-","backButton":"XOqA4Hw6S6g-","backButtonVisible":"b4VHPS-RG34-","spinning":"McL3-VWXVbs-"};

function SideDrawerBackButton({ onClick, }) {
    const { backPortal, registerComponent, unRegisterComponent } = useSideDrawerContext();
    useEffect(() => {
        registerComponent("backButton");
        return () => {
            unRegisterComponent("backButton");
        };
    }, []);
    if (!backPortal)
        return null;
    return createPortal(React__default.createElement(Button, { ariaLabel: "Back", icon: "longArrowLeft", variation: "subtle", type: "tertiary", onClick: onClick }), backPortal);
}

function SideDrawerFooter({ children }) {
    const { footerPortal } = useSideDrawerContext();
    if (!footerPortal)
        return null;
    return createPortal(React__default.createElement(React__default.Fragment, null, children), footerPortal);
}

const variants = {
    initial: { x: "100%" },
    visible: { x: 0, transitionEnd: { x: 0 } },
    hidden: { x: "100%" },
};
const inlineVariants = {
    initial: { x: "25%", opacity: 0 },
    visible: { x: 0, opacity: 1, transitionEnd: { x: 0 } },
    hidden: { x: "25%", opacity: 0 },
};
const useSideDrawerState = (open) => {
    const [ref, setRef] = useState(null);
    const [components, setComponents] = useState({
        backButton: false,
    });
    const { toolbar, title, actions, backButton, footer } = useSlotIDs();
    useRefocusOnActivator(open);
    const sideDrawerRef = useFocusTrap(open);
    const [headerShadowRef, noHeaderShadow] = useInView();
    const [footerShadowRef, noFooterShadow] = useInView();
    return {
        ref,
        setRef,
        components,
        setComponents,
        slots: { toolbar, title, actions, backButton, footer },
        sideDrawerRef,
        headerShadowRef,
        noHeaderShadow,
        footerShadowRef,
        noFooterShadow,
    };
};
function SideDrawer({ children, onRequestClose, open, variation = "base", scrollDirection, inline = false, UNSAFE_className, UNSAFE_style, }) {
    var _a;
    const { ref, setRef, components, setComponents, slots, sideDrawerRef, headerShadowRef, noHeaderShadow, footerShadowRef, noFooterShadow, } = useSideDrawerState(open);
    useRefocusOnActivator(open);
    const container = ((_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.body) || null;
    const isMounted = useIsMounted();
    if (!isMounted.current && !container)
        return null;
    const SideDrawerRender = (React__default.createElement(SideDrawerContext.Provider, { value: {
            actionPortal: ref === null || ref === void 0 ? void 0 : ref.querySelector(slots.actions.selector),
            titlePortal: ref === null || ref === void 0 ? void 0 : ref.querySelector(slots.title.selector),
            toolbarPortal: ref === null || ref === void 0 ? void 0 : ref.querySelector(slots.toolbar.selector),
            backPortal: ref === null || ref === void 0 ? void 0 : ref.querySelector(slots.backButton.selector),
            footerPortal: ref === null || ref === void 0 ? void 0 : ref.querySelector(slots.footer.selector),
            components,
            registerComponent: key => setComponents(prev => (Object.assign(Object.assign({}, prev), { [key]: true }))),
            unRegisterComponent: key => setComponents(prev => (Object.assign(Object.assign({}, prev), { [key]: false }))),
        } },
        open && !inline && (React__default.createElement("button", { className: styles.overlay, "aria-label": "Close", onClick: onRequestClose, type: "button" })),
        React__default.createElement(AnimatePresence, { initial: false }, open && (React__default.createElement(motion.div, { className: classnames(styles.drawer, {
                [styles.reverseScroll]: scrollDirection === "reverse",
                [styles.inline]: inline,
            }, UNSAFE_className === null || UNSAFE_className === void 0 ? void 0 : UNSAFE_className.container), ref: setRef, "data-elevation": "elevated", variants: inline ? inlineVariants : variants, initial: "initial", animate: "visible", exit: "hidden", transition: {
                duration: tokens["timing-base"] / 1000,
            }, style: UNSAFE_style === null || UNSAFE_style === void 0 ? void 0 : UNSAFE_style.container },
            React__default.createElement("div", { ref: sideDrawerRef, role: "dialog", className: classnames(styles.container, styles.hasShadow, {
                    [styles.subtle]: variation === "subtle",
                }), tabIndex: 0, onKeyDown: handleKeyDown },
                React__default.createElement("div", { className: classnames(styles.header, {
                        [styles.hasShadow]: headerShadowRef.current && !noHeaderShadow,
                    }) },
                    React__default.createElement(Flex, { template: ["grow", "shrink"], align: "start" },
                        React__default.createElement(Flex, { template: ["shrink", "grow"], align: "start", gap: "none" },
                            React__default.createElement("div", Object.assign({ className: classnames(styles.backButton, {
                                    [styles.backButtonVisible]: components.backButton,
                                }) }, slots.backButton.attr)),
                            React__default.createElement("div", Object.assign({ className: classnames(styles.heading) }, slots.title.attr))),
                        React__default.createElement("div", { className: styles.headerActions },
                            React__default.createElement("div", Object.assign({ className: styles.hideWhenEmpty }, slots.actions.attr)),
                            React__default.createElement(Button, { ariaLabel: "Close", icon: "cross", onClick: onRequestClose, type: "tertiary", variation: "subtle" }))),
                    React__default.createElement("div", Object.assign({ className: styles.hideWhenEmpty }, slots.toolbar.attr))),
                React__default.createElement("div", { className: styles.content },
                    React__default.createElement("div", { className: styles.headerShadowListener, ref: headerShadowRef }),
                    children,
                    React__default.createElement("div", { className: styles.footerShadowListener, ref: footerShadowRef })),
                React__default.createElement("div", Object.assign({ className: classnames(styles.footer, styles.hideWhenEmpty, {
                        [styles.hasShadow]: footerShadowRef.current && !noFooterShadow,
                    }) }, slots.footer.attr))))))));
    if (inline) {
        return SideDrawerRender;
    }
    return createPortal(SideDrawerRender, container);
    function handleKeyDown(event) {
        if (event.key === "Escape") {
            event.stopPropagation();
            onRequestClose();
        }
    }
}
function useSlotIDs() {
    const toolbar = useSlotID();
    const title = useSlotID("title");
    const actions = useSlotID();
    const backButton = useSlotID("back");
    const footer = useSlotID();
    return { toolbar, title, actions, backButton, footer };
}
function useSlotID(prefix) {
    const id = useId();
    const prefixedId = prefix ? `${prefix}-${id}` : id;
    const attrKey = "data-side-drawer-slot";
    const selector = `[${attrKey}="${prefixedId}"]`;
    const attr = { [attrKey]: prefixedId };
    return { id: prefixedId, selector, attr };
}
SideDrawer.Title = SideDrawerTitle;
SideDrawer.Toolbar = SideDrawerToolbar;
SideDrawer.Actions = SideDrawerActions;
SideDrawer.BackButton = SideDrawerBackButton;
SideDrawer.Footer = SideDrawerFooter;

export { SideDrawer as S };
