import type { Class } from "@common/types"; export namespace Resources { const resources = new Map>(); export function get(id: string): T | undefined; export function get(ctor: Class, id: string): T | undefined; export function get(ctorOrId: Class | string, id?: string): T | undefined { const ctor = typeof ctorOrId === 'string' ? undefined : ctorOrId; id = typeof ctorOrId === 'string' ? ctorOrId : id; if (id == null) return undefined; if (ctor == null) { for (const ns of resources.values()) { if (ns.has(id)) { return ns.get(id) as T; } } return undefined } return resources.get(ctor)?.get(id) as T; } export function add(id: string, value: NonNullable): string { const ctor = value.constructor; const namespace: Map = resources.get(ctor) ?? new Map(); if (namespace.has(id)) throw new Error(`Resource "${id}" is already registered`); namespace.set(id, value); resources.set(ctor, namespace); return id; } }