import { f as clampTimerTimeoutMs, j as resolveTimerTimeoutMs } from "./number-coercion-CJQ8TR--.js";
import { f as parseConfiguredModelVisibilityEntries } from "./model-selection-shared-VDjgTYof.js";
const modelCatalogBrowseDeps = {
	setTimeout: globalThis.setTimeout,
	clearTimeout: globalThis.clearTimeout
};
function resolveModelCatalogBrowseTimeoutMs(value) {
	return clampTimerTimeoutMs(value, 1) ?? resolveTimerTimeoutMs(750, 1);
}
async function loadModelCatalogForBrowse(params) {
	if ((params.view ?? "default") === "all") return await params.loadCatalog({ readOnly: false });
	if (parseConfiguredModelVisibilityEntries({ cfg: params.cfg }).providerWildcards.size > 0) 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 { loadModelCatalogForBrowse as t };
