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

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

export type UserId = string & { readonly __brand: "api.UserId" };

export const UserId = {
  of(value: string): Result<UserId, ValidationError> {
    if (!(value.length > 0)) {
      return Err({ field: "UserId", message: "must be non-empty", value });
    }
    return Ok(value as UserId);
  },
  unsafe(value: string): UserId {
    return value as UserId;
  },
};

export interface Event {
  readonly id: string;
}

export const Event = {
};

export const api = {
  async http_POST_ingest(body: Event, deps: { who: { tag: "User", identity: UserId } | { tag: "Hook" } }): Promise<HttpResult<string>> {
    switch (deps.who.tag) {
      case "User": {
        const u = deps.who.identity;
        return HttpResult.Ok(u);
      }
      case "Hook": {
        return HttpResult.Ok(body.id);
      }
    }
    throw new Error("non-exhaustive match");
  },
};

export function serialise_Event(value: Event): JsonValue {
  return {
    id: value.id as JsonValue,
  };
}

export function deserialise_Event(json: JsonValue, path: string = "$"): Result<Event, 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"];
  return Ok({ id: __id } as Event);
}


