@universe-platform/ui 0.1.0-beta.66 → 0.1.0-beta.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -70,9 +70,14 @@ __export(src_exports, {
70
70
  DEFAULT_PLACEMENT_STATUS_COLORS: () => DEFAULT_PLACEMENT_STATUS_COLORS,
71
71
  DatePickerCell: () => DatePickerCell,
72
72
  DynamicFiltersBlock: () => DynamicFiltersBlock,
73
+ EMPTY_IF_CONDITION: () => EMPTY_IF_CONDITION,
73
74
  FORMULA_COLUMN_TYPE: () => FORMULA_COLUMN_TYPE,
75
+ FORMULA_FUNCTION_GROUPS: () => FORMULA_FUNCTION_GROUPS,
76
+ FORMULA_OPERATOR_GROUPS: () => FORMULA_OPERATOR_GROUPS,
74
77
  FORMULA_SOURCE_TYPES: () => FORMULA_SOURCE_TYPES,
78
+ FORMULA_TYPE_LABELS: () => FORMULA_TYPE_LABELS,
75
79
  LinkPickerCell: () => LinkPickerCell,
80
+ MAX_FILTER_CONDITIONS: () => MAX_FILTER_CONDITIONS,
76
81
  NON_TEXT_TYPES: () => NON_TEXT_TYPES,
77
82
  PASTEL_PALETTES: () => PASTEL_PALETTES,
78
83
  PAYMENT_REPORTS_ACTIONS: () => PAYMENT_REPORTS_ACTIONS,
@@ -108,6 +113,8 @@ __export(src_exports, {
108
113
  applyFormulaColumns: () => applyFormulaColumns,
109
114
  applyRelationCopy: () => applyRelationCopy,
110
115
  buildColumnOptions: () => buildColumnOptions,
116
+ buildIfConditionExpression: () => buildIfConditionExpression,
117
+ buildIfExpression: () => buildIfExpression,
111
118
  buildRelationCopyPatch: () => buildRelationCopyPatch,
112
119
  buildRelationRef: () => buildRelationRef,
113
120
  calculateMinColumnWidth: () => calculateMinColumnWidth,
@@ -117,11 +124,14 @@ __export(src_exports, {
117
124
  columnRefLabel: () => columnRefLabel,
118
125
  computeFormulaValue: () => computeFormulaValue,
119
126
  describePerRecordFormula: () => describePerRecordFormula,
127
+ describeRefFilter: () => describeRefFilter,
120
128
  enrichRowsWithRelations: () => enrichRowsWithRelations,
121
129
  evaluateFormula: () => evaluateFormula,
122
130
  extractFormulaFields: () => extractFormulaFields,
123
131
  extractUniqueVariableIndices: () => extractUniqueVariableIndices,
124
132
  extractVariableIndices: () => extractVariableIndices,
133
+ filterKindOfType: () => filterKindOfType,
134
+ filterSignature: () => filterSignature,
125
135
  findVariableAtPosition: () => findVariableAtPosition,
126
136
  formatConflictCellValue: () => formatConflictCellValue,
127
137
  formatFileSize: () => formatFileSize,
@@ -133,6 +143,8 @@ __export(src_exports, {
133
143
  getDefaultFilterColumns: () => getDefaultFilterColumns,
134
144
  getDeterministicPastelBadge: () => getDeterministicPastelBadge,
135
145
  getEligibleCopyTargets: () => getEligibleCopyTargets,
146
+ getFilterOps: () => getFilterOps,
147
+ getIfConditionOps: () => getIfConditionOps,
136
148
  getNextVariableIndex: () => getNextVariableIndex,
137
149
  getRelationButtonColumns: () => getRelationButtonColumns,
138
150
  getRelationCopyLockedTargets: () => getRelationCopyLockedTargets,
@@ -157,6 +169,7 @@ __export(src_exports, {
157
169
  isValidTemplateName: () => isValidTemplateName,
158
170
  isWordToken: () => isWordToken,
159
171
  makeCellRefKey: () => makeCellRefKey,
172
+ matchesRefFilter: () => matchesRefFilter,
160
173
  measureTextWidth: () => measureTextWidth,
161
174
  missingPaymentReportsFields: () => missingPaymentReportsFields,
162
175
  normalizePerRecordFormula: () => normalizePerRecordFormula,
@@ -176,6 +189,7 @@ __export(src_exports, {
176
189
  resolveUniverseTheme: () => resolveUniverseTheme,
177
190
  sanitizeDefaultFilter: () => sanitizeDefaultFilter,
178
191
  sanitizePhoneNumber: () => sanitizePhoneNumber,
192
+ sanitizeRefFilter: () => sanitizeRefFilter,
179
193
  sanitizeTemplateNameInput: () => sanitizeTemplateNameInput,
180
194
  seamlessVariableEngine: () => seamlessVariableEngine,
181
195
  splitHeaderTitle: () => splitHeaderTitle,
@@ -881,6 +895,187 @@ function formatFileSize(bytes) {
881
895
  return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
882
896
  }
883
897
 
898
+ // ../shared/src/formulaFilters.ts
899
+ var MAX_FILTER_CONDITIONS = 5;
900
+ var MAX_VALUE_LENGTH = 200;
901
+ function filterKindOfType(type) {
902
+ const t = String(type || "").toUpperCase();
903
+ if (t === "NUMBER" || t === "CURRENCY") return "number";
904
+ if (t === "DATE") return "date";
905
+ if (t === "BOOLEAN") return "boolean";
906
+ if (t === "RELATION") return "relation";
907
+ return "text";
908
+ }
909
+ var op = (id, label, needsValue = true) => ({ id, label, needsValue });
910
+ var EMPTY_OPS = [op("empty", "\u05E8\u05D9\u05E7", false), op("notempty", "\u05DC\u05D0 \u05E8\u05D9\u05E7", false)];
911
+ var OPS_BY_KIND = {
912
+ text: [
913
+ op("eq", "\u05E9\u05D5\u05D5\u05D4 \u05DC"),
914
+ op("ne", "\u05E9\u05D5\u05E0\u05D4 \u05DE"),
915
+ op("contains", "\u05DE\u05DB\u05D9\u05DC"),
916
+ op("notcontains", "\u05DC\u05D0 \u05DE\u05DB\u05D9\u05DC"),
917
+ op("starts", "\u05DE\u05EA\u05D7\u05D9\u05DC \u05D1"),
918
+ op("ends", "\u05DE\u05E1\u05EA\u05D9\u05D9\u05DD \u05D1"),
919
+ op("in", "\u05D0\u05D7\u05D3 \u05DE\u05EA\u05D5\u05DA (\u05DE\u05D5\u05E4\u05E8\u05D3\u05D9\u05DD \u05D1\u05E4\u05E1\u05D9\u05E7)"),
920
+ ...EMPTY_OPS
921
+ ],
922
+ relation: [
923
+ op("eq", "\u05E9\u05D5\u05D5\u05D4 \u05DC"),
924
+ op("ne", "\u05E9\u05D5\u05E0\u05D4 \u05DE"),
925
+ op("contains", "\u05DE\u05DB\u05D9\u05DC"),
926
+ op("in", "\u05D0\u05D7\u05D3 \u05DE\u05EA\u05D5\u05DA (\u05DE\u05D5\u05E4\u05E8\u05D3\u05D9\u05DD \u05D1\u05E4\u05E1\u05D9\u05E7)"),
927
+ ...EMPTY_OPS
928
+ ],
929
+ number: [
930
+ op("eq", "\u05E9\u05D5\u05D5\u05D4 \u05DC"),
931
+ op("ne", "\u05E9\u05D5\u05E0\u05D4 \u05DE"),
932
+ op("gt", "\u05D2\u05D3\u05D5\u05DC \u05DE"),
933
+ op("lt", "\u05E7\u05D8\u05DF \u05DE"),
934
+ op("ge", "\u05D2\u05D3\u05D5\u05DC \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4 \u05DC"),
935
+ op("le", "\u05E7\u05D8\u05DF \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4 \u05DC"),
936
+ ...EMPTY_OPS
937
+ ],
938
+ date: [
939
+ op("eq", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA"),
940
+ op("ne", "\u05DC\u05D0 \u05D1\u05EA\u05D0\u05E8\u05D9\u05DA"),
941
+ op("gt", "\u05D0\u05D7\u05E8\u05D9"),
942
+ op("lt", "\u05DC\u05E4\u05E0\u05D9"),
943
+ op("ge", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA \u05D0\u05D5 \u05D0\u05D7\u05E8\u05D9\u05D5"),
944
+ op("le", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA \u05D0\u05D5 \u05DC\u05E4\u05E0\u05D9\u05D5"),
945
+ ...EMPTY_OPS
946
+ ],
947
+ boolean: [op("yes", "\u05DE\u05E1\u05D5\u05DE\u05DF (\u05DB\u05DF)", false), op("no", "\u05DC\u05D0 \u05DE\u05E1\u05D5\u05DE\u05DF (\u05DC\u05D0)", false)]
948
+ };
949
+ var getFilterOps = (type) => OPS_BY_KIND[filterKindOfType(type)];
950
+ var ALL_OPS = new Set(Object.values(OPS_BY_KIND).flat().map((o) => o.id));
951
+ var NEEDS_VALUE = new Map(Object.values(OPS_BY_KIND).flat().map((o) => [o.id, o.needsValue]));
952
+ function sanitizeRefFilter(raw) {
953
+ if (!raw || typeof raw !== "object") return null;
954
+ const r = raw;
955
+ if (!Array.isArray(r.conditions)) return null;
956
+ const conditions = [];
957
+ for (const c of r.conditions.slice(0, MAX_FILTER_CONDITIONS)) {
958
+ if (!c || typeof c !== "object") continue;
959
+ const field = typeof c.field === "string" ? c.field.trim().slice(0, MAX_VALUE_LENGTH) : "";
960
+ const opId = typeof c.op === "string" ? c.op : "";
961
+ if (!field || !ALL_OPS.has(opId)) continue;
962
+ const needs = NEEDS_VALUE.get(opId) === true;
963
+ const value = typeof c.value === "string" ? c.value.trim().slice(0, MAX_VALUE_LENGTH) : typeof c.value === "number" ? String(c.value) : "";
964
+ if (needs && value === "") continue;
965
+ conditions.push({ field, op: opId, value: needs ? value : "" });
966
+ }
967
+ if (conditions.length === 0) return null;
968
+ return { join: r.join === "OR" ? "OR" : "AND", conditions };
969
+ }
970
+ function filterSignature(f) {
971
+ var _a;
972
+ if (!f || !((_a = f.conditions) == null ? void 0 : _a.length)) return "";
973
+ return `${f.join}:${f.conditions.map((c) => `${c.field}${c.op}${c.value}`).join("")}`;
974
+ }
975
+ var SHORT_LABELS = {
976
+ eq: "=",
977
+ ne: "\u2260",
978
+ gt: ">",
979
+ lt: "<",
980
+ ge: "\u2265",
981
+ le: "\u2264",
982
+ contains: "\u05DE\u05DB\u05D9\u05DC",
983
+ notcontains: "\u05DC\u05D0 \u05DE\u05DB\u05D9\u05DC",
984
+ starts: "\u05DE\u05EA\u05D7\u05D9\u05DC \u05D1",
985
+ ends: "\u05DE\u05E1\u05EA\u05D9\u05D9\u05DD \u05D1",
986
+ in: "\u05D0\u05D7\u05D3 \u05DE\u05EA\u05D5\u05DA",
987
+ empty: "\u05E8\u05D9\u05E7",
988
+ notempty: "\u05DC\u05D0 \u05E8\u05D9\u05E7",
989
+ yes: "\u05DE\u05E1\u05D5\u05DE\u05DF",
990
+ no: "\u05DC\u05D0 \u05DE\u05E1\u05D5\u05DE\u05DF"
991
+ };
992
+ function describeRefFilter(f) {
993
+ var _a;
994
+ if (!f || !((_a = f.conditions) == null ? void 0 : _a.length)) return "";
995
+ const sep = f.join === "OR" ? " \u05D0\u05D5 " : " \u05D5\u05D2\u05DD ";
996
+ return f.conditions.map((c) => {
997
+ const needs = NEEDS_VALUE.get(c.op) === true;
998
+ return needs ? `${c.field} ${SHORT_LABELS[c.op]} ${c.value}` : `${c.field} ${SHORT_LABELS[c.op]}`;
999
+ }).join(sep);
1000
+ }
1001
+ var norm = (x) => String(x != null ? x : "").trim().toLowerCase();
1002
+ var isBlankValue = (x) => x === null || x === void 0 || typeof x === "string" && x.trim() === "" || Array.isArray(x) && x.every(isBlankValue);
1003
+ var isoDate = (x) => typeof x === "string" && /^\d{4}-\d{2}-\d{2}/.test(x.trim()) ? x.trim().slice(0, 10) : null;
1004
+ var numOf = (x) => {
1005
+ if (typeof x === "number") return Number.isFinite(x) ? x : null;
1006
+ if (typeof x === "string" && x.trim() !== "") {
1007
+ const n = Number(x);
1008
+ return Number.isFinite(n) ? n : null;
1009
+ }
1010
+ return null;
1011
+ };
1012
+ function compare(l, r) {
1013
+ const ld = isoDate(l), rd = isoDate(r);
1014
+ if (ld && rd) return ld < rd ? -1 : ld > rd ? 1 : 0;
1015
+ const ln = numOf(l), rn = numOf(r);
1016
+ if (ln !== null && rn !== null) return ln < rn ? -1 : ln > rn ? 1 : 0;
1017
+ const a = norm(l), b = norm(r);
1018
+ return a < b ? -1 : a > b ? 1 : 0;
1019
+ }
1020
+ var NEGATED = { ne: "eq", notcontains: "contains" };
1021
+ function matchSingle(opId, v, target) {
1022
+ switch (opId) {
1023
+ case "eq":
1024
+ return !isBlankValue(v) && compare(v, target) === 0;
1025
+ case "ne":
1026
+ return isBlankValue(v) || compare(v, target) !== 0;
1027
+ case "contains":
1028
+ return norm(v).includes(norm(target));
1029
+ case "notcontains":
1030
+ return !norm(v).includes(norm(target));
1031
+ case "starts":
1032
+ return norm(v).startsWith(norm(target));
1033
+ case "ends":
1034
+ return norm(v).endsWith(norm(target));
1035
+ case "in": {
1036
+ const list = target.split(",").map(norm).filter(Boolean);
1037
+ return list.includes(norm(v));
1038
+ }
1039
+ case "gt":
1040
+ return !isBlankValue(v) && compare(v, target) > 0;
1041
+ case "lt":
1042
+ return !isBlankValue(v) && compare(v, target) < 0;
1043
+ case "ge":
1044
+ return !isBlankValue(v) && compare(v, target) >= 0;
1045
+ case "le":
1046
+ return !isBlankValue(v) && compare(v, target) <= 0;
1047
+ case "empty":
1048
+ return isBlankValue(v);
1049
+ case "notempty":
1050
+ return !isBlankValue(v);
1051
+ case "yes": {
1052
+ if (v === true) return true;
1053
+ if (typeof v === "number") return v !== 0;
1054
+ return ["true", "1", "yes", "\u05DB\u05DF"].includes(norm(v));
1055
+ }
1056
+ case "no":
1057
+ return !matchSingle("yes", v, target);
1058
+ default:
1059
+ return false;
1060
+ }
1061
+ }
1062
+ function matchCondition(c, raw) {
1063
+ if (c.op === "empty") return isBlankValue(raw);
1064
+ if (c.op === "notempty") return !isBlankValue(raw);
1065
+ if (Array.isArray(raw)) {
1066
+ const positive = NEGATED[c.op];
1067
+ if (positive) return !raw.some((item) => matchSingle(positive, item, c.value));
1068
+ return raw.some((item) => matchSingle(c.op, item, c.value));
1069
+ }
1070
+ return matchSingle(c.op, raw, c.value);
1071
+ }
1072
+ function matchesRefFilter(filter, getValue) {
1073
+ var _a;
1074
+ if (!filter || !((_a = filter.conditions) == null ? void 0 : _a.length)) return true;
1075
+ const test = (c) => matchCondition(c, getValue(c.field));
1076
+ return filter.join === "OR" ? filter.conditions.some(test) : filter.conditions.every(test);
1077
+ }
1078
+
884
1079
  // ../shared/src/formula.ts
885
1080
  var FORMULA_COLUMN_TYPE = "FORMULA";
886
1081
  var FORMULA_SOURCE_TYPES = ["NUMBER", "CURRENCY", "STRING", "BOOLEAN", "DATE", "TIME", "PHONE"];
@@ -907,7 +1102,10 @@ function makeCellRefKey(existing, field, agg) {
907
1102
  for (let n = 2; (existing || []).some((r) => r.key === key); n++) key = `${base} (${n})`;
908
1103
  return key;
909
1104
  }
910
- var perRecordRefValueKey = (r) => `ref:${r.tableId}::${r.agg ? "" : r.recordId || ""}::${r.field}::${r.agg || ""}`;
1105
+ var perRecordRefValueKey = (r) => {
1106
+ const sig = r.agg ? filterSignature(r.filter) : "";
1107
+ return `ref:${r.tableId}::${r.agg ? "" : r.recordId || ""}::${r.field}::${r.agg || ""}${sig ? `::${sig}` : ""}`;
1108
+ };
911
1109
  function sanitizeRefs(raw) {
912
1110
  var _a, _b, _c, _d;
913
1111
  if (!Array.isArray(raw)) return [];
@@ -920,15 +1118,16 @@ function sanitizeRefs(raw) {
920
1118
  const field = typeof r.field === "string" ? r.field : "";
921
1119
  if (!key.startsWith("@") || seen.has(key) || !tableId || !field) continue;
922
1120
  const agg = PER_RECORD_SOURCE_AGGS.includes(r.agg) ? r.agg : void 0;
1121
+ const filter = agg ? sanitizeRefFilter(r.filter) : null;
923
1122
  seen.add(key);
924
- out.push(__spreadValues({
1123
+ out.push(__spreadValues(__spreadValues({
925
1124
  key,
926
1125
  tableId,
927
1126
  tableName: String((_b = r.tableName) != null ? _b : "").slice(0, 200),
928
1127
  recordId: agg ? "" : String((_c = r.recordId) != null ? _c : ""),
929
1128
  recordLabel: agg ? "" : String((_d = r.recordLabel) != null ? _d : "").slice(0, 200),
930
1129
  field
931
- }, agg ? { agg } : {}));
1130
+ }, agg ? { agg } : {}), filter ? { filter } : {}));
932
1131
  }
933
1132
  return out;
934
1133
  }
@@ -955,7 +1154,8 @@ function describePerRecordFormula(f) {
955
1154
  if (!f) return "";
956
1155
  let text2 = f.expression;
957
1156
  for (const r of f.refs) {
958
- const path = r.agg ? `${PER_RECORD_AGG_LABELS[r.agg]} ${r.tableName} \u203A ${r.field}` : `${r.tableName} \u203A ${r.recordLabel || "\u05E8\u05E9\u05D5\u05DE\u05D4"} \u203A ${r.field}`;
1157
+ const filterText = r.agg ? describeRefFilter(r.filter) : "";
1158
+ const path = r.agg ? `${PER_RECORD_AGG_LABELS[r.agg]} ${r.tableName} \u203A ${r.field}${filterText ? ` [${filterText}]` : ""}` : `${r.tableName} \u203A ${r.recordLabel || "\u05E8\u05E9\u05D5\u05DE\u05D4"} \u203A ${r.field}`;
959
1159
  text2 = text2.split(`{${r.key}}`).join(`[${path}]`);
960
1160
  }
961
1161
  return text2;
@@ -1005,10 +1205,10 @@ function tokenize(src) {
1005
1205
  i += id[0].length;
1006
1206
  continue;
1007
1207
  }
1008
- const op = /^(>=|<=|<>|[-+*/%(),<>=&])/.exec(rest);
1009
- if (op) {
1010
- toks.push({ k: "op", v: op[0] });
1011
- i += op[0].length;
1208
+ const op2 = /^(>=|<=|<>|[-+*/%(),<>=&])/.exec(rest);
1209
+ if (op2) {
1210
+ toks.push({ k: "op", v: op2[0] });
1211
+ i += op2[0].length;
1012
1212
  continue;
1013
1213
  }
1014
1214
  throw new Error(`\u05EA\u05D5 \u05DC\u05D0 \u05DE\u05D5\u05DB\u05E8 \u05D1\u05E0\u05D5\u05E1\u05D7\u05D4: ${c}`);
@@ -1022,7 +1222,7 @@ var toNum = (x) => {
1022
1222
  };
1023
1223
  var isBlank = (x) => x === null || x === void 0 || x === "";
1024
1224
  var isNumLike = (x) => typeof x === "number" || typeof x === "string" && x.trim() !== "" && Number.isFinite(Number(x));
1025
- var isoDate = (x) => typeof x === "string" && /^\d{4}-\d{2}-\d{2}/.test(x) ? x.slice(0, 10) : null;
1225
+ var isoDate2 = (x) => typeof x === "string" && /^\d{4}-\d{2}-\d{2}/.test(x) ? x.slice(0, 10) : null;
1026
1226
  var truthy = (x) => {
1027
1227
  if (x === null || x === "") return false;
1028
1228
  if (typeof x === "number") return x !== 0 && !Number.isNaN(x);
@@ -1031,7 +1231,7 @@ var truthy = (x) => {
1031
1231
  };
1032
1232
  var text = (x) => String(x != null ? x : "").trim().toLowerCase();
1033
1233
  function compareValues(l, r) {
1034
- const ld = isoDate(l), rd = isoDate(r);
1234
+ const ld = isoDate2(l), rd = isoDate2(r);
1035
1235
  if (ld && rd) return ld < rd ? -1 : ld > rd ? 1 : 0;
1036
1236
  if ((isNumLike(l) || isBlank(l)) && (isNumLike(r) || isBlank(r))) {
1037
1237
  const a2 = toNum(l), b2 = toNum(r);
@@ -1072,9 +1272,9 @@ function evaluateFormula(expr, getField) {
1072
1272
  function parseExpr() {
1073
1273
  let l = parseConcat();
1074
1274
  while (isOp(">", "<", ">=", "<=", "=", "<>")) {
1075
- const op = eat().v, r = parseConcat();
1275
+ const op2 = eat().v, r = parseConcat();
1076
1276
  const c = compareValues(l, r);
1077
- l = (op === ">" ? c > 0 : op === "<" ? c < 0 : op === ">=" ? c >= 0 : op === "<=" ? c <= 0 : op === "=" ? c === 0 : c !== 0) ? 1 : 0;
1277
+ l = (op2 === ">" ? c > 0 : op2 === "<" ? c < 0 : op2 === ">=" ? c >= 0 : op2 === "<=" ? c <= 0 : op2 === "=" ? c === 0 : c !== 0) ? 1 : 0;
1078
1278
  }
1079
1279
  return l;
1080
1280
  }
@@ -1090,17 +1290,17 @@ function evaluateFormula(expr, getField) {
1090
1290
  function parseAdd() {
1091
1291
  let l = parseMul();
1092
1292
  while (isOp("+", "-")) {
1093
- const op = eat().v, r = parseMul();
1094
- l = op === "+" ? toNum(l) + toNum(r) : toNum(l) - toNum(r);
1293
+ const op2 = eat().v, r = parseMul();
1294
+ l = op2 === "+" ? toNum(l) + toNum(r) : toNum(l) - toNum(r);
1095
1295
  }
1096
1296
  return l;
1097
1297
  }
1098
1298
  function parseMul() {
1099
1299
  let l = parseUnary();
1100
1300
  while (isOp("*", "/", "%")) {
1101
- const op = eat().v, r = parseUnary();
1301
+ const op2 = eat().v, r = parseUnary();
1102
1302
  const a = toNum(l), b = toNum(r);
1103
- l = op === "*" ? a * b : op === "/" ? a / b : a % b;
1303
+ l = op2 === "*" ? a * b : op2 === "/" ? a / b : a % b;
1104
1304
  }
1105
1305
  return l;
1106
1306
  }
@@ -1184,7 +1384,7 @@ function evaluateFormula(expr, getField) {
1184
1384
  case "ISEMPTY":
1185
1385
  return isBlank(args[0]) ? 1 : 0;
1186
1386
  case "DAYS": {
1187
- const a = isoDate(args[0]), b = isoDate(args[1]);
1387
+ const a = isoDate2(args[0]), b = isoDate2(args[1]);
1188
1388
  return a && b ? Math.round((dayNumber(a) - dayNumber(b)) / 864e5) : null;
1189
1389
  }
1190
1390
  default:
@@ -1366,6 +1566,156 @@ function cleanPlacementFilterDefaults(defaults, allowedKeys) {
1366
1566
  return Object.keys(out).length > 0 ? out : void 0;
1367
1567
  }
1368
1568
 
1569
+ // ../shared/src/formulaBuilder.ts
1570
+ var FORMULA_TYPE_LABELS = {
1571
+ NUMBER: "\u05DE\u05E1\u05E4\u05E8",
1572
+ CURRENCY: "\u05DE\u05D8\u05D1\u05E2",
1573
+ STRING: "\u05D8\u05E7\u05E1\u05D8",
1574
+ PHONE: "\u05D8\u05DC\u05E4\u05D5\u05DF",
1575
+ TIME: "\u05E9\u05E2\u05D4",
1576
+ DATE: "\u05EA\u05D0\u05E8\u05D9\u05DA",
1577
+ BOOLEAN: "\u05DB\u05DF/\u05DC\u05D0"
1578
+ };
1579
+ var FORMULA_OPERATOR_GROUPS = [
1580
+ {
1581
+ label: "\u05D7\u05E9\u05D1\u05D5\u05DF",
1582
+ items: [
1583
+ { label: "+ \u05D7\u05D9\u05D1\u05D5\u05E8", insert: " + " },
1584
+ { label: "- \u05D7\u05D9\u05E1\u05D5\u05E8", insert: " - " },
1585
+ { label: "* \u05DB\u05E4\u05DC", insert: " * " },
1586
+ { label: "/ \u05D7\u05D9\u05DC\u05D5\u05E7", insert: " / " },
1587
+ { label: "% \u05E9\u05D0\u05E8\u05D9\u05EA", insert: " % " }
1588
+ ]
1589
+ },
1590
+ {
1591
+ label: "\u05D4\u05E9\u05D5\u05D5\u05D0\u05D4",
1592
+ items: [
1593
+ { label: "= \u05E9\u05D5\u05D5\u05D4", insert: " = " },
1594
+ { label: "<> \u05E9\u05D5\u05E0\u05D4", insert: " <> " },
1595
+ { label: "> \u05D2\u05D3\u05D5\u05DC \u05DE", insert: " > " },
1596
+ { label: "< \u05E7\u05D8\u05DF \u05DE", insert: " < " },
1597
+ { label: ">= \u05D2\u05D3\u05D5\u05DC \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4", insert: " >= " },
1598
+ { label: "<= \u05E7\u05D8\u05DF \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4", insert: " <= " }
1599
+ ]
1600
+ },
1601
+ { label: "\u05D8\u05E7\u05E1\u05D8", items: [{ label: "& \u05D7\u05D9\u05D1\u05D5\u05E8 \u05D8\u05E7\u05E1\u05D8\u05D9\u05DD", insert: " & " }] },
1602
+ {
1603
+ label: "\u05E1\u05D9\u05DE\u05E0\u05D9\u05DD",
1604
+ items: [
1605
+ { label: "( \u05E4\u05EA\u05D9\u05D7\u05EA \u05E1\u05D5\u05D2\u05E8\u05D9\u05D9\u05DD", insert: "(" },
1606
+ { label: ") \u05E1\u05D2\u05D9\u05E8\u05EA \u05E1\u05D5\u05D2\u05E8\u05D9\u05D9\u05DD", insert: ")" },
1607
+ { label: ", \u05E4\u05E1\u05D9\u05E7 (\u05D1\u05D9\u05DF \u05E4\u05E8\u05DE\u05D8\u05E8\u05D9\u05DD)", insert: ", " }
1608
+ ]
1609
+ }
1610
+ ];
1611
+ var FORMULA_FUNCTION_GROUPS = [
1612
+ {
1613
+ label: "\u05EA\u05E0\u05D0\u05D9\u05DD \u05D5\u05DC\u05D5\u05D2\u05D9\u05E7\u05D4",
1614
+ items: [
1615
+ { label: "\u05D0\u05DD... \u05D0\u05D6... \u05D0\u05D7\u05E8\u05EA (IF)", template: "IF(, , )" },
1616
+ { label: "\u05D5\u05D2\u05DD (AND)", template: "AND(, )" },
1617
+ { label: "\u05D0\u05D5 (OR)", template: "OR(, )" },
1618
+ { label: "\u05DC\u05D0 (NOT)", template: "NOT()" },
1619
+ { label: "\u05D4\u05D0\u05DD \u05E8\u05D9\u05E7 (ISEMPTY)", template: "ISEMPTY()" }
1620
+ ]
1621
+ },
1622
+ {
1623
+ label: "\u05D8\u05E7\u05E1\u05D8",
1624
+ items: [
1625
+ { label: "\u05DE\u05DB\u05D9\u05DC \u05D8\u05E7\u05E1\u05D8 (CONTAINS)", template: 'CONTAINS(, "")' },
1626
+ { label: "\u05DE\u05EA\u05D7\u05D9\u05DC \u05D1 (STARTSWITH)", template: 'STARTSWITH(, "")' },
1627
+ { label: "\u05DE\u05E1\u05EA\u05D9\u05D9\u05DD \u05D1 (ENDSWITH)", template: 'ENDSWITH(, "")' }
1628
+ ]
1629
+ },
1630
+ {
1631
+ label: "\u05DE\u05E1\u05E4\u05E8\u05D9\u05DD",
1632
+ items: [
1633
+ { label: "\u05E2\u05D9\u05D2\u05D5\u05DC (ROUND)", template: "ROUND(, 2)" },
1634
+ { label: "\u05DE\u05D9\u05E0\u05D9\u05DE\u05D5\u05DD (MIN)", template: "MIN(, )" },
1635
+ { label: "\u05DE\u05E7\u05E1\u05D9\u05DE\u05D5\u05DD (MAX)", template: "MAX(, )" },
1636
+ { label: "\u05E2\u05E8\u05DA \u05DE\u05D5\u05D7\u05DC\u05D8 (ABS)", template: "ABS()" }
1637
+ ]
1638
+ },
1639
+ {
1640
+ label: "\u05EA\u05D0\u05E8\u05D9\u05DB\u05D9\u05DD",
1641
+ items: [{ label: "\u05D4\u05E4\u05E8\u05E9 \u05D9\u05DE\u05D9\u05DD (DAYS: \u05E1\u05D5\u05E3, \u05D4\u05EA\u05D7\u05DC\u05D4)", template: "DAYS(, )" }]
1642
+ }
1643
+ ];
1644
+ var NUMBER_RE = /^-?\d+(\.\d+)?$/;
1645
+ var isFieldRef = (s) => s.startsWith("{") && s.endsWith("}");
1646
+ var quote = (s) => `"${s.replace(/"/g, "")}"`;
1647
+ var literal = (raw, forceText) => {
1648
+ const v = raw.trim();
1649
+ if (isFieldRef(v)) return v;
1650
+ if (!forceText && NUMBER_RE.test(v)) return v;
1651
+ return quote(v);
1652
+ };
1653
+ var operand = (raw) => {
1654
+ const v = raw.trim();
1655
+ if (v === "") return '""';
1656
+ if (isFieldRef(v) || NUMBER_RE.test(v)) return v;
1657
+ return quote(v);
1658
+ };
1659
+ var cmp = (id, label, sym) => ({
1660
+ id,
1661
+ label,
1662
+ needsValue: true,
1663
+ build: (f, v, t) => `${f} ${sym} ${literal(v, t)}`
1664
+ });
1665
+ var EMPTY_OPS2 = [
1666
+ { id: "empty", label: "\u05E8\u05D9\u05E7", needsValue: false, build: (f) => `ISEMPTY(${f})` },
1667
+ { id: "notempty", label: "\u05DC\u05D0 \u05E8\u05D9\u05E7", needsValue: false, build: (f) => `NOT(ISEMPTY(${f}))` }
1668
+ ];
1669
+ var NUMBER_OPS = [
1670
+ cmp("eq", "\u05E9\u05D5\u05D5\u05D4 \u05DC", "="),
1671
+ cmp("ne", "\u05E9\u05D5\u05E0\u05D4 \u05DE", "<>"),
1672
+ cmp("gt", "\u05D2\u05D3\u05D5\u05DC \u05DE", ">"),
1673
+ cmp("lt", "\u05E7\u05D8\u05DF \u05DE", "<"),
1674
+ cmp("ge", "\u05D2\u05D3\u05D5\u05DC \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4 \u05DC", ">="),
1675
+ cmp("le", "\u05E7\u05D8\u05DF \u05D0\u05D5 \u05E9\u05D5\u05D5\u05D4 \u05DC", "<="),
1676
+ ...EMPTY_OPS2
1677
+ ];
1678
+ var DATE_OPS = [
1679
+ cmp("eq", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA", "="),
1680
+ cmp("ne", "\u05DC\u05D0 \u05D1\u05EA\u05D0\u05E8\u05D9\u05DA", "<>"),
1681
+ cmp("gt", "\u05D0\u05D7\u05E8\u05D9", ">"),
1682
+ cmp("lt", "\u05DC\u05E4\u05E0\u05D9", "<"),
1683
+ cmp("ge", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA \u05D0\u05D5 \u05D0\u05D7\u05E8\u05D9\u05D5", ">="),
1684
+ cmp("le", "\u05D1\u05EA\u05D0\u05E8\u05D9\u05DA \u05D0\u05D5 \u05DC\u05E4\u05E0\u05D9\u05D5", "<="),
1685
+ ...EMPTY_OPS2
1686
+ ];
1687
+ var TEXT_OPS = [
1688
+ cmp("eq", "\u05E9\u05D5\u05D5\u05D4 \u05DC", "="),
1689
+ cmp("ne", "\u05E9\u05D5\u05E0\u05D4 \u05DE", "<>"),
1690
+ { id: "contains", label: "\u05DE\u05DB\u05D9\u05DC", needsValue: true, build: (f, v) => `CONTAINS(${f}, ${literal(v, true)})` },
1691
+ { id: "starts", label: "\u05DE\u05EA\u05D7\u05D9\u05DC \u05D1", needsValue: true, build: (f, v) => `STARTSWITH(${f}, ${literal(v, true)})` },
1692
+ { id: "ends", label: "\u05DE\u05E1\u05EA\u05D9\u05D9\u05DD \u05D1", needsValue: true, build: (f, v) => `ENDSWITH(${f}, ${literal(v, true)})` },
1693
+ ...EMPTY_OPS2
1694
+ ];
1695
+ var BOOL_OPS = [
1696
+ { id: "yes", label: "\u05DE\u05E1\u05D5\u05DE\u05DF (\u05DB\u05DF)", needsValue: false, build: (f) => `${f} = 1` },
1697
+ { id: "no", label: "\u05DC\u05D0 \u05DE\u05E1\u05D5\u05DE\u05DF (\u05DC\u05D0)", needsValue: false, build: (f) => `${f} = 0` }
1698
+ ];
1699
+ var opsOf = (type) => type === "NUMBER" || type === "CURRENCY" ? NUMBER_OPS : type === "DATE" ? DATE_OPS : type === "BOOLEAN" ? BOOL_OPS : TEXT_OPS;
1700
+ var isTextual = (type) => !(type === "NUMBER" || type === "CURRENCY");
1701
+ var EMPTY_IF_CONDITION = { field: "", op: "", value: "" };
1702
+ var getIfConditionOps = (type) => opsOf(type).map(({ id, label, needsValue }) => ({ id, label, needsValue }));
1703
+ function buildIfConditionExpression(columns, c) {
1704
+ const col = columns.find((x) => x.name === c.field);
1705
+ if (!col) return null;
1706
+ const op2 = opsOf(col.type).find((o) => o.id === c.op);
1707
+ if (!op2) return null;
1708
+ if (op2.needsValue && !c.value.trim()) return null;
1709
+ return op2.build(`{${col.name}}`, c.value.trim(), isTextual(col.type));
1710
+ }
1711
+ function buildIfExpression(columns, conds, joiner, thenVal, elseVal) {
1712
+ const parts = conds.map((c) => buildIfConditionExpression(columns, c));
1713
+ if (parts.some((p) => p === null)) return { error: "\u05D9\u05E9 \u05DC\u05D4\u05E9\u05DC\u05D9\u05DD \u05D1\u05DB\u05DC \u05EA\u05E0\u05D0\u05D9: \u05E9\u05D3\u05D4, \u05E4\u05E2\u05D5\u05DC\u05D4 \u05D5\u05E2\u05E8\u05DA" };
1714
+ const condText = parts.length === 1 ? parts[0] : `${joiner}(${parts.join(", ")})`;
1715
+ if (thenVal === void 0 || elseVal === void 0) return { expression: condText };
1716
+ return { expression: `IF(${condText}, ${operand(thenVal)}, ${operand(elseVal)})` };
1717
+ }
1718
+
1369
1719
  // src/components/TableBlock/TableBlockClient.tsx
1370
1720
  var import_react19 = __toESM(require("react"));
1371
1721
 
@@ -4025,6 +4375,8 @@ var NUMERIC = ["NUMBER", "CURRENCY"];
4025
4375
  var OPS = ["+", "-", "*", "/", "(", ")"];
4026
4376
  var selCls = "w-full px-2 py-1 border border-slate-300 rounded text-sm bg-white";
4027
4377
  var lblCls = "block text-[11px] text-slate-600 mb-0.5";
4378
+ var smallBtn = "px-2 py-1 text-xs rounded border border-slate-300 bg-white hover:bg-slate-100";
4379
+ var typeLabel = (t) => FORMULA_TYPE_LABELS[t] || (t === "RELATION" ? "\u05E7\u05E9\u05E8" : t === "FORMULA" ? "\u05E0\u05D5\u05E1\u05D7\u05D4" : t);
4028
4380
  var RecordPicker = ({ tableId, executeAction, value, onPick }) => {
4029
4381
  const [q, setQ] = (0, import_react9.useState)("");
4030
4382
  const [options, setOptions] = (0, import_react9.useState)([]);
@@ -4072,7 +4424,88 @@ var RecordPicker = ({ tableId, executeAction, value, onPick }) => {
4072
4424
  )
4073
4425
  ] });
4074
4426
  };
4075
- var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear, onClose }) => {
4427
+ var FilterEditor = ({ columns, conds, join, onChange }) => {
4428
+ const update = (i, patch) => onChange(conds.map((c, idx) => idx === i ? __spreadValues(__spreadValues({}, c), patch) : c), join);
4429
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "space-y-1.5 pt-1.5 border-t border-slate-200", children: [
4430
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05E1\u05D9\u05E0\u05D5\u05DF: \u05DC\u05E1\u05DB\u05DD \u05E8\u05E7 \u05E8\u05E9\u05D5\u05DE\u05D5\u05EA \u05E9\u05E2\u05D5\u05DE\u05D3\u05D5\u05EA \u05D1\u05EA\u05E0\u05D0\u05D9 (\u05DC\u05D0 \u05D7\u05D5\u05D1\u05D4)" }),
4431
+ conds.map((c, i) => {
4432
+ const col = columns.find((x) => x.name === c.field);
4433
+ const ops = col ? getFilterOps(col.type) : [];
4434
+ const op2 = ops.find((o) => o.id === c.op);
4435
+ const kind = filterKindOfType(col == null ? void 0 : col.type);
4436
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "space-y-1 p-1.5 rounded bg-white border border-slate-200", children: [
4437
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex items-center justify-between", children: [
4438
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "text-[11px] font-medium text-slate-600", children: i === 0 ? "\u05E8\u05E7 \u05D0\u05DD" : join === "AND" ? "\u05D5\u05D2\u05DD" : "\u05D0\u05D5" }),
4439
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4440
+ "button",
4441
+ {
4442
+ type: "button",
4443
+ className: "text-[11px] text-rose-600 hover:underline",
4444
+ onClick: () => onChange(conds.filter((_, idx) => idx !== i), join),
4445
+ children: "\u05D4\u05E1\u05E8"
4446
+ }
4447
+ )
4448
+ ] }),
4449
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4450
+ "select",
4451
+ {
4452
+ className: selCls,
4453
+ value: c.field,
4454
+ onChange: (e) => update(i, { field: e.target.value, op: "", value: "" }),
4455
+ children: [
4456
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05E9\u05D3\u05D4 --" }),
4457
+ columns.map((x) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("option", { value: x.name, children: [
4458
+ x.name,
4459
+ " (",
4460
+ typeLabel(x.type),
4461
+ ")"
4462
+ ] }, x.name))
4463
+ ]
4464
+ }
4465
+ ),
4466
+ col && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: c.op, onChange: (e) => update(i, { op: e.target.value, value: "" }), children: [
4467
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05E4\u05E2\u05D5\u05DC\u05D4 --" }),
4468
+ ops.map((o) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: o.id, children: o.label }, o.id))
4469
+ ] }),
4470
+ (op2 == null ? void 0 : op2.needsValue) && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4471
+ "input",
4472
+ {
4473
+ className: selCls,
4474
+ type: kind === "date" ? "date" : "text",
4475
+ value: c.value,
4476
+ onChange: (e) => update(i, { value: e.target.value }),
4477
+ placeholder: kind === "relation" ? "\u05D4\u05E2\u05E8\u05DA \u05DB\u05E4\u05D9 \u05E9\u05DE\u05D5\u05E6\u05D2 \u05D1\u05D8\u05D1\u05DC\u05D4" : "\u05E2\u05E8\u05DA \u05DC\u05D4\u05E9\u05D5\u05D5\u05D0\u05D4"
4478
+ }
4479
+ )
4480
+ ] }, i);
4481
+ }),
4482
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex items-center gap-2", children: [
4483
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4484
+ "button",
4485
+ {
4486
+ type: "button",
4487
+ disabled: conds.length >= MAX_FILTER_CONDITIONS,
4488
+ className: smallBtn,
4489
+ onClick: () => onChange([...conds, { field: "", op: "", value: "" }], join),
4490
+ children: "+ \u05D4\u05D5\u05E1\u05E3 \u05EA\u05E0\u05D0\u05D9 \u05E1\u05D9\u05E0\u05D5\u05DF"
4491
+ }
4492
+ ),
4493
+ conds.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4494
+ "select",
4495
+ {
4496
+ className: "px-2 py-1 border border-slate-300 rounded text-xs bg-white",
4497
+ value: join,
4498
+ onChange: (e) => onChange(conds, e.target.value),
4499
+ children: [
4500
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "AND", children: "\u05DB\u05DC \u05D4\u05EA\u05E0\u05D0\u05D9\u05DD (\u05D5\u05D2\u05DD)" }),
4501
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "OR", children: "\u05DC\u05E4\u05D7\u05D5\u05EA \u05D0\u05D7\u05D3 (\u05D0\u05D5)" })
4502
+ ]
4503
+ }
4504
+ )
4505
+ ] })
4506
+ ] });
4507
+ };
4508
+ var CellFormulaEditor = ({ initial, rowColumns, context, anchor, onApply, onClear, onClose }) => {
4076
4509
  const executeAction = context == null ? void 0 : context.executeAction;
4077
4510
  const taRef = (0, import_react9.useRef)(null);
4078
4511
  const [expression, setExpression] = (0, import_react9.useState)((initial == null ? void 0 : initial.expression) || "");
@@ -4085,6 +4518,13 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4085
4518
  const [rec, setRec] = (0, import_react9.useState)({ id: "", label: "" });
4086
4519
  const [fld, setFld] = (0, import_react9.useState)("");
4087
4520
  const [agg, setAgg] = (0, import_react9.useState)("SUM");
4521
+ const [fConds, setFConds] = (0, import_react9.useState)([]);
4522
+ const [fJoin, setFJoin] = (0, import_react9.useState)("AND");
4523
+ const [ifConds, setIfConds] = (0, import_react9.useState)([__spreadValues({}, EMPTY_IF_CONDITION)]);
4524
+ const [ifJoin, setIfJoin] = (0, import_react9.useState)("AND");
4525
+ const [thenVal, setThenVal] = (0, import_react9.useState)("");
4526
+ const [elseVal, setElseVal] = (0, import_react9.useState)("");
4527
+ const [builderError, setBuilderError] = (0, import_react9.useState)("");
4088
4528
  (0, import_react9.useEffect)(() => {
4089
4529
  (async () => {
4090
4530
  var _a, _b;
@@ -4099,8 +4539,11 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4099
4539
  })();
4100
4540
  }, [executeAction]);
4101
4541
  const table = tables == null ? void 0 : tables.find((t) => t.id === tId);
4102
- const tableFields = ((table == null ? void 0 : table.columns) || []).filter((c) => panel === "agg" ? NUMERIC.includes(c.type) : true);
4103
- const insert = (text2) => {
4542
+ const valueFields = ((table == null ? void 0 : table.columns) || []).filter(
4543
+ (c) => FORMULA_SOURCE_TYPES.includes(c.type) && (panel === "agg" ? NUMERIC.includes(c.type) : true)
4544
+ );
4545
+ const rowFieldNames = (0, import_react9.useMemo)(() => rowColumns.map((c) => c.name), [rowColumns]);
4546
+ const insert = (text2, caret) => {
4104
4547
  var _a, _b;
4105
4548
  const el = taRef.current;
4106
4549
  const s = (_a = el == null ? void 0 : el.selectionStart) != null ? _a : expression.length;
@@ -4108,38 +4551,53 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4108
4551
  const next = expression.slice(0, s) + text2 + expression.slice(e);
4109
4552
  setExpression(next);
4110
4553
  setError("");
4554
+ const pos = s + (caret != null ? caret : text2.length);
4111
4555
  requestAnimationFrame(() => {
4112
4556
  el == null ? void 0 : el.focus();
4113
- const p = s + text2.length;
4114
- el == null ? void 0 : el.setSelectionRange(p, p);
4557
+ el == null ? void 0 : el.setSelectionRange(pos, pos);
4115
4558
  });
4116
4559
  };
4560
+ const readFilter = () => {
4561
+ const started = fConds.filter((c) => c.field || c.op || c.value);
4562
+ const filter = sanitizeRefFilter({ join: fJoin, conditions: started });
4563
+ const incomplete = started.length > 0 && (!filter || filter.conditions.length !== started.length);
4564
+ return { filter, incomplete };
4565
+ };
4117
4566
  const addRef = () => {
4118
4567
  if (!table || !fld) return;
4119
4568
  if (panel === "cell" && !rec.id) return;
4120
4569
  const isAgg = panel === "agg";
4121
- const same = refs.find((r) => r.tableId === table.id && r.field === fld && (isAgg ? r.agg === agg : !r.agg && r.recordId === rec.id));
4570
+ const { filter, incomplete } = isAgg ? readFilter() : { filter: null, incomplete: false };
4571
+ if (incomplete) {
4572
+ setError("\u05D9\u05E9 \u05DC\u05D4\u05E9\u05DC\u05D9\u05DD \u05D1\u05DB\u05DC \u05EA\u05E0\u05D0\u05D9 \u05E1\u05D9\u05E0\u05D5\u05DF: \u05E9\u05D3\u05D4, \u05E4\u05E2\u05D5\u05DC\u05D4 \u05D5\u05E2\u05E8\u05DA (\u05D0\u05D5 \u05DC\u05D4\u05E1\u05D9\u05E8 \u05D0\u05D5\u05EA\u05D5)");
4573
+ return;
4574
+ }
4575
+ const sig = filterSignature(filter);
4576
+ const same = refs.find((r) => r.tableId === table.id && r.field === fld && (isAgg ? r.agg === agg && filterSignature(r.filter) === sig : !r.agg && r.recordId === rec.id));
4122
4577
  let key;
4123
4578
  if (same) key = same.key;
4124
4579
  else {
4125
4580
  key = makeCellRefKey(refs, fld, isAgg ? agg : void 0);
4126
- setRefs((prev) => [...prev, __spreadValues({
4581
+ setRefs((prev) => [...prev, __spreadValues(__spreadValues({
4127
4582
  key,
4128
4583
  tableId: table.id,
4129
4584
  tableName: table.name,
4130
4585
  recordId: isAgg ? "" : rec.id,
4131
4586
  recordLabel: isAgg ? "" : rec.label,
4132
4587
  field: fld
4133
- }, isAgg ? { agg } : {})]);
4588
+ }, isAgg ? { agg } : {}), isAgg && filter ? { filter } : {})]);
4134
4589
  }
4135
4590
  insert(`{${key}}`);
4591
+ setError("");
4136
4592
  setPanel("");
4137
4593
  setFld("");
4594
+ setFConds([]);
4138
4595
  };
4139
4596
  const used = (0, import_react9.useMemo)(() => new Set(extractFormulaFields(expression)), [expression]);
4140
4597
  const usedRefs = refs.filter((r) => used.has(r.key));
4598
+ const live = expression.trim() ? validateFormula(expression, [...rowFieldNames, ...refs.map((r) => r.key)]) : null;
4141
4599
  const apply = () => {
4142
- const err = validateFormula(expression, [...rowFields, ...refs.map((r) => r.key)]);
4600
+ const err = validateFormula(expression, [...rowFieldNames, ...refs.map((r) => r.key)]);
4143
4601
  if (err) {
4144
4602
  setError(err);
4145
4603
  return;
@@ -4151,7 +4609,28 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4151
4609
  setTId("");
4152
4610
  setFld("");
4153
4611
  setRec({ id: "", label: "" });
4612
+ setFConds([]);
4613
+ setError("");
4154
4614
  };
4615
+ const updateIfCond = (i, patch) => setIfConds((prev) => prev.map((c, idx) => idx === i ? __spreadValues(__spreadValues({}, c), patch) : c));
4616
+ const insertBuilt = (withIf) => {
4617
+ const res = withIf ? buildIfExpression(rowColumns, ifConds, ifJoin, thenVal, elseVal) : buildIfExpression(rowColumns, ifConds, ifJoin);
4618
+ if ("error" in res) {
4619
+ setBuilderError(res.error);
4620
+ return;
4621
+ }
4622
+ setBuilderError("");
4623
+ insert(res.expression);
4624
+ };
4625
+ const rowFieldOptions = /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
4626
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: rowColumns.length === 0 ? "-- \u05D0\u05D9\u05DF \u05E9\u05D3\u05D5\u05EA \u05DE\u05EA\u05D0\u05D9\u05DE\u05D9\u05DD \u05D1\u05D8\u05D1\u05DC\u05D4 \u05D4\u05D6\u05D5 --" : "-- \u05D1\u05D7\u05E8 \u05E9\u05D3\u05D4 --" }),
4627
+ rowColumns.map((c) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("option", { value: c.name, children: [
4628
+ c.name,
4629
+ " (",
4630
+ typeLabel(c.type),
4631
+ ")"
4632
+ ] }, c.name))
4633
+ ] });
4155
4634
  return (0, import_react_dom3.createPortal)(
4156
4635
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
4157
4636
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "fixed inset-0 z-[9998]", onClick: onClose }),
@@ -4159,7 +4638,7 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4159
4638
  "div",
