// Generated by bynkc — do not edit by hand.
// context ops.jobs

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

import * as platform_time from "../platform-time/handlers.js";

export type TickError =
    { readonly tag: "Unavailable" };

export const TickError = {
  Unavailable: { tag: "Unavailable" } as TickError,
};

export const tick = {
  async call(deps: { Clock: platform_time.Clock }): Promise<Result<number, TickError>> {
    const t = await deps.Clock.now();
    return Ok(t);
  },
};

export function serialise_TickError(value: TickError): JsonValue {
  switch (value.tag) {
    case "Unavailable":
      return { kind: "Unavailable" };
  }
}

export function deserialise_TickError(json: JsonValue, path: string = "$"): Result<TickError, 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 "Unavailable":
      return Ok({ tag: "Unavailable" } as TickError);
    default:
      return Err({ kind: "StructuralMismatch", path, expected: "sum variant kind", actual: String(kind) });
  }
}


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

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

