// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { SemVer } from "./types.ts";
import { isWildcardComparator } from "./_shared.ts";

function formatNumber(value: number) {
  if (value === Number.POSITIVE_INFINITY) {
    return "∞";
  } else if (value === Number.NEGATIVE_INFINITY) {
    return "⧞";
  } else {
    return value.toFixed(0);
  }
}

/**
 * Format a SemVer object into a string.
 *
 * If any number is NaN then NaN will be printed.
 *
 * If any number is positive or negative infinity then '∞' or '⧞' will be printed instead.
 *
 * @example Usage
 * ```ts
 * import { format } from "@std/semver/format";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const semver = {
 *   major: 1,
 *   minor: 2,
 *   patch: 3,
 * };
 * assertEquals(format(semver), "1.2.3");
 * ```
 *
 * @param semver The SemVer to format
 * @returns The string representation of a semantic version.
 */
export function format(semver: SemVer): string {
  if (isWildcardComparator(semver)) {
    return "*";
  }

  const major = formatNumber(semver.major);
  const minor = formatNumber(semver.minor);
  const patch = formatNumber(semver.patch);
  const pre = semver.prerelease?.join(".") ?? "";
  const build = semver.build?.join(".") ?? "";

  const primary = `${major}.${minor}.${patch}`;
  const release = [primary, pre].filter((v) => v).join("-");
  return [release, build].filter((v) => v).join("+");
}

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-cache-status":"HIT","content-type":"text/typescript","cf-ray":"a223e657ca697db6-LAX","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","alt-svc":"h3=\":443\"; ma=86400","set-cookie":"__cf_bm=NvanO0azhtQ7gwooJnD0gkFKX_gzdGGwO_NTgaxjmJA-1785241875.1618783-1.0.1.1-144nzFdCFT8BSW3W4aw6DYEfLMSJqVhESxjGkMbBFxQwzoEXcQwG2eE2X5.bvvq0XFhliGIbL_e5tPXzNA.nrwPdlMYbYuIRHyQsNu6cr9HsD4QTCueBJtJW_q3KjxMU; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","accept-ranges":"bytes","etag":"\"6a046e07026674d43c8ee0eb2e204d9f\"","x-jsr-backend":"modules","content-length":"1493","cache-control":"public, max-age=31536000, immutable","vary":"Origin","access-control-allow-origin":"*","server":"cloudflare","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","x-robots-tag":"noindex","age":"3116065","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=%2FLzV0ImZ6zJa3OpDlezNUmBdwMZrDrKgE7V6rL%2F9wnMqTIUcKL8xk8CM7yJceO%2Bp0UciQylx6i0dQ72G4%2BUt5UWLa4geq%2F1rQRcRna3j9NQ3sEcOssqSJhk%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"},"url":"https://jsr.io/@std/semver/0.224.3/format.ts","time":1785241875}