export class ServiceManager {
    /**
     * Registered bindings.
     */
    #bindings = new Map();
    /**
     * Registers a service by a specified key.
     * @param key The key to use to lookup the service.
     * @param instance The instance of the service class.
     */
    register(key, instance) {
        if (this.#bindings.has(key)) {
            throw new Error(`Trying to register ${String(key)} service, but it is already registered.`);
        }
        if (!instance) {
            throw new Error(`Trying to register ${String(key)} service, but the manager did not receive an instance.`);
        }
        this.#bindings.set(key, instance);
    }
    /**
     * Returns a service registered in the application.
     * If the service is not found it throws an error.
     *
     * It ensures a service is running before returning the service.
     *
     * @param key The key of the service to retrieve.
     * @returns The service for the given key.
     */
    async get(key) {
        const service = this.#bindings.get(key);
        if (!service) {
            throw new Error(`Trying to get ${String(key)} service, but it is not registered.`);
        }
        await service.start();
        return service;
    }
    async unload() {
        for (const service of this.#bindings.values()) {
            await service.stop();
            await service.destroy();
        }
    }
}
//# sourceMappingURL=ServiceManager.js.map