import { isValuelessOperator } from "@open-mercato/shared/lib/query/advanced-filter";
const MULTI_VALUE_OPS = /* @__PURE__ */ new Set(["is_any_of", "is_none_of", "has_any_of", "has_all_of", "has_none_of"]);
function isBlank(value) {
  if (value == null) return true;
  if (typeof value === "string") return value.trim().length === 0;
  return false;
}
function validateRule(rule) {
  if (isValuelessOperator(rule.operator)) return null;
  const op = rule.operator;
  if (op === "between") {
    if (!Array.isArray(rule.value) || rule.value.length !== 2 || isBlank(rule.value[0]) || isBlank(rule.value[1])) {
      return { ruleId: rule.id, messageKey: "ui.advancedFilter.error.betweenIncomplete", message: "Pick both endpoints" };
    }
    return null;
  }
  if (MULTI_VALUE_OPS.has(op)) {
    if (!Array.isArray(rule.value) || rule.value.length === 0) {
      return { ruleId: rule.id, messageKey: "ui.advancedFilter.error.missingValue", message: "Pick at least one value" };
    }
    return null;
  }
  if (isBlank(rule.value)) {
    return { ruleId: rule.id, messageKey: "ui.advancedFilter.error.missingValue", message: "Pick a value to apply this filter" };
  }
  return null;
}
function walkGroup(group, acc) {
  for (const child of group.children) {
    if (child.type === "rule") {
      const err = validateRule(child);
      if (err) acc.push(err);
    } else {
      walkGroup(child, acc);
    }
  }
}
function validateTreeForApply(tree, fields) {
  void fields;
  const errors = [];
  walkGroup(tree.root, errors);
  return errors.length === 0 ? { ok: true } : { ok: false, errors };
}
export {
  validateTreeForApply
};
//# sourceMappingURL=filterValidation.js.map
