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

/**
 * Returns difference between two versions by the release type,
 * or `undefined` if the versions are the same.
 *
 * @example Usage
 * ```ts
 * import { parse, difference } from "@std/semver";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const s0 = parse("1.2.3");
 * const s1 = parse("1.2.4");
 * const s2 = parse("1.3.0");
 * const s3 = parse("2.0.0");
 *
 * assertEquals(difference(s0, s1), "patch");
 * assertEquals(difference(s0, s2), "minor");
 * assertEquals(difference(s0, s3), "major");
 * assertEquals(difference(s0, s0), undefined);
 * ```
 *
 * @param s0 The first SemVer to compare
 * @param s1 The second SemVer to compare
 * @returns The release type difference or `undefined` if the versions are the same
 */
export function difference(s0: SemVer, s1: SemVer): ReleaseType | undefined {
  const hasPrerelease = s0.prerelease?.length || s1.prerelease?.length;

  if (s0.major !== s1.major) return hasPrerelease ? "premajor" : "major";
  if (s0.minor !== s1.minor) return hasPrerelease ? "preminor" : "minor";
  if (s0.patch !== s1.patch) return hasPrerelease ? "prepatch" : "patch";

  if (compareIdentifier(s0.prerelease, s1.prerelease) !== 0) {
    return "prerelease";
  }
}

// denoCacheMetadata={"headers":{"content-type":"text/typescript","date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-cache-status":"HIT","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';","cf-ray":"a223e657ba557db6-LAX","set-cookie":"__cf_bm=Ys0Lr_PTbJUDx8x7d0.iCCV4UmsR_d.piKaFXjZwdqc-1785241875.1543112-1.0.1.1-YI0F6fk9HQaZaT0.S0LQdxLZnBWKntpu_SvOdtWdv85ibdLg1lwRW154d5mrYiqhuachZTQmkNnJcGlA3AfYHqcGr8pvQAbeNkzarXmSTAQ3EXz0BaNRfmY6VmO6MP98; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=1JoTPb0oq%2FQYyWsm5jfVHjIokizblg5XRCzCL92yTBtsadGp3iossv0AneoXU8C25kP5nm2rJzBOKwJ%2F9ARNT9BzfLHrw05aJCBRHaq7WE532a%2BIsremA%2B8%3D\"}]}","access-control-allow-origin":"*","x-jsr-backend":"modules","cross-origin-resource-policy":"cross-origin","cache-control":"public, max-age=31536000, immutable","vary":"Origin","access-control-expose-headers":"*","content-length":"1439","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=86400","etag":"\"9d94832d1f395372e07dbfb047746480\"","x-robots-tag":"noindex","age":"3087495","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","accept-ranges":"bytes","server":"cloudflare"},"url":"https://jsr.io/@std/semver/0.224.3/difference.ts","time":1785241875}