import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useCallback, useEffect, useId, useState } from "react";
import { v4 as uuid } from "uuid";
import { KdaProvider } from "../providers/Kda.js";
import { KdaDialog } from "./KdaDialog.js";
import { KdaDialogArbiter } from "./KdaDialogArbiter.js";
import { KdaReplaceConfirmationDialog } from "./KdaReplaceConfirmationDialog.js";
const arbiter = KdaDialogArbiter.getInstance();
export function KdaDialogController({ requestedDefinition, separators, excludeTags, includeTags, onRequestedDefinitionChange, ...dialogProps }) {
    const [activeDefinition, setActiveDefinition] = useState(undefined);
    const [pendingDefinition, setPendingDefinition] = useState(undefined);
    const titleId = useId();
    const isReplaceConfirmationOpen = !!pendingDefinition;
    const [ownerId] = useState(() => uuid());
    const handleDelegatedOpen = useCallback((definition) => {
        onRequestedDefinitionChange?.(definition);
    }, [onRequestedDefinitionChange]);
    useEffect(() => {
        return arbiter.registerOwner(ownerId, handleDelegatedOpen);
    }, [ownerId, handleDelegatedOpen]);
    useEffect(() => {
        if (!requestedDefinition) {
            setActiveDefinition(undefined);
            setPendingDefinition(undefined);
            arbiter.release(ownerId);
            return;
        }
        if (!activeDefinition) {
            const granted = arbiter.requestOpen(ownerId, requestedDefinition);
            if (!granted) {
                // Request delegated to another active controller. Clear the host request to avoid re-trigger loops.
                onRequestedDefinitionChange?.(undefined);
                return;
            }
            setActiveDefinition(requestedDefinition);
            return;
        }
        // Whenever a dialog is already open and a *different* request arrives, always ask for confirmation.
        // This prevents showing confirmation immediately on the initial open (where active === requested).
        if (!pendingDefinition && requestedDefinition !== activeDefinition) {
            setPendingDefinition(requestedDefinition);
        }
    }, [activeDefinition, ownerId, onRequestedDefinitionChange, pendingDefinition, requestedDefinition]);
    const onCloseInternal = useCallback(() => {
        setActiveDefinition(undefined);
        setPendingDefinition(undefined);
        arbiter.release(ownerId);
        onRequestedDefinitionChange?.(undefined);
        dialogProps.onClose?.();
    }, [dialogProps, ownerId, onRequestedDefinitionChange]);
    const onCancelReplace = useCallback(() => {
        setPendingDefinition(undefined);
        if (activeDefinition) {
            onRequestedDefinitionChange?.(activeDefinition);
        }
        focusTitleElement(titleId);
    }, [activeDefinition, onRequestedDefinitionChange, titleId]);
    const onConfirmReplace = useCallback(() => {
        if (!pendingDefinition) {
            return;
        }
        setActiveDefinition(pendingDefinition);
        setPendingDefinition(undefined);
        onRequestedDefinitionChange?.(pendingDefinition);
        focusTitleElement(titleId);
    }, [onRequestedDefinitionChange, pendingDefinition, titleId]);
    if (!activeDefinition) {
        return null;
    }
    return (_jsxs(_Fragment, { children: [
            _jsx(KdaReplaceConfirmationDialog, { isOpen: isReplaceConfirmationOpen, onCancel: onCancelReplace, onConfirm: onConfirmReplace }), _jsx(KdaProvider, { definition: activeDefinition, separators: separators, includeTags: includeTags, excludeTags: excludeTags, children: _jsx(KdaDialog, { ...dialogProps, titleElementId: titleId, onClose: onCloseInternal }) })
        ] }));
}
function focusTitleElement(titleElementId) {
    const titleElement = document.getElementById(titleElementId);
    if (titleElement) {
        titleElement.focus();
    }
}
//# sourceMappingURL=KdaDialogController.js.map