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

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

export type Level = number & { readonly __brand: "gauge.Level" };

export const Level = {
  of(value: number): Result<Level, ValidationError> {
    if (!Number.isInteger(value)) {
      return Err({ field: "Level", message: "must be an integer", value });
    }
    if (!(value > 0)) {
      return Err({ field: "Level", message: "must be positive", value });
    }
    return Ok(value as Level);
  },
  unsafe(value: number): Level {
    return value as Level;
  },
};

export interface GaugeState {
  readonly level: Level;
}

const __GaugeRegistry = new StateRegistry();
function __zeroOfGaugeState(): GaugeState { return { level: Level.unsafe(1) }; }

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

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

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

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

}

export function __makeGauge(key: string, env?: { GAUGE?: DurableObjectNamespace }): Gauge {
  return makeAgent(__GaugeRegistry, env?.GAUGE, key, (state) => new Gauge(state));
}

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

