import type { IntentRegistry } from '@astrale-os/shell' import { useCallback } from 'react' import type { IntentPayload } from './envelope.js' import { useShell } from '../session/shell.hook.js' import { intentMessage } from './envelope.js' import { resolveIntentTarget } from './resolve-intent-target.js' /** Fire-and-forget intent sender returned by {@link useEmitIntent}. */ export interface EmitIntent { ( name: K, payload: IntentPayload, /** `to`: `'parent'` (reserved) or a child windowId. Default: the parent, if any. */ opts?: { to?: 'parent' | string }, ): void } /** * Return a stable sender for intents that do not need a reply. Emitted messages * carry no correlation id, so listeners can handle navigation and notifications * without retaining a pending request. */ export function useEmitIntent(): EmitIntent { const shell = useShell() return useCallback( ( name: K, payload: IntentPayload, opts?: { to?: 'parent' | string }, ): void => { const target = resolveIntentTarget(shell, opts?.to) if (target.kind === 'invalid') throw new Error(`useEmitIntent: ${target.reason}`) const message = intentMessage(name, payload) if (target.kind === 'parent') shell.parent!.send(message) else shell.children.send(target.windowId, message) }, [shell], ) }