// Generated by bynkc — do not edit by hand.
// commons shortener.core

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

/**
 * Core vocabulary for the URL shortener.
 *
 * These types are shared across the analytics and links contexts. Because
 * commons declarations mix into each using context, each context gets its own
 * nominal copy — but they remain structurally compatible, so values can cross
 * context boundaries via the structural-projection mechanism.
 *
 * Refined String types (rather than opaque) so that their underlying string
 * representation serialises directly across HTTP and Service Binding boundaries.
 */

/**
 * A short code identifying a shortened link: six to eight alphanumeric
 * characters. The refinement is validated whenever a ShortCode is constructed
 * from untrusted input (an HTTP path segment, a request body, a deserialised
 * cross-context payload).
 */
export type ShortCode = string & { readonly __brand: "ShortCode" };

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

/**
 * A target URL. Constrained to look like an http(s) URL. Validated on
 * construction from untrusted input.
 */
export type Url = string & { readonly __brand: "Url" };

export const Url = {
  of(value: string): Result<Url, ValidationError> {
    if (!new RegExp("^" + "https?://.+" + "$").test(value)) {
      return Err({ field: "Url", message: "must match /https?://.+/", value });
    }
    return Ok(value as Url);
  },
  unsafe(value: string): Url {
    return value as Url;
  },
};

