import { BaseRegistry } from './BaseRegistry.js';
import { filterWarpRoutesIds } from './warp-utils.js';
/**
 * Shared code for sync registries like the FileSystem and Partial registries.
 * This is required because of the inconsistent sync/async methods across registries.
 * If the Infra package can be updated to work with async-only methods, this code can be moved to the BaseRegistry class.
 */
export class SynchronousRegistry extends BaseRegistry {
    getChains() {
        return Object.keys(this.listRegistryContent().chains);
    }
    getChainMetadata(chainName) {
        return this.getMetadata()[chainName] || null;
    }
    getChainAddresses(chainName) {
        return this.getAddresses()[chainName] || null;
    }
    addChain(chain) {
        const currentChains = this.listRegistryContent().chains;
        if (currentChains[chain.chainName])
            throw new Error(`Chain ${chain.chainName} already exists in registry`);
        this.createOrUpdateChain(chain);
    }
    updateChain(chain) {
        const currentChains = this.listRegistryContent();
        if (!currentChains.chains[chain.chainName]) {
            this.logger.debug(`Chain ${chain.chainName} not found in registry, adding it now`);
        }
        this.createOrUpdateChain(chain);
    }
    removeChain(chainName) {
        const currentChains = this.listRegistryContent().chains;
        if (!currentChains[chainName])
            throw new Error(`Chain ${chainName} does not exist in registry`);
        if (this.listContentCache?.chains[chainName])
            delete this.listContentCache.chains[chainName];
        if (this.metadataCache?.[chainName])
            delete this.metadataCache[chainName];
        if (this.addressCache?.[chainName])
            delete this.addressCache[chainName];
    }
    getWarpRoute(routeId) {
        return this.getWarpRoutesForIds([routeId])[0] || null;
    }
    getWarpDeployConfig(routeId) {
        return this.getWarpDeployConfigForIds([routeId])[0] || null;
    }
    /**
     * Retrieves a filtered map of the warp routes configs
     */
    getWarpRoutes(filter) {
        const warpRoutes = this.listRegistryContent().deployments.warpRoutes;
        const { ids: routeIds } = filterWarpRoutesIds(warpRoutes, filter);
        const configs = this.getWarpRoutesForIds(routeIds);
        const idsWithConfigs = routeIds.map((id, i) => [id, configs[i]]);
        return Object.fromEntries(idsWithConfigs);
    }
    /**
     * Retrieves a map of all the warp routes deployment configs
     */
    getWarpDeployConfigs(filter) {
        const warpDeployConfig = this.listRegistryContent().deployments.warpDeployConfig;
        const { ids: routeIds } = filterWarpRoutesIds(warpDeployConfig, filter);
        const configs = this.getWarpDeployConfigForIds(routeIds);
        const idsWithConfigs = routeIds.map((id, i) => [
            id,
            configs[i],
        ]);
        return Object.fromEntries(idsWithConfigs);
    }
}