4160
4639
  {
4161
4640
  dir: "rtl",
4162
- className: "fixed z-[9999] w-[340px] max-h-[80vh] overflow-auto p-3 bg-white border border-slate-200 rounded-lg shadow-xl space-y-2",
4641
+ className: "fixed z-[9999] w-[380px] max-h-[85vh] overflow-auto p-3 bg-white border border-slate-200 rounded-lg shadow-xl space-y-2",
4163
4642
  style: { top: anchor.top, left: anchor.left },
4164
4643
  children: [
4165
4644
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "text-sm font-semibold text-slate-700", children: "\u05E0\u05D5\u05E1\u05D7\u05D4 \u05DC\u05EA\u05D0" }),
@@ -4168,7 +4647,7 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4168
4647
  {
4169
4648
  ref: taRef,
4170
4649
  dir: "ltr",
4171
- rows: 2,
4650
+ rows: 3,
4172
4651
  value: expression,
4173
4652
  onChange: (e) => {
4174
4653
  setExpression(e.target.value);
@@ -4178,6 +4657,7 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4178
4657
  className: "w-full px-2 py-1.5 border border-slate-300 rounded text-sm font-mono bg-white"
4179
4658
  }
4180
4659
  ),
4660
+ live && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "text-[11px] text-rose-500", children: live }),
4181
4661
  usedRefs.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "flex flex-wrap gap-1", children: usedRefs.map((r) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4182
4662
  "span",
4183
4663
  {
@@ -4189,11 +4669,55 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4189
4669
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "text-slate-400", children: [
4190
4670
  "\xB7 ",
4191
4671
  r.tableName
4672
+ ] }),
4673
+ r.filter && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "text-amber-600", children: [
4674
+ " \xB7 ",
4675
+ describeRefFilter(r.filter)
4192
4676
  ] })
