import { useEffect, useState } from "react";
export function useAgentProfileSelection(input) {
    const { agent, agentStub, profiles, setAgent } = input;
    const [activeProfile, setActiveProfile] = useState(undefined);
    const [selectedModelId, setSelectedModelId] = useState(undefined);
    useEffect(() => {
        if (!profiles || profiles.length === 0)
            return;
        const profileIdToUse = agent?.profileId || agentStub.profileId;
        const normalizedProfileId = profileIdToUse?.toLowerCase();
        const candidate = normalizedProfileId
            ? profiles.find((p) => p.id?.toLowerCase() === normalizedProfileId)
            : undefined;
        if (!candidate) {
            setActiveProfile(undefined);
            return;
        }
        setActiveProfile((prev) => {
            if (!prev || prev.id !== candidate.id)
                return candidate;
            if (prev === candidate)
                return prev;
            return candidate;
        });
    }, [
        profiles,
        agent?.id,
        agent?.profileId,
        agentStub.id,
        agentStub.profileId,
    ]);
    useEffect(() => {
        if (!activeProfile)
            return;
        const agentModelName = agent?.model;
        const models = activeProfile.models || [];
        let nextModelId = undefined;
        if (agentModelName) {
            const match = models.find((model) => (model.name || "").toLowerCase() === agentModelName.toLowerCase());
            if (match)
                nextModelId = match.id;
        }
        if (!nextModelId) {
            nextModelId = activeProfile.defaultModelId || models[0]?.id;
        }
        setSelectedModelId(nextModelId || undefined);
        if (!agentModelName && nextModelId) {
            const modelName = models.find((model) => model.id === nextModelId)?.name || "";
            if (modelName) {
                setAgent((prev) => (prev ? { ...prev, model: modelName } : prev));
            }
        }
    }, [activeProfile?.id, agent?.model, setAgent]);
    return {
        activeProfile,
        setActiveProfile,
        selectedModelId,
        setSelectedModelId,
    };
}
//# sourceMappingURL=useAgentProfileSelection.js.map