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

import { Ok, Err, Some, None, type Result, type Option, type ValidationError, type JsonValue, type BoundaryError, type ServiceBinding, callService, boundaryError } from "../../runtime.js";

export interface Order {
  readonly id: string;
  readonly total: number;
  readonly tags: ReadonlyMap<string, number>;
}

export const Order = {
};

export const orders = {
  async call(ids: readonly string[], deps: {}): Promise<Result<readonly Order[], void>> {
    const none: ReadonlyMap<string, number> = new Map<string, number>();
    return Ok([{ id: "a", total: 1, tags: none }, { id: "b", total: 2, tags: none }]);
  },
};

export function serialise_Order(value: Order): JsonValue {
  return {
    id: value.id as JsonValue,
    total: value.total as JsonValue,
    tags: serialise_Map_String_Int(value.tags),
  };
}

export function deserialise_Order(json: JsonValue, path: string = "$"): Result<Order, 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["total"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.total`, expected: "number", actual: typeof obj["total"] });
  }
  if (!Number.isInteger(obj["total"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.total`, expected: "integer", actual: String(obj["total"]) });
  }
  const __total = obj["total"];
  const __r_tags = deserialise_Map_String_Int(obj["tags"], `${path}.tags`);
  if (__r_tags.tag === "Err") return __r_tags;
  const __tags = __r_tags.value;
  return Ok({ id: __id, total: __total, tags: __tags } as Order);
}


export function serialise_List_String(value: readonly string[]): JsonValue {
  return value.map((v) => v as JsonValue);
}

export function deserialise_List_String(json: JsonValue, path: string = "$"): Result<readonly string[], BoundaryError> {
  if (!Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "array", actual: typeof json });
  }
  const out: string[] = [];
  for (let i = 0; i < json.length; i++) {
  const item = json[i];
  if (typeof item !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}[${i}]`, expected: "string", actual: typeof item });
  }
  const __el = item;
  out.push(__el);
  }
  return Ok(out);
}

export function serialise_Result_List_Order_Unit(value: Result<readonly Order[], void>): JsonValue {
  if (value.tag === "Ok") return { kind: "Ok", value: serialise_List_Order(value.value) };
  return { kind: "Err", error: null };
}

export function deserialise_Result_List_Order_Unit(json: JsonValue, path: string = "$"): Result<Result<readonly Order[], void>, 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 (obj["kind"] === "Ok") {
  const __r_v = deserialise_List_Order(obj["value"], `${path}.value`);
  if (__r_v.tag === "Err") return __r_v;
  const __v = __r_v.value;
    return Ok(Ok(__v) as Result<readonly Order[], void>);
  } else if (obj["kind"] === "Err") {
  const __e = undefined;
    return Ok(Err(__e) as Result<readonly Order[], void>);
  }
  return Err({ kind: "StructuralMismatch", path, expected: "Ok | Err", actual: String(obj["kind"]) });
}

export function serialise_List_Order(value: readonly Order[]): JsonValue {
  return value.map((v) => serialise_Order(v));
}

export function deserialise_List_Order(json: JsonValue, path: string = "$"): Result<readonly Order[], BoundaryError> {
  if (!Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "array", actual: typeof json });
  }
  const out: Order[] = [];
  for (let i = 0; i < json.length; i++) {
  const item = json[i];
  const __r_el = deserialise_Order(item, `${path}[${i}]`);
  if (__r_el.tag === "Err") return __r_el;
  const __el = __r_el.value;
  out.push(__el);
  }
  return Ok(out);
}

export function serialise_Map_String_Int(value: ReadonlyMap<string, number>): JsonValue {
  const entries: JsonValue[] = [];
  for (const [k, v] of value) {
    entries.push([k as JsonValue, v as JsonValue]);
  }
  return entries;
}

export function deserialise_Map_String_Int(json: JsonValue, path: string = "$"): Result<ReadonlyMap<string, number>, BoundaryError> {
  if (!Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "array", actual: typeof json });
  }
  const out = new Map<string, number>();
  for (let i = 0; i < json.length; i++) {
  const entry = json[i];
  if (!Array.isArray(entry) || entry.length !== 2) {
    return Err({ kind: "StructuralMismatch", path: `${path}[${i}]`, expected: "[key, value] entry", actual: typeof entry });
  }
  const entryK = entry[0];
  const entryV = entry[1];
  if (typeof entryK !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}[${i}][0]`, expected: "string", actual: typeof entryK });
  }
  const __k = entryK;
  if (typeof entryV !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}[${i}][1]`, expected: "number", actual: typeof entryV });
  }
  if (!Number.isInteger(entryV)) {
    return Err({ kind: "StructuralMismatch", path: `${path}[${i}][1]`, expected: "integer", actual: String(entryV) });
  }
  const __v = entryV;
  out.set(__k, __v);
  }
  return Ok(out);
}

