// Generated by bynkc — do not edit by hand.
// context demo.counter

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

/**
 * A minimal stateful agent in the bundle target: instantiation lowers through the
 * generated factory, the method call is a direct call, and state persists per key
 * across calls within a session.
 */

export type CounterId = string & { readonly __brand: "demo.counter.CounterId" };

export const CounterId = {
  of(value: string): Result<CounterId, ValidationError> {
    if (!new RegExp("^" + "[a-z0-9]{1,16}" + "$").test(value)) {
      return Err({ field: "CounterId", message: "must match /[a-z0-9]{1,16}/", value });
    }
    return Ok(value as CounterId);
  },
  unsafe(value: string): CounterId {
    return value as CounterId;
  },
};

export type CounterError =
    { readonly tag: "Overflow" };

export const CounterError = {
  Overflow: { tag: "Overflow" } as CounterError,
};

export interface CounterState {
  readonly count: number;
}

const __CounterRegistry = new StateRegistry();
function __zeroOfCounterState(): CounterState { return { count: 0 }; }

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

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

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

  async increment(deps: {}): Promise<Result<number, CounterError>> {
    const currentState = await this.loadState();
    const next = currentState.count + 1;
    await this.commitState({ ...currentState, count: next });
    return Ok(next);
  }

  async current(deps: {}): Promise<Result<number, CounterError>> {
    const currentState = await this.loadState();
    return Ok(currentState.count);
  }

}

export function __makeCounter(key: CounterId, env?: { COUNTER?: DurableObjectNamespace }): Counter {
  return makeAgent(__CounterRegistry, env?.COUNTER, key, (state) => new Counter(state));
}

export const bump = {
  async call(id: CounterId, deps: {}): Promise<Result<number, CounterError>> {
    const counter = __makeCounter(id);
    const total = await counter.increment(deps);
    return total;
  },
};

export const read = {
  async call(id: CounterId, deps: {}): Promise<Result<number, CounterError>> {
    const counter = __makeCounter(id);
    const total = await counter.current(deps);
    return total;
  },
};

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

export interface DemoCounterDeps {
}

export function makeSurface(deps: DemoCounterDeps) {
  return {
    async bump(id: CounterId): Promise<Result<number, CounterError>> {
      return bump.call(id, deps);
    },
    async read(id: CounterId): Promise<Result<number, CounterError>> {
      return read.call(id, deps);
    },
  };
}