4193
4677
  ]
4194
4678
  },
4195
4679
  r.key
4196
4680
  )) }),
4681
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "grid grid-cols-3 gap-1.5", children: [
4682
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4683
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D4\u05D5\u05E1\u05E3 \u05E9\u05D3\u05D4" }),
4684
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4685
+ "select",
4686
+ {
4687
+ className: selCls,
4688
+ value: "",
4689
+ disabled: rowColumns.length === 0,
4690
+ onChange: (e) => e.target.value && insert(`{${e.target.value}}`),
4691
+ children: rowFieldOptions
4692
+ }
4693
+ )
4694
+ ] }),
4695
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4696
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D4\u05D5\u05E1\u05E3 \u05D0\u05D5\u05E4\u05E8\u05D8\u05D5\u05E8" }),
4697
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: "", onChange: (e) => e.target.value && insert(e.target.value), children: [
4698
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 --" }),
4699
+ FORMULA_OPERATOR_GROUPS.map((g) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("optgroup", { label: g.label, children: g.items.map((it) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: it.insert, children: it.label }, it.label)) }, g.label))
4700
+ ] })
4701
+ ] }),
4702
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4703
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D4\u05D5\u05E1\u05E3 \u05E4\u05D5\u05E0\u05E7\u05E6\u05D9\u05D4" }),
4704
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4705
+ "select",
4706
+ {
4707
+ className: selCls,
4708
+ value: "",
4709
+ onChange: (e) => {
4710
+ const tpl = e.target.value;
4711
+ if (tpl) insert(tpl, tpl.indexOf("(") + 1);
4712
+ },
4713
+ children: [
4714
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 --" }),
4715
+ FORMULA_FUNCTION_GROUPS.map((g) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("optgroup", { label: g.label, children: g.items.map((it) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: it.template, children: it.label }, it.label)) }, g.label))
4716
+ ]
4717
+ }
4718
+ )
4719
+ ] })
4720
+ ] }),
4197
4721
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "flex items-center gap-1", style: { direction: "ltr" }, children: OPS.map((o) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4198
4722
  "button",
4199
4723
  {
@@ -4204,13 +4728,6 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4204
4728
  },
4205
4729
  o
4206
4730
  )) }),
