// Generated by bynkc — do not edit by hand.
// context cache.store

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

import * as bynk_cloudflare from "../../bynk/cloudflare.js";

export const cache = {
  async call(key: string, value: string, deps: { Kv: bynk_cloudflare.Kv }): Promise<Option<string>> {
    const previous = await deps.Kv.get(key);
    const __r0 = await deps.Kv.put(key, value);
    const __r1 = await deps.Kv.delete("stale");
    return previous;
  },
};

export function serialise_Option_String(value: Option<string>): JsonValue {
  if (value.tag === "Some") return { kind: "Some", value: value.value as JsonValue };
  return { kind: "None" };
}

export function deserialise_Option_String(json: JsonValue, path: string = "$"): Result<Option<string>, 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"] === "Some") {
  if (typeof obj["value"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.value`, expected: "string", actual: typeof obj["value"] });
  }
  const __v = obj["value"];
    return Ok(Some(__v) as Option<string>);
  } else if (obj["kind"] === "None") {
    return Ok(None as Option<string>);
  }
  return Err({ kind: "StructuralMismatch", path, expected: "Some | None", actual: String(obj["kind"]) });
}

