"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var find_up_exports = {};
__export(find_up_exports, {
  findUp: () => findUp,
  findUpSync: () => findUpSync
});
module.exports = __toCommonJS(find_up_exports);
var import_node_process = __toESM(require("node:process"));
var import_predicates = require("../arrays/predicates");
var import_abort = require("../process/abort");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
var import_normalize = require("../paths/normalize");
const abortSignal = (0, import_abort.getAbortSignal)();
// @__NO_SIDE_EFFECTS__
async function findUp(name, options) {
  const { cwd = import_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 = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  let dir = path.resolve(cwd);
  const { root } = path.parse(dir);
  const names = (0, import_predicates.isArray)(name) ? name : [name];
  while (dir) {
    for (const n of names) {
      if (signal?.aborted) {
        return void 0;
      }
      const thePath = path.join(dir, n);
      try {
        const stats = await fs.promises.stat(thePath);
        if (!onlyDirectories && stats.isFile()) {
          return (0, import_normalize.normalizePath)(thePath);
        }
        if (!onlyFiles && stats.isDirectory()) {
          return (0, import_normalize.normalizePath)(thePath);
        }
      } catch {
      }
    }
    if (dir === root) {
      break;
    }
    dir = path.dirname(dir);
  }
  return void 0;
}
// @__NO_SIDE_EFFECTS__
function findUpSync(name, options) {
  const { cwd = import_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 = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  let dir = path.resolve(cwd);
  const { root } = path.parse(dir);
  const stopDir = stopAt ? path.resolve(stopAt) : void 0;
  const names = (0, import_predicates.isArray)(name) ? name : [name];
  while (dir) {
    if (stopDir && dir === stopDir) {
      for (const n of names) {
        const thePath = path.join(dir, n);
        try {
          const stats = fs.statSync(thePath);
          if (!onlyDirectories && stats.isFile()) {
            return (0, import_normalize.normalizePath)(thePath);
          }
          if (!onlyFiles && stats.isDirectory()) {
            return (0, import_normalize.normalizePath)(thePath);
          }
        } catch {
        }
      }
      return void 0;
    }
    for (const n of names) {
      const thePath = path.join(dir, n);
      try {
        const stats = fs.statSync(thePath);
        if (!onlyDirectories && stats.isFile()) {
          return (0, import_normalize.normalizePath)(thePath);
        }
        if (!onlyFiles && stats.isDirectory()) {
          return (0, import_normalize.normalizePath)(thePath);
        }
      } catch {
      }
    }
    if (dir === root) {
      break;
    }
    dir = path.dirname(dir);
  }
  return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findUp,
  findUpSync
});
