// 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.
 *
 * @example Usage
 * ```ts
 * import { join } from "@std/path/posix/join";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const path = join("/foo", "bar", "baz/asdf", "quux", "..");
 * assertEquals(path, "/foo/bar/baz/asdf");
 * ```
 *
 * @param paths The paths to join.
 * @returns The joined path.
 */
export function join(...paths: string[]): string {
  if (paths.length === 0) return ".";
  paths.forEach((path) => assertPath(path));
  const joined = paths.filter((path) => path.length > 0).join("/");
  return joined === "" ? "." : normalize(joined);
}

// denoCacheMetadata={"headers":{"age":"126790","access-control-allow-origin":"*","x-jsr-backend":"modules","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","access-control-expose-headers":"*","content-type":"text/typescript","cf-ray":"a223e658bd3b7db6-LAX","date":"Tue, 28 Jul 2026 12:31: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';","accept-ranges":"bytes","x-content-type-options":"nosniff","vary":"Origin","cache-control":"public, max-age=31536000, immutable","x-robots-tag":"noindex","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","cross-origin-resource-policy":"cross-origin","content-length":"876","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=dLaPD3m28nYU%2BWbp7WR0sEGdrVrmhHNu9SbOyHiJwVz91FHmA8u6wwAGzkz8Ykkpog81uTFGx1sq7oYWCW8xH6mP8Y2WwH%2BrSo2Ar17mQTzfqm1lUR6GPqk%3D\"}]}","cf-cache-status":"HIT","etag":"\"5578fe74b5a8aa31c59314ab3c73438b\"","set-cookie":"__cf_bm=e39dMBZHgYRIJq6LaSEM.yZDqAFCV2KbSNJgGGv3USo-1785241875.3133743-1.0.1.1-2zrKlZlgyQwlr3Of8d0jh.ewDyD17w.t4qSN2QMWfZc2fmPA6iEPMKZpjUEKCrI6LJckASXBaWR7Js4DaGreHmmChdKl7sJvHHNi0ueCExdgjGO6pPjyp0Tcixsx5eBX; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT"},"url":"https://jsr.io/@std/path/1.0.0-rc.2/posix/join.ts","time":1785241875}