// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { Comparator, SemVer } from "./types.ts";
import { isWildcardComparator } from "./_shared.ts";
import { compare } from "./compare.ts";

function testComparator(version: SemVer, comparator: Comparator): boolean {
  if (isWildcardComparator(comparator)) {
    return true;
  }
  const cmp = compare(version, comparator);
  switch (comparator.operator) {
    case "=":
    case undefined: {
      return cmp === 0;
    }
    case "!=": {
      return cmp !== 0;
    }
    case ">": {
      return cmp > 0;
    }
    case "<": {
      return cmp < 0;
    }
    case ">=": {
      return cmp >= 0;
    }
    case "<=": {
      return cmp <= 0;
    }
  }
}

export function testComparatorSet(
  version: SemVer,
  set: Comparator[],
): boolean {
  for (const comparator of set) {
    if (!testComparator(version, comparator)) {
      return false;
    }
  }
  if (version.prerelease && version.prerelease.length > 0) {
    // Find the comparator that is allowed to have prereleases
    // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
    // That should allow `1.2.3-pr.2` to pass.
    // However, `1.2.4-alpha.notready` should NOT be allowed,
    // even though it's within the range set by the comparators.
    for (const comparator of set) {
      if (isWildcardComparator(comparator)) {
        continue;
      }
      const { major, minor, patch, prerelease } = comparator;
      if (prerelease && prerelease.length > 0) {
        if (
          version.major === major && version.minor === minor &&
          version.patch === patch
        ) {
          return true;
        }
      }
    }
    return false;
  }
  return true;
}

// denoCacheMetadata={"headers":{"x-jsr-backend":"modules","cache-control":"public, max-age=31536000, immutable","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=cNDK53uvflqh%2Fp%2BL93is1VBIkUj5OVMW4OmdL7myienG9G0E5EFqg%2BAZ0xdcf3b3WzMf24%2FHJUNJxV9t%2BjvRApBMZ2m0aPS6MUIVbZcW%2FlDeC80NKmd7mrg%3D\"}]}","set-cookie":"__cf_bm=k1XrYatbbzhPQu2p1.SloPwqSZKkKmB_wZ7WykwADvk-1785241875.2690165-1.0.1.1-vJnwanmqt3SlwKO3putDG6xz8oyG2bOrMN3BdYbJc1yOBCe21juPFs01anUrjjOBETi2QfEexXXJT0LgsaF1HIXlCcYKGzvb5gJOtCf9EZ16W6ojpiqjiqPa5KgyKoUY; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","vary":"Origin","alt-svc":"h3=\":443\"; ma=86400","accept-ranges":"bytes","content-length":"1730","x-robots-tag":"noindex","access-control-allow-origin":"*","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":"a223e6586c4d7db6-LAX","etag":"\"a5a7de9348b11a340e175f5459f7bad6\"","cf-cache-status":"HIT","age":"608576","cross-origin-resource-policy":"cross-origin","access-control-expose-headers":"*","x-content-type-options":"nosniff","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-type":"text/typescript","server":"cloudflare"},"url":"https://jsr.io/@std/semver/0.224.3/_test_comparator_set.ts","time":1785241875}