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

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

export interface TaskState {
  readonly retries: number;
  readonly done: boolean;
}

const __TaskRegistry = new StateRegistry();
function __zeroOfTaskState(): TaskState { return { retries: 3, done: false }; }

function __rehydrateTaskState(s: TaskState): void {
  { const __r = ((__v) => typeof __v === "number" && Number.isInteger(__v) ? Ok(__v) : Err({ kind: "StructuralMismatch", path: "retries", expected: "number", actual: typeof __v } as BoundaryError))((s.retries as unknown as JsonValue)); if (__r.tag === "Err") throw rehydrationViolation("Task", __r.error); }
  { const __r = ((__v) => typeof __v === "boolean" ? Ok(__v) : Err({ kind: "StructuralMismatch", path: "done", expected: "boolean", actual: typeof __v } as BoundaryError))((s.done as unknown as JsonValue)); if (__r.tag === "Err") throw rehydrationViolation("Task", __r.error); }
}

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

  private async loadState(): Promise<TaskState> {
    const stored = await this.state.storage.get<TaskState>("state");
    if (stored === undefined) return __zeroOfTaskState();
    const __merged = { ...__zeroOfTaskState(), ...stored };
    __rehydrateTaskState(__merged);
    return __merged;
  }

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

  async peek(deps: {}): Promise<Result<void, string>> {
    const __state = await this.loadState();
    return Ok(undefined);
  }

}

export function __makeTask(key: string, env?: { TASK?: DurableObjectNamespace }): Task {
  return makeAgent(__TaskRegistry, env?.TASK, key, (state) => new Task(state));
}

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

