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

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

import * as demo_counter from "../demo-counter/handlers.js";

export type ApiError =
    { readonly tag: "Failed" };

export const ApiError = {
  Failed: { tag: "Failed" } as ApiError,
};

export const tick = {
  async call(id: string, deps: { env: { DEMO_COUNTER: ServiceBinding } }): Promise<Result<number, ApiError>> {
    const r = await callService(deps.env.DEMO_COUNTER, "bump", id as JsonValue, demo_counter.deserialise_Result_Int_CounterError, "demo.api");
    switch (r.tag) {
      case "Ok": {
        const n = r.value;
        return Ok(n);
      }
      case "Err": {
        return Err(ApiError.Failed);
      }
    }
    throw new Error("non-exhaustive match");
  },
};

export function serialise_ApiError(value: ApiError): JsonValue {
  switch (value.tag) {
    case "Failed":
      return { kind: "Failed" };
  }
}

export function deserialise_ApiError(json: JsonValue, path: string = "$"): Result<ApiError, 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 };
  const kind = obj["kind"];
  switch (kind) {
    case "Failed":
      return Ok({ tag: "Failed" } as ApiError);
    default:
      return Err({ kind: "StructuralMismatch", path, expected: "sum variant kind", actual: String(kind) });
  }
}


export function serialise_Result_Int_ApiError(value: Result<number, ApiError>): JsonValue {
  if (value.tag === "Ok") return { kind: "Ok", value: value.value as JsonValue };
  return { kind: "Err", error: serialise_ApiError(value.error) };
}

export function deserialise_Result_Int_ApiError(json: JsonValue, path: string = "$"): Result<Result<number, ApiError>, 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") {
  if (typeof obj["value"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.value`, expected: "number", actual: typeof obj["value"] });
  }
  if (!Number.isInteger(obj["value"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.value`, expected: "integer", actual: String(obj["value"]) });
  }
  const __v = obj["value"];
    return Ok(Ok(__v) as Result<number, ApiError>);
  } else if (obj["kind"] === "Err") {
  const __r_e = deserialise_ApiError(obj["error"], `${path}.error`);
  if (__r_e.tag === "Err") return __r_e;
  const __e = __r_e.value;
    return Ok(Err(__e) as Result<number, ApiError>);
  }
  return Err({ kind: "StructuralMismatch", path, expected: "Ok | Err", actual: String(obj["kind"]) });
}

