// src/lib/match.ts
import { minimatch } from "minimatch";
function makeMatcher(pattern, options = {}) {
  const patterns = Array.isArray(pattern) ? pattern : [pattern];
  return (input) => patterns.some((p) => minimatch(input, p, options));
}
function isMatch(input, pattern, options = {}) {
  return makeMatcher(pattern, options)(input);
}

// src/actions/deplint/ruleConfig.ts
var VALID_RULE_LEVELS = /* @__PURE__ */ new Set(["off", "warn", "error"]);
var VALID_RANGE_BOUNDS = /* @__PURE__ */ new Set(["infer", "major", "minor"]);
var VALID_RANGE_FORMS = /* @__PURE__ */ new Set(["broad", "shorthand"]);
var VALID_WORKSPACE_PROTOCOLS = /* @__PURE__ */ new Set(["workspace:~", "workspace:^", "workspace:*"]);
var BUILT_IN_RULE_OPTION_KEYS = {
  "deplint.peerDependencies.range-style": /* @__PURE__ */ new Set(["bound", "form", "ignore"]),
  "deplint.workspace.protocol": /* @__PURE__ */ new Set(["ignore", "protocol"])
};
function isRecord(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isRuleLevel(value) {
  return typeof value === "string" && VALID_RULE_LEVELS.has(value);
}
function severityFromLevel(level) {
  return level === "warn" ? "warning" : "error";
}
function formatRuleConfigLocation(ruleId) {
  return `commands.deplint.rules["${ruleId}"]`;
}
function parseRuleConfig(ruleId, value) {
  if (typeof value === "string") {
    if (!isRuleLevel(value)) {
      throw new TypeError(`${formatRuleConfigLocation(ruleId)} must be one of "off", "warn", or "error"`);
    }
    return { level: value, options: {} };
  }
  if (Array.isArray(value)) {
    if (value.length !== 2) {
      throw new TypeError(`${formatRuleConfigLocation(ruleId)} tuple must be [level, options]`);
    }
    const level = value[0];
    if (!isRuleLevel(level)) {
      throw new TypeError(`${formatRuleConfigLocation(ruleId)} level must be one of "off", "warn", or "error"`);
    }
    const options = value[1];
    if (!isRecord(options)) {
      throw new TypeError(`${formatRuleConfigLocation(ruleId)} options must be an object`);
    }
    return { level, options: { ...options } };
  }
  throw new TypeError(`${formatRuleConfigLocation(ruleId)} must be a level or [level, options] tuple`);
}
function validateIgnore(ruleId, options) {
  if (options.ignore === void 0) return;
  if (!Array.isArray(options.ignore) || options.ignore.some((pattern) => typeof pattern !== "string")) {
    throw new TypeError(`${formatRuleConfigLocation(ruleId)}.ignore must be an array of strings`);
  }
}
function validateBuiltInOptions(ruleId, options, builtInRuleIds) {
  validateIgnore(ruleId, options);
  if (!builtInRuleIds.has(ruleId)) return;
  const allowedKeys = BUILT_IN_RULE_OPTION_KEYS[ruleId] ?? /* @__PURE__ */ new Set(["ignore"]);
  for (const key of Object.keys(options)) {
    if (!allowedKeys.has(key)) {
      throw new TypeError(`${formatRuleConfigLocation(ruleId)} has unknown option "${key}"`);
    }
  }
  if (ruleId === "deplint.peerDependencies.range-style" && options.bound !== void 0 && (typeof options.bound !== "string" || !VALID_RANGE_BOUNDS.has(options.bound))) {
    throw new TypeError(`${formatRuleConfigLocation(ruleId)}.bound must be "infer", "major", or "minor"`);
  }
  if (ruleId === "deplint.peerDependencies.range-style" && options.form !== void 0 && (typeof options.form !== "string" || !VALID_RANGE_FORMS.has(options.form))) {
    throw new TypeError(`${formatRuleConfigLocation(ruleId)}.form must be "broad" or "shorthand"`);
  }
  if (ruleId === "deplint.workspace.protocol" && options.protocol !== void 0 && (typeof options.protocol !== "string" || !VALID_WORKSPACE_PROTOCOLS.has(options.protocol))) {
    throw new TypeError(`${formatRuleConfigLocation(ruleId)}.protocol must be "workspace:~", "workspace:^", or "workspace:*"`);
  }
}
function normalizeDeplintRules(rules, config, builtInRuleIds) {
  const ruleConfigs = config?.rules ?? {};
  const ruleIds = new Set(rules.map((rule) => rule.id));
  const unknownRuleIds = Object.keys(ruleConfigs).filter((ruleId) => !ruleIds.has(ruleId));
  if (unknownRuleIds.length > 0) {
    throw new TypeError(`Unknown deplint rule ${unknownRuleIds.map((ruleId) => `"${ruleId}"`).join(", ")} in commands.deplint.rules`);
  }
  return rules.map((rule) => {
    const ruleConfig = ruleConfigs[rule.id];
    if (ruleConfig === void 0) {
      return {
        ...rule,
        enabled: true,
        meta: { ...rule.meta, severity: "warning" },
        options: {}
      };
    }
    const { level, options } = parseRuleConfig(rule.id, ruleConfig);
    validateBuiltInOptions(rule.id, options, builtInRuleIds);
    return {
      ...rule,
      enabled: level !== "off",
      meta: level === "off" ? { ...rule.meta } : { ...rule.meta, severity: severityFromLevel(level) },
      options
    };
  });
}
function isDependencyIgnoredByRule(dependency, options) {
  if (!(dependency != null && dependency.length > 0)) return false;
  return options?.ignore?.some((pattern) => pattern === dependency || isMatch(dependency, pattern)) ?? false;
}
export {
  isDependencyIgnoredByRule,
  normalizeDeplintRules
};
//# sourceMappingURL=ruleConfig.mjs.map
