export type AdjacencyMap = Readonly<Record<string, readonly string[]>>

export type TransitionPredicate<Ctx = unknown> = (ctx: Ctx) => boolean

export function isValidTransition<Ctx = unknown>(
  adjacency: AdjacencyMap,
  from: string,
  to: string,
  predicate?: TransitionPredicate<Ctx>,
  ctx?: Ctx,
): boolean {
  const allowed = adjacency[from]
  if (!allowed?.includes(to)) {
    return false
  }
  if (predicate && !predicate(ctx as Ctx)) {
    return false
  }
  return true
}
