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

/**
 * Attempt to parse a string as a semantic version, returning either a `SemVer`
 * object or throws a TypeError.
 *
 * @example Usage
 * ```ts
 * import { parse } from "@std/semver/parse";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const version = parse("1.2.3");
 * assertEquals(version, {
 *   major: 1,
 *   minor: 2,
 *   patch: 3,
 *   prerelease: [],
 *   build: [],
 * });
 * ```
 *
 * @param version The version string to parse
 * @returns A valid SemVer
 */
export function parse(version: string): SemVer {
  if (typeof version !== "string") {
    throw new TypeError(
      `version must be a string`,
    );
  }

  if (version.length > MAX_LENGTH) {
    throw new TypeError(
      `version is longer than ${MAX_LENGTH} characters`,
    );
  }

  version = version.trim();

  const groups = version.match(FULL_REGEXP)?.groups;
  if (!groups) throw new TypeError(`Invalid Version: ${version}`);

  const major = parseNumber(groups.major!, "Invalid major version");
  const minor = parseNumber(groups.minor!, "Invalid minor version");
  const patch = parseNumber(groups.patch!, "Invalid patch version");

  const prerelease = groups.prerelease
    ? parsePrerelease(groups.prerelease)
    : [];
  const build = groups.buildmetadata ? parseBuild(groups.buildmetadata) : [];

  return {
    major,
    minor,
    patch,
    prerelease,
    build,
  };
}

// denoCacheMetadata={"headers":{"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';","x-content-type-options":"nosniff","set-cookie":"__cf_bm=A3lKAnw8HFaOFaOPGckqDPMQLjRYCN7B7ToIeg.FzPM-1785241875.1650238-1.0.1.1-vR.bUHN_MUnoi_Ioyx.gjO.fYP5f7OEFGvECfzW_RKEO6SSExerorFliPRaRP.0ruV.LxTVrMyxhfVtw_OdPlthwLmVtxjAVBQZABO5hmI.DAJcxbXLa0u3CMGssZogJ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","alt-svc":"h3=\":443\"; ma=86400","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=VQ9%2FwxIBAgk7Nq6tz4yaT2qchKgW4YsZnly8tNilh%2FASup7P3YPY6q%2Ba6kFy0stc5rByeNnKfktaJzXocMAPqGI36j3VjRUyTWUJTuEsRDMV8%2BaodHZqEf4%3D\"}]}","x-jsr-backend":"modules","access-control-allow-origin":"*","x-robots-tag":"noindex","content-length":"1673","cf-ray":"a223e657ca757db6-LAX","cf-cache-status":"HIT","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","age":"2653708","content-type":"text/typescript","accept-ranges":"bytes","etag":"\"377b68fc650c5d1423ea1857bf867363\"","cross-origin-resource-policy":"cross-origin","server":"cloudflare","date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin"},"url":"https://jsr.io/@std/semver/0.224.3/parse.ts","time":1785241875}