{"version":3,"file":"state-machine.mjs","names":["createStateMachine","errors","options","defaultErrors","legal","states","state","transitions","can","from","to","assert"],"sources":["../src/state-machine.ts"],"sourcesContent":["import { defaultErrors, type ErrorFactory } from './errors'\n\n/**\n * Typed transition legality from a constants tuple (the conventions.md\n * vocabulary pattern). The transition map is typed against the tuple, so an\n * invalid literal cannot compile; `assert` throws via the injected\n * ErrorFactory so it drops straight into a command guard.\n */\nexport function createStateMachine<const State extends string>(\n\tstates: readonly State[],\n\t// NoInfer: the vocabulary comes from the tuple; a stray literal in the\n\t// transition map must fail to compile, not widen the state union.\n\ttransitions: Partial<Record<NoInfer<State>, readonly NoInfer<State>[]>>,\n\toptions?: { errors?: ErrorFactory },\n) {\n\tconst errors = options?.errors ?? defaultErrors\n\tconst legal = new Map<State, ReadonlySet<State>>(\n\t\tstates.map((state) => [state, new Set(transitions[state] ?? [])]),\n\t)\n\n\tfunction can(from: State, to: State): boolean {\n\t\treturn legal.get(from)?.has(to) ?? false\n\t}\n\n\tfunction assert(from: State, to: State): void {\n\t\tif (!can(from, to)) {\n\t\t\terrors.throw({\n\t\t\t\tcode: 'INVALID_TRANSITION',\n\t\t\t\tmessage: `Transition \"${from}\" → \"${to}\" is not legal`,\n\t\t\t})\n\t\t}\n\t}\n\n\treturn Object.freeze({ states, can, assert })\n}\n"],"mappings":"6CAQA,SAAgBA,EACf,EAGA,EACA,EACC,CACD,IAAMC,EAASC,GAAS,QAAUC,EAC5BC,EAAQ,IAAI,IACjBC,EAAO,IAAK,GAAU,CAACC,EAAO,IAAI,IAAIC,EAAYD,IAAU,CAAC,CAAC,CAAC,CAAC,CACjE,EAEA,SAASE,EAAI,EAAa,EAAoB,CAC7C,OAAOJ,EAAM,IAAIK,CAAI,CAAC,EAAE,IAAIC,CAAE,GAAK,EACpC,CAEA,SAASC,EAAO,EAAa,EAAiB,CACxCH,EAAIC,EAAMC,CAAE,GAChBT,EAAO,MAAM,CACZ,KAAM,qBACN,QAAS,eAAeQ,EAAK,OAAOC,EAAG,eACxC,CAAC,CAEH,CAEA,OAAO,OAAO,OAAO,CAAE,SAAQ,MAAK,QAAO,CAAC,CAC7C"}