// Generated by bynkc — do not edit by hand.
// commons demo.admit

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "../runtime.js";

export type Quantity = number & { readonly __brand: "Quantity" };

export const Quantity = {
  of(value: number): Result<Quantity, ValidationError> {
    if (!Number.isInteger(value)) {
      return Err({ field: "Quantity", message: "must be an integer", value });
    }
    if (!(value >= 1 && value <= 100)) {
      return Err({ field: "Quantity", message: "must be in range [1, 100]", value });
    }
    return Ok(value as Quantity);
  },
  unsafe(value: number): Quantity {
    return value as Quantity;
  },
};

export type Code = string & { readonly __brand: "Code" };

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

export function defaultQty(): Quantity {
  return Quantity.unsafe(5);
}

export function sampleCode(): Code {
  const c: Code = Code.unsafe("abc123");
  return c;
}

export function checked(): Result<Quantity, ValidationError> {
  return Ok(Quantity.unsafe(50));
}

export function clamp(q: Quantity): Quantity {
  return q;
}

export function useClamp(): Quantity {
  return clamp(Quantity.unsafe(10));
}