4207
- rowFields.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4208
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05E9\u05D3\u05D4 \u05DE\u05D0\u05D5\u05EA\u05D4 \u05E9\u05D5\u05E8\u05D4" }),
4209
- /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: "", onChange: (e) => e.target.value && insert(`{${e.target.value}}`), children: [
4210
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D4\u05D5\u05E1\u05E3 \u05E9\u05D3\u05D4 --" }),
4211
- rowFields.map((f) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: f, children: f }, f))
4212
- ] })
4213
- ] }),
4214
4731
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex gap-1.5", children: [
4215
4732
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4216
4733
  "button",
@@ -4237,14 +4754,23 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4237
4754
  tables && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
4238
4755
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4239
4756
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D8\u05D1\u05DC\u05D4" }),
4240
- /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: tId, onChange: (e) => {
4241
- setTId(e.target.value);
4242
- setFld("");
4243
- setRec({ id: "", label: "" });
4244
- }, children: [
4245
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05D8\u05D1\u05DC\u05D4 --" }),
4246
- tables.filter((t) => panel === "cell" || t.columns.some((c) => NUMERIC.includes(c.type))).map((t) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: t.id, children: t.name }, t.id))
4247
- ] })
4757
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4758
+ "select",
4759
+ {
4760
+ className: selCls,
4761
+ value: tId,
4762
+ onChange: (e) => {
4763
+ setTId(e.target.value);
4764
+ setFld("");
4765
+ setRec({ id: "", label: "" });
4766
+ setFConds([]);
4767
+ },
4768
+ children: [
4769
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05D8\u05D1\u05DC\u05D4 --" }),
4770
+ tables.filter((t) => panel === "cell" || t.columns.some((c) => NUMERIC.includes(c.type))).map((t) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: t.id, children: t.name }, t.id))
4771
+ ]
4772
+ }
4773
+ )
4248
4774
  ] }),
