"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_paths__internal = require('./_internal.js');
const require_paths_predicates = require('./predicates.js');
const require_paths_normalize = require('./normalize.js');

//#region src/paths/resolve.ts
/**
* @file Path resolution utilities — `resolve`, `relative`, `relativeResolve`.
*   Split out of `paths/normalize.ts` for size hygiene.
*
*   - `resolve` — Node-style `path.resolve()` over absolute-path semantics
*   - `relative` — relative path from one absolute to another
*   - `relativeResolve` — `relative` + `normalizePath` convenience wrapper
*/
/**
* Calculate the relative path from one path to another.
*
* Both inputs are resolved to absolute paths first, then compared to find the
* longest common base, and finally a relative path is constructed using `../`
* for parent-directory traversal.
*
* Windows file systems are case-insensitive; the comparison reflects that.
*
* @example
*   ;```typescript
*   relative('/foo/bar', '/foo/baz') // '../baz'
*   relative('/foo/bar/baz', '/foo') // '../..'
*   relative('/foo', '/foo/bar') // 'bar'
*   relative('/foo/bar', '/foo/bar') // ''
*   ```
*
* @param {string} from - Source path.
* @param {string} to - Destination path.
*
* @returns {string} Relative path from `from` to `to`, or empty string if equal
*/
/*@__NO_SIDE_EFFECTS__*/
function relative(from, to) {
	if (from === to) return "";
	const actualFrom = /* @__PURE__ */ resolve(from);
	const actualTo = /* @__PURE__ */ resolve(to);
	if (actualFrom === actualTo) return "";
	/* c8 ignore start - Windows-only case-insensitive comparison. */
	if (require_constants_platform.WIN32) {
		if (actualFrom.toLowerCase() === actualTo.toLowerCase()) return "";
	}
	/* c8 ignore stop */
	const fromStart = 1;
	const fromEnd = actualFrom.length;
	const fromLen = fromEnd - fromStart;
	const toStart = 1;
	const toLen = actualTo.length - toStart;
	const length = fromLen < toLen ? fromLen : toLen;
	let lastCommonSep = -1;
	let i = 0;
	for (; i < length; i += 1) {
		let fromCode = require_primordials_string.StringPrototypeCharCodeAt(actualFrom, fromStart + i);
		let toCode = require_primordials_string.StringPrototypeCharCodeAt(actualTo, toStart + i);
		/* c8 ignore start - Windows-only case folding. */
		if (require_constants_platform.WIN32) {
			if (fromCode >= 65 && fromCode <= 90) fromCode += 32;
			if (toCode >= 65 && toCode <= 90) toCode += 32;
		}
		/* c8 ignore stop */
		if (fromCode !== toCode) break;
		if (/* @__PURE__ */ require_paths_predicates.isPathSeparator(require_primordials_string.StringPrototypeCharCodeAt(actualFrom, fromStart + i))) lastCommonSep = i;
	}
	/* c8 ignore start */
	if (i === length) {
		if (toLen > length) {
			if (/* @__PURE__ */ require_paths_predicates.isPathSeparator(require_primordials_string.StringPrototypeCharCodeAt(actualTo, toStart + i))) return actualTo.slice(toStart + i + 1);
			if (i === 0) return actualTo.slice(toStart + i);
		} else if (fromLen > length) {
			if (/* @__PURE__ */ require_paths_predicates.isPathSeparator(require_primordials_string.StringPrototypeCharCodeAt(actualFrom, fromStart + i))) lastCommonSep = i;
			else if (i === 0) lastCommonSep = 0;
		}
	}
	/* c8 ignore stop */
	let out = "";
	for (i = fromStart + lastCommonSep + 1; i <= fromEnd; i += 1) {
		const code = require_primordials_string.StringPrototypeCharCodeAt(actualFrom, i);
		if (i === fromEnd || /* @__PURE__ */ require_paths_predicates.isPathSeparator(code)) out += out.length === 0 ? ".." : "/..";
	}
	return out + actualTo.slice(toStart + lastCommonSep);
}
/**
* Get the normalized relative path from one path to another.
*
* Computes the relative path using `relative()` then runs the result through
* `normalizePath()`. Empty strings (same path) are preserved verbatim rather
* than collapsed to `.`.
*
* @example
*   ;```typescript
*   relativeResolve('/foo/bar', '/foo/baz') // '../baz'
*   relativeResolve('/foo/bar', '/foo/bar') // ''
*   relativeResolve('/foo/./bar', '/foo/baz') // '../baz'
*   ```
*
* @param {string} from - Source path.
* @param {string} to - Destination path.
*
* @returns {string} Normalized relative path, or empty string if equal
*/
/*@__NO_SIDE_EFFECTS__*/
function relativeResolve(from, to) {
	const rel = /* @__PURE__ */ relative(from, to);
	if (rel === "") return "";
	return /* @__PURE__ */ require_paths_normalize.normalizePath(rel);
}
/**
* Resolve an absolute path from path segments.
*
* Mimics Node.js `path.resolve()`: processes segments right-to-left, stops at
* the first absolute segment, and prepends the cwd if no absolute segment is
* found. The final path is normalized.
*
* @example
*   ;```typescript
*   resolve('foo', 'bar', 'baz') // '/cwd/foo/bar/baz'
*   resolve('/foo', 'bar', 'baz') // '/foo/bar/baz'
*   resolve('foo', '/bar', 'baz') // '/bar/baz'
*   resolve() // '/cwd'
*   ```
*
* @param {...string} segments - Path segments to resolve.
*
* @returns {string} The resolved absolute path
*/
/*@__NO_SIDE_EFFECTS__*/
function resolve(...segments) {
	let resolvedPath = "";
	let resolvedAbsolute = false;
	for (let i = segments.length - 1; i >= 0 && !resolvedAbsolute; i -= 1) {
		const segment = segments[i];
		/* c8 ignore start */
		if (typeof segment !== "string" || segment.length === 0) continue;
		resolvedPath = segment + (resolvedPath.length === 0 ? "" : `/${resolvedPath}`);
		resolvedAbsolute = /* @__PURE__ */ require_paths_predicates.isAbsolute(segment);
	}
	if (!resolvedAbsolute) resolvedPath = /* @__PURE__ */ require("node:process").cwd() + (resolvedPath.length === 0 ? "" : `/${resolvedPath}`);
	/* c8 ignore stop */
	return /* @__PURE__ */ require_paths_normalize.normalizePath(resolvedPath);
}

//#endregion
exports.relative = relative;
exports.relativeResolve = relativeResolve;
exports.resolve = resolve;