/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cacache_exports = {};
__export(cacache_exports, {
  clear: () => clear,
  get: () => get,
  put: () => put,
  remove: () => remove,
  safeGet: () => safeGet,
  withTmp: () => withTmp
});
module.exports = __toCommonJS(cacache_exports);
var import_paths = require("./paths");
function getCacache() {
  return require("./external/cacache");
}
function patternToRegex(pattern) {
  const escaped = pattern.replaceAll(/[.+?^${}()|[\]\\]/g, "\\$&");
  const regexPattern = escaped.replaceAll("*", ".*");
  return new RegExp(`^${regexPattern}`);
}
function matchesPattern(key, pattern) {
  if (!pattern.includes("*")) {
    return key.startsWith(pattern);
  }
  const regex = patternToRegex(pattern);
  return regex.test(key);
}
async function clear(options) {
  const opts = { __proto__: null, ...options };
  const cacache = getCacache();
  const cacheDir = (0, import_paths.getSocketCacacheDir)();
  if (!opts.prefix) {
    try {
      await cacache.rm.all(cacheDir);
      return;
    } catch (e) {
      if (e?.code !== "ENOTEMPTY") {
        throw e;
      }
      return;
    }
  }
  const hasWildcard = opts.prefix.includes("*");
  if (!hasWildcard) {
    let removed2 = 0;
    const stream2 = cacache.ls.stream(cacheDir);
    for await (const entry of stream2) {
      if (entry.key.startsWith(opts.prefix)) {
        try {
          await cacache.rm.entry(cacheDir, entry.key);
          removed2++;
        } catch {
        }
      }
    }
    return removed2;
  }
  let removed = 0;
  const stream = cacache.ls.stream(cacheDir);
  for await (const entry of stream) {
    if (matchesPattern(entry.key, opts.prefix)) {
      try {
        await cacache.rm.entry(cacheDir, entry.key);
        removed++;
      } catch {
      }
    }
  }
  return removed;
}
async function get(key, options) {
  if (key.includes("*")) {
    throw new TypeError(
      'Cache key cannot contain wildcards (*). Wildcards are only supported in clear({ prefix: "pattern*" }).'
    );
  }
  const cacache = getCacache();
  return await cacache.get((0, import_paths.getSocketCacacheDir)(), key, options);
}
async function put(key, data, options) {
  if (key.includes("*")) {
    throw new TypeError(
      'Cache key cannot contain wildcards (*). Wildcards are only supported in clear({ prefix: "pattern*" }).'
    );
  }
  const cacache = getCacache();
  return await cacache.put((0, import_paths.getSocketCacacheDir)(), key, data, options);
}
async function remove(key) {
  if (key.includes("*")) {
    throw new TypeError(
      'Cache key cannot contain wildcards (*). Use clear({ prefix: "pattern*" }) to remove multiple entries.'
    );
  }
  const cacache = getCacache();
  return await cacache.rm.entry((0, import_paths.getSocketCacacheDir)(), key);
}
async function safeGet(key, options) {
  try {
    return await get(key, options);
  } catch {
    return void 0;
  }
}
async function withTmp(callback) {
  const cacache = getCacache();
  return await cacache.tmp.withTmp(
    (0, import_paths.getSocketCacacheDir)(),
    {},
    callback
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clear,
  get,
  put,
  remove,
  safeGet,
  withTmp
});
//# sourceMappingURL=cacache.js.map
