"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_paths_normalize = require('../paths/normalize.js');
const require_node_fs = require('../node/fs.js');
const require_node_path = require('../node/path.js');
const require_process_abort = require('../process/abort.js');
const require_arrays_predicates = require('../arrays/predicates.js');
const require_smol_path = require('../smol/path.js');
const require_paths_walk = require('../paths/walk.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/fs/find-up.ts
/**
* @file Walk parent directories to locate a file or directory by name. The
*   traversal includes the filesystem root (or the caller-supplied `stopAt`
*   boundary) — historically the loop exited at `dir === root` _before_
*   visiting root itself, so a match at e.g. `/.foo` was never found. The
*   current shape visits root, then breaks.
*/
const abortSignal = require_process_abort.getAbortSignal();
/**
* Find a file or directory by traversing up parent directories. Searches from
* the starting directory upward to the filesystem root. Useful for finding
* configuration files or project roots.
*
* @example
*   ;```ts
*   // Find package.json starting from current directory
*   const pkgPath = await findUp('package.json')
*
*   // Find any of multiple config files
*   const configPath = await findUp(['.config.js', '.config.json'])
*
*   // Find a directory instead of file
*   const nodeModules = await findUp('node_modules', { onlyDirectories: true })
*   ```
*
* @param name - Filename(s) to search for.
* @param options - Search options including cwd and type filters.
*
* @returns Normalized absolute path if found, undefined otherwise
*/
/*@__NO_SIDE_EFFECTS__*/
async function findUp(name, options) {
	const { cwd = node_process.default.cwd(), signal = abortSignal } = {
		__proto__: null,
		...options
	};
	let { onlyDirectories = false, onlyFiles = true } = {
		__proto__: null,
		...options
	};
	if (onlyDirectories) onlyFiles = false;
	if (onlyFiles) onlyDirectories = false;
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const names = require_arrays_predicates.isArray(name) ? name : [name];
	for (const dir of require_paths_walk.walkUp(cwd)) for (const n of names) {
		if (signal?.aborted) return;
		const thePath = path.join(dir, n);
		try {
			const stats = await fs.promises.stat(thePath);
			if (!onlyDirectories && stats.isFile()) return /* @__PURE__ */ require_paths_normalize.normalizePath(thePath);
			if (!onlyFiles && stats.isDirectory()) return /* @__PURE__ */ require_paths_normalize.normalizePath(thePath);
		} catch {}
	}
}
/**
* Synchronously find a file or directory by traversing up parent directories.
* Searches from the starting directory upward to the filesystem root or
* `stopAt` directory. Useful for finding configuration files or project roots
* in synchronous contexts.
*
* @example
*   ;```ts
*   // Find package.json starting from current directory
*   const pkgPath = findUpSync('package.json')
*
*   // Find .git directory but stop at home directory
*   const gitPath = findUpSync('.git', {
*     onlyDirectories: true,
*     stopAt: process.env.HOME,
*   })
*
*   // Find any of multiple config files
*   const configPath = findUpSync(['.eslintrc.js', '.eslintrc.json'])
*   ```
*
* @param name - Filename(s) to search for.
* @param options - Search options including cwd, stopAt, and type filters.
*
* @returns Normalized absolute path if found, undefined otherwise
*/
/*@__NO_SIDE_EFFECTS__*/
function findUpSync(name, options) {
	const { cwd = node_process.default.cwd(), stopAt } = {
		__proto__: null,
		...options
	};
	let { onlyDirectories = false, onlyFiles = true } = {
		__proto__: null,
		...options
	};
	if (onlyDirectories) onlyFiles = false;
	if (onlyFiles) onlyDirectories = false;
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const names = require_arrays_predicates.isArray(name) ? name : [name];
	/* c8 ignore start - native findUp arm only on socket-btm smol binaries; getSmolPath() is undefined on stock Node. */
	const smolFindUp = (/* @__PURE__ */ require_smol_path.getSmolPath())?.findUp;
	if (smolFindUp && stopAt === void 0) {
		const found = smolFindUp(path.resolve(cwd), names, { onlyDirectories });
		return found === void 0 ? void 0 : /* @__PURE__ */ require_paths_normalize.normalizePath(found);
	}
	/* c8 ignore stop */
	for (const dir of require_paths_walk.walkUp(cwd, { stopAt })) for (const n of names) {
		const thePath = path.join(dir, n);
		try {
			const stats = fs.statSync(thePath);
			if (!onlyDirectories && stats.isFile()) return /* @__PURE__ */ require_paths_normalize.normalizePath(thePath);
			if (!onlyFiles && stats.isDirectory()) return /* @__PURE__ */ require_paths_normalize.normalizePath(thePath);
		} catch {}
	}
}

//#endregion
exports.findUp = findUp;
exports.findUpSync = findUpSync;