import { getWorkflowTriggers, searchPluginMethods, WorkflowTrigger, type PluginMethodResponseDto, type WorkflowTriggerResponseDto, } from '@immich/sdk'; import { t } from 'svelte-i18n'; import { SvelteMap } from 'svelte/reactivity'; import { get } from 'svelte/store'; import { authManager } from '$lib/managers/auth-manager.svelte'; import { eventManager } from '$lib/managers/event-manager.svelte'; class PluginManager { #loading: Promise | undefined; #methodMap = new SvelteMap(); #methods = $state([]); #triggers = $state([]); constructor() { eventManager.on({ AuthLogout: () => this.clearCache(), AuthUserLoaded: () => this.initialize(), }); // loaded event might have already happened if (authManager.authenticated) { void this.initialize(); } } get triggers() { return this.#triggers; } ready() { return this.initialize(); } getMethod(key: string) { return this.#methodMap.get(key); } getMethodLabel(key: string) { const method = this.getMethod(key); return method?.title ?? get(t)('unknown'); } getTrigger(trigger: WorkflowTrigger) { const result = this.#triggers.find((t) => t.trigger === trigger); if (!result) { throw new Error(`Unknown trigger type: ${trigger}`); } return result; } private clearCache() { this.#loading = undefined; this.#methodMap = new SvelteMap(); } private initialize() { if (!this.#loading) { this.#loading = this.load(); } return this.#loading; } private async load() { const [methods, triggers] = await Promise.all([searchPluginMethods({}), getWorkflowTriggers()]); this.#methods = methods; for (const method of this.#methods) { this.#methodMap.set(method.key, method); } this.#triggers = triggers; } } export const pluginManager = new PluginManager();