@marktiderman/genesis-core 2.14.0 → 2.16.0

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.
Files changed (101) hide show
  1. package/dist/aggregate-Y5IJ5aPn.d.ts +34 -0
  2. package/dist/{chunk-7PUFVNPB.js → chunk-2IGAUXYH.js} +5 -3
  3. package/dist/chunk-2IGAUXYH.js.map +1 -0
  4. package/dist/{chunk-KA3BUJ4H.js → chunk-42NVJJTT.js} +3 -3
  5. package/dist/{chunk-KA3BUJ4H.js.map → chunk-42NVJJTT.js.map} +1 -1
  6. package/dist/{chunk-JN47G4UE.js → chunk-7EFTWOMA.js} +56 -2
  7. package/dist/chunk-7EFTWOMA.js.map +1 -0
  8. package/dist/{chunk-P3FHXRS2.js → chunk-ALLWIK62.js} +6 -6
  9. package/dist/{chunk-P3FHXRS2.js.map → chunk-ALLWIK62.js.map} +1 -1
  10. package/dist/chunk-APFXV6FZ.js +130 -0
  11. package/dist/chunk-APFXV6FZ.js.map +1 -0
  12. package/dist/chunk-BDPRWVFW.js +17 -0
  13. package/dist/chunk-BDPRWVFW.js.map +1 -0
  14. package/dist/{chunk-S2VYJWB4.js → chunk-E3K2A3PK.js} +3 -3
  15. package/dist/{chunk-S2VYJWB4.js.map → chunk-E3K2A3PK.js.map} +1 -1
  16. package/dist/{chunk-OYDJGPDY.js → chunk-FXPODKCK.js} +5 -5
  17. package/dist/{chunk-OYDJGPDY.js.map → chunk-FXPODKCK.js.map} +1 -1
  18. package/dist/{chunk-SDAJSJNN.js → chunk-IYGVJFCV.js} +3 -3
  19. package/dist/{chunk-SDAJSJNN.js.map → chunk-IYGVJFCV.js.map} +1 -1
  20. package/dist/chunk-J54XXKQD.js +104 -0
  21. package/dist/chunk-J54XXKQD.js.map +1 -0
  22. package/dist/{chunk-KV2HW4G4.js → chunk-LB6FMQQM.js} +11 -3
  23. package/dist/chunk-LB6FMQQM.js.map +1 -0
  24. package/dist/{chunk-G6GQPXQS.js → chunk-M5IX7C6U.js} +5 -5
  25. package/dist/{chunk-G6GQPXQS.js.map → chunk-M5IX7C6U.js.map} +1 -1
  26. package/dist/{chunk-DFABPMEQ.js → chunk-POUI7GAI.js} +5 -5
  27. package/dist/{chunk-DFABPMEQ.js.map → chunk-POUI7GAI.js.map} +1 -1
  28. package/dist/{chunk-NE2D74BP.js → chunk-QGWQ6LK3.js} +5 -5
  29. package/dist/{chunk-NE2D74BP.js.map → chunk-QGWQ6LK3.js.map} +1 -1
  30. package/dist/{chunk-2K4747KX.js → chunk-R3YXYVWF.js} +7 -2
  31. package/dist/chunk-R3YXYVWF.js.map +1 -0
  32. package/dist/{chunk-RFHUOXZC.js → chunk-SOO54YYY.js} +4 -4
  33. package/dist/{chunk-RFHUOXZC.js.map → chunk-SOO54YYY.js.map} +1 -1
  34. package/dist/{chunk-YZV3Z6ZA.js → chunk-SQLM5CIJ.js} +3 -3
  35. package/dist/{chunk-YZV3Z6ZA.js.map → chunk-SQLM5CIJ.js.map} +1 -1
  36. package/dist/{chunk-WDPM5TBO.js → chunk-UDSTWXXZ.js} +3 -3
  37. package/dist/{chunk-WDPM5TBO.js.map → chunk-UDSTWXXZ.js.map} +1 -1
  38. package/dist/chunk-XPBS5YE4.js +24 -0
  39. package/dist/chunk-XPBS5YE4.js.map +1 -0
  40. package/dist/{chunk-AHFIS42U.js → chunk-ZYOJWU62.js} +5 -5
  41. package/dist/{chunk-AHFIS42U.js.map → chunk-ZYOJWU62.js.map} +1 -1
  42. package/dist/csv.d.ts +1 -1
  43. package/dist/form-types.d.ts +1 -1
  44. package/dist/headless/index.d.ts +334 -0
  45. package/dist/headless/index.js +490 -0
  46. package/dist/headless/index.js.map +1 -0
  47. package/dist/hooks/index.d.ts +4 -2
  48. package/dist/hooks/index.js +20 -17
  49. package/dist/hooks/mutation-mode.d.ts +1 -1
  50. package/dist/hooks/mutation-mode.js +5 -4
  51. package/dist/hooks/related-keys.d.ts +1 -1
  52. package/dist/hooks/use-aggregate.d.ts +25 -0
  53. package/dist/hooks/use-aggregate.js +10 -0
  54. package/dist/hooks/use-aggregate.js.map +1 -0
  55. package/dist/hooks/use-capabilities.d.ts +1 -1
  56. package/dist/hooks/use-capabilities.js +4 -2
  57. package/dist/hooks/use-draft.js +2 -2
  58. package/dist/hooks/use-infinite-resource.d.ts +1 -1
  59. package/dist/hooks/use-infinite-resource.js +6 -4
  60. package/dist/hooks/use-link-options.d.ts +1 -1
  61. package/dist/hooks/use-link-options.js +8 -6
  62. package/dist/hooks/use-live.d.ts +1 -1
  63. package/dist/hooks/use-live.js +4 -2
  64. package/dist/hooks/use-one.d.ts +1 -1
  65. package/dist/hooks/use-one.js +6 -4
  66. package/dist/hooks/use-persisted-ui-state.js +4 -2
  67. package/dist/hooks/use-relation-label.d.ts +1 -1
  68. package/dist/hooks/use-relation-label.js +4 -2
  69. package/dist/hooks/use-resource-form.d.ts +1 -1
  70. package/dist/hooks/use-resource-form.js +8 -7
  71. package/dist/hooks/use-resource.d.ts +1 -1
  72. package/dist/hooks/use-resource.js +7 -6
  73. package/dist/hooks/use-write.d.ts +1 -1
  74. package/dist/index.d.ts +110 -6
  75. package/dist/index.js +129 -20
  76. package/dist/index.js.map +1 -1
  77. package/dist/navigation.d.ts +77 -1
  78. package/dist/navigation.js +32 -2
  79. package/dist/navigation.js.map +1 -1
  80. package/dist/provider/index.d.ts +12 -4
  81. package/dist/provider/index.js +3 -2
  82. package/dist/resource-view/index.d.ts +456 -166
  83. package/dist/resource-view/index.js +251 -5
  84. package/dist/resource-view/index.js.map +1 -1
  85. package/dist/schema/index.d.ts +3 -3
  86. package/dist/schema/index.js +1 -2
  87. package/dist/testing/index.d.ts +2 -2
  88. package/dist/{types-CtJxwoyv.d.ts → types-BriIV4e_.d.ts} +78 -3
  89. package/dist/workspaces-CxpiFu2-.d.ts +263 -0
  90. package/dist/workspaces.d.ts +2 -160
  91. package/dist/workspaces.js +2 -1
  92. package/dist/{write-outcome-Goy1v39G.d.ts → write-outcome-CkQXgtJq.d.ts} +1 -1
  93. package/package.json +14 -4
  94. package/dist/chunk-2K4747KX.js.map +0 -1
  95. package/dist/chunk-7PUFVNPB.js.map +0 -1
  96. package/dist/chunk-JN47G4UE.js.map +0 -1
  97. package/dist/chunk-KV2HW4G4.js.map +0 -1
  98. package/dist/chunk-UHOMTFBD.js +0 -72
  99. package/dist/chunk-UHOMTFBD.js.map +0 -1
  100. package/dist/chunk-YDMKMNDS.js +0 -8
  101. package/dist/chunk-YDMKMNDS.js.map +0 -1
@@ -1,8 +1,9 @@
1
1
  import { readinessOf } from '../chunk-PP3XFFQM.js';
2
- import { keyFields, encodeRecordKey } from '../chunk-KV2HW4G4.js';
2
+ import { fieldControl, keyFields, encodeRecordKey } from '../chunk-LB6FMQQM.js';
3
3
  import { NO_VIEWER } from '../chunk-OZYF55E3.js';
4
4
  export { NO_VIEWER } from '../chunk-OZYF55E3.js';
5
- import { isTimeZone } from '../chunk-WXENWPWP.js';
5
+ import { toAggregateValues } from '../chunk-J54XXKQD.js';
6
+ import { isTimeZone, deviceTimeZone, dayIn, addDays, startOfWeek, isDay } from '../chunk-WXENWPWP.js';
6
7
  import { isDevelopment, reportError } from '../chunk-NOPS5EIH.js';
7
8
  import '../chunk-HUNGCTRA.js';
8
9
  import { titleCase } from '../chunk-RYEXWQFD.js';
@@ -138,10 +139,13 @@ function resolveLink(owner, name, reference, link, labelField) {
138
139
  });
139
140
  }
140
141
 
142
+ // src/resource-view/types.ts
143
+ var RECORD_OPEN_MODES = Object.freeze(["side-peek", "center-peek", "page"]);
144
+
141
145
  // src/resource-view/define-view.ts
142
146
  function deriveDisplay(field) {
143
147
  if (field.references) return "relation";
144
- switch (field.widget) {
148
+ switch (fieldControl(field)) {
145
149
  case "url":
146
150
  return "url";
147
151
  case "email":
@@ -407,6 +411,12 @@ function defineView(contract, definition = {}) {
407
411
  }
408
412
  for (const name of scope) known(name, "scope");
409
413
  if (definition.groupBy) known(definition.groupBy, "groupBy");
414
+ const rawOpen = definition.open;
415
+ if (rawOpen !== void 0 && !RECORD_OPEN_MODES.includes(rawOpen)) {
416
+ problems.push(`open is ${RECORD_OPEN_MODES.map((m) => `"${m}"`).join(", ")}, not ${JSON.stringify(rawOpen)}`);
417
+ }
418
+ const rawRecordView = definition.recordView;
419
+ if (rawRecordView !== void 0 && typeof rawRecordView !== "function") problems.push("recordView is a function that returns the record view (`() => brandRecordView`)");
410
420
  const rawCalendar = definition.calendar;
411
421
  let calendar;
412
422
  if (rawCalendar !== void 0) {
@@ -716,6 +726,26 @@ function rowPicture(summary) {
716
726
  return initials ? { kind: "initials", text: initials } : null;
717
727
  }
718
728
 
729
+ // src/resource-view/group.ts
730
+ var isEmpty = (value) => value === null || value === void 0 || value === "";
731
+ function groupRows(view, rows, options = {}) {
732
+ const field = options.field ?? view.definition.groupBy;
733
+ if (!field) return rows.length ? [Object.freeze({ key: "", value: null, label: "", rows: Object.freeze([...rows]) })] : [];
734
+ const groups = /* @__PURE__ */ new Map();
735
+ for (const row of rows) {
736
+ const value = row[field];
737
+ const key = isEmpty(value) ? "" : String(value);
738
+ let group = groups.get(key);
739
+ if (!group) {
740
+ const label = isEmpty(value) ? options.emptyLabel ?? `No ${view.label(field).toLowerCase()}` : view.valueLabel(field, value);
741
+ group = { key, value: isEmpty(value) ? null : value, label, rows: [] };
742
+ groups.set(key, group);
743
+ }
744
+ group.rows.push(row);
745
+ }
746
+ return [...groups.values()].map((g) => Object.freeze({ ...g, rows: Object.freeze(g.rows) }));
747
+ }
748
+
719
749
  // src/resource-view/tree.ts
720
750
  var rootish = (parent) => parent == null || parent === "";
721
751
  function treeRows(rows, options) {
@@ -839,6 +869,7 @@ var WORKSPACE = /* @__PURE__ */ Symbol.for("genesis.resource-workspace");
839
869
  var REFUSED = "This isn't allowed.";
840
870
  var TONES = /* @__PURE__ */ new Set(["success", "warning", "danger", "info", "muted"]);
841
871
  var SECTION_KEYS = /* @__PURE__ */ new Set(["id", "label", "list", "settings"]);
872
+ var DOCUMENT_KEYS = /* @__PURE__ */ new Set(["cover", "icon", "properties", "body"]);
842
873
  var dev = () => process.env.NODE_ENV !== "production";
843
874
  function unsettable(field) {
844
875
  if (!field) return null;
@@ -865,8 +896,10 @@ function defineRecordView(view, definition) {
865
896
  const writable = new Set(view.writable("edit"));
866
897
  const actionNames = new Set(view.actions().map((a) => a.name));
867
898
  const input = Array.isArray(definition?.sections) ? definition.sections : [];
899
+ const template = definition?.template ?? "standard";
900
+ if (template !== "standard" && template !== "document") problems.push(`template is "standard" or "document", not ${JSON.stringify(template)}`);
868
901
  if (!Array.isArray(definition?.sections)) problems.push("sections isn't a list");
869
- else if (input.length === 0) problems.push("sections is empty");
902
+ else if (input.length === 0 && template !== "document") problems.push("sections is empty");
870
903
  const ids = /* @__PURE__ */ new Set();
871
904
  const settingsSeen = /* @__PURE__ */ new Set();
872
905
  const sections = [];
@@ -939,7 +972,29 @@ function defineRecordView(view, definition) {
939
972
  if (definition?.readiness !== void 0 && typeof definition.readiness !== "function") {
940
973
  problems.push("readiness isn't a function");
941
974
  }
975
+ const doc = definition?.document;
976
+ if (doc !== void 0) {
977
+ if (!doc || typeof doc !== "object" || Array.isArray(doc)) problems.push("document isn't an object");
978
+ else {
979
+ if (template !== "document") problems.push(`document is set, but template isn't "document"`);
980
+ for (const key of Object.keys(doc)) if (!DOCUMENT_KEYS.has(key)) problems.push(`document.${key} isn't a document setting (cover, icon, properties, body)`);
981
+ for (const key of ["cover", "icon", "body"]) {
982
+ const name = doc[key];
983
+ if (name === void 0) continue;
984
+ const field = view.field(String(name));
985
+ if (!field) problems.push(`document.${key} names "${String(name)}", which isn't a field`);
986
+ else if (field.type !== "string" || field.array) problems.push(`document.${key} "${String(name)}" is a ${field.array ? "list" : field.type}, not a text field`);
987
+ }
988
+ if (doc.properties !== void 0) {
989
+ if (!Array.isArray(doc.properties)) problems.push("document.properties isn't a list");
990
+ else for (const name of doc.properties) if (!view.field(String(name))) problems.push(`document.properties names "${String(name)}", which isn't a field`);
991
+ }
992
+ }
993
+ }
942
994
  if (problems.length) throw new ViewDefinitionError(where, problems);
995
+ const document = Object.freeze(
996
+ doc ? { ...doc, ...doc.properties ? { properties: Object.freeze([...doc.properties].map(String)) } : {} } : {}
997
+ );
943
998
  const frozenSections = Object.freeze(sections);
944
999
  const byId = new Map(frozenSections.map((s) => [s.id, s]));
945
1000
  const actionByName = new Map(view.actions().map((a) => [a.name, a]));
@@ -1008,6 +1063,8 @@ function defineRecordView(view, definition) {
1008
1063
  sections: frozenSections,
1009
1064
  headerActions,
1010
1065
  hasReadiness: Boolean(readiness),
1066
+ template,
1067
+ document,
1011
1068
  section: (id) => byId.get(id),
1012
1069
  recordId: (row) => encodeRecordKey(view.contract, row),
1013
1070
  settingsLocked: (row, viewer) => {
@@ -1043,6 +1100,195 @@ function defineRecordView(view, definition) {
1043
1100
  var defineWorkspace = defineRecordView;
1044
1101
  var isWorkspace = isRecordView;
1045
1102
 
1046
- export { ViewDefinitionError, boardChoices, boardProblems, calendarProblems, cascadeSelection, defineRecordView, defineView, defineWorkspace, deriveDisplay, filterParams, galleryProblems, initialExpanded, initialsOf, isRecordView, isWorkspace, plural, rowPicture, sameFormValue, scopeFilter, scopeValues, setExpanded, singular, treeRows, valueText };
1103
+ // src/resource-view/widget.ts
1104
+ var ID = /^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/;
1105
+ function metricProblems(where, metric, view) {
1106
+ if (!metric || metric.fn === "count") return [];
1107
+ if (!["sum", "avg", "min", "max"].includes(metric.fn)) return [`${where}.fn: one of count, sum, avg, min, max`];
1108
+ if (!view.field(metric.field)) return [`${where}.field: "${metric.field}" isn't a field of ${view.resource}`];
1109
+ return [];
1110
+ }
1111
+ function widgetProblems(input) {
1112
+ const w = input;
1113
+ if (!w || typeof w !== "object") return ["expected a widget"];
1114
+ const problems = [];
1115
+ if (typeof w.id !== "string" || !ID.test(w.id)) problems.push("id: a letter, then letters, digits, - or _");
1116
+ if (typeof w.label !== "string" || !w.label.trim()) problems.push("label: expected a non-empty string");
1117
+ const view = w.view;
1118
+ if (!view || typeof view !== "object" || typeof view.field !== "function") return [...problems, "view: a view from defineView"];
1119
+ if (w.preset !== void 0 && !view.definition.presets?.[w.preset]) problems.push(`preset: ${view.resource} has no preset "${w.preset}"`);
1120
+ for (const field of Object.keys(w.filter ?? {})) if (!view.field(field)) problems.push(`filter.${field}: not a field of ${view.resource}`);
1121
+ const currency = w.format?.currency;
1122
+ if (currency !== void 0) {
1123
+ try {
1124
+ new Intl.NumberFormat("en", { style: "currency", currency: String(currency) });
1125
+ } catch {
1126
+ problems.push("format.currency: an ISO 4217 code (USD, EUR)");
1127
+ }
1128
+ }
1129
+ switch (w.kind) {
1130
+ case "number":
1131
+ problems.push(...metricProblems("metric", w.metric, view));
1132
+ break;
1133
+ case "progress":
1134
+ problems.push(...metricProblems("metric", w.metric, view));
1135
+ if (typeof w.goal === "number") {
1136
+ if (!(w.goal > 0)) problems.push("goal: a number above 0, or a total");
1137
+ } else if (!w.goal || typeof w.goal !== "object") problems.push("goal: a number above 0, or a total");
1138
+ else problems.push(...metricProblems("goal", w.goal, view));
1139
+ for (const field of Object.keys(w.goalFilter ?? {})) if (!view.field(field)) problems.push(`goalFilter.${field}: not a field of ${view.resource}`);
1140
+ break;
1141
+ case "streak":
1142
+ if (typeof w.dateField !== "string" || !view.field(w.dateField)) problems.push(`dateField: not a field of ${view.resource}`);
1143
+ if (w.unit !== void 0 && w.unit !== "day" && w.unit !== "week") problems.push("unit: day or week");
1144
+ if (w.limit !== void 0 && !(Number.isInteger(w.limit) && w.limit > 0 && w.limit <= 1e3)) problems.push("limit: a whole number from 1 to 1000");
1145
+ break;
1146
+ case "list":
1147
+ if (w.limit !== void 0 && !(Number.isInteger(w.limit) && w.limit > 0 && w.limit <= 50)) problems.push("limit: a whole number from 1 to 50");
1148
+ if (w.sort && !view.field(w.sort.field)) problems.push(`sort.field: not a field of ${view.resource}`);
1149
+ break;
1150
+ case "chart":
1151
+ if (typeof w.groupBy !== "string" || !view.field(w.groupBy)) problems.push(`groupBy: not a field of ${view.resource}`);
1152
+ problems.push(...metricProblems("metric", w.metric, view));
1153
+ if (w.chart !== void 0 && w.chart !== "bar" && w.chart !== "line") problems.push("chart: bar or line");
1154
+ if (w.limit !== void 0 && !(Number.isInteger(w.limit) && w.limit > 0 && w.limit <= 1e3)) problems.push("limit: a whole number from 1 to 1000");
1155
+ break;
1156
+ default:
1157
+ problems.push("kind: one of number, progress, streak, list, chart");
1158
+ }
1159
+ return problems;
1160
+ }
1161
+ function defineWidget(input) {
1162
+ const problems = widgetProblems(input);
1163
+ if (problems.length) throw new ViewDefinitionError(`widget ${String(input?.id)}`, problems);
1164
+ return Object.freeze({ ...input, __widget: true });
1165
+ }
1166
+ function isWidget(value) {
1167
+ return !!value && typeof value === "object" && value.__widget === true;
1168
+ }
1169
+ function fieldsRead(w) {
1170
+ switch (w.kind) {
1171
+ case "number":
1172
+ return w.metric && w.metric.fn !== "count" ? [w.metric.field] : [];
1173
+ case "progress":
1174
+ return [
1175
+ ...w.metric && w.metric.fn !== "count" ? [w.metric.field] : [],
1176
+ ...typeof w.goal === "object" && w.goal.fn !== "count" ? [w.goal.field] : []
1177
+ ];
1178
+ case "streak":
1179
+ return [w.dateField];
1180
+ case "chart":
1181
+ return [w.groupBy, ...w.metric && w.metric.fn !== "count" ? [w.metric.field] : []];
1182
+ case "list":
1183
+ return [];
1184
+ }
1185
+ }
1186
+ function resolveWidgetAccess(widget, viewer) {
1187
+ for (const field of fieldsRead(widget)) {
1188
+ if (widget.view.field(field)?.permissions?.read === false) return `You can't see ${widget.view.label(field).toLowerCase()}.`;
1189
+ }
1190
+ return widget.can ? widget.can(viewer) : true;
1191
+ }
1192
+ function resolveWidgetRequest(widget, scope) {
1193
+ const { view } = widget;
1194
+ const preset = widget.preset ? view.definition.presets?.[widget.preset] : void 0;
1195
+ if (widget.preset && !preset) throw new ViewDefinitionError(view.resource, [`no preset "${widget.preset}"`]);
1196
+ const scoped = filterParams(scopeFilter(view, scope));
1197
+ const filters = [...filterParams(preset?.filter), ...scoped, ...filterParams(widget.filter)];
1198
+ const orderBy = view.definition.orderBy;
1199
+ const viewSort = orderBy ? { field: orderBy.field, order: orderBy.direction ?? "asc", ...orderBy.nulls ? { nulls: orderBy.nulls } : {} } : void 0;
1200
+ switch (widget.kind) {
1201
+ case "number":
1202
+ return { resource: view.resource, filters, aggregate: { metrics: { value: widget.metric ?? { fn: "count" } }, filters } };
1203
+ case "progress": {
1204
+ const request = { resource: view.resource, filters, aggregate: { metrics: { value: widget.metric ?? { fn: "count" } }, filters } };
1205
+ if (typeof widget.goal === "object") {
1206
+ const goalFilters = [...scoped, ...filterParams(widget.goalFilter)];
1207
+ request.goal = { metrics: { goal: widget.goal }, filters: goalFilters };
1208
+ }
1209
+ return request;
1210
+ }
1211
+ case "streak":
1212
+ return { resource: view.resource, filters, rows: { perPage: widget.limit ?? 400, sort: { field: widget.dateField, order: "desc", nulls: "last" } } };
1213
+ case "list": {
1214
+ const sort = widget.sort ?? viewSort;
1215
+ return { resource: view.resource, filters, rows: { perPage: widget.limit ?? 5, ...sort ? { sort } : {} } };
1216
+ }
1217
+ case "chart":
1218
+ return { resource: view.resource, filters, rows: { perPage: widget.limit ?? 1e3 } };
1219
+ }
1220
+ }
1221
+ function dayOfValue(value, zone) {
1222
+ if (typeof value === "string" && isDay(value)) return value;
1223
+ if (typeof value !== "string" && !(value instanceof Date) && typeof value !== "number") return void 0;
1224
+ const at = value instanceof Date ? value : new Date(value);
1225
+ return Number.isNaN(at.getTime()) ? void 0 : dayIn(zone, at);
1226
+ }
1227
+ function resolveStreak(values, options = {}) {
1228
+ const unit = options.unit ?? "day";
1229
+ const zone = options.timeZone ?? deviceTimeZone();
1230
+ const step = unit === "day" ? 1 : 7;
1231
+ const bucket = (day) => unit === "day" ? day : startOfWeek(day, 1);
1232
+ const seen = /* @__PURE__ */ new Set();
1233
+ for (const value of values) {
1234
+ const day = dayOfValue(value, zone);
1235
+ if (day) seen.add(bucket(day));
1236
+ }
1237
+ const now = bucket(options.today ?? dayIn(zone));
1238
+ const today = seen.has(now);
1239
+ let at = today ? now : addDays(now, -step);
1240
+ let current = 0;
1241
+ while (seen.has(at)) {
1242
+ current++;
1243
+ at = addDays(at, -step);
1244
+ }
1245
+ return { current, today, unit };
1246
+ }
1247
+ function resolveChartSeries(widget, rows) {
1248
+ const groups = /* @__PURE__ */ new Map();
1249
+ for (const row of rows) {
1250
+ const raw = row[widget.groupBy];
1251
+ const key = raw === null || raw === void 0 || raw === "" ? "(none)" : String(raw);
1252
+ const list = groups.get(key) ?? [];
1253
+ list.push(row);
1254
+ groups.set(key, list);
1255
+ }
1256
+ const choices = widget.view.field(widget.groupBy)?.enumValues ?? [];
1257
+ const keys = [
1258
+ ...choices.filter((c) => groups.has(c)),
1259
+ ...[...groups.keys()].filter((k) => !choices.includes(k)).sort((a, b) => a.localeCompare(b, void 0, { numeric: true }))
1260
+ ];
1261
+ const metric = widget.metric ?? { fn: "count" };
1262
+ return keys.map((key) => {
1263
+ const list = groups.get(key);
1264
+ const value = toAggregateValues(list, { value: metric })["value"] ?? 0;
1265
+ return { key, label: key === "(none)" ? "None" : widget.view.valueLabel(widget.groupBy, key), value };
1266
+ });
1267
+ }
1268
+ function toWidgetText(value, format = "number", locale) {
1269
+ if (value === null || value === void 0 || !Number.isFinite(value)) return "\u2014";
1270
+ if (typeof format === "function") return format(value);
1271
+ if (typeof format === "object") {
1272
+ return new Intl.NumberFormat(locale, {
1273
+ style: "currency",
1274
+ currency: format.currency,
1275
+ ...format.maximumFractionDigits !== void 0 ? { maximumFractionDigits: format.maximumFractionDigits } : {}
1276
+ }).format(value);
1277
+ }
1278
+ switch (format) {
1279
+ case "compact":
1280
+ return new Intl.NumberFormat(locale, { notation: "compact", maximumFractionDigits: 1 }).format(value);
1281
+ case "percent":
1282
+ return new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 0 }).format(value);
1283
+ default:
1284
+ return new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }).format(value);
1285
+ }
1286
+ }
1287
+ function resolveProgress(value, goal) {
1288
+ if (value === null || value === void 0 || goal === null || goal === void 0 || !(goal > 0)) return null;
1289
+ return Math.max(0, value / goal);
1290
+ }
1291
+
1292
+ export { RECORD_OPEN_MODES, ViewDefinitionError, boardChoices, boardProblems, calendarProblems, cascadeSelection, defineRecordView, defineView, defineWidget, defineWorkspace, deriveDisplay, filterParams, galleryProblems, groupRows, initialExpanded, initialsOf, isRecordView, isWidget, isWorkspace, plural, resolveChartSeries, resolveProgress, resolveStreak, resolveWidgetAccess, resolveWidgetRequest, rowPicture, sameFormValue, scopeFilter, scopeValues, setExpanded, singular, toWidgetText, treeRows, valueText, widgetProblems };
1047
1293
  //# sourceMappingURL=index.js.map
1048
1294
  //# sourceMappingURL=index.js.map