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

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

export interface CreateItem {
  readonly name: string;
  readonly quantity: number;
}

export const CreateItem = {
};

export interface ItemView {
  readonly id: string;
  readonly name: string;
}

export const ItemView = {
};

export const api = {
  async http_POST_items(body: CreateItem, deps: {}): Promise<HttpResult<ItemView>> {
    return HttpResult.Created({ id: "abc", name: body.name });
  },
};

export function serialise_CreateItem(value: CreateItem): JsonValue {
  return {
    name: value.name as JsonValue,
    quantity: value.quantity as JsonValue,
  };
}

export function deserialise_CreateItem(json: JsonValue, path: string = "$"): Result<CreateItem, 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["name"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.name`, expected: "string", actual: typeof obj["name"] });
  }
  const __name = obj["name"];
  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({ name: __name, quantity: __quantity } as CreateItem);
}

export function serialise_ItemView(value: ItemView): JsonValue {
  return {
    id: value.id as JsonValue,
    name: value.name as JsonValue,
  };
}

export function deserialise_ItemView(json: JsonValue, path: string = "$"): Result<ItemView, 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["id"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.id`, expected: "string", actual: typeof obj["id"] });
  }
  const __id = obj["id"];
  if (typeof obj["name"] !== "string") {
    return Err({ kind: "StructuralMismatch", path: `${path}.name`, expected: "string", actual: typeof obj["name"] });
  }
  const __name = obj["name"];
  return Ok({ id: __id, name: __name } as ItemView);
}