4249
4775
  panel === "cell" && tId && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4250
4776
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05E8\u05E9\u05D5\u05DE\u05D4" }),
@@ -4262,13 +4788,26 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4262
4788
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05E2\u05DE\u05D5\u05D3\u05D4" }),
4263
4789
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: fld, onChange: (e) => setFld(e.target.value), children: [
4264
4790
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05E2\u05DE\u05D5\u05D3\u05D4 --" }),
4265
- tableFields.filter((c) => FORMULA_SOURCE_TYPES.includes(c.type)).map((c) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: c.name, children: c.name }, c.name))
4791
+ valueFields.map((c) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: c.name, children: c.name }, c.name))
4266
4792
  ] })
4267
4793
  ] }),
4268
4794
  panel === "agg" && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4269
4795
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05E4\u05E2\u05D5\u05DC\u05D4" }),
4270
4796
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("select", { className: selCls, value: agg, onChange: (e) => setAgg(e.target.value), children: PER_RECORD_SOURCE_AGGS.map((a) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: a, children: PER_RECORD_AGG_LABELS[a] }, a)) })
4271
4797
  ] }),
4798
+ panel === "agg" && table && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4799
+ FilterEditor,
4800
+ {
4801
+ columns: table.columns.filter((c) => !String(c.name).startsWith("_")),
4802
+ conds: fConds,
4803
+ join: fJoin,
4804
+ onChange: (c, j) => {
4805
+ setFConds(c);
4806
+ setFJoin(j);
4807
+ setError("");
4808
+ }
4809
+ }
4810
+ ),
4272
4811
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4273
4812
  "button",
4274
4813
  {
@@ -4281,6 +4820,138 @@ var CellFormulaEditor = ({ initial, rowFields, context, anchor, onApply, onClear
4281
4820
  )
4282
4821
  ] })
4283
4822
  ] }),
4823
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("details", { className: "rounded border border-slate-200 bg-white", children: [
4824
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("summary", { className: "cursor-pointer select-none px-2 py-1.5 text-xs font-semibold text-slate-700", children: "\u05D1\u05D5\u05E0\u05D4 \u05EA\u05E0\u05D0\u05D9 (\u05D0\u05DD... \u05D0\u05D6... \u05D0\u05D7\u05E8\u05EA)" }),
4825
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "p-2 space-y-2 border-t border-slate-200", children: [
4826
+ ifConds.map((c, i) => {
4827
+ const col = rowColumns.find((x) => x.name === c.field);
4828
+ const ops = col ? getIfConditionOps(col.type) : [];
4829
+ const op2 = ops.find((o) => o.id === c.op);
4830
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "space-y-1.5 p-2 rounded bg-slate-50 border border-slate-200", children: [
4831
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex items-center justify-between", children: [
4832
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "text-[11px] font-medium text-slate-600", children: i === 0 ? "\u05D0\u05DD" : ifJoin === "AND" ? "\u05D5\u05D2\u05DD" : "\u05D0\u05D5" }),
4833
+ ifConds.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4834
+ "button",
4835
+ {
4836
+ type: "button",
4837
+ className: "text-[11px] text-rose-600 hover:underline",
4838
+ onClick: () => setIfConds((prev) => prev.filter((_, idx) => idx !== i)),
4839
+ children: "\u05D4\u05E1\u05E8 \u05EA\u05E0\u05D0\u05D9"
4840
+ }
4841
+ )
4842
+ ] }),
4843
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4844
+ "select",
4845
+ {
4846
+ className: selCls,
4847
+ value: c.field,
4848
+ onChange: (e) => updateIfCond(i, { field: e.target.value, op: "", value: "" }),
4849
+ children: rowFieldOptions
4850
+ }
4851
+ ),
4852
+ col && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("select", { className: selCls, value: c.op, onChange: (e) => updateIfCond(i, { op: e.target.value, value: "" }), children: [
4853
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "-- \u05D1\u05D7\u05E8 \u05E4\u05E2\u05D5\u05DC\u05D4 --" }),
4854
+ ops.map((o) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: o.id, children: o.label }, o.id))
4855
+ ] }),
4856
+ (op2 == null ? void 0 : op2.needsValue) && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "grid grid-cols-2 gap-1.5", children: [
4857
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4858
+ "input",
4859
+ {
4860
+ type: (col == null ? void 0 : col.type) === "DATE" ? "date" : "text",
4861
+ value: c.value,
4862
+ onChange: (e) => updateIfCond(i, { value: e.target.value }),
4863
+ placeholder: "\u05E2\u05E8\u05DA \u05DC\u05D4\u05E9\u05D5\u05D5\u05D0\u05D4",
4864
+ className: selCls
4865
+ }
4866
+ ),
4867
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4868
+ "select",
4869
+ {
4870
+ className: selCls,
4871
+ value: "",
4872
+ onChange: (e) => e.target.value && updateIfCond(i, { value: `{${e.target.value}}` }),
4873
+ children: [
4874
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "...\u05D0\u05D5 \u05E9\u05D3\u05D4 \u05D0\u05D7\u05E8" }),
4875
+ rowColumns.filter((s) => s.name !== c.field).map((s) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: s.name, children: s.name }, s.name))
4876
+ ]
4877
+ }
4878
+ )
4879
+ ] })
4880
+ ] }, i);
4881
+ }),
4882
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex items-center gap-2", children: [
4883
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4884
+ "button",
4885
+ {
4886
+ type: "button",
4887
+ disabled: ifConds.length >= 5,
4888
+ className: smallBtn,
4889
+ onClick: () => setIfConds((prev) => [...prev, __spreadValues({}, EMPTY_IF_CONDITION)]),
4890
+ children: "+ \u05D4\u05D5\u05E1\u05E3 \u05EA\u05E0\u05D0\u05D9"
4891
+ }
4892
+ ),
4893
+ ifConds.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4894
+ "select",
4895
+ {
4896
+ className: "px-2 py-1 border border-slate-300 rounded text-xs bg-white",
4897
+ value: ifJoin,
4898
+ onChange: (e) => setIfJoin(e.target.value),
4899
+ children: [
4900
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "AND", children: "\u05DB\u05DC \u05D4\u05EA\u05E0\u05D0\u05D9\u05DD (\u05D5\u05D2\u05DD)" }),
4901
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "OR", children: "\u05DC\u05E4\u05D7\u05D5\u05EA \u05D0\u05D7\u05D3 (\u05D0\u05D5)" })
4902
+ ]
4903
+ }
4904
+ )
4905
+ ] }),
4906
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "grid grid-cols-2 gap-2", children: [
4907
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4908
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D0\u05DD \u05D4\u05EA\u05E0\u05D0\u05D9 \u05DE\u05EA\u05E7\u05D9\u05D9\u05DD, \u05D4\u05EA\u05D5\u05E6\u05D0\u05D4:" }),
4909
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("input", { value: thenVal, onChange: (e) => setThenVal(e.target.value), placeholder: "\u05DE\u05E1\u05E4\u05E8, \u05D8\u05E7\u05E1\u05D8 \u05D0\u05D5 \u05E9\u05D3\u05D4", className: selCls }),
4910
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4911
+ "select",
4912
+ {
4913
+ className: `${selCls} mt-1`,
4914
+ value: "",
4915
+ onChange: (e) => e.target.value && setThenVal(`{${e.target.value}}`),
4916
+ children: [
4917
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "...\u05D0\u05D5 \u05E9\u05D3\u05D4" }),
4918
+ rowColumns.map((s) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: s.name, children: s.name }, s.name))
4919
+ ]
4920
+ }
4921
+ )
4922
+ ] }),
4923
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { children: [
4924
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("label", { className: lblCls, children: "\u05D0\u05D7\u05E8\u05EA, \u05D4\u05EA\u05D5\u05E6\u05D0\u05D4:" }),
4925
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("input", { value: elseVal, onChange: (e) => setElseVal(e.target.value), placeholder: "\u05DE\u05E1\u05E4\u05E8, \u05D8\u05E7\u05E1\u05D8 \u05D0\u05D5 \u05E9\u05D3\u05D4", className: selCls }),
4926
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
4927
+ "select",
4928
+ {
4929
+ className: `${selCls} mt-1`,
4930
+ value: "",
4931
+ onChange: (e) => e.target.value && setElseVal(`{${e.target.value}}`),
4932
+ children: [
4933
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: "", children: "...\u05D0\u05D5 \u05E9\u05D3\u05D4" }),
4934
+ rowColumns.map((s) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("option", { value: s.name, children: s.name }, s.name))
4935
+ ]
4936
+ }
4937
+ )
4938
+ ] })
4939
+ ] }),
4940
+ builderError && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { className: "text-[11px] text-rose-500", children: builderError }),
4941
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex flex-wrap gap-2", children: [
4942
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
4943
+ "button",
4944
+ {
4945
+ type: "button",
4946
+ onClick: () => insertBuilt(true),
4947
+ className: "px-3 py-1.5 text-xs rounded bg-slate-800 text-white hover:bg-slate-700",
4948
+ children: "\u05D4\u05D5\u05E1\u05E3 \u05EA\u05E0\u05D0\u05D9 IF \u05DC\u05E0\u05D5\u05E1\u05D7\u05D4"
4949
+ }
4950
+ ),
4951
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", onClick: () => insertBuilt(false), className: smallBtn, children: "\u05D4\u05D5\u05E1\u05E3 \u05E8\u05E7 \u05D0\u05EA \u05D4\u05EA\u05E0\u05D0\u05D9" })
4952
+ ] })
4953
+ ] })
4954
+ ] }),
4284
4955
  error && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "text-xs text-red-600", children: error }),
