import "./agent-scope-CZ-IyS6E.js";
import { s as resolveDefaultAgentDir } from "./agent-scope-config-CT5O0iC8.js";
import { t as resolveModelPluginMetadataSnapshot } from "./model-discovery-context-DzvbaJC9.js";
import { i as modelKey } from "./model-selection-normalize-DknR-OD2.js";
import { n as discoverModels, t as discoverAuthStorage } from "./agent-model-discovery-C78ReycX.js";
import { i as shouldSuppressBuiltInModelFromManifest, r as shouldSuppressBuiltInModel } from "./model-suppression-wF_RtNBq.js";
import "./shared-BAXACrqC.js";
import { n as formatErrorWithStack, r as shouldFallbackToAuthHeuristics, t as MODEL_AVAILABILITY_UNAVAILABLE_CODE } from "./list.errors-BKEXWuM3.js";
import "./list.model-row-DvZAnLCX.js";
//#region src/agents/model-registry-loader.ts
function loadAgentModelRegistry(config, options = {}) {
	const agentDir = resolveDefaultAgentDir(config);
	const authStorage = discoverAuthStorage(agentDir, {
		readOnly: options.readOnly ?? true,
		skipCredentials: options.skipCredentials,
		config,
		workspaceDir: options.workspaceDir
	});
	const pluginMetadataSnapshot = resolveModelPluginMetadataSnapshot({
		config,
		workspaceDir: options.workspaceDir
	});
	return {
		agentDir,
		registry: discoverModels(authStorage, agentDir, {
			config,
			...pluginMetadataSnapshot ? { pluginMetadataSnapshot } : {},
			providerFilter: options.providerFilter,
			...options.workspaceDir ? { workspaceDir: options.workspaceDir } : {},
			normalizeModels: options.normalizeModels
		})
	};
}
//#endregion
//#region src/commands/models/list.registry.ts
function createAvailabilityUnavailableError(message) {
	const err = new Error(message);
	err.code = MODEL_AVAILABILITY_UNAVAILABLE_CODE;
	return err;
}
function normalizeAvailabilityError(err) {
	if (shouldFallbackToAuthHeuristics(err) && err instanceof Error) return err;
	return createAvailabilityUnavailableError(`Model availability unavailable: getAvailable() failed.\n${formatErrorWithStack(err)}`);
}
function validateAvailableModels(availableModels) {
	if (!Array.isArray(availableModels)) throw createAvailabilityUnavailableError("Model availability unavailable: getAvailable() returned a non-array value.");
	for (const model of availableModels) if (!model || typeof model !== "object" || typeof model.provider !== "string" || typeof model.id !== "string") throw createAvailabilityUnavailableError("Model availability unavailable: getAvailable() returned invalid model entries.");
	return availableModels;
}
function loadAvailableModels(registry, cfg, opts) {
	let availableModels;
	try {
		availableModels = registry.getAvailable();
	} catch (err) {
		throw normalizeAvailabilityError(err);
	}
	try {
		return validateAvailableModels(availableModels).filter((model) => opts?.runtimeSuppression === false ? !shouldSuppressBuiltInModelFromManifest({
			provider: model.provider,
			id: model.id,
			config: cfg
		}) : !shouldSuppressBuiltInModel({
			provider: model.provider,
			id: model.id,
			baseUrl: model.baseUrl,
			config: cfg
		}));
	} catch (err) {
		throw normalizeAvailabilityError(err);
	}
}
async function loadModelRegistry(cfg, opts) {
	const runtimeSuppression = opts?.normalizeModels !== false;
	const { registry } = loadAgentModelRegistry(cfg, {
		skipCredentials: opts?.loadAvailability === false,
		workspaceDir: opts?.workspaceDir,
		providerFilter: opts?.providerFilter,
		normalizeModels: opts?.normalizeModels
	});
	const models = registry.getAll().filter((model) => runtimeSuppression ? !shouldSuppressBuiltInModel({
		provider: model.provider,
		id: model.id,
		baseUrl: model.baseUrl,
		config: cfg
	}) : !shouldSuppressBuiltInModelFromManifest({
		provider: model.provider,
		id: model.id,
		config: cfg
	}));
	let availableKeys;
	let availabilityErrorMessage;
	if (opts?.loadAvailability !== false) try {
		const availableModels = loadAvailableModels(registry, cfg, { runtimeSuppression });
		availableKeys = new Set(availableModels.map((model) => modelKey(model.provider, model.id)));
	} catch (err) {
		if (!shouldFallbackToAuthHeuristics(err)) throw err;
		availableKeys = void 0;
		if (!availabilityErrorMessage) availabilityErrorMessage = formatErrorWithStack(err);
	}
	return {
		registry,
		models,
		availableKeys,
		availabilityErrorMessage
	};
}
//#endregion
//#region src/commands/models/list.registry-load.ts
async function loadListModelRegistry(cfg, opts) {
	const loaded = await loadModelRegistry(cfg, opts);
	return {
		...loaded,
		discoveredKeys: new Set(loaded.models.map((model) => modelKey(model.provider, model.id)))
	};
}
function findConfiguredRegistryModel(params) {
	const model = params.registry.find(params.entry.ref.provider, params.entry.ref.model);
	if (!model) return;
	if (shouldSuppressBuiltInModel({
		provider: model.provider,
		id: model.id,
		baseUrl: model.baseUrl,
		config: params.cfg
	})) return;
	return model;
}
function loadConfiguredListModelRegistry(cfg, entries, opts) {
	const { registry } = loadAgentModelRegistry(cfg, {
		workspaceDir: opts?.workspaceDir,
		providerFilter: opts?.providerFilter
	});
	const discoveredKeys = /* @__PURE__ */ new Set();
	const availableKeys = /* @__PURE__ */ new Set();
	for (const entry of entries) {
		const model = findConfiguredRegistryModel({
			registry,
			entry,
			cfg
		});
		if (!model) continue;
		const key = modelKey(model.provider, model.id);
		discoveredKeys.add(key);
		if (registry.hasConfiguredAuth(model)) availableKeys.add(key);
	}
	return {
		registry,
		discoveredKeys,
		availableKeys
	};
}
//#endregion
export { loadConfiguredListModelRegistry, loadListModelRegistry };
