// Generated by bynkc — do not edit by hand.
// context shop.orders

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 from "../../bynk.js";

export interface Order {
  readonly sku: string;
  readonly placedAt: number;
}

export const Order = {
};

export const ordering = {
  async call(sku: string, deps: { Clock: bynk.Clock; Logger: bynk.Logger }): Promise<Order> {
    const __r0 = await deps.Logger.info("placing order");
    const now = await deps.Clock.now();
    return { sku: sku, placedAt: now };
  },
};

export function serialise_Order(value: Order): JsonValue {
  return {
    sku: value.sku as JsonValue,
    placedAt: value.placedAt as JsonValue,
  };
}

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


