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

import { assertPath } from "../_common/assert_path.ts";
import { normalize } from "./normalize.ts";

/**
 * Join all given a sequence of `paths`,then normalizes the resulting path.
 * @param paths to be joined and normalized
 */
export function join(...paths: string[]): string {
  if (paths.length === 0) return ".";

  let joined: string | undefined;
  for (let i = 0; i < paths.length; ++i) {
    const path = paths[i]!;
    assertPath(path);
    if (path.length > 0) {
      if (!joined) joined = path;
      else joined += `/${path}`;
    }
  }
  if (!joined) return ".";
  return normalize(joined);
}

// denoCacheMetadata={"headers":{"report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Z8OzX33KOAhA64jSmDRt2aZVGB5%2Fz7y523b1%2BUrbBDldETxNHyEoQFLxPA2mXD6tSN4daLTUlhI13%2B2uNsx5eyyRk8hd%2BVmRZev2vF6ghhWXFiHYycI8Yew%3D\"}]}","server":"cloudflare","cf-cache-status":"HIT","x-jsr-backend":"modules","set-cookie":"__cf_bm=GJ7WwYCc5y4HheWvnvrg16hd0zMD3VQzGz1.Se.4WQ4-1785241875.2564876-1.0.1.1-I7czAgFYEpC2vqs66KNhkZMUTdRvuUcOyRkVwZrofv3mvcLIfdRlAwP4_0h1kedxOF6WXZWXPM9lGDmZbooVJ0Ov0Z.1uWOTYQAkiKK4V.i1XBoqdIAz70AAJaYEPnXZ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-allow-origin":"*","content-length":"721","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';","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-type":"text/typescript","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","cross-origin-resource-policy":"cross-origin","x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400","vary":"Origin","cf-ray":"a223e6585c0f7db6-LAX","etag":"\"ff34cab15102e215f38b15c468693eab\"","access-control-expose-headers":"*","x-content-type-options":"nosniff","age":"3916336"},"url":"https://jsr.io/@std/path/0.224.0/posix/join.ts","time":1785241875}