/** SDK-owned platform module. This implementation is maintained in goodvibes-sdk. */
export class MediaProviderRegistry {
    providers = new Map();
    register(provider, options = {}) {
        const id = provider.id.trim();
        if (!id)
            throw new Error('Media provider id is required');
        if (this.providers.has(id) && !options.replace) {
            throw new Error(`Media provider already registered: ${id}`);
        }
        const registered = { ...provider, id };
        this.providers.set(id, registered);
        return () => {
            if (this.providers.get(id) === registered)
                this.unregister(id);
        };
    }
    unregister(id) {
        return this.providers.delete(id);
    }
    get(id) {
        return this.providers.get(id) ?? null;
    }
    list() {
        return [...this.providers.values()]
            .map((provider) => ({
            id: provider.id,
            label: provider.label,
            capabilities: [...provider.capabilities],
        }))
            .sort((a, b) => a.label.localeCompare(b.label) || a.id.localeCompare(b.id));
    }
    findProvider(capability, providerId) {
        if (providerId) {
            const provider = this.get(providerId);
            return provider?.capabilities.includes(capability) ? provider : null;
        }
        return [...this.providers.values()].find((provider) => provider.capabilities.includes(capability)) ?? null;
    }
    async status() {
        const statuses = [];
        for (const provider of this.providers.values()) {
            if (provider.status) {
                statuses.push(await provider.status());
                continue;
            }
            statuses.push({
                id: provider.id,
                label: provider.label,
                state: 'healthy',
                capabilities: [...provider.capabilities],
                configured: true,
                metadata: {},
            });
        }
        return statuses.sort((a, b) => a.label.localeCompare(b.label) || a.id.localeCompare(b.id));
    }
}
