"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const parseRouteParams = require("./parseRouteParams.js");
function isDateString(value) {
  if (typeof value !== "string") return false;
  return /^\d{4}\/\d{2}\/\d{2}$/.test(value);
}
function compareDate(a, b) {
  return a.localeCompare(b);
}
function checkFilterMatch(filterConfig, data, fields = []) {
  if (!filterConfig || !data) {
    return false;
  }
  return checkGroupMatch(filterConfig, data, fields);
}
function checkGroupMatch(group, data, fields) {
  const { operator, conditions, groups } = group;
  const results = [];
  if (conditions && conditions.length > 0) {
    conditions.forEach((cond) => {
      results.push(checkConditionMatch(cond, data, fields));
    });
  }
  if (groups && groups.length > 0) {
    groups.forEach((g) => {
      results.push(checkGroupMatch(g, data, fields));
    });
  }
  if (operator == "and") {
    return results.every((r) => r == true);
  } else {
    return results.some((r) => r == true);
  }
}
function checkConditionMatch(condition, data, fields) {
  const { field, operator, value } = condition;
  const fieldInfo = fields.find((f) => f.id == field);
  const fieldName = (fieldInfo == null ? void 0 : fieldInfo.fieldName) || field;
  const dataValue = data[fieldName];
  const parsedValue = parseRouteParams.parseSingleParamValue(value, data, fields);
  switch (operator) {
    case "eq":
      return dataValue == parsedValue;
    case "ne":
      return dataValue != parsedValue;
    case "gt":
      if (isDateString(dataValue) && isDateString(parsedValue)) {
        return compareDate(dataValue, parsedValue) > 0;
      }
      return Number(dataValue) > Number(parsedValue);
    case "gte":
      if (isDateString(dataValue) && isDateString(parsedValue)) {
        return compareDate(dataValue, parsedValue) >= 0;
      }
      return Number(dataValue) >= Number(parsedValue);
    case "lt":
      if (isDateString(dataValue) && isDateString(parsedValue)) {
        return compareDate(dataValue, parsedValue) < 0;
      }
      return Number(dataValue) < Number(parsedValue);
    case "lte":
      if (isDateString(dataValue) && isDateString(parsedValue)) {
        return compareDate(dataValue, parsedValue) <= 0;
      }
      return Number(dataValue) <= Number(parsedValue);
    case "range":
      if (!parsedValue || parsedValue.min == null || parsedValue.max == null) return false;
      if (isDateString(dataValue) && isDateString(parsedValue.min) && isDateString(parsedValue.max)) {
        return compareDate(dataValue, parsedValue.min) >= 0 && compareDate(dataValue, parsedValue.max) <= 0;
      }
      const numValue = Number(dataValue);
      return numValue >= Number(parsedValue.min) && numValue <= Number(parsedValue.max);
    case "contains":
      if (!dataValue || !parsedValue) return false;
      return String(dataValue).toLowerCase().includes(String(parsedValue).toLowerCase());
    case "not_contains":
      if (!dataValue || !parsedValue) return true;
      return !String(dataValue).toLowerCase().includes(String(parsedValue).toLowerCase());
    case "in_any":
      if (!Array.isArray(parsedValue)) {
        return dataValue == parsedValue;
      }
      return parsedValue.some((v) => dataValue == v);
    case "ni_any":
      if (!Array.isArray(parsedValue)) {
        return dataValue != parsedValue;
      }
      return !parsedValue.some((v) => dataValue == v);
    case "is_null":
      return dataValue == null || dataValue == void 0 || dataValue == "";
    case "is_not_null":
      return dataValue !== null && dataValue !== void 0 && dataValue !== "";
    default:
      return dataValue == parsedValue;
  }
}
function checkFieldInFilterConfig(filterConfig, fieldId) {
  if (!fieldId) {
    return true;
  }
  return checkFieldInGroup(filterConfig, fieldId);
}
function checkFieldInGroup(group, targetFieldId) {
  const { conditions, groups } = group;
  if (conditions && conditions.length > 0) {
    const foundInConditions = conditions.some((cond) => cond.field == targetFieldId);
    if (foundInConditions) {
      return true;
    }
  }
  if (groups && groups.length > 0) {
    return groups.some((g) => checkFieldInGroup(g, targetFieldId));
  }
  return false;
}
function checkFieldInResultConfig(resultConfig, fieldId, fields = []) {
  var _a, _b;
  if (!fieldId || !Array.isArray(resultConfig)) {
    return true;
  }
  let fieldName = fieldId;
  if (fields && fields.length > 0) {
    const field = fields.find((f) => f.id == fieldId);
    if (field) {
      fieldName = field.fieldName;
    }
  }
  for (const item of resultConfig) {
    if ((_b = (_a = item.formula) == null ? void 0 : _a.value) == null ? void 0 : _b.text) {
      const formulaText = item.formula.value.text;
      const fieldPattern = new RegExp(`\\{${fieldName}\\}`, "i");
      if (fieldPattern.test(formulaText)) {
        return true;
      }
    }
  }
  return false;
}
exports.checkFieldInFilterConfig = checkFieldInFilterConfig;
exports.checkFieldInResultConfig = checkFieldInResultConfig;
exports.checkFilterMatch = checkFilterMatch;
