// 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 { join } from "./join.ts";
import { SEPARATOR } from "./constants.ts";
import { normalizeGlob } from "./normalize_glob.ts";

export type { GlobOptions };

/** Like join(), but doesn't collapse "**\/.." when `globstar` is true. */
export function joinGlobs(
  globs: string[],
  { extended = true, globstar = false }: GlobOptions = {},
): string {
  if (!globstar || globs.length === 0) {
    return join(...globs);
  }
  if (globs.length === 0) return ".";
  let joined: string | undefined;
  for (const glob of globs) {
    const path = glob;
    if (path.length > 0) {
      if (!joined) joined = path;
      else joined += `${SEPARATOR}${path}`;
    }
  }
  if (!joined) return ".";
  return normalizeGlob(joined, { extended, globstar });
}

// denoCacheMetadata={"headers":{"server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e6583bd27db6-LAX","accept-ranges":"bytes","etag":"\"46de0370ee8bc1af2ab9da466c65e785\"","content-type":"text/typescript","vary":"Origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","set-cookie":"__cf_bm=AjXGYY6j245wyyzqQsTDjLLLBvV6wWcnWfNHA0P_9a8-1785241875.2438943-1.0.1.1-KGLCGMNF3z3VQKJydy9Mqfb.eZaiiFhg.WyFRq4JxgTs4STO_bMqp6JajVN6C5zynqDsQEH4QxPorEBQizvtyxuUSYFqIkJb_grrNPY_QHYw9TOVsHaueLQwB__uc1U0; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","access-control-allow-origin":"*","content-length":"931","x-robots-tag":"noindex","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","age":"44352","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=NSudkkMQAzN9n%2Bf9iKptKHeuuasgb9nQS83PW763C8IMIN0otLuQY3u%2BUFysCTKU7IVXIhrNYbk%2BQCnGu7q7kjxOxPjIAMW%2FpZgu0OLlT2op7r5Cb39%2Fj4E%3D\"}]}","cache-control":"public, max-age=31536000, immutable","cf-cache-status":"HIT","x-content-type-options":"nosniff","access-control-expose-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';","cross-origin-resource-policy":"cross-origin","x-jsr-backend":"modules"},"url":"https://jsr.io/@std/path/0.224.0/windows/join_globs.ts","time":1785241875}