// Generated by bynkc — do not edit by hand.
// commons chrono.time

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

import { Duration } from "./duration.js";

/**
 * A point in time, opaque integer milliseconds since Unix epoch.
 */
export type Timestamp = number & { readonly __brand: "Timestamp" };

export const Timestamp = {
  of(value: number): Result<Timestamp, ValidationError> {
    if (!Number.isInteger(value)) {
      return Err({ field: "Timestamp", message: "must be an integer", value });
    }
    if (!(value >= 0)) {
      return Err({ field: "Timestamp", message: "must be non-negative", value });
    }
    return Ok(value as Timestamp);
  },
  unsafe(value: number): Timestamp {
    return value as Timestamp;
  },
  equals(self: Timestamp, other: Timestamp): boolean {
    return self === other;
  },
  before(self: Timestamp, other: Timestamp): boolean {
    return (self as number) < (other as number);
  },
  after(self: Timestamp, other: Timestamp): boolean {
    return (self as number) > (other as number);
  },
  /**
   * Compute the duration between two timestamps. Returns positive when
   * the second timestamp is after the first.
   */
  diff(self: Timestamp, later: Timestamp): Duration {
    return Duration.unsafe((later as number) - (self as number));
  },
  /**
   * Add a duration to a timestamp, producing a new timestamp.
   * Returns None if the result would be negative (before epoch).
   */
  add(self: Timestamp, delta: Duration): Option<Timestamp> {
    const result = (self as number) + (delta as number);
    return (result < 0 ? None : Some(Timestamp.unsafe(result)));
  },
};

