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

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

export type Sku = string & { readonly __brand: "inventory.Sku" };

export const Sku = {
  of(value: string): Result<Sku, ValidationError> {
    if (!new RegExp("^" + "[A-Z]{3}-[0-9]{4}" + "$").test(value)) {
      return Err({ field: "Sku", message: "must match /[A-Z]{3}-[0-9]{4}/", value });
    }
    return Ok(value as Sku);
  },
  unsafe(value: string): Sku {
    return value as Sku;
  },
};

export interface StockItem {
  readonly sku: Sku;
  readonly quantity: number;
}

export const StockItem = {
};

export type StockError =
    { readonly tag: "NotFound" }
  | { readonly tag: "OutOfStock" };

export const StockError = {
  NotFound: { tag: "NotFound" } as StockError,
  OutOfStock: { tag: "OutOfStock" } as StockError,
};

export const check = {
  async call(sku: Sku, deps: {}): Promise<Result<StockItem, StockError>> {
    return Err(StockError.NotFound);
  },
};

export function serialise_Sku(value: Sku): JsonValue {
  return value as unknown as string;
}

export function deserialise_Sku(json: JsonValue, path: string = "$"): Result<Sku, BoundaryError> {
  if (typeof json !== "string") {
    return Err({ kind: "StructuralMismatch", path, expected: "string", actual: typeof json });
  }
  const validated = (typeof (Sku as any).of === "function")
    ? (Sku as any).of(json)
    : Ok(json as unknown as Sku);
  if (validated.tag === "Err") {
    return Err({ kind: "RefinementViolation", path, violation: validated.error });
  }
  return Ok(validated.value as Sku);
}

export function serialise_StockError(value: StockError): JsonValue {
  switch (value.tag) {
    case "NotFound":
      return { kind: "NotFound" };
    case "OutOfStock":
      return { kind: "OutOfStock" };
  }
}

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

export function serialise_StockItem(value: StockItem): JsonValue {
  return {
    sku: serialise_Sku(value.sku),
    quantity: value.quantity as JsonValue,
  };
}

export function deserialise_StockItem(json: JsonValue, path: string = "$"): Result<StockItem, 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 __r_sku = deserialise_Sku(obj["sku"], `${path}.sku`);
  if (__r_sku.tag === "Err") return __r_sku;
  const __sku = __r_sku.value;
  if (typeof obj["quantity"] !== "number") {
    return Err({ kind: "StructuralMismatch", path: `${path}.quantity`, expected: "number", actual: typeof obj["quantity"] });
  }
  if (!Number.isInteger(obj["quantity"])) {
    return Err({ kind: "StructuralMismatch", path: `${path}.quantity`, expected: "integer", actual: String(obj["quantity"]) });
  }
  const __quantity = obj["quantity"];
  return Ok({ sku: __sku, quantity: __quantity } as StockItem);
}


export function serialise_Result_StockItem_StockError(value: Result<StockItem, StockError>): JsonValue {
  if (value.tag === "Ok") return { kind: "Ok", value: serialise_StockItem(value.value) };
  return { kind: "Err", error: serialise_StockError(value.error) };
}

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

