"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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 matcher_exports = {};
__export(matcher_exports, {
  getGlobMatcher: () => getGlobMatcher,
  getMatchesGlob: () => getMatchesGlob
});
module.exports = __toCommonJS(matcher_exports);
var import_array = require("../primordials/array");
var import_json = require("../primordials/json");
var import_object = require("../primordials/object");
var import_string = require("../primordials/string");
var import_internal = require("./_internal");
let _matchesGlob;
let _matchesGlobProbed = false;
// @__NO_SIDE_EFFECTS__
function getGlobMatcher(glob, options) {
  const patterns = (0, import_array.ArrayIsArray)(glob) ? glob : [glob];
  const sortedPatterns = [...patterns].sort();
  const sortedOptions = options ? (0, import_object.ObjectKeys)(options).sort().map((k) => {
    const value = options[k];
    const normalized = (0, import_array.ArrayIsArray)(value) ? [...value].sort() : value;
    return `${k}:${(0, import_json.JSONStringify)(normalized)}`;
  }).join(",") : "";
  const key = `${sortedPatterns.join("|")}:${sortedOptions}`;
  const existing = import_internal.matcherCache.get(key);
  if (existing) {
    import_internal.matcherCache.delete(key);
    import_internal.matcherCache.set(key, existing);
    return existing;
  }
  if (import_internal.matcherCache.size >= import_internal.MATCHER_CACHE_MAX_SIZE) {
    const oldest = import_internal.matcherCache.keys().next().value;
    if (oldest !== void 0) {
      import_internal.matcherCache.delete(oldest);
    }
  }
  let matcher;
  if (patterns.length === 1 && !(0, import_string.StringPrototypeStartsWith)(patterns[0], "!") && options !== void 0 && options.nocase === false && options.dot === false && (options.ignore === void 0 || options.ignore.length === 0)) {
    const matchesGlob = /* @__PURE__ */ getMatchesGlob();
    if (matchesGlob !== void 0) {
      const pattern = patterns[0];
      matcher = (p) => matchesGlob(p, pattern);
    }
  }
  if (matcher === void 0) {
    const positivePatterns = patterns.filter(
      (p) => !(0, import_string.StringPrototypeStartsWith)(p, "!")
    );
    const negativePatterns = patterns.filter((p) => (0, import_string.StringPrototypeStartsWith)(p, "!")).map((p) => p.slice(1));
    const matchOptions = {
      dot: true,
      nocase: true,
      ...options,
      ...negativePatterns.length > 0 ? { ignore: negativePatterns } : {}
    };
    const picomatch = (0, import_internal.getPicomatch)();
    matcher = picomatch(
      positivePatterns.length > 0 ? positivePatterns : patterns,
      matchOptions
    );
  }
  import_internal.matcherCache.set(key, matcher);
  return matcher;
}
// @__NO_SIDE_EFFECTS__
function getMatchesGlob() {
  if (!_matchesGlobProbed) {
    const fn = require("node:path").matchesGlob;
    if (typeof fn === "function") {
      _matchesGlob = fn;
    }
    _matchesGlobProbed = true;
  }
  return _matchesGlob;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getGlobMatcher,
  getMatchesGlob
});
