//#region src/extensions/manifest-registry.ts
var ManifestRegistry = class ManifestRegistry {
	entries = /* @__PURE__ */ new Map();
	providerIndex = /* @__PURE__ */ new Map();
	speechProviderIndex = /* @__PURE__ */ new Map();
	mediaUnderstandingProviderIndex = /* @__PURE__ */ new Map();
	channelIndex = /* @__PURE__ */ new Map();
	modelPrefixIndex = /* @__PURE__ */ new Map();
	envVarIndex = /* @__PURE__ */ new Map();
	static fromDiscovered(discovered) {
		const registry = new ManifestRegistry();
		for (const ext of discovered) registry.addEntry({
			id: ext.id,
			manifest: ext.manifest,
			source: ext.source,
			path: ext.path
		});
		return registry;
	}
	addEntry(entry) {
		this.entries.set(entry.id, entry);
		this.buildIndexesForEntry(entry);
	}
	buildIndexesForEntry(entry) {
		const manifest = entry.manifest;
		if (manifest.providers) for (const providerId of manifest.providers) this.providerIndex.set(providerId, entry.id);
		for (const providerId of manifest.speechProviders ?? []) this.speechProviderIndex.set(providerId, entry.id);
		for (const providerId of manifest.contracts?.speechProviders ?? []) this.speechProviderIndex.set(providerId, entry.id);
		for (const providerId of manifest.mediaUnderstandingProviders ?? []) this.mediaUnderstandingProviderIndex.set(providerId, entry.id);
		for (const providerId of manifest.contracts?.mediaUnderstandingProviders ?? []) this.mediaUnderstandingProviderIndex.set(providerId, entry.id);
		if (manifest.channels) for (const channelId of manifest.channels) this.channelIndex.set(channelId, entry.id);
		if (manifest.modelSupport?.modelPrefixes) for (const prefix of manifest.modelSupport.modelPrefixes) this.modelPrefixIndex.set(prefix, entry.id);
		if (manifest.providerAuthEnvVars) for (const envVars of Object.values(manifest.providerAuthEnvVars)) for (const envVar of envVars) this.envVarIndex.set(envVar, entry.id);
		if (manifest.channelEnvVars) for (const envVars of Object.values(manifest.channelEnvVars)) for (const envVar of envVars) this.envVarIndex.set(envVar, entry.id);
	}
	getAllEntries() {
		return Array.from(this.entries.values());
	}
	getEntry(id) {
		return this.entries.get(id);
	}
	findByProvider(providerId) {
		const extensionId = this.providerIndex.get(providerId);
		return extensionId ? this.entries.get(extensionId) : void 0;
	}
	findBySpeechProvider(providerId) {
		const extensionId = this.speechProviderIndex.get(providerId);
		return extensionId ? this.entries.get(extensionId) : void 0;
	}
	findByMediaUnderstandingProvider(providerId) {
		const extensionId = this.mediaUnderstandingProviderIndex.get(providerId);
		return extensionId ? this.entries.get(extensionId) : void 0;
	}
	findByChannel(channelId) {
		const extensionId = this.channelIndex.get(channelId);
		return extensionId ? this.entries.get(extensionId) : void 0;
	}
	findByModelId(modelId) {
		const normalizedModelId = modelId.toLowerCase();
		for (const [prefix, extensionId] of this.modelPrefixIndex) if (normalizedModelId.startsWith(prefix.toLowerCase())) return this.entries.get(extensionId);
		for (const entry of this.entries.values()) if (this.matchesModelPatterns(normalizedModelId, entry.manifest.modelSupport)) return entry;
	}
	findByEnvVar(envVarName) {
		const extensionId = this.envVarIndex.get(envVarName);
		return extensionId ? this.entries.get(extensionId) : void 0;
	}
	detectAvailableByEnv(env) {
		const found = /* @__PURE__ */ new Set();
		const results = [];
		for (const [envVar, extensionId] of this.envVarIndex) if (env[envVar] && !found.has(extensionId)) {
			found.add(extensionId);
			const entry = this.entries.get(extensionId);
			if (entry) results.push(entry);
		}
		return results;
	}
	listProviderExtensions() {
		return this.getAllEntries().filter((entry) => entry.manifest.providers && entry.manifest.providers.length > 0);
	}
	listChannelExtensions() {
		return this.getAllEntries().filter((entry) => entry.manifest.channels && entry.manifest.channels.length > 0);
	}
	listEnabledByDefault() {
		return this.getAllEntries().filter((entry) => entry.manifest.enabledByDefault === true);
	}
	matchesModelPatterns(modelId, modelSupport) {
		if (!modelSupport?.modelPatterns) return false;
		for (const pattern of modelSupport.modelPatterns) try {
			if (new RegExp(pattern, "i").test(modelId)) return true;
		} catch {}
		return false;
	}
};
//#endregion
export { ManifestRegistry };

//# sourceMappingURL=manifest-registry.js.map