import { f as clampTimerTimeoutMs, j as resolveTimerTimeoutMs } from "./number-coercion-CJQ8TR--.js";
import { f as parseConfiguredModelVisibilityEntries } from "./model-selection-shared-Dnay2Q8U.js";
const modelCatalogBrowseDeps = {
	setTimeout: globalThis.setTimeout,
	clearTimeout: globalThis.clearTimeout
};
/** True when a browse view cannot be answered from read-only cached catalog entries. */
function modelCatalogBrowseRequiresFullDiscovery(params) {
	const view = params.view ?? "default";
	return view === "all" || view === "configured" && parseConfiguredModelVisibilityEntries({ cfg: params.cfg }).providerWildcards.size > 0;
}
function resolveModelCatalogBrowseTimeoutMs(value) {
	return clampTimerTimeoutMs(value, 1) ?? resolveTimerTimeoutMs(750, 1);
}
/** Loads catalog entries for browse views, using read-only discovery unless full catalog is required. */
async function loadModelCatalogForBrowse(params) {
	const view = params.view ?? "default";
	if (modelCatalogBrowseRequiresFullDiscovery({
		cfg: params.cfg,
		view
	})) return await params.loadCatalog({ readOnly: false });
	let timeout;
	const timeoutMs = resolveModelCatalogBrowseTimeoutMs(params.timeoutMs);
	const timedOut = Symbol("model-catalog-browse-timeout");
	const catalogPromise = params.loadCatalog({ readOnly: true });
	const timeoutPromise = new Promise((resolve) => {
		timeout = modelCatalogBrowseDeps.setTimeout(() => resolve(timedOut), timeoutMs);
		timeout.unref?.();
	});
	try {
		const result = await Promise.race([catalogPromise, timeoutPromise]);
		if (result === timedOut) {
			catalogPromise.catch(() => void 0);
			params.onTimeout?.(timeoutMs);
			return [];
		}
		return result;
	} finally {
		if (timeout) modelCatalogBrowseDeps.clearTimeout(timeout);
	}
}
//#endregion
export { modelCatalogBrowseRequiresFullDiscovery as n, loadModelCatalogForBrowse as t };
