import type { CordoConfig } from "./files/config" import { CordoMagic } from "./magic" export namespace Hooks { export function callHook< Name extends keyof CordoConfig['hooks'], Value = Parameters>[0], Context = Parameters>[1] >( hookName: Name, value: Value, context?: Context, config?: NonNullable ): ReturnType> { if (!config) { if (!CordoMagic.inContext()) return value as any const contextConfig = CordoMagic.getConfig() if (!contextConfig) { console.warn(`Calling hook '${hookName}' failed, no config provided and no config found in context.`) return value as any } config = contextConfig } const hook = config.hooks[hookName] if (!hook) return value as any if (hookName === 'transformUserFacingText') { // @ts-expect-error context = { ...(context || {}), interaction: CordoMagic.getInvoker() } } // @ts-expect-error return hook(value, context) } export function isDefined(name: keyof CordoConfig['hooks']) { return !!CordoMagic.getConfig()?.hooks?.[name] } }