// 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 Note {
  readonly id: string;
  readonly owner: string;
}

export const Note = {
};

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

export function serialise_Note(value: Note): JsonValue {
  return {
    id: value.id as JsonValue,
    owner: value.owner as JsonValue,
  };
}

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


