import { getApiKeyFromEnv, init_env_keys } from "../env-keys.js";
import { getDefaultAuthProfileStore } from "./auth-profile-store.js";
//#region src/providers/auth-runtime/resolve-auth.ts
init_env_keys();
function resolveAuthProfileForProvider(options) {
	const { providerId } = options;
	if (!providerId) return {
		apiKey: void 0,
		mode: "api-key",
		source: "none"
	};
	const profile = pickProfile(options.store ?? getDefaultAuthProfileStore(), providerId, options.profileName);
	if (profile) {
		const key = (typeof profile.apiKey === "string" && profile.apiKey.length > 0 ? profile.apiKey : void 0) ?? (typeof profile.oauthAccessToken === "string" && profile.oauthAccessToken.length > 0 ? profile.oauthAccessToken : void 0);
		if (key) return {
			apiKey: key,
			mode: profile.mode,
			profileId: profile.profileId,
			source: "store",
			meta: profile.meta
		};
	}
	const cfgKey = readCfgApiKey(options.cfg, providerId);
	if (cfgKey) {
		const azure = readCfgAzureMeta(options.cfg, providerId);
		return {
			apiKey: cfgKey,
			mode: azure ? "azure-key" : "api-key",
			source: "config",
			meta: azure
		};
	}
	const envKey = options.envReader ? readFromCustomEnv(options.envReader, providerId) : getApiKeyFromEnv(providerId);
	if (envKey) return {
		apiKey: envKey,
		mode: "api-key",
		source: "env"
	};
	return {
		apiKey: void 0,
		mode: "api-key",
		source: "none"
	};
}
function pickProfile(store, providerId, profileName) {
	try {
		return store.get(providerId, profileName);
	} catch {
		return;
	}
}
function readCfgApiKey(cfg, providerId) {
	const providers = cfg?.providers;
	if (!providers || typeof providers !== "object") return void 0;
	const entry = providers[providerId];
	if (!entry || typeof entry !== "object") return void 0;
	const apiKey = entry.apiKey;
	return typeof apiKey === "string" && apiKey.length > 0 ? apiKey : void 0;
}
function readCfgAzureMeta(cfg, providerId) {
	const providers = cfg?.providers;
	if (!providers || typeof providers !== "object") return void 0;
	const entry = providers[providerId];
	if (!entry || typeof entry !== "object") return void 0;
	const azure = entry.azure;
	return azure && typeof azure === "object" ? azure : void 0;
}
function readFromCustomEnv(reader, providerId) {
	const v = reader(providerId.toUpperCase().replace(/-/g, "_") + "_API_KEY");
	return typeof v === "string" && v.length > 0 ? v : void 0;
}
//#endregion
export { resolveAuthProfileForProvider };

//# sourceMappingURL=resolve-auth.js.map