// Generated by bynkc — do not edit by hand.
// context shop.inventory

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 Reservation {
  readonly id: string;
  readonly orderId: string;
  readonly region: string;
  readonly qty: number;
}

export const Reservation = {
};

export interface InventoryState {
  readonly reservations: Record<string, Reservation>;
  readonly reservations__idx_orderId: Record<string, string[]>;
}

const __InventoryRegistry = new StateRegistry();
function __zeroOfInventoryState(): InventoryState { return { reservations: {}, reservations__idx_orderId: {} }; }

function __rehydrateInventoryState(s: InventoryState): void {
  for (const __v of Object.values(s.reservations)) { const __r = deserialise_Reservation((__v as unknown as JsonValue), "reservations"); if (__r.tag === "Err") throw rehydrationViolation("Inventory", __r.error); }
  for (const __k of Object.keys(s.reservations)) { const __r = ((__v) => typeof __v === "string" ? Ok(__v) : Err({ kind: "StructuralMismatch", path: "reservations", expected: "string", actual: typeof __v } as BoundaryError))((__k as unknown as JsonValue)); if (__r.tag === "Err") throw rehydrationViolation("Inventory", __r.error); }
}

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

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

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

  async inRegion(rg: string, deps: {}): Promise<number> {
    const __state = await this.loadState();
    return ((() => Object.values(__state.reservations).filter((__x) => ((r) => r.region === rg)(__x))))().length;
  }

}

export function __makeInventory(key: string, env?: { INVENTORY?: DurableObjectNamespace }): Inventory {
  return makeAgent(__InventoryRegistry, env?.INVENTORY, key, (state) => new Inventory(state));
}

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

export function serialise_Reservation(value: Reservation): JsonValue {
  return {
    id: value.id as JsonValue,
    orderId: value.orderId as JsonValue,
    region: value.region as JsonValue,
    qty: value.qty as JsonValue,
  };
}

export function deserialise_Reservation(json: JsonValue, path: string = "$"): Result<Reservation, BoundaryError> {
  if (typeof json !== "object" || json === null || Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "object", actual: typeof json });
  }
  const obj = json as { [k: string]: JsonValue };
  if (typeof obj["id"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.id`, expected: "string", actual: typeof obj["id"] });
  }
  const __id = obj["id"];
  if (typeof obj["orderId"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.orderId`, expected: "string", actual: typeof obj["orderId"] });
  }
  const __orderId = obj["orderId"];
  if (typeof obj["region"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.region`, expected: "string", actual: typeof obj["region"] });
  }
  const __region = obj["region"];
  if (typeof obj["qty"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.qty`, expected: "number", actual: typeof obj["qty"] });
  }
  if (!Number.isInteger(obj["qty"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.qty`, expected: "integer", actual: String(obj["qty"]) });
  }
  const __qty = obj["qty"];
  return Ok({ id: __id, orderId: __orderId, region: __region, qty: __qty } as Reservation);
}


