// Generated by bynkc — do not edit by hand.
// commons inventory

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

export function sum(xs: readonly number[]): number {
  return ((__xs: readonly number[], __acc: number, __f: (acc: number, x: number) => number) => { for (const __x of __xs) __acc = __f(__acc, __x); return __acc; })(xs, 0, (acc, x) => acc + x);
}

export function firstOrZero(xs: readonly number[]): number {
  const __r0 = ((__xs: readonly number[], __i: number) => __i >= 0 && __i < __xs.length ? Some(__xs[__i] as number) : None)(xs, 0);
  switch (__r0.tag) {
    case "Some": {
      const x = __r0.value;
      return x;
    }
    case "None": {
      return 0;
    }
  }
  throw new Error("non-exhaustive match");
}

export function demo(): number {
  const xs = [1, 2, 3];
  const ys = [0, ...xs];
  const m: ReadonlyMap<string, number> = new Map(new Map<string, number>()).set("a", 1);
  const __r0 = ((__m: ReadonlyMap<string, number>, __k: string) => __m.has(__k) ? Some(__m.get(__k) as number) : None)(m, "a");
  switch (__r0.tag) {
    case "Some": {
      const v = __r0.value;
      return sum(ys) + v + (m).size + (xs).length;
    }
    case "None": {
      return firstOrZero(ys);
    }
  }
  throw new Error("non-exhaustive match");
}

export function keyList(m: ReadonlyMap<string, number>): readonly string[] {
  return [...(m).keys()];
}

