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

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

export interface Quote {
  readonly sku: string;
  readonly price: number;
  readonly qty: number;
}

export const Quote = {
};

export const quotes = {
  async call(sku: string, price: number, deps: {}): Promise<Result<Quote, void>> {
    return Ok({ sku: sku, price: price * 1.2, qty: 1 });
  },
};

export function serialise_Quote(value: Quote): JsonValue {
  return {
    sku: value.sku as JsonValue,
    price: ((v: number) => { if (!Number.isFinite(v)) throw new Error("non-finite Float at boundary"); return v as JsonValue; })(value.price),
    qty: value.qty as JsonValue,
  };
}

export function deserialise_Quote(json: JsonValue, path: string = "$"): Result<Quote, 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["sku"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.sku`, expected: "string", actual: typeof obj["sku"] });
  }
  const __sku = obj["sku"];
  if (typeof obj["price"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.price`, expected: "number", actual: typeof obj["price"] });
  }
  if (!Number.isFinite(obj["price"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.price`, expected: "finite number", actual: String(obj["price"]) });
  }
  const __price = obj["price"];
  if (typeof obj["qty"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.qty`, expected: "number", actual: typeof obj["qty"] });
  }
  if (!Number.isInteger(obj["qty"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.qty`, expected: "integer", actual: String(obj["qty"]) });
  }
  const __qty = obj["qty"];
  return Ok({ sku: __sku, price: __price, qty: __qty } as Quote);
}


export function serialise_Result_Quote_Unit(value: Result<Quote, void>): JsonValue {
  if (value.tag === "Ok") return { kind: "Ok", value: serialise_Quote(value.value) };
  return { kind: "Err", error: null };
}

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

