"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_string = require('../primordials/string.js');
const require_constants_platform = require('../constants/platform.js');
const require_strings_search = require('../strings/search.js');
const require_paths__internal = require('./_internal.js');
const require_paths_conversion = require('./conversion.js');
const require_paths_predicates = require('./predicates.js');
const require_paths_resolve = require('./resolve.js');

//#region src/paths/normalize.ts
/**
* @file Path normalization — the core `normalizePath` and its MSYS drive-letter
*   helper. The rest of the path module's surface (predicates, conversion,
*   resolution) lives in sibling leaves and is re-exported here so existing
*   `paths/normalize` importers keep working.
*
*   - `normalizePath` — backslash → forward-slash, segment collapse, UNC +
*     namespace preservation
*   - `msysDriveToNative` — `/c/path` → `C:/path` on Windows
*/
const DRIVE_LETTER_REGEXP = /^[A-Za-z]:$/;
function msysDriveToNative(normalized) {
	/* c8 ignore start - Windows-only branch. */
	if (require_constants_platform.WIN32) return normalized.replace(require_paths__internal.msysDriveRegExp, (_, letter, sep) => `${letter.toUpperCase()}:${sep || "/"}`);
	/* c8 ignore stop */
	return normalized;
}
/**
* Normalize a path by converting backslashes to forward slashes and collapsing
* segments.
*
* - Converts all backslashes (`\`) to forward slashes (`/`)
* - Collapses repeated slashes
* - Resolves `.` and `..` segments
* - Preserves UNC path prefixes (`//server/share`)
* - Preserves Windows namespace prefixes (`//./`, `//?/`)
* - Returns `.` for empty or collapsed paths
* - On Windows: MSYS drive letters `/c/path` become `C:/path`
*
* @example
*   ;```typescript
*   normalizePath('foo/bar//baz') // 'foo/bar/baz'
*   normalizePath('foo/./bar') // 'foo/bar'
*   normalizePath('foo/bar/../baz') // 'foo/baz'
*   normalizePath('C:\\Users\\u\\file.txt') // 'C:/Users/u/file.txt'
*   normalizePath('\\\\server\\share\\file') // '//server/share/file'
*   normalizePath('') // '.'
*   ```
*
* @param {string | Buffer | URL} pathLike - The path to normalize.
*
* @returns {string} The normalized path
*
* @security
* **WARNING**: This function resolves `..` patterns as part of normalization, which means
* paths like `/../etc/passwd` become `/etc/passwd`. When processing untrusted user input
* (HTTP requests, file uploads, URL parameters), you MUST validate for path traversal
* attacks BEFORE calling this function.
*/
/*@__NO_SIDE_EFFECTS__*/
function normalizePath(pathLike) {
	const filepath = /* @__PURE__ */ require_paths__internal.pathLikeToString(pathLike);
	const { length } = filepath;
	if (length === 0) return ".";
	if (length < 2) return length === 1 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 0) === 92 ? "/" : filepath;
	let code = 0;
	let start = 0;
	let prefix = "";
	if (length > 4 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 3) === 92) {
		const code2 = require_primordials_string.StringPrototypeCharCodeAt(filepath, 2);
		if ((code2 === 63 || code2 === 46) && require_primordials_string.StringPrototypeCharCodeAt(filepath, 0) === 92 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 1) === 92) {
			start = 2;
			prefix = "//";
		}
	}
	if (start === 0)
 /* c8 ignore start - UNC path detection (\\server\share). Rare
	input; not exercised by typical test fixtures. */
	if (length > 2 && (require_primordials_string.StringPrototypeCharCodeAt(filepath, 0) === 92 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 1) === 92 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 2) !== 92 || require_primordials_string.StringPrototypeCharCodeAt(filepath, 0) === 47 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 1) === 47 && require_primordials_string.StringPrototypeCharCodeAt(filepath, 2) !== 47)) {
		let firstSegmentEnd = -1;
		let hasSecondSegment = false;
		let i = 2;
		while (i < length && (require_primordials_string.StringPrototypeCharCodeAt(filepath, i) === 47 || require_primordials_string.StringPrototypeCharCodeAt(filepath, i) === 92)) i++;
		while (i < length) {
			const char = require_primordials_string.StringPrototypeCharCodeAt(filepath, i);
			if (char === 47 || char === 92) {
				firstSegmentEnd = i;
				break;
			}
			i++;
		}
		if (firstSegmentEnd > 2) {
			i = firstSegmentEnd;
			while (i < length && (require_primordials_string.StringPrototypeCharCodeAt(filepath, i) === 47 || require_primordials_string.StringPrototypeCharCodeAt(filepath, i) === 92)) i++;
			if (i < length) hasSecondSegment = true;
		}
		if (firstSegmentEnd > 2 && hasSecondSegment) {
			start = 2;
			prefix = "//";
		} else {
			code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
			while (code === 47 || code === 92) {
				start += 1;
				code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
			}
			if (start) prefix = "/";
		}
	} else {
		code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
		while (code === 47 || code === 92) {
			start += 1;
			code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
		}
		if (start) prefix = "/";
	}
	let nextIndex = /* @__PURE__ */ require_strings_search.search(filepath, require_paths__internal.slashRegExp, { fromIndex: start });
	/* c8 ignore start */
	if (nextIndex === -1) {
		const segment = filepath.slice(start);
		if (segment === "." || segment.length === 0) return prefix || ".";
		if (segment === "..") return prefix ? require_primordials_string.StringPrototypeSlice(prefix, 0, -1) || "/" : "..";
		return msysDriveToNative(prefix + segment);
	}
	/* c8 ignore stop */
	/* c8 ignore start */
	let collapsed = "";
	let segmentCount = 0;
	let leadingDotDots = 0;
	while (nextIndex !== -1) {
		const segment = filepath.slice(start, nextIndex);
		if (segment.length > 0 && segment !== ".") if (segment === "..") {
			if (segmentCount > 0) {
				const lastSeparatorIndex = collapsed.lastIndexOf("/");
				if (lastSeparatorIndex === -1) {
					collapsed = "";
					segmentCount = 0;
					if (leadingDotDots > 0 && !prefix) {
						collapsed = "..";
						leadingDotDots = 1;
					}
				} else {
					const lastSegmentStart = lastSeparatorIndex + 1;
					if (collapsed.slice(lastSegmentStart) === "..") {
						collapsed = `${collapsed}/${segment}`;
						leadingDotDots += 1;
					} else {
						collapsed = collapsed.slice(0, lastSeparatorIndex);
						segmentCount -= 1;
					}
				}
			} else if (!prefix) {
				collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
				leadingDotDots += 1;
			}
		} else {
			collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
			segmentCount += 1;
		}
		start = nextIndex + 1;
		code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
		while (code === 47 || code === 92) {
			start += 1;
			code = require_primordials_string.StringPrototypeCharCodeAt(filepath, start);
		}
		nextIndex = /* @__PURE__ */ require_strings_search.search(filepath, require_paths__internal.slashRegExp, { fromIndex: start });
	}
	const lastSegment = filepath.slice(start);
	if (lastSegment.length > 0 && lastSegment !== ".") if (lastSegment === "..") {
		if (segmentCount > 0) {
			const lastSeparatorIndex = collapsed.lastIndexOf("/");
			if (lastSeparatorIndex === -1) {
				collapsed = "";
				segmentCount = 0;
				if (leadingDotDots > 0 && !prefix) {
					collapsed = "..";
					leadingDotDots = 1;
				}
			} else {
				const lastSegmentStart = lastSeparatorIndex + 1;
				if (collapsed.slice(lastSegmentStart) === "..") {
					collapsed = `${collapsed}/${lastSegment}`;
					leadingDotDots += 1;
				} else {
					collapsed = collapsed.slice(0, lastSeparatorIndex);
					segmentCount -= 1;
				}
			}
		} else if (!prefix) {
			collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
			leadingDotDots += 1;
		}
	} else {
		collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
		segmentCount += 1;
	}
	/* c8 ignore stop */
	if (collapsed.length === 0) return prefix || ".";
	if (DRIVE_LETTER_REGEXP.test(collapsed) && (require_primordials_string.StringPrototypeCharCodeAt(filepath, 2) === 47 || require_primordials_string.StringPrototypeCharCodeAt(filepath, 2) === 92)) return msysDriveToNative(`${prefix}${collapsed}/`);
	return msysDriveToNative(prefix + collapsed);
}

//#endregion
exports.fromUnixPath = require_paths_conversion.fromUnixPath;
exports.getUrl = require_paths__internal.getUrl;
exports.isAbsolute = require_paths_predicates.isAbsolute;
exports.isNodeModules = require_paths_predicates.isNodeModules;
exports.isPath = require_paths_predicates.isPath;
exports.isPathSeparator = require_paths_predicates.isPathSeparator;
exports.isRelative = require_paths_predicates.isRelative;
exports.isUnixPath = require_paths_predicates.isUnixPath;
exports.isWindowsDeviceRoot = require_paths_predicates.isWindowsDeviceRoot;
exports.msysDriveToNative = msysDriveToNative;
exports.normalizePath = normalizePath;
exports.pathLikeToString = require_paths__internal.pathLikeToString;
exports.relative = require_paths_resolve.relative;
exports.relativeResolve = require_paths_resolve.relativeResolve;
exports.resolve = require_paths_resolve.resolve;
exports.splitPath = require_paths_conversion.splitPath;
exports.toUnixPath = require_paths_conversion.toUnixPath;
exports.trimLeadingDotSlash = require_paths_conversion.trimLeadingDotSlash;