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

import type { GlobOptions } from "../_common/glob_to_reg_exp.ts";
import { normalize } from "./normalize.ts";
import { SEPARATOR_PATTERN } from "./constants.ts";

export type { GlobOptions };

/** Like normalize(), but doesn't collapse "**\/.." when `globstar` is true. */
export function normalizeGlob(
  glob: string,
  { globstar = false }: GlobOptions = {},
): string {
  if (glob.match(/\0/g)) {
    throw new Error(`Glob contains invalid characters: "${glob}"`);
  }
  if (!globstar) {
    return normalize(glob);
  }
  const s = SEPARATOR_PATTERN.source;
  const badParentPattern = new RegExp(
    `(?<=(${s}|^)\\*\\*${s})\\.\\.(?=${s}|$)`,
    "g",
  );
  return normalize(glob.replace(badParentPattern, "\0")).replace(/\0/g, "..");
}

// denoCacheMetadata={"headers":{"content-length":"857","cf-cache-status":"HIT","age":"44352","access-control-allow-origin":"*","etag":"\"868a526f9177f76b07667799c494758d\"","cache-control":"public, max-age=31536000, immutable","date":"Tue, 28 Jul 2026 12:31:15 GMT","accept-ranges":"bytes","server":"cloudflare","vary":"Origin","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=RPTQfPRnWAejYCf2nMVIvUr4YwK9xrn7Uid7vG27CcXE%2FO2HsSPoqeLPhSZ80rVXjsuZgQyElQpZRY5NsuRU6iL%2BJ0fks1%2FwR3pPicnHQP%2BlGkgQpdEf5eo%3D\"}]}","cross-origin-resource-policy":"cross-origin","cf-ray":"a223e6586c3a7db6-LAX","access-control-expose-headers":"*","x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400","content-type":"text/typescript","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","set-cookie":"__cf_bm=Fg6VaFEz1EriKPCKlWFp6xQWThMOPpQXr4IWwWNL4qc-1785241875.2660549-1.0.1.1-0ow0sgdLORLQoyUCwNc_rUlqRIqTDO029Lr04M3ZtUocn8qkyf3h1LLRC_C0M5PH56nLvAIvHoAiW_nbgGCB0Z7famNWgWgzVIU8yO1yDfdiSsUANhg7PIrfYlS1..RI; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","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-jsr-backend":"modules"},"url":"https://jsr.io/@std/path/0.224.0/posix/normalize_glob.ts","time":1785241875}