import { getDefaultModel, getModel } from "./model-registry.js";
/**
 * Pick a provider/model the user is logged into, preferring the saved defaults.
 * Mirrors the CLI's resolveActiveProvider, but NEVER throws when logged out:
 * instead it falls back to `preferred` + its default model and reports
 * `loggedIn: false`. This is what lets the ezcoder-app sidecar boot (and serve the
 * login endpoints) for a fresh user with no credentials — throwing here used to
 * kill the sidecar before it listened, making login impossible.
 *
 * Credentials resolve lazily at prompt time, so a logged-out boot is safe: a
 * prompt sent before login fails cleanly through the run's error handling.
 */
export async function resolveStartOrFallback(auth, allProviders, preferred, savedModel) {
    const loggedIn = [];
    for (const p of allProviders) {
        if (await auth.hasProviderAuth(p))
            loggedIn.push(p);
    }
    if (loggedIn.length === 0) {
        // Logged-out fallback: boot with the preferred provider's default model so
        // the sidecar still starts and the login flow is reachable.
        return { provider: preferred, model: getDefaultModel(preferred).id, loggedIn: false };
    }
    if (loggedIn.includes(preferred)) {
        const saved = savedModel ? getModel(savedModel) : undefined;
        return {
            provider: preferred,
            model: saved?.provider === preferred ? saved.id : getDefaultModel(preferred).id,
            loggedIn: true,
        };
    }
    const provider = loggedIn[0];
    return { provider, model: getDefaultModel(provider).id, loggedIn: true };
}
//# sourceMappingURL=resolve-start.js.map