import { BasicConfig, Utils as QbUtils, type Config, type ImmutableTree, type JsonGroup, type JsonItem, } from "@react-awesome-query-builder/ui"; import type { AlertQueryField, AlertQueryOperator } from "./types"; const NUMERIC_OPERATORS_DEFAULT: AlertQueryOperator[] = [ "equal", "less", "less_or_equal", "greater", "greater_or_equal", ]; export const ALERT_QUERY_FIELDS: AlertQueryField[] = [ { key: "userMetric.max_loan_amount", label: "Borrowing Capacity", type: "number", unit: "dollar", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, }, { key: "userMetric.debt_outstanding", label: "Outstanding Debt", type: "number", unit: "dollar", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, }, { key: "userMetric.lvr", label: "Current LVR", type: "number", unit: "percent", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, max: 100, }, { key: "userMetric.has_met_buying_goal", label: "Has Met Buying Goal", type: "boolean", }, { key: "userMetric.excess_monthly_surplus", label: "Excess Monthly Surplus", type: "number", unit: "dollar", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, }, { key: "userMetric.equity", label: "Equity Amount", type: "number", unit: "dollar", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, }, { key: "userMetric.max_debt_interest_rate", label: "Max Debt Interest Rate", type: "number", unit: "percent", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, max: 100, }, { key: "userMetric.min_debt_interest_rate", label: "Min Debt Interest Rate", type: "number", unit: "percent", operators: NUMERIC_OPERATORS_DEFAULT, min: 0, max: 100, }, ]; export const ALL_NUMERIC_OPERATORS: AlertQueryOperator[] = NUMERIC_OPERATORS_DEFAULT; export const BOOLEAN_OPERATORS: AlertQueryOperator[] = ["equal"]; export const OPERATOR_LABELS: Record = { equal: "=", less: "<", less_or_equal: "≤", greater: ">", greater_or_equal: "≥", }; export const SEVERITY_LABELS = { HEALTHY: "Healthy", WATCH: "Watch", NEED_ACTION: "Need Action", } as const; // react-awesome-query-builder config — drives ImmutableTree state. export const QB_CONFIG: Config = { ...BasicConfig, settings: { ...BasicConfig.settings, maxNesting: 3, maxNumberOfRules: 5, }, fields: { "userMetric.max_loan_amount": { label: "Borrowing Capacity", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0 }, }, "userMetric.debt_outstanding": { label: "Outstanding Debt", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0 }, }, "userMetric.lvr": { label: "Current LVR", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0, max: 100 }, }, "userMetric.has_met_buying_goal": { label: "Has Met Buying Goal", type: "boolean", operators: ["equal"], valueSources: ["value"], }, "userMetric.excess_monthly_surplus": { label: "Excess Monthly Surplus", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0 }, }, "userMetric.equity": { label: "Equity Amount", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0 }, }, "userMetric.max_debt_interest_rate": { label: "Max Debt Interest Rate", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0, max: 100 }, }, "userMetric.min_debt_interest_rate": { label: "Min Debt Interest Rate", type: "number", operators: NUMERIC_OPERATORS_DEFAULT, valueSources: ["value"], fieldSettings: { min: 0, max: 100 }, }, }, }; // Default query — children1 in array form (matches JsonGroup type). export const EMPTY_QUERY_VALUE: JsonGroup = { id: "root", type: "group", properties: { conjunction: "AND", not: false }, children1: [ { id: "rule-init", type: "rule", properties: { field: "userMetric.max_loan_amount", operator: "greater_or_equal", value: [null], valueSrc: ["value"], }, } as JsonItem, ], }; /** Convert a JsonGroup to an ImmutableTree usable by the query builder. */ export function createAlertTree(query?: JsonGroup | null): ImmutableTree { const q = query ?? EMPTY_QUERY_VALUE; // eslint-disable-next-line @typescript-eslint/no-explicit-any const loaded = QbUtils.loadTree(q as any); return QbUtils.sanitizeTree(loaded, QB_CONFIG).fixedTree; }