4285
4956
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex gap-2 pt-1", children: [
4286
4957
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
@@ -4328,16 +4999,16 @@ var PerRecordSourceCell = ({ value, column, onChange, readOnly, context }) => {
4328
4999
  const btnRef = (0, import_react9.useRef)(null);
4329
5000
  const [open, setOpen] = (0, import_react9.useState)(false);
4330
5001
  const [pos, setPos] = (0, import_react9.useState)({ top: 0, left: 0 });
4331
- const rowFields = (0, import_react9.useMemo)(() => {
5002
+ const rowColumns = (0, import_react9.useMemo)(() => {
4332
5003
  const cols = Array.isArray(context == null ? void 0 : context.columns) ? context.columns : [];
4333
- return cols.filter((c) => (c == null ? void 0 : c.name) && c.name !== column.name && !String(c.name).startsWith("_") && FORMULA_SOURCE_TYPES.includes(String(c.type).toUpperCase())).map((c) => c.name);
5004
+ return cols.filter((c) => (c == null ? void 0 : c.name) && c.name !== column.name && !String(c.name).startsWith("_") && FORMULA_SOURCE_TYPES.includes(String(c.type).toUpperCase())).map((c) => ({ name: c.name, type: String(c.type).toUpperCase() }));
4334
5005
  }, [context == null ? void 0 : context.columns, column.name]);
4335
5006
  const openEditor = () => {
4336
5007
  var _a2;
4337
5008
  const r = (_a2 = btnRef.current) == null ? void 0 : _a2.getBoundingClientRect();
4338
5009
  if (r) {
4339
- const left = Math.max(8, Math.min(r.left, window.innerWidth - 356));
4340
- const top = Math.min(r.bottom + 4, Math.max(8, window.innerHeight - 420));
5010
+ const left = Math.max(8, Math.min(r.left, window.innerWidth - 396));
5011
+ const top = Math.min(r.bottom + 4, Math.max(8, window.innerHeight - 440));
4341
5012
  setPos({ top, left });
4342
5013
  }
4343
5014
  setOpen(true);
@@ -4360,7 +5031,7 @@ var PerRecordSourceCell = ({ value, column, onChange, readOnly, context }) => {
4360
5031
  CellFormulaEditor,
4361
5032
  {
4362
5033
  initial: current,
4363
- rowFields,
5034
+ rowColumns,
4364
5035
  context,
4365
5036
  anchor: pos,
4366
5037
  onClose: () => setOpen(false),
@@ -7228,12 +7899,12 @@ function TableBlockClient({
7228
7899
  setLocalColumns((prev) => mergeColumnDefs(prev, res.schema.columns));
7229
7900
  }
7230
7901
  if (res.records && Array.isArray(res.records)) {
7231
- const norm = normalizeRecords(res.records);
7902
+ const norm2 = normalizeRecords(res.records);
7232
7903
  const map = /* @__PURE__ */ new Map();
7233
- norm.forEach((r) => map.set(r.id, __spreadValues({}, r.data)));
7904
+ norm2.forEach((r) => map.set(r.id, __spreadValues({}, r.data)));
7234
7905
  setBaseRecordsMap(map);
7235
- setServerRecords(norm);
7236
- setData(mergeWithDirty(norm));
7906
+ setServerRecords(norm2);
7907
+ setData(mergeWithDirty(norm2));
7237
7908
  }
7238
7909
  if (res.meta) {
7239
7910
  const total = typeof res.meta.totalRecords === "number" ? res.meta.totalRecords : 0;
@@ -7269,13 +7940,13 @@ function TableBlockClient({
7269
7940
  }, [page, limit, debouncedSearch, filtersKey, fetchTableData, defaultFiltersReady]);
7270
7941
  (0, import_react19.useEffect)(() => {
7271
7942
  if (initialRecords && defaultFiltersReady) {
7272
- const norm = normalizeRecords(initialRecords);
7273
- setServerRecords(norm);
7943
+ const norm2 = normalizeRecords(initialRecords);
7944
+ setServerRecords(norm2);
7274
7945
  const hasActiveFilters = Boolean(debouncedSearch) || Object.keys(columnFilters).length > 0;
7275
7946
  if (hasActiveFilters && executeActionRef.current) {
7276
7947
  fetchTableData(page, limit, debouncedSearch, filtersKey);
7277
7948
  } else if (hasActiveFilters) {
7278
- let locallyFiltered = norm;
7949
+ let locallyFiltered = norm2;
7279
7950
  if (debouncedSearch) {
7280
7951
  const q = debouncedSearch.toLowerCase();
7281
7952
  locallyFiltered = locallyFiltered.filter(
@@ -7326,7 +7997,7 @@ function TableBlockClient({
7326
7997
  }
7327
7998
  setData(mergeWithDirty(locallyFiltered));
7328
7999
  } else {
7329
- setData(mergeWithDirty(norm));
8000
+ setData(mergeWithDirty(norm2));
7330
8001
  }
7331
8002
  }
7332
8003
  }, [initialRecords, mergeWithDirty, debouncedSearch, columnFilters, fetchTableData, page, limit, filtersKey, defaultFiltersReady]);
@@ -18248,9 +18919,14 @@ var UNIVERSE_COMPONENTS = {
18248
18919
  DEFAULT_PLACEMENT_STATUS_COLORS,
18249
18920
  DatePickerCell,
18250
18921
  DynamicFiltersBlock,
18922
+ EMPTY_IF_CONDITION,
18251
18923
  FORMULA_COLUMN_TYPE,
18924
+ FORMULA_FUNCTION_GROUPS,
18925
+ FORMULA_OPERATOR_GROUPS,
18252
18926
  FORMULA_SOURCE_TYPES,
18927
+ FORMULA_TYPE_LABELS,
18253
18928
  LinkPickerCell,
18929
+ MAX_FILTER_CONDITIONS,
18254
18930
  NON_TEXT_TYPES,
18255
18931
  PASTEL_PALETTES,
18256
18932
  PAYMENT_REPORTS_ACTIONS,
@@ -18286,6 +18962,8 @@ var UNIVERSE_COMPONENTS = {
18286
18962
  applyFormulaColumns,
18287
18963
  applyRelationCopy,
18288
18964
  buildColumnOptions,
18965
+ buildIfConditionExpression,
18966
+ buildIfExpression,
18289
18967
  buildRelationCopyPatch,
18290
18968
  buildRelationRef,
18291
18969
  calculateMinColumnWidth,
@@ -18295,11 +18973,14 @@ var UNIVERSE_COMPONENTS = {
18295
18973
  columnRefLabel,
18296
18974
  computeFormulaValue,
18297
18975
  describePerRecordFormula,
18976
+ describeRefFilter,
18298
18977
  enrichRowsWithRelations,
18299
18978
  evaluateFormula,
18300
18979
  extractFormulaFields,
18301
18980
  extractUniqueVariableIndices,
18302
18981
  extractVariableIndices,
18982
+ filterKindOfType,
18983
+ filterSignature,
18303
18984
  findVariableAtPosition,
18304
18985
  formatConflictCellValue,
18305
18986
  formatFileSize,
@@ -18311,6 +18992,8 @@ var UNIVERSE_COMPONENTS = {
18311
18992
  getDefaultFilterColumns,
18312
18993
  getDeterministicPastelBadge,
18313
18994
  getEligibleCopyTargets,
18995
+ getFilterOps,
18996
+ getIfConditionOps,
18314
18997
  getNextVariableIndex,
18315
18998
  getRelationButtonColumns,
18316
18999
  getRelationCopyLockedTargets,
@@ -18335,6 +19018,7 @@ var UNIVERSE_COMPONENTS = {
18335
19018
  isValidTemplateName,
18336
19019
  isWordToken,
18337
19020
  makeCellRefKey,
19021
+ matchesRefFilter,
18338
19022
  measureTextWidth,
18339
19023
  missingPaymentReportsFields,
18340
19024
  normalizePerRecordFormula,
@@ -18354,6 +19038,7 @@ var UNIVERSE_COMPONENTS = {
18354
19038
  resolveUniverseTheme,
18355
19039
  sanitizeDefaultFilter,
18356
19040
  sanitizePhoneNumber,
19041
+ sanitizeRefFilter,
18357
19042
  sanitizeTemplateNameInput,
18358
19043
  seamlessVariableEngine,
18359
19044
  splitHeaderTitle,