import { useEffect } from "react";
import { cleanId } from "../../utils/id-helper";
export function useEditorUrlSync(params) {
    const { isInitialLoad, setIsInitialLoad, searchParams, setCurrentWizardId, setWorkspaceId, setCompareMode, loadItem, currentItemDescriptor, loadItemDescriptor, } = params;
    // Initial URL-derived state
    useEffect(() => {
        if (!isInitialLoad)
            return;
        const itemid = searchParams.get("itemid");
        const wizardId = searchParams.get("wizardid");
        const urlWorkspace = searchParams.get("workspace");
        if (wizardId)
            setCurrentWizardId(wizardId);
        if (urlWorkspace)
            setWorkspaceId(urlWorkspace);
        else if (!itemid)
            setWorkspaceId("home");
    }, [isInitialLoad, searchParams]);
    // Initial compare mode
    useEffect(() => {
        if (!isInitialLoad)
            return;
        if (searchParams.has("compare")) {
            const compareValue = searchParams.get("compare") === "true";
            setCompareMode(compareValue);
        }
    }, [isInitialLoad, searchParams]);
    // Initial item load
    useEffect(() => {
        const itemid = isInitialLoad ? searchParams.get("itemid") : null;
        const itemId = cleanId(loadItemDescriptor?.id ?? itemid ?? undefined);
        const language = loadItemDescriptor?.language ??
            (isInitialLoad
                ? (searchParams.get("lang") ?? searchParams.get("language"))
                : null);
        const version = loadItemDescriptor?.version ??
            (isInitialLoad && searchParams.has("version")
                ? parseInt(searchParams.get("version"))
                : 0);
        if (!itemId || !language)
            return;
        if (currentItemDescriptor?.id === itemId &&
            currentItemDescriptor?.language === language &&
            (!version || currentItemDescriptor?.version === version)) {
            return;
        }
        loadItem({ id: itemId, language, version });
    }, [isInitialLoad, searchParams, loadItemDescriptor, currentItemDescriptor]);
    // Mark initial load done when appropriate (simple heuristic: once we derived state at least once)
    useEffect(() => {
        if (isInitialLoad) {
            setIsInitialLoad(false);
        }
    }, [isInitialLoad]);
}
//# sourceMappingURL=useEditorUrlSync.js.map