"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 which_exports = {};
__export(which_exports, {
  which: () => which,
  whichReal: () => whichReal,
  whichRealSync: () => whichRealSync,
  whichSync: () => whichSync
});
module.exports = __toCommonJS(which_exports);
var import_which = __toESM(require("../external/which"));
var import_normalize = require("../paths/normalize");
var import_array = require("../primordials/array");
var import_internal = require("./_internal");
var import_resolve = require("./resolve");
async function which(binName, options) {
  if ((0, import_normalize.isPath)(binName)) {
    return binName;
  }
  try {
    const result = await (0, import_which.default)(
      binName,
      options
    );
    return result;
  } catch {
    return null;
  }
}
async function whichReal(binName, options) {
  const fs = (0, import_internal.getFs)();
  const opts = { __proto__: null, nothrow: true, ...options };
  if (opts.all) {
    const cachedAll = import_internal.binPathAllCache.get(binName);
    if (cachedAll && cachedAll.length > 0) {
      if (fs.existsSync(cachedAll[0])) {
        return cachedAll;
      }
      import_internal.binPathAllCache.delete(binName);
    }
  } else {
    const cached = import_internal.binPathCache.get(binName);
    if (cached) {
      if (fs.existsSync(cached)) {
        return cached;
      }
      import_internal.binPathCache.delete(binName);
    }
  }
  const result = await (0, import_which.default)(
    binName,
    opts
  );
  if (opts?.all) {
    const paths = (0, import_array.ArrayIsArray)(result) ? result : typeof result === "string" ? [result] : void 0;
    if (paths?.length) {
      const resolved2 = (0, import_array.ArrayPrototypeMap)(paths, (p) => (0, import_resolve.resolveRealBinSync)(p));
      import_internal.binPathAllCache.set(binName, resolved2);
      return resolved2;
    }
    return paths;
  }
  if (!result) {
    return void 0;
  }
  const resolved = (0, import_resolve.resolveRealBinSync)(result);
  import_internal.binPathCache.set(binName, resolved);
  return resolved;
}
function whichRealSync(binName, options) {
  const fs = (0, import_internal.getFs)();
  const opts = { __proto__: null, nothrow: true, ...options };
  if (opts.all) {
    const cachedAll = import_internal.binPathAllCache.get(binName);
    if (cachedAll && cachedAll.length > 0) {
      if (fs.existsSync(cachedAll[0])) {
        return cachedAll;
      }
      import_internal.binPathAllCache.delete(binName);
    }
  } else {
    const cached = import_internal.binPathCache.get(binName);
    if (cached) {
      if (fs.existsSync(cached)) {
        return cached;
      }
      import_internal.binPathCache.delete(binName);
    }
  }
  const result = whichSync(binName, opts);
  if (opts.all) {
    const paths = (0, import_array.ArrayIsArray)(result) ? result : typeof result === "string" ? [result] : void 0;
    if (paths?.length) {
      const resolved2 = (0, import_array.ArrayPrototypeMap)(paths, (p) => (0, import_resolve.resolveRealBinSync)(p));
      import_internal.binPathAllCache.set(binName, resolved2);
      return resolved2;
    }
    return paths;
  }
  if (!result) {
    return void 0;
  }
  const resolved = (0, import_resolve.resolveRealBinSync)(result);
  import_internal.binPathCache.set(binName, resolved);
  return resolved;
}
function whichSync(binName, options) {
  if ((0, import_normalize.isPath)(binName)) {
    return binName;
  }
  try {
    const result = import_which.default.sync(
      binName,
      options
    );
    return result;
  } catch {
    return null;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  which,
  whichReal,
  whichRealSync,
  whichSync
});
