// Sleep backend — trivial, shows how simple a backend can be
import type { EffectBackend } from "../backend";
import type { State } from "../../eval/machine";
import type { OpCall } from "../opcall";
import type { Val } from "../../eval/values";

export class SleepBackend implements EffectBackend {
  readonly ops = ["sleep.op"] as const;

  async handle(_state: State, opcall: OpCall): Promise<State> {
    const msArg = opcall.args[0];
    const ms = msArg?.tag === "Num" ? msArg.n : 1000;
    await new Promise(resolve => setTimeout(resolve, ms));
    return opcall.resumption.invoke({ tag: "Unit" } as Val) as State;
  }
}
