// Generated by bynkc — do not edit by hand.
// context turnstile

import { Ok, Err, Some, None, type Result, type Option, type ValidationError, type DurableObjectState, type DurableObjectNamespace, StateRegistry, makeAgent } from "./runtime.js";

export type Gate =
    { readonly tag: "Locked" }
  | { readonly tag: "Unlocked" };

export const Gate = {
  Locked: { tag: "Locked" } as Gate,
  Unlocked: { tag: "Unlocked" } as Gate,
};

export interface TurnstileState {
  readonly gate: Gate;
}

const __TurnstileRegistry = new StateRegistry();
function __zeroOfTurnstileState(): TurnstileState { return { gate: Gate.Locked }; }

export class Turnstile {
  state: DurableObjectState;
  constructor(state: DurableObjectState) {
    this.state = state;
  }

  private async loadState(): Promise<TurnstileState> {
    const stored = await this.state.storage.get<TurnstileState>("state");
    return stored ?? __zeroOfTurnstileState();
  }

  private async commitState(s: TurnstileState): Promise<void> {
    await this.state.storage.put("state", s);
  }

  async unlock(deps: {}): Promise<Result<void, string>> {
    const currentState = await this.loadState();
    await this.commitState({ ...currentState, gate: Gate.Unlocked });
    return Ok(undefined);
  }

}

export function __makeTurnstile(key: string, env?: { TURNSTILE?: DurableObjectNamespace }): Turnstile {
  return makeAgent(__TurnstileRegistry, env?.TURNSTILE, key, (state) => new Turnstile(state));
}

export function __resetAgents(): void {
  __TurnstileRegistry.reset();
}

