import { useEffect, useLayoutEffect, useState, } from "react";
function resolveProfile(profiles, profileId) {
    const normalizedProfileId = profileId?.toLowerCase();
    return normalizedProfileId
        ? profiles.find((profile) => profile.id?.toLowerCase() === normalizedProfileId)
        : undefined;
}
export function isAgentProfileResolved(input) {
    // The session starts with a placeholder built from the stub. Treating that
    // placeholder as authoritative exposes the composer one render before the
    // load pipeline installs the draft metadata and marks the session ready.
    // Require both the ready transition and the matching session identity so a
    // reused terminal cannot briefly expose the previous agent's composer.
    if (input.loadStatus !== "ready" ||
        !input.agent ||
        input.agent.id.trim().toLowerCase() !==
            input.agentStub.id.trim().toLowerCase()) {
        return false;
    }
    const expectedProfileId = input.agent.profileId?.trim() || input.agentStub.profileId?.trim() || "";
    const activeProfileId = input.activeProfile?.id?.trim() || "";
    return (expectedProfileId.length > 0 &&
        activeProfileId.length > 0 &&
        expectedProfileId.toLowerCase() === activeProfileId.toLowerCase());
}
export function isGreetingProfileSwitcherReady(input) {
    if (!input.requiresSwitcher || input.messageCount > 0) {
        return true;
    }
    return (!input.hasError &&
        !input.hideGreeting &&
        !input.isLoading &&
        input.hasActiveProfile);
}
export function useAgentProfileSelection(input) {
    const { agent, agentStub, profiles, setAgent } = input;
    const [activeProfile, setActiveProfile] = useState(() => resolveProfile(profiles, agent?.profileId || agentStub.profileId));
    const [selectedModelId, setSelectedModelId] = useState(undefined);
    useLayoutEffect(() => {
        if (!profiles || profiles.length === 0)
            return;
        const profileIdToUse = agent?.profileId || agentStub.profileId;
        const candidate = resolveProfile(profiles, profileIdToUse);
        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