// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { NIL_UUID } from "./constants.ts";

/**
 * Determines whether the UUID is the
 * {@link https://www.rfc-editor.org/rfc/rfc4122#section-4.1.7 | nil UUID}.
 *
 * @param id UUID value.
 *
 * @returns `true` if the UUID is the nil UUID, otherwise `false`.
 *
 * @example Usage
 * ```ts
 * import { isNil } from "@std/uuid";
 * import { assert, assertFalse } from "@std/assert";
 *
 * assert(isNil("00000000-0000-0000-0000-000000000000"));
 * assertFalse(isNil(crypto.randomUUID()));
 * ```
 */
export function isNil(id: string): boolean {
  return id === NIL_UUID;
}

/**
 * Determines whether a string is a valid UUID.
 *
 * @param uuid UUID value.
 *
 * @returns `true` if the string is a valid UUID, otherwise `false`.
 *
 * @example Usage
 * ```ts
 * import { validate } from "@std/uuid";
 * import { assert, assertFalse } from "@std/assert";
 *
 * assert(validate("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b"));
 * assertFalse(validate("not a UUID"));
 * ```
 */
export function validate(uuid: string): boolean {
  return /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
    .test(
      uuid,
    );
}

/**
 * Detect RFC version of a UUID.
 *
 * @param uuid UUID value.
 *
 * @returns The RFC version of the UUID.
 *
 * @example Usage
 * ```ts
 * import { version } from "@std/uuid";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * assertEquals(version("d9428888-122b-11e1-b85c-61cd3cbb3210"), 1);
 * assertEquals(version("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b"), 4);
 * ```
 */
export function version(uuid: string): number {
  if (!validate(uuid)) {
    throw new TypeError("Invalid UUID");
  }

  return parseInt(uuid[14]!, 16);
}

// denoCacheMetadata={"headers":{"vary":"Origin","x-jsr-backend":"modules","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';","set-cookie":"__cf_bm=WsZ3YyD5XdYI25c.q7DVMCYO4TCcZb.Mf3bROSHoYVY-1785241875.1937885-1.0.1.1-dJbVjPF6BRnHdz0QAIl4KSfDP8f3k8sGerhfwej2qOJkXvtBSVBb6UI6yu0CtiLcDr_HGtm6XYKvPZ3MHJQrH463M0B.B5cyWl75AIo9_i_erYE_EnHgVIddMus4ZwyE; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","access-control-allow-origin":"*","content-length":"1839","cache-control":"public, max-age=31536000, immutable","x-robots-tag":"noindex","date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-expose-headers":"*","etag":"\"bfe5b30b737353eda3eaae38a0bd4e1b\"","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=14rznF03dy5G1MDwpOBiTq5mwlvq6CPBtmJv8kjnCLQACJ%2BdzZ3vECG8p5iuOsRm%2F5xzquqiNByxvD%2Fq%2Bm%2FH3p95o5XGhb5te4SFcD3H7l3JvS6TTz8pzYE%3D\"}]}","cross-origin-resource-policy":"cross-origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","alt-svc":"h3=\":443\"; ma=86400","content-type":"text/typescript","cf-ray":"a223e657eb007db6-LAX","server":"cloudflare","x-content-type-options":"nosniff"},"url":"https://jsr.io/@std/uuid/0.224.3/common.ts","time":1785241875}