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