@akanjs/store 0.0.38 → 0.0.40

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.
@@ -0,0 +1,1244 @@
1
+ "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var storeDecorators_exports = {};
21
+ __export(storeDecorators_exports, {
22
+ MixStore: () => MixStore,
23
+ Store: () => Store,
24
+ Toast: () => Toast,
25
+ createActions: () => createActions,
26
+ createState: () => createState,
27
+ makeStore: () => makeStore,
28
+ rootStoreOf: () => rootStoreOf,
29
+ scalarStateOf: () => scalarStateOf,
30
+ st: () => st,
31
+ stateOf: () => stateOf
32
+ });
33
+ module.exports = __toCommonJS(storeDecorators_exports);
34
+ var import_base = require("@akanjs/base");
35
+ var import_common = require("@akanjs/common");
36
+ var import_constant = require("@akanjs/constant");
37
+ var import_dictionary = require("@akanjs/dictionary");
38
+ var import_signal = require("@akanjs/signal");
39
+ var import_react = require("react");
40
+ var import_zustand = require("zustand");
41
+ var import_middleware = require("zustand/middleware");
42
+ var import_immer = require("zustand/middleware/immer");
43
+ const st = {};
44
+ let StoreStorage = class StoreStorage2 {
45
+ static {
46
+ __name(this, "StoreStorage");
47
+ }
48
+ };
49
+ const getStoreMeta = /* @__PURE__ */ __name((storeName) => {
50
+ const storeMeta = Reflect.getMetadata(storeName, StoreStorage.prototype);
51
+ if (!storeMeta) throw new Error(`storeMeta is not defined: ${storeName}`);
52
+ return storeMeta;
53
+ }, "getStoreMeta");
54
+ const setStoreMeta = /* @__PURE__ */ __name((storeName, storeMeta) => {
55
+ Reflect.defineMetadata(storeName, storeMeta, StoreStorage.prototype);
56
+ }, "setStoreMeta");
57
+ const getStoreNames = /* @__PURE__ */ __name(() => {
58
+ const storeNames = Reflect.getMetadataKeys(StoreStorage.prototype);
59
+ if (!storeNames) throw new Error(`storeNames is not defined`);
60
+ return storeNames;
61
+ }, "getStoreNames");
62
+ const createState = /* @__PURE__ */ __name((gql) => {
63
+ const [fieldName, className] = [
64
+ (0, import_common.lowerlize)(gql.refName),
65
+ (0, import_common.capitalize)(gql.refName)
66
+ ];
67
+ const names = {
68
+ model: fieldName,
69
+ Model: className,
70
+ modelLoading: `${fieldName}Loading`,
71
+ modelForm: `${fieldName}Form`,
72
+ modelFormLoading: `${fieldName}FormLoading`,
73
+ modelSubmit: `${fieldName}Submit`,
74
+ modelViewAt: `${fieldName}ViewAt`,
75
+ modelModal: `${fieldName}Modal`,
76
+ modelOperation: `${fieldName}Operation`,
77
+ defaultModel: `default${className}`,
78
+ defaultModelInsight: `default${className}Insight`,
79
+ modelList: `${fieldName}List`,
80
+ modelListLoading: `${fieldName}ListLoading`,
81
+ modelInitList: `${fieldName}InitList`,
82
+ modelInitAt: `${fieldName}InitAt`,
83
+ modelSelection: `${fieldName}Selection`,
84
+ modelInsight: `${fieldName}Insight`,
85
+ lastPageOfModel: `lastPageOf${className}`,
86
+ pageOfModel: `pageOf${className}`,
87
+ limitOfModel: `limitOf${className}`,
88
+ queryArgsOfModel: `queryArgsOf${className}`,
89
+ sortOfModel: `sortOf${className}`
90
+ };
91
+ const baseState = {
92
+ [names.model]: null,
93
+ [names.modelLoading]: true,
94
+ [names.modelForm]: {
95
+ ...gql[names.defaultModel]
96
+ },
97
+ [names.modelFormLoading]: true,
98
+ [names.modelSubmit]: {
99
+ disabled: true,
100
+ loading: false,
101
+ times: 0
102
+ },
103
+ [names.modelViewAt]: /* @__PURE__ */ new Date(0),
104
+ [names.modelModal]: null,
105
+ [names.modelOperation]: "sleep"
106
+ };
107
+ const sliceState = gql.slices.reduce((acc, { sliceName, defaultArgs }) => {
108
+ const SliceName = (0, import_common.capitalize)(sliceName);
109
+ const namesOfSlice = {
110
+ defaultModel: SliceName.replace(names.Model, names.defaultModel),
111
+ modelList: sliceName.replace(names.model, names.modelList),
112
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
113
+ modelInitList: sliceName.replace(names.model, names.modelInitList),
114
+ modelInitAt: sliceName.replace(names.model, names.modelInitAt),
115
+ modelSelection: sliceName.replace(names.model, names.modelSelection),
116
+ modelInsight: sliceName.replace(names.model, names.modelInsight),
117
+ lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
118
+ pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
119
+ limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
120
+ queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
121
+ sortOfModel: SliceName.replace(names.Model, names.sortOfModel)
122
+ };
123
+ const singleSliceState = {
124
+ [namesOfSlice.defaultModel]: {
125
+ ...gql[names.defaultModel]
126
+ },
127
+ [namesOfSlice.modelList]: new import_base.DataList(),
128
+ [namesOfSlice.modelListLoading]: true,
129
+ [namesOfSlice.modelInitList]: new import_base.DataList(),
130
+ [namesOfSlice.modelInitAt]: /* @__PURE__ */ new Date(0),
131
+ [namesOfSlice.modelSelection]: new import_base.DataList(),
132
+ [namesOfSlice.modelInsight]: gql[names.defaultModelInsight],
133
+ [namesOfSlice.lastPageOfModel]: 1,
134
+ [namesOfSlice.pageOfModel]: 1,
135
+ [namesOfSlice.limitOfModel]: 20,
136
+ [namesOfSlice.queryArgsOfModel]: defaultArgs,
137
+ [namesOfSlice.sortOfModel]: "latest"
138
+ };
139
+ return Object.assign(acc, singleSliceState);
140
+ }, {});
141
+ return {
142
+ ...baseState,
143
+ ...sliceState
144
+ };
145
+ }, "createState");
146
+ const createActions = /* @__PURE__ */ __name((gql) => {
147
+ const formSetterActions = makeFormSetter(gql);
148
+ const baseActions = makeActions(gql);
149
+ return {
150
+ ...formSetterActions,
151
+ ...baseActions
152
+ };
153
+ }, "createActions");
154
+ const makeFormSetter = /* @__PURE__ */ __name((gql) => {
155
+ const fileGql = (0, import_signal.getGqlOnStorage)("file");
156
+ const [fieldName, className] = [
157
+ (0, import_common.lowerlize)(gql.refName),
158
+ (0, import_common.capitalize)(gql.refName)
159
+ ];
160
+ const modelRef = (0, import_constant.getFullModelRef)(gql.refName);
161
+ const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
162
+ const names = {
163
+ model: fieldName,
164
+ Model: className,
165
+ modelForm: `${fieldName}Form`,
166
+ writeOnModel: `writeOn${className}`,
167
+ addModelFiles: `add${className}Files`
168
+ };
169
+ const baseSetAction = {
170
+ [names.writeOnModel]: function(path, value) {
171
+ this.set((state) => {
172
+ (0, import_common.pathSet)(state[names.modelForm], path, value);
173
+ });
174
+ }
175
+ };
176
+ const fieldSetAction = fieldMetas.reduce((acc, fieldMeta) => {
177
+ const [fieldKeyName, classKeyName] = [
178
+ (0, import_common.lowerlize)(fieldMeta.key),
179
+ (0, import_common.capitalize)(fieldMeta.key)
180
+ ];
181
+ const namesOfField = {
182
+ field: fieldKeyName,
183
+ Field: classKeyName,
184
+ setFieldOnModel: `set${classKeyName}On${className}`,
185
+ addFieldOnModel: `add${classKeyName}On${className}`,
186
+ subFieldOnModel: `sub${classKeyName}On${className}`,
187
+ addOrSubFieldOnModel: `addOrSub${classKeyName}On${className}`,
188
+ uploadFieldOnModel: `upload${classKeyName}On${className}`
189
+ };
190
+ const singleFieldSetAction = {
191
+ [namesOfField.setFieldOnModel]: function(value) {
192
+ this.set((state) => {
193
+ const setValue = fieldMeta.isClass ? (0, import_signal.immerify)(fieldMeta.modelRef, value) : value;
194
+ state[names.modelForm][namesOfField.field] = setValue;
195
+ });
196
+ },
197
+ ...fieldMeta.isArray ? {
198
+ [namesOfField.addFieldOnModel]: function(value, options = {}) {
199
+ const form = this.get()[names.modelForm];
200
+ const length = form[namesOfField.field].length;
201
+ if (options.limit && options.limit <= length) return;
202
+ const idx = options.idx ?? length;
203
+ const setValue = fieldMeta.isClass ? (0, import_signal.immerify)(fieldMeta.modelRef, value) : value;
204
+ this.set((state) => {
205
+ state[names.modelForm][namesOfField.field] = [
206
+ ...form[namesOfField.field].slice(0, idx),
207
+ ...Array.isArray(setValue) ? setValue : [
208
+ setValue
209
+ ],
210
+ ...form[namesOfField.field].slice(idx)
211
+ ];
212
+ });
213
+ },
214
+ [namesOfField.subFieldOnModel]: function(idx) {
215
+ const form = this.get()[names.modelForm];
216
+ this.set((state) => {
217
+ state[names.modelForm][namesOfField.field] = typeof idx === "number" ? form[namesOfField.field].filter((_, i) => i !== idx) : form[namesOfField.field].filter((_, i) => !idx.includes(i));
218
+ });
219
+ },
220
+ [namesOfField.addOrSubFieldOnModel]: function(value, options = {}) {
221
+ const { [names.modelForm]: form } = this.get();
222
+ const index = form[namesOfField.field].findIndex((v) => v === value);
223
+ if (index === -1) this[namesOfField.addFieldOnModel](value, options);
224
+ else this[namesOfField.subFieldOnModel](index);
225
+ }
226
+ } : {},
227
+ ...fieldMeta.name === "File" ? {
228
+ [namesOfField.uploadFieldOnModel]: async function(fileList, index) {
229
+ const form = this.get()[names.modelForm];
230
+ if (!fileList.length) return;
231
+ const files = await gql[names.addModelFiles](fileList, form.id);
232
+ if (fieldMeta.isArray) {
233
+ const idx = index ?? form[namesOfField.field].length;
234
+ this.set((state) => {
235
+ state[names.modelForm][namesOfField.field] = [
236
+ ...form[namesOfField.field].slice(0, idx),
237
+ ...files,
238
+ ...form[namesOfField.field].slice(idx)
239
+ ];
240
+ });
241
+ } else {
242
+ this.set((state) => {
243
+ state[names.modelForm][namesOfField.field] = files[0];
244
+ });
245
+ }
246
+ files.map((file) => {
247
+ const intervalKey = setInterval(() => {
248
+ void (async () => {
249
+ const currentFile = await fileGql.file(file.id);
250
+ if (fieldMeta.isArray) this.set((state) => {
251
+ state[names.modelForm][namesOfField.field] = state[names.modelForm][namesOfField.field].map((file2) => file2.id === currentFile.id ? currentFile : file2);
252
+ });
253
+ else this.set((state) => {
254
+ state[names.modelForm][namesOfField.field] = currentFile;
255
+ });
256
+ if (currentFile.status !== "uploading") clearInterval(intervalKey);
257
+ })();
258
+ }, 3e3);
259
+ });
260
+ }
261
+ } : {}
262
+ };
263
+ return Object.assign(acc, singleFieldSetAction);
264
+ }, {});
265
+ return Object.assign(fieldSetAction, baseSetAction);
266
+ }, "makeFormSetter");
267
+ const makeActions = /* @__PURE__ */ __name((gql) => {
268
+ const [fieldName, className] = [
269
+ (0, import_common.lowerlize)(gql.refName),
270
+ (0, import_common.capitalize)(gql.refName)
271
+ ];
272
+ const modelRef = (0, import_constant.getFullModelRef)(className);
273
+ const lightModelRef = (0, import_constant.getLightModelRef)(modelRef);
274
+ const names = {
275
+ model: fieldName,
276
+ _model: `_${fieldName}`,
277
+ Model: className,
278
+ purifyModel: `purify${className}`,
279
+ crystalizeModel: `crystalize${className}`,
280
+ lightCrystalizeModel: `lightCrystalize${className}`,
281
+ crystalizeInsight: `crystalize${className}Insight`,
282
+ modelOperation: `${fieldName}Operation`,
283
+ defaultModel: `default${className}`,
284
+ modelInsight: `${fieldName}Insight`,
285
+ modelForm: `${fieldName}Form`,
286
+ modelSubmit: `${fieldName}Submit`,
287
+ modelLoading: `${fieldName}Loading`,
288
+ modelFormLoading: `${fieldName}FormLoading`,
289
+ modelList: `${fieldName}List`,
290
+ modelListLoading: `${fieldName}ListLoading`,
291
+ modelSelection: `${fieldName}Selection`,
292
+ createModelInForm: `create${className}InForm`,
293
+ updateModelInForm: `modify${className}InForm`,
294
+ createModel: `create${className}`,
295
+ updateModel: `update${className}`,
296
+ removeModel: `remove${className}`,
297
+ checkModelSubmitable: `check${className}Submitable`,
298
+ submitModel: `submit${className}`,
299
+ newModel: `new${className}`,
300
+ editModel: `edit${className}`,
301
+ mergeModel: `merge${className}`,
302
+ viewModel: `view${className}`,
303
+ setModel: `set${className}`,
304
+ resetModel: `reset${className}`,
305
+ modelViewAt: `${fieldName}ViewAt`,
306
+ modelModal: `${fieldName}Modal`,
307
+ initModel: `init${className}`,
308
+ modelInitList: `${fieldName}InitList`,
309
+ modelInitAt: `${fieldName}InitAt`,
310
+ refreshModel: `refresh${className}`,
311
+ selectModel: `select${className}`,
312
+ setPageOfModel: `setPageOf${className}`,
313
+ addPageOfModel: `addPageOf${className}`,
314
+ setLimitOfModel: `setLimitOf${className}`,
315
+ setQueryArgsOfModel: `setQueryArgsOf${className}`,
316
+ setSortOfModel: `setSortOf${className}`,
317
+ lastPageOfModel: `lastPageOf${className}`,
318
+ pageOfModel: `pageOf${className}`,
319
+ limitOfModel: `limitOf${className}`,
320
+ queryArgsOfModel: `queryArgsOf${className}`,
321
+ sortOfModel: `sortOf${className}`
322
+ };
323
+ const baseAction = {
324
+ [names.createModelInForm]: async function({ idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
325
+ const SliceName = (0, import_common.capitalize)(sliceName);
326
+ const namesOfSlice = {
327
+ defaultModel: SliceName.replace(names.Model, names.defaultModel),
328
+ modelList: sliceName.replace(names.model, names.modelList),
329
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
330
+ modelInsight: sliceName.replace(names.model, names.modelInsight)
331
+ };
332
+ const currentState = this.get();
333
+ const modelForm = currentState[names.modelForm];
334
+ const modelList = currentState[namesOfSlice.modelList];
335
+ const modelListLoading = currentState[namesOfSlice.modelListLoading];
336
+ const modelInsight = currentState[namesOfSlice.modelInsight];
337
+ const defaultModel = currentState[namesOfSlice.defaultModel];
338
+ const modelInput = gql[names.purifyModel](modelForm);
339
+ if (!modelInput) return;
340
+ this.set({
341
+ [names.modelLoading]: true
342
+ });
343
+ const model = await gql[names.createModel](modelInput, {
344
+ onError
345
+ });
346
+ const newModelList = modelListLoading ? modelList : new import_base.DataList([
347
+ ...modelList.slice(0, idx ?? 0),
348
+ model,
349
+ ...modelList.slice(idx ?? 0)
350
+ ]);
351
+ const newModelInsight = gql[names.crystalizeInsight]({
352
+ ...modelInsight,
353
+ count: modelInsight.count + 1
354
+ });
355
+ this.set({
356
+ [names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
357
+ [names.model]: model,
358
+ [names.modelLoading]: false,
359
+ [namesOfSlice.modelList]: newModelList,
360
+ [namesOfSlice.modelInsight]: newModelInsight,
361
+ [names.modelViewAt]: /* @__PURE__ */ new Date(),
362
+ [names.modelModal]: modal ?? null,
363
+ ...typeof path === "string" && path ? {
364
+ [path]: model
365
+ } : {}
366
+ });
367
+ await onSuccess?.(model);
368
+ },
369
+ [names.updateModelInForm]: async function({ path, modal, sliceName = names.model, onError, onSuccess } = {}) {
370
+ const SliceName = (0, import_common.capitalize)(sliceName);
371
+ const namesOfSlice = {
372
+ defaultModel: SliceName.replace(names.Model, names.defaultModel)
373
+ };
374
+ const currentState = this.get();
375
+ const model = currentState[names.model];
376
+ const modelForm = currentState[names.modelForm];
377
+ const defaultModel = currentState[namesOfSlice.defaultModel];
378
+ const modelInput = gql[names.purifyModel](modelForm);
379
+ if (!modelInput) return;
380
+ if (model?.id === modelForm.id) this.set({
381
+ [names.modelLoading]: modelForm.id
382
+ });
383
+ const updatedModel = await gql[names.updateModel](modelForm.id, modelInput, {
384
+ onError
385
+ });
386
+ this.set({
387
+ ...model?.id === updatedModel.id ? {
388
+ [names.model]: updatedModel,
389
+ [names.modelLoading]: false,
390
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
391
+ } : {},
392
+ [names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
393
+ [names.modelModal]: modal ?? null,
394
+ ...typeof path === "string" && path ? {
395
+ [path]: updatedModel
396
+ } : {}
397
+ });
398
+ const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
399
+ gql.slices.forEach(({ sliceName: sliceName2 }) => {
400
+ const namesOfSlice2 = {
401
+ modelList: sliceName2.replace(names.model, names.modelList),
402
+ modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
403
+ };
404
+ const currentState2 = this.get();
405
+ const modelList = currentState2[namesOfSlice2.modelList];
406
+ const modelListLoading = currentState2[namesOfSlice2.modelListLoading];
407
+ if (modelListLoading || !modelList.has(updatedModel.id)) return;
408
+ const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
409
+ this.set({
410
+ [namesOfSlice2.modelList]: newModelList
411
+ });
412
+ });
413
+ await onSuccess?.(updatedModel);
414
+ },
415
+ [names.createModel]: async function(data, { idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
416
+ const SliceName = (0, import_common.capitalize)(sliceName);
417
+ const namesOfSlice = {
418
+ defaultModel: SliceName.replace(names.Model, names.defaultModel),
419
+ modelList: sliceName.replace(names.model, names.modelList),
420
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
421
+ modelInsight: sliceName.replace(names.model, names.modelInsight)
422
+ };
423
+ const currentState = this.get();
424
+ const modelList = currentState[namesOfSlice.modelList];
425
+ const modelListLoading = currentState[namesOfSlice.modelListLoading];
426
+ const modelInsight = currentState[namesOfSlice.modelInsight];
427
+ const modelInput = gql[names.purifyModel](data);
428
+ if (!modelInput) return;
429
+ this.set({
430
+ [names.modelLoading]: true
431
+ });
432
+ const model = await gql[names.createModel](modelInput, {
433
+ onError
434
+ });
435
+ const newModelList = modelListLoading ? modelList : new import_base.DataList([
436
+ ...modelList.slice(0, idx ?? 0),
437
+ model,
438
+ ...modelList.slice(idx ?? 0)
439
+ ]);
440
+ const newModelInsight = gql[names.crystalizeInsight]({
441
+ ...modelInsight,
442
+ count: modelInsight.count + 1
443
+ });
444
+ this.set({
445
+ [names.model]: model,
446
+ [names.modelLoading]: false,
447
+ [namesOfSlice.modelList]: newModelList,
448
+ [namesOfSlice.modelInsight]: newModelInsight,
449
+ [names.modelViewAt]: /* @__PURE__ */ new Date(),
450
+ [names.modelModal]: modal ?? null,
451
+ ...typeof path === "string" && path ? {
452
+ [path]: model
453
+ } : {}
454
+ });
455
+ await onSuccess?.(model);
456
+ },
457
+ [names.updateModel]: async function(id, data, { idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
458
+ const currentState = this.get();
459
+ const model = currentState[names.model];
460
+ const modelInput = gql[names.purifyModel](data);
461
+ if (!modelInput) return;
462
+ if (model?.id === id) this.set({
463
+ [names.modelLoading]: id
464
+ });
465
+ const updatedModel = await gql[names.updateModel](id, modelInput, {
466
+ onError
467
+ });
468
+ this.set({
469
+ ...model?.id === updatedModel.id ? {
470
+ [names.model]: updatedModel,
471
+ [names.modelLoading]: false,
472
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
473
+ } : {},
474
+ [names.modelModal]: modal ?? null,
475
+ ...typeof path === "string" && path ? {
476
+ [path]: updatedModel
477
+ } : {}
478
+ });
479
+ const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
480
+ gql.slices.forEach(({ sliceName: sliceName2 }) => {
481
+ const namesOfSlice = {
482
+ modelList: sliceName2.replace(names.model, names.modelList),
483
+ modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
484
+ };
485
+ const currentState2 = this.get();
486
+ const modelList = currentState2[namesOfSlice.modelList];
487
+ const modelListLoading = currentState2[namesOfSlice.modelListLoading];
488
+ if (modelListLoading || !modelList.has(updatedModel.id)) return;
489
+ const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
490
+ this.set({
491
+ [namesOfSlice.modelList]: newModelList
492
+ });
493
+ });
494
+ await onSuccess?.(updatedModel);
495
+ },
496
+ [names.removeModel]: async function(id, options) {
497
+ const { modal, ...fetchPolicyOptions } = options ?? {};
498
+ const model = await gql[names.removeModel](id, fetchPolicyOptions);
499
+ const lightModel = gql[names.lightCrystalizeModel](model);
500
+ gql.slices.forEach(({ sliceName }) => {
501
+ const namesOfSlice = {
502
+ modelList: sliceName.replace(names.model, names.modelList),
503
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
504
+ modelSelection: sliceName.replace(names.model, names.modelSelection),
505
+ modelInsight: sliceName.replace(names.model, names.modelInsight)
506
+ };
507
+ const currentState = this.get();
508
+ const modelList = currentState[namesOfSlice.modelList];
509
+ const modelListLoading = currentState[namesOfSlice.modelListLoading];
510
+ const modelSelection = currentState[namesOfSlice.modelSelection];
511
+ const modelInsight = currentState[namesOfSlice.modelInsight];
512
+ if (modelListLoading || !modelList.has(model.id)) return;
513
+ const newModelList = new import_base.DataList(modelList);
514
+ if (model.removedAt) {
515
+ newModelList.delete(id);
516
+ const newModelInsight = gql[names.crystalizeInsight]({
517
+ ...modelInsight,
518
+ count: modelInsight.count - 1
519
+ });
520
+ const newModelSelection = new import_base.DataList(modelSelection);
521
+ newModelSelection.delete(id);
522
+ this.set({
523
+ [namesOfSlice.modelList]: newModelList,
524
+ [namesOfSlice.modelInsight]: newModelInsight,
525
+ ...modelSelection.has(model.id) ? {
526
+ [namesOfSlice.modelSelection]: newModelSelection
527
+ } : {},
528
+ ...modal !== void 0 ? {
529
+ [names.modelModal]: modal
530
+ } : {}
531
+ });
532
+ } else {
533
+ newModelList.set(lightModel);
534
+ this.set({
535
+ [namesOfSlice.modelList]: newModelList,
536
+ ...modal !== void 0 ? {
537
+ [names.modelModal]: modal
538
+ } : {}
539
+ });
540
+ }
541
+ });
542
+ },
543
+ [names.checkModelSubmitable]: function(disabled) {
544
+ const currentState = this.get();
545
+ const modelForm = currentState[names.modelForm];
546
+ const modelSubmit = currentState[names.modelSubmit];
547
+ const modelInput = gql[names.purifyModel](modelForm);
548
+ this.set({
549
+ [names.modelSubmit]: {
550
+ ...modelSubmit,
551
+ disabled: !modelInput || disabled
552
+ }
553
+ });
554
+ },
555
+ [names.submitModel]: async function(option) {
556
+ const currentState = this.get();
557
+ const modelForm = currentState[names.modelForm];
558
+ const modelSubmit = currentState[names.modelSubmit];
559
+ this.set({
560
+ [names.modelSubmit]: {
561
+ ...modelSubmit,
562
+ loading: true
563
+ }
564
+ });
565
+ if (modelForm.id) await this[names.updateModelInForm](option);
566
+ else await this[names.createModelInForm](option);
567
+ this.set({
568
+ [names.modelSubmit]: {
569
+ ...modelSubmit,
570
+ loading: false,
571
+ times: modelSubmit.times + 1
572
+ }
573
+ });
574
+ },
575
+ [names.newModel]: function(partial = {}, { modal, setDefault, sliceName = names.model } = {}) {
576
+ const SliceName = (0, import_common.capitalize)(sliceName);
577
+ const namesOfSlice = {
578
+ defaultModel: SliceName.replace(names.Model, names.defaultModel)
579
+ };
580
+ const currentState = this.get();
581
+ const defaultModel = currentState[namesOfSlice.defaultModel];
582
+ this.set({
583
+ [names.modelForm]: (0, import_signal.immerify)(modelRef, {
584
+ ...defaultModel,
585
+ ...partial
586
+ }),
587
+ [namesOfSlice.defaultModel]: setDefault ? (0, import_signal.immerify)(modelRef, {
588
+ ...defaultModel,
589
+ ...partial
590
+ }) : defaultModel,
591
+ [names.model]: null,
592
+ [names.modelModal]: modal ?? "edit",
593
+ [names.modelFormLoading]: false
594
+ });
595
+ },
596
+ [names.editModel]: async function(modelOrId, { modal, onError } = {}) {
597
+ const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
598
+ this.set({
599
+ [names.modelFormLoading]: id,
600
+ [names.modelModal]: modal ?? "edit"
601
+ });
602
+ this.set((state) => {
603
+ state[names.modelForm].id = id;
604
+ });
605
+ const model = await gql[names.model](id, {
606
+ onError
607
+ });
608
+ const modelForm = (0, import_common.deepObjectify)(model);
609
+ this.set({
610
+ [names.model]: model,
611
+ [names.modelFormLoading]: false,
612
+ [names.modelViewAt]: /* @__PURE__ */ new Date(),
613
+ [names.modelForm]: modelForm
614
+ });
615
+ },
616
+ [names.mergeModel]: async function(modelOrId, data, options) {
617
+ const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
618
+ const currentState = this.get();
619
+ const model = currentState[names.model];
620
+ if (id === model?.id) this.set({
621
+ modelLoading: id
622
+ });
623
+ const updatedModel = await gql[names.mergeModel](modelOrId, data, options);
624
+ this.set({
625
+ [names.model]: id === model?.id ? updatedModel : model,
626
+ [names.modelLoading]: false
627
+ });
628
+ const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
629
+ gql.slices.forEach(({ sliceName }) => {
630
+ const namesOfSlice = {
631
+ modelList: sliceName.replace(names.model, names.modelList),
632
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading)
633
+ };
634
+ const currentState2 = this.get();
635
+ const modelList = currentState2[namesOfSlice.modelList];
636
+ const modelListLoading = currentState2[namesOfSlice.modelListLoading];
637
+ if (modelListLoading || !modelList.has(updatedModel.id)) return;
638
+ const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
639
+ this.set({
640
+ [namesOfSlice.modelList]: newModelList
641
+ });
642
+ });
643
+ },
644
+ [names.viewModel]: async function(modelOrId, { modal, onError } = {}) {
645
+ const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
646
+ this.set({
647
+ [names.modelModal]: modal ?? "view",
648
+ [names.modelLoading]: id
649
+ });
650
+ const model = await gql[names.model](id, {
651
+ onError
652
+ });
653
+ this.set({
654
+ [names.model]: model,
655
+ [names.modelViewAt]: /* @__PURE__ */ new Date(),
656
+ [names.modelLoading]: false
657
+ });
658
+ },
659
+ [names.setModel]: function(fullOrLightModel) {
660
+ const currentState = this.get();
661
+ const model = currentState[names.model];
662
+ const isFull = fullOrLightModel instanceof modelRef;
663
+ if (isFull) {
664
+ const crystalizedModel = gql[names.crystalizeModel](fullOrLightModel);
665
+ this.set({
666
+ [names.model]: crystalizedModel
667
+ });
668
+ } else if (model?.id === fullOrLightModel.id) {
669
+ const crystalizedModel = gql[names.crystalizeModel]({
670
+ ...model,
671
+ ...fullOrLightModel
672
+ });
673
+ this.set({
674
+ [names.model]: crystalizedModel
675
+ });
676
+ }
677
+ const lightModel = gql[names.lightCrystalizeModel](fullOrLightModel);
678
+ gql.slices.forEach(({ sliceName }) => {
679
+ const namesOfSlice = {
680
+ modelList: sliceName.replace(names.model, names.modelList),
681
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading)
682
+ };
683
+ const modelList = currentState[namesOfSlice.modelList];
684
+ const modelListLoading = currentState[namesOfSlice.modelListLoading];
685
+ if (modelListLoading || !modelList.has(lightModel.id)) return;
686
+ this.set({
687
+ [namesOfSlice.modelList]: modelList.set(lightModel).save()
688
+ });
689
+ });
690
+ },
691
+ [names.resetModel]: function(model) {
692
+ const currentState = this.get();
693
+ const defaultModel = currentState[names.defaultModel];
694
+ this.set({
695
+ [names.model]: model ?? null,
696
+ [names.modelViewAt]: /* @__PURE__ */ new Date(0),
697
+ [names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
698
+ [names.modelModal]: null
699
+ });
700
+ return model ?? null;
701
+ }
702
+ };
703
+ const sliceAction = gql.slices.reduce((acc, { sliceName, argLength }) => {
704
+ const SliceName = (0, import_common.capitalize)(sliceName);
705
+ const namesOfSlice = {
706
+ defaultModel: SliceName.replace(names.Model, names.defaultModel),
707
+ modelInsight: sliceName.replace(names.model, names.modelInsight),
708
+ modelList: sliceName.replace(names.model, names.modelList),
709
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
710
+ initModel: SliceName.replace(names.Model, names.initModel),
711
+ modelInitList: SliceName.replace(names.Model, names.modelInitList),
712
+ modelInitAt: SliceName.replace(names.Model, names.modelInitAt),
713
+ refreshModel: SliceName.replace(names.Model, names.refreshModel),
714
+ selectModel: SliceName.replace(names.Model, names.selectModel),
715
+ setPageOfModel: SliceName.replace(names.Model, names.setPageOfModel),
716
+ addPageOfModel: SliceName.replace(names.Model, names.addPageOfModel),
717
+ setLimitOfModel: SliceName.replace(names.Model, names.setLimitOfModel),
718
+ setQueryArgsOfModel: SliceName.replace(names.Model, names.setQueryArgsOfModel),
719
+ setSortOfModel: SliceName.replace(names.Model, names.setSortOfModel),
720
+ lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
721
+ pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
722
+ limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
723
+ queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
724
+ sortOfModel: SliceName.replace(names.Model, names.sortOfModel),
725
+ modelSelection: SliceName.replace(names.Model, names.modelSelection)
726
+ };
727
+ const singleSliceAction = {
728
+ [namesOfSlice.initModel]: async function(...args) {
729
+ const initArgLength = Math.min(args.length, argLength);
730
+ const initForm = {
731
+ invalidate: false,
732
+ ...args[argLength] ?? {}
733
+ };
734
+ const queryArgs = new Array(initArgLength).fill(null).map((_, i) => args[i]);
735
+ const defaultModel = (0, import_signal.immerify)(modelRef, {
736
+ ...gql[names.defaultModel],
737
+ ...initForm.default ?? {}
738
+ });
739
+ this.set({
740
+ [names.defaultModel]: defaultModel
741
+ });
742
+ await this[namesOfSlice.refreshModel](...initArgLength === argLength ? [
743
+ ...queryArgs,
744
+ initForm
745
+ ] : queryArgs);
746
+ },
747
+ [namesOfSlice.refreshModel]: async function(...args) {
748
+ const refreshArgLength = Math.min(args.length, argLength);
749
+ const currentState = this.get();
750
+ const existingQueryArgs = currentState[namesOfSlice.queryArgsOfModel];
751
+ const queryArgs = [
752
+ ...new Array(refreshArgLength).fill(null).map((_, i) => args[i]),
753
+ ...existingQueryArgs.slice(refreshArgLength, argLength)
754
+ ];
755
+ const initForm = args[argLength] ?? {};
756
+ const { page = currentState[namesOfSlice.pageOfModel], limit = currentState[namesOfSlice.limitOfModel], sort = currentState[namesOfSlice.sortOfModel], invalidate = true } = initForm;
757
+ const modelOperation = currentState[names.modelOperation];
758
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
759
+ const pageOfModel = currentState[namesOfSlice.pageOfModel];
760
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
761
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
762
+ if (!invalidate && ![
763
+ "sleep",
764
+ "reset"
765
+ ].includes(modelOperation) && (0, import_common.isQueryEqual)(queryArgs, queryArgsOfModel) && page === pageOfModel && limit === limitOfModel && (0, import_common.isQueryEqual)(sort, sortOfModel)) return;
766
+ else this.set({
767
+ [namesOfSlice.modelListLoading]: true
768
+ });
769
+ const [modelDataList, modelInsight] = await Promise.all([
770
+ gql[namesOfSlice.modelList](...queryArgs, (page - 1) * limit, limit, sort, {
771
+ onError: initForm.onError
772
+ }),
773
+ gql[namesOfSlice.modelInsight](...queryArgs, {
774
+ onError: initForm.onError
775
+ })
776
+ ]);
777
+ const modelList = new import_base.DataList(modelDataList);
778
+ this.set({
779
+ [namesOfSlice.modelList]: modelList,
780
+ [namesOfSlice.modelListLoading]: false,
781
+ [namesOfSlice.modelInsight]: modelInsight,
782
+ [namesOfSlice.modelInitList]: modelList,
783
+ [namesOfSlice.modelInitAt]: /* @__PURE__ */ new Date(),
784
+ [namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limit) + 1, 1),
785
+ [namesOfSlice.limitOfModel]: limit,
786
+ [namesOfSlice.queryArgsOfModel]: queryArgs,
787
+ [namesOfSlice.sortOfModel]: sort,
788
+ [namesOfSlice.pageOfModel]: page,
789
+ [names.modelOperation]: "idle"
790
+ });
791
+ },
792
+ [namesOfSlice.selectModel]: function(model, { refresh, remove } = {}) {
793
+ const models = Array.isArray(model) ? model : [
794
+ model
795
+ ];
796
+ const currentState = this.get();
797
+ const modelSelection = currentState[namesOfSlice.modelSelection];
798
+ if (refresh) this.set({
799
+ [namesOfSlice.modelSelection]: new import_base.DataList(models)
800
+ });
801
+ else if (remove) {
802
+ const newModelSelection = new import_base.DataList(modelSelection);
803
+ models.map((model2) => newModelSelection.delete(model2.id));
804
+ this.set({
805
+ [namesOfSlice.modelSelection]: newModelSelection
806
+ });
807
+ } else {
808
+ this.set({
809
+ [namesOfSlice.modelSelection]: new import_base.DataList([
810
+ ...modelSelection.values,
811
+ ...models
812
+ ])
813
+ });
814
+ }
815
+ },
816
+ [namesOfSlice.setPageOfModel]: async function(page, options) {
817
+ const currentState = this.get();
818
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
819
+ const pageOfModel = currentState[namesOfSlice.pageOfModel];
820
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
821
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
822
+ if (pageOfModel === page) return;
823
+ this.set({
824
+ [namesOfSlice.modelListLoading]: true
825
+ });
826
+ const modelDataList = await gql[namesOfSlice.modelList](...queryArgsOfModel, (page - 1) * limitOfModel, limitOfModel, sortOfModel, options);
827
+ const modelList = new import_base.DataList(modelDataList);
828
+ this.set({
829
+ [namesOfSlice.modelList]: modelList,
830
+ [namesOfSlice.pageOfModel]: page,
831
+ [namesOfSlice.modelListLoading]: false
832
+ });
833
+ },
834
+ [namesOfSlice.addPageOfModel]: async function(page, options) {
835
+ const currentState = this.get();
836
+ const modelList = currentState[namesOfSlice.modelList];
837
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
838
+ const pageOfModel = currentState[namesOfSlice.pageOfModel];
839
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
840
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
841
+ if (pageOfModel === page) return;
842
+ const addFront = page < pageOfModel;
843
+ const modelDataList = await gql[namesOfSlice.modelList](...queryArgsOfModel, (page - 1) * limitOfModel, limitOfModel, sortOfModel, options);
844
+ const newModelList = new import_base.DataList(addFront ? [
845
+ ...modelDataList,
846
+ ...modelList
847
+ ] : [
848
+ ...modelList,
849
+ ...modelDataList
850
+ ]);
851
+ this.set({
852
+ [namesOfSlice.modelList]: newModelList,
853
+ [namesOfSlice.pageOfModel]: page
854
+ });
855
+ },
856
+ [namesOfSlice.setLimitOfModel]: async function(limit, options) {
857
+ const currentState = this.get();
858
+ const modelInsight = currentState[namesOfSlice.modelInsight];
859
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
860
+ const pageOfModel = currentState[namesOfSlice.pageOfModel];
861
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
862
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
863
+ if (limitOfModel === limit) return;
864
+ const skip = (pageOfModel - 1) * limitOfModel;
865
+ const page = Math.max(Math.floor((skip - 1) / limit) + 1, 1);
866
+ const modelDataList = await gql[namesOfSlice.modelList](...queryArgsOfModel, (page - 1) * limit, limit, sortOfModel, options);
867
+ const modelList = new import_base.DataList(modelDataList);
868
+ this.set({
869
+ [namesOfSlice.modelList]: modelList,
870
+ [namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limit) + 1, 1),
871
+ [namesOfSlice.limitOfModel]: limit,
872
+ [namesOfSlice.pageOfModel]: page
873
+ });
874
+ },
875
+ [namesOfSlice.setQueryArgsOfModel]: async function(...args) {
876
+ const queryArgs = new Array(argLength).fill(null).map((_, i) => args[i]);
877
+ const options = args[argLength];
878
+ const currentState = this.get();
879
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
880
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
881
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
882
+ if ((0, import_common.isQueryEqual)(queryArgsOfModel, queryArgs)) {
883
+ import_common.Logger.trace(`${namesOfSlice.queryArgsOfModel} store-level cache hit`);
884
+ return;
885
+ }
886
+ this.set({
887
+ [namesOfSlice.modelListLoading]: true
888
+ });
889
+ const [modelDataList, modelInsight] = await Promise.all([
890
+ gql[namesOfSlice.modelList](...queryArgs, 0, limitOfModel, sortOfModel, options),
891
+ gql[namesOfSlice.modelInsight](...queryArgs, options)
892
+ ]);
893
+ const modelList = new import_base.DataList(modelDataList);
894
+ this.set({
895
+ [namesOfSlice.queryArgsOfModel]: queryArgs,
896
+ [namesOfSlice.modelList]: modelList,
897
+ [namesOfSlice.modelInsight]: modelInsight,
898
+ [namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limitOfModel) + 1, 1),
899
+ [namesOfSlice.pageOfModel]: 1,
900
+ [namesOfSlice.modelSelection]: /* @__PURE__ */ new Map(),
901
+ [namesOfSlice.modelListLoading]: false
902
+ });
903
+ },
904
+ [namesOfSlice.setSortOfModel]: async function(sort, options) {
905
+ const currentState = this.get();
906
+ const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
907
+ const limitOfModel = currentState[namesOfSlice.limitOfModel];
908
+ const sortOfModel = currentState[namesOfSlice.sortOfModel];
909
+ if (sortOfModel === sort) return;
910
+ this.set({
911
+ [namesOfSlice.modelListLoading]: true
912
+ });
913
+ const modelDataList = await gql[namesOfSlice.modelList](...queryArgsOfModel, 0, limitOfModel, sort, options);
914
+ const modelList = new import_base.DataList(modelDataList);
915
+ this.set({
916
+ [namesOfSlice.modelList]: modelList,
917
+ [namesOfSlice.sortOfModel]: sort,
918
+ [namesOfSlice.pageOfModel]: 1,
919
+ [namesOfSlice.modelListLoading]: false
920
+ });
921
+ }
922
+ };
923
+ return Object.assign(acc, singleSliceAction);
924
+ }, {});
925
+ return {
926
+ ...baseAction,
927
+ ...sliceAction
928
+ };
929
+ }, "makeActions");
930
+ const stateOf = /* @__PURE__ */ __name((gql, state) => {
931
+ const applyState = Object.assign(createState(gql), state);
932
+ const applyAction = createActions(gql);
933
+ setStoreMeta(gql.refName, {
934
+ refName: gql.refName,
935
+ useKeys: Object.keys(applyState),
936
+ doKeys: Object.keys(applyAction),
937
+ slices: gql.slices
938
+ });
939
+ const applyStore = {
940
+ ...applyState,
941
+ ...applyAction
942
+ };
943
+ let StateStore = class StateStore {
944
+ static {
945
+ __name(this, "StateStore");
946
+ }
947
+ get;
948
+ set;
949
+ pick;
950
+ };
951
+ Object.keys(applyStore).forEach((key) => Object.defineProperty(StateStore.prototype, key, {
952
+ value: applyStore[key]
953
+ }));
954
+ return StateStore;
955
+ }, "stateOf");
956
+ const scalarStateOf = /* @__PURE__ */ __name((refName, state) => {
957
+ const applyState = state;
958
+ setStoreMeta(refName, {
959
+ refName,
960
+ useKeys: Object.keys(applyState),
961
+ doKeys: [],
962
+ slices: []
963
+ });
964
+ let StateStore = class StateStore {
965
+ static {
966
+ __name(this, "StateStore");
967
+ }
968
+ get;
969
+ set;
970
+ pick;
971
+ };
972
+ Object.keys(applyState).forEach((key) => Object.defineProperty(StateStore.prototype, key, {
973
+ value: applyState[key]
974
+ }));
975
+ return StateStore;
976
+ }, "scalarStateOf");
977
+ const Store = /* @__PURE__ */ __name((returnsOrObj) => {
978
+ const refName = typeof returnsOrObj === "object" ? returnsOrObj.name : (0, import_common.lowerlize)((0, import_constant.getClassMeta)(returnsOrObj()).refName);
979
+ const storeMeta = getStoreMeta(refName);
980
+ return function(target) {
981
+ const customDoKeys = Object.getOwnPropertyNames(target.prototype).filter((key) => key !== "constructor");
982
+ setStoreMeta(refName, {
983
+ ...storeMeta,
984
+ doKeys: [
985
+ ...storeMeta.doKeys,
986
+ ...customDoKeys
987
+ ]
988
+ });
989
+ };
990
+ }, "Store");
991
+ const createSelectors = /* @__PURE__ */ __name((_store, store = {}) => {
992
+ store.get = _store.getState;
993
+ store.set = (s) => {
994
+ if (typeof s === "function") _store.setState((st2) => {
995
+ s(st2);
996
+ });
997
+ else _store.setState(s);
998
+ };
999
+ store.sel = (selectFn, equals) => _store(selectFn, equals);
1000
+ const state = store.get();
1001
+ store.sub = _store.subscribe;
1002
+ const useReference = /* @__PURE__ */ __name((selectFn) => {
1003
+ const ref = (0, import_react.useRef)(selectFn(store.get()));
1004
+ (0, import_react.useEffect)(() => {
1005
+ return store.sub(selectFn, (val) => ref.current = val);
1006
+ }, []);
1007
+ return ref;
1008
+ }, "useReference");
1009
+ store.ref = useReference;
1010
+ const existingUse = store.use;
1011
+ const existingDo = store.do;
1012
+ const existingSlice = store.slice;
1013
+ if (!existingUse) Object.assign(store, {
1014
+ use: {}
1015
+ });
1016
+ if (!existingDo) Object.assign(store, {
1017
+ do: {}
1018
+ });
1019
+ if (!existingSlice) Object.assign(store, {
1020
+ slice: {}
1021
+ });
1022
+ for (const k of Object.keys(state)) {
1023
+ if (typeof state[k] !== "function") {
1024
+ store.use[k] = () => store.sel((s) => s[k]);
1025
+ const setKey = `set${(0, import_common.capitalize)(k)}`;
1026
+ if (!state[setKey]) store.do[setKey] = (value) => {
1027
+ store.set({
1028
+ [k]: value
1029
+ });
1030
+ };
1031
+ } else {
1032
+ store.do[k] = async (...args) => {
1033
+ try {
1034
+ import_common.Logger.verbose(`${k} action loading...`);
1035
+ const start = Date.now();
1036
+ await state[k](...args);
1037
+ const end = Date.now();
1038
+ import_common.Logger.verbose(`=> ${k} action dispatched (${end - start}ms)`);
1039
+ } catch (e) {
1040
+ const errKey = typeof e === "string" ? e : e.message;
1041
+ import_dictionary.msg.error(errKey, {
1042
+ key: k
1043
+ });
1044
+ throw e;
1045
+ }
1046
+ };
1047
+ }
1048
+ }
1049
+ const storeNames = getStoreNames();
1050
+ for (const storeName of storeNames) {
1051
+ const [fieldName, className] = [
1052
+ (0, import_common.lowerlize)(storeName),
1053
+ (0, import_common.capitalize)(storeName)
1054
+ ];
1055
+ const names = {
1056
+ model: fieldName,
1057
+ Model: className,
1058
+ defaultModel: `default${className}`,
1059
+ modelInsight: `${fieldName}Insight`,
1060
+ modelList: `${fieldName}List`,
1061
+ modelListLoading: `${fieldName}ListLoading`,
1062
+ modelInitList: `${fieldName}InitList`,
1063
+ modelInitAt: `${fieldName}InitAt`,
1064
+ pageOfModel: `pageOf${className}`,
1065
+ limitOfModel: `limitOf${className}`,
1066
+ queryArgsOfModel: `queryArgsOf${className}`,
1067
+ sortOfModel: `sortOf${className}`,
1068
+ modelSelection: `${fieldName}Selection`,
1069
+ initModel: `init${className}`,
1070
+ refreshModel: `refresh${className}`,
1071
+ selectModel: `select${className}`,
1072
+ setPageOfModel: `setPageOf${className}`,
1073
+ addPageOfModel: `addPageOf${className}`,
1074
+ setLimitOfModel: `setLimitOf${className}`,
1075
+ setQueryArgsOfModel: `setQueryArgsOf${className}`,
1076
+ setSortOfModel: `setSortOf${className}`,
1077
+ lastPageOfModel: `lastPageOf${className}`
1078
+ };
1079
+ const storeMeta = getStoreMeta(storeName);
1080
+ storeMeta.slices.forEach(({ sliceName, argLength, refName }) => {
1081
+ const SliceName = (0, import_common.capitalize)(sliceName);
1082
+ const namesOfSliceState = {
1083
+ defaultModel: SliceName.replace(names.Model, names.defaultModel),
1084
+ modelInitList: SliceName.replace(names.Model, names.modelInitList),
1085
+ modelInsight: sliceName.replace(names.model, names.modelInsight),
1086
+ modelList: sliceName.replace(names.model, names.modelList),
1087
+ modelListLoading: sliceName.replace(names.model, names.modelListLoading),
1088
+ modelInitAt: SliceName.replace(names.Model, names.modelInitAt),
1089
+ lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
1090
+ pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
1091
+ limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
1092
+ queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
1093
+ sortOfModel: SliceName.replace(names.Model, names.sortOfModel),
1094
+ modelSelection: SliceName.replace(names.Model, names.modelSelection)
1095
+ };
1096
+ const namesOfSliceAction = {
1097
+ initModel: SliceName.replace(names.Model, names.initModel),
1098
+ refreshModel: SliceName.replace(names.Model, names.refreshModel),
1099
+ selectModel: SliceName.replace(names.Model, names.selectModel),
1100
+ setPageOfModel: SliceName.replace(names.Model, names.setPageOfModel),
1101
+ addPageOfModel: SliceName.replace(names.Model, names.addPageOfModel),
1102
+ setLimitOfModel: SliceName.replace(names.Model, names.setLimitOfModel),
1103
+ setQueryArgsOfModel: SliceName.replace(names.Model, names.setQueryArgsOfModel),
1104
+ setSortOfModel: SliceName.replace(names.Model, names.setSortOfModel)
1105
+ };
1106
+ store.slice[sliceName] = {
1107
+ do: {},
1108
+ use: {}
1109
+ };
1110
+ const targetSlice = store.slice[sliceName];
1111
+ Object.keys(namesOfSliceAction).forEach((key) => {
1112
+ targetSlice.do[names[key]] = store.do[namesOfSliceAction[key]];
1113
+ });
1114
+ Object.keys(namesOfSliceState).map((key) => {
1115
+ targetSlice.use[names[key]] = store.use[namesOfSliceState[key]];
1116
+ targetSlice.do[`set${(0, import_common.capitalize)(names[key])}`] = store.do[`set${(0, import_common.capitalize)(namesOfSliceState[key])}`];
1117
+ });
1118
+ targetSlice.sliceName = sliceName;
1119
+ targetSlice.refName = refName;
1120
+ targetSlice.argLength = argLength;
1121
+ });
1122
+ }
1123
+ return store;
1124
+ }, "createSelectors");
1125
+ const makePicker = /* @__PURE__ */ __name((set, get) => (...fields) => {
1126
+ const state = get();
1127
+ const ret = {};
1128
+ for (const field of fields) {
1129
+ const val = state[field];
1130
+ if (!val) throw new Error(`Field ${field} is not ready`);
1131
+ if (typeof val === "string" && val.length === 0) throw new Error(`Field is empty string (${field})`);
1132
+ else if ([
1133
+ "self",
1134
+ "me"
1135
+ ].includes(field) && !state[field].id?.length) throw new Error("Self or Me Id is not defined");
1136
+ ret[field] = val;
1137
+ }
1138
+ return ret;
1139
+ }, "makePicker");
1140
+ const makeStore = /* @__PURE__ */ __name((st2, storeRef, { library } = {}) => {
1141
+ if (library) return st2;
1142
+ const zustandStore = (0, import_zustand.create)((0, import_middleware.devtools)((0, import_middleware.subscribeWithSelector)((0, import_immer.immer)((set, get) => {
1143
+ const store = {};
1144
+ const pick = makePicker(set, get);
1145
+ Object.getOwnPropertyNames(storeRef.prototype).forEach((key) => {
1146
+ const descriptor = Object.getOwnPropertyDescriptor(storeRef.prototype, key);
1147
+ if (descriptor) store[key] = descriptor.value;
1148
+ });
1149
+ Object.assign(store, {
1150
+ set,
1151
+ get,
1152
+ pick
1153
+ });
1154
+ return store;
1155
+ })), {
1156
+ name: "root",
1157
+ anonymousActionType: "root",
1158
+ type: "root"
1159
+ }));
1160
+ return createSelectors(zustandStore, st2);
1161
+ }, "makeStore");
1162
+ const MixStore = /* @__PURE__ */ __name((s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, s21, s22, s23, s24, s25, s26, s27, s28, s29, s30) => {
1163
+ const stores = [
1164
+ s1,
1165
+ s2,
1166
+ s3,
1167
+ s4,
1168
+ s5,
1169
+ s6,
1170
+ s7,
1171
+ s8,
1172
+ s9,
1173
+ s10,
1174
+ s11,
1175
+ s12,
1176
+ s13,
1177
+ s14,
1178
+ s15,
1179
+ s16,
1180
+ s17,
1181
+ s18,
1182
+ s19,
1183
+ s20,
1184
+ s21,
1185
+ s22,
1186
+ s23,
1187
+ s24,
1188
+ s25,
1189
+ s26,
1190
+ s27,
1191
+ s28,
1192
+ s29,
1193
+ s30
1194
+ ].filter((s) => !!s);
1195
+ let Mix = class Mix {
1196
+ static {
1197
+ __name(this, "Mix");
1198
+ }
1199
+ };
1200
+ (0, import_common.applyMixins)(Mix, stores);
1201
+ return Mix;
1202
+ }, "MixStore");
1203
+ const rootStoreOf = /* @__PURE__ */ __name((store) => {
1204
+ return Object.getPrototypeOf(store);
1205
+ }, "rootStoreOf");
1206
+ const Toast = /* @__PURE__ */ __name(({ root, duration = 3 } = {}) => {
1207
+ return function(target, key, descriptor) {
1208
+ const originMethod = descriptor.value;
1209
+ descriptor.value = async function(...args) {
1210
+ try {
1211
+ import_dictionary.msg.loading(`${root ? `${root}.` : ""}${key}-loading`, {
1212
+ key,
1213
+ duration
1214
+ });
1215
+ const result = await originMethod.apply(this, args);
1216
+ import_dictionary.msg.success(`${root ? `${root}.` : ""}${key}-success`, {
1217
+ key,
1218
+ duration
1219
+ });
1220
+ return result;
1221
+ } catch (err) {
1222
+ const errKey = typeof err === "string" ? err : err.message;
1223
+ import_dictionary.msg.error(errKey, {
1224
+ key,
1225
+ duration
1226
+ });
1227
+ import_common.Logger.error(`${key} action error return: ${err}`);
1228
+ }
1229
+ };
1230
+ };
1231
+ }, "Toast");
1232
+ // Annotate the CommonJS export names for ESM import in node:
1233
+ 0 && (module.exports = {
1234
+ MixStore,
1235
+ Store,
1236
+ Toast,
1237
+ createActions,
1238
+ createState,
1239
+ makeStore,
1240
+ rootStoreOf,
1241
+ scalarStateOf,
1242
+ st,
1243
+ stateOf
1244
+ });