@monorise/react 3.0.2 → 3.0.4-dev.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 (40) hide show
  1. package/dist/actions/core.action.d.ts.map +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2411 -50
  4. package/dist/index.js.map +1 -1
  5. package/dist/tsup.config.d.ts +3 -0
  6. package/dist/tsup.config.d.ts.map +1 -0
  7. package/package.json +5 -4
  8. package/dist/actions/app.action.js +0 -122
  9. package/dist/actions/app.action.js.map +0 -1
  10. package/dist/actions/auth.action.js +0 -63
  11. package/dist/actions/auth.action.js.map +0 -1
  12. package/dist/actions/config.action.js +0 -31
  13. package/dist/actions/config.action.js.map +0 -1
  14. package/dist/actions/core.action.js +0 -944
  15. package/dist/actions/core.action.js.map +0 -1
  16. package/dist/lib/api.js +0 -81
  17. package/dist/lib/api.js.map +0 -1
  18. package/dist/lib/constant.js +0 -2
  19. package/dist/lib/constant.js.map +0 -1
  20. package/dist/lib/entity.js +0 -42
  21. package/dist/lib/entity.js.map +0 -1
  22. package/dist/lib/utils.js +0 -44
  23. package/dist/lib/utils.js.map +0 -1
  24. package/dist/services/auth.service.js +0 -32
  25. package/dist/services/auth.service.js.map +0 -1
  26. package/dist/services/core.service.js +0 -171
  27. package/dist/services/core.service.js.map +0 -1
  28. package/dist/services/filestore.service.js +0 -135
  29. package/dist/services/filestore.service.js.map +0 -1
  30. package/dist/store/monorise.store.js +0 -52
  31. package/dist/store/monorise.store.js.map +0 -1
  32. package/dist/tsconfig.tsbuildinfo +0 -1
  33. package/dist/types/api.type.js +0 -11
  34. package/dist/types/api.type.js.map +0 -1
  35. package/dist/types/axios.type.js +0 -3
  36. package/dist/types/axios.type.js.map +0 -1
  37. package/dist/types/monorise.type.js +0 -2
  38. package/dist/types/monorise.type.js.map +0 -1
  39. package/dist/types/mutual.type.js +0 -2
  40. package/dist/types/mutual.type.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,53 +1,2414 @@
1
- import { initAppActions } from './actions/app.action';
2
- import { initAuthActions } from './actions/auth.action';
3
- import { initConfigActions } from './actions/config.action';
4
- import { initCoreActions } from './actions/core.action';
5
- import { initAxiosInterceptor, injectAxiosInterceptor } from './lib/api';
6
- import { getEntityRequestKey, getMutualRequestKey, getTagRequestKey, getUniqueFieldRequestKey, } from './lib/utils';
7
- import initAuthService from './services/auth.service';
8
- import initCoreService from './services/core.service';
9
- import initFilestoreService from './services/filestore.service';
10
- import { initMonoriseStore } from './store/monorise.store';
11
- const initMonorise = () => {
12
- const { monoriseStore: store, setOptions: setMonoriseOptions } = initMonoriseStore();
13
- const appActions = initAppActions(store);
14
- const configActions = initConfigActions(store);
15
- const axios = initAxiosInterceptor(store, appActions);
16
- const authService = initAuthService(axios);
17
- const filestoreService = initFilestoreService(axios);
18
- const coreService = initCoreService(store, axios);
19
- const authActions = initAuthActions(store, authService);
20
- const coreActions = initCoreActions(store, appActions, coreService);
21
- const axiosInterceptor = injectAxiosInterceptor(appActions, authActions, axios);
22
- const config = (opts) => {
23
- setMonoriseOptions({
24
- modals: opts.modals,
25
- });
26
- configActions.setConfig(opts.entityConfig);
27
- authService.setOptions({
28
- apiBaseUrl: opts.authBaseUrl,
29
- });
30
- filestoreService.setOptions({
31
- apiBaseUrl: opts.filestoreBaseUrl,
32
- });
33
- coreService.setOptions({
34
- entityApiBaseUrl: opts.entityBaseUrl,
35
- mutualApiBaseUrl: opts.mutualBaseUrl,
36
- tagApiBaseUrl: opts.tagBaseUrl,
37
- });
38
- axiosInterceptor.setOptions({
39
- onApiSuccess: opts.onApiSuccess,
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+
33
+ // actions/app.action.ts
34
+ import { produce } from "immer";
35
+ import { useEffect, useState } from "react";
36
+ var initAppActions = (store2) => {
37
+ let options = {};
38
+ const { modals = {} } = options;
39
+ const startLoading2 = ({
40
+ requestKey,
41
+ isInterruptive,
42
+ message,
43
+ request: request2
44
+ }) => {
45
+ store2.setState(
46
+ produce((state) => {
47
+ const stackType = isInterruptive ? "intLoadStack" : "loadStack";
48
+ const requestKeyCount = state.app[stackType].get(requestKey);
49
+ state.app.loadingMessage = typeof message === "string" ? message : "Loading";
50
+ state.app.ongoingRequests.set(requestKey, request2);
51
+ state.app[stackType].set(
52
+ requestKey,
53
+ requestKeyCount ? requestKeyCount + 1 : 1
54
+ );
55
+ }),
56
+ void 0,
57
+ `mr/start-loading/${requestKey}`
58
+ );
59
+ };
60
+ const endLoading2 = ({ requestKey, isInterruptive }) => {
61
+ setTimeout(() => {
62
+ store2.setState(
63
+ produce((state) => {
64
+ const stackType = isInterruptive ? "intLoadStack" : "loadStack";
65
+ const requestKeyCount = state.app[stackType].get(requestKey);
66
+ if (state.app.ongoingRequests.has(requestKey)) {
67
+ state.app.ongoingRequests.delete(requestKey);
68
+ }
69
+ if (typeof requestKeyCount === "number" && requestKeyCount > 1) {
70
+ state.app[stackType].set(requestKey, requestKeyCount - 1);
71
+ } else {
72
+ state.app[stackType].delete(requestKey);
73
+ }
74
+ }),
75
+ void 0,
76
+ `mr/end-loading/${requestKey}`
77
+ );
78
+ }, 500);
79
+ };
80
+ const checkIsLoading = (requestKey) => {
81
+ const { intLoadStack, loadStack } = store2.getState().app;
82
+ if (!requestKey) {
83
+ return intLoadStack.size > 0 || loadStack.size > 0;
84
+ }
85
+ const activeIntLoadingCount = intLoadStack.get(requestKey);
86
+ const isIntLoading = typeof activeIntLoadingCount === "number" && activeIntLoadingCount > 0;
87
+ const activeLoadingCount = loadStack.get(requestKey);
88
+ const isLoading = typeof activeLoadingCount === "number" && activeLoadingCount > 0;
89
+ return isIntLoading || isLoading;
90
+ };
91
+ const setError2 = ({ requestKey, error }) => store2.setState(
92
+ produce((state) => {
93
+ state.app.errorStack.set(requestKey, error);
94
+ }),
95
+ void 0,
96
+ `mr/set-error/${requestKey}`
97
+ );
98
+ const getError2 = (requestKey) => {
99
+ const { errorStack } = store2.getState().app;
100
+ return errorStack.get(requestKey);
101
+ };
102
+ const clearError2 = (requestKey) => {
103
+ const { errorStack } = store2.getState().app;
104
+ if (errorStack.has(requestKey)) {
105
+ store2.setState(
106
+ produce((state) => {
107
+ state.app.errorStack.delete(requestKey);
108
+ }),
109
+ void 0,
110
+ `mr/clear-error/${requestKey}`
111
+ );
112
+ }
113
+ };
114
+ const openModal2 = (modal) => store2.setState(
115
+ produce((state) => {
116
+ state.app.modal = modal;
117
+ }),
118
+ void 0,
119
+ `mr/open-modal/${modal.name}`
120
+ );
121
+ const closeModal2 = () => store2.setState(
122
+ produce((state) => {
123
+ state.app.modal = { name: null };
124
+ }),
125
+ void 0,
126
+ "mr/close-modal"
127
+ );
128
+ const useLoadStore2 = (requestKey) => {
129
+ const { loadStack, intLoadStack } = store2((state) => ({
130
+ loadStack: state.app.loadStack,
131
+ intLoadStack: state.app.intLoadStack
132
+ }));
133
+ const [isLoading, setIsLoading] = useState(false);
134
+ useEffect(() => {
135
+ if (loadStack && intLoadStack && requestKey) {
136
+ setIsLoading(checkIsLoading(requestKey));
137
+ }
138
+ }, [loadStack, intLoadStack, requestKey]);
139
+ return isLoading;
140
+ };
141
+ const useInterruptiveLoadStore2 = () => {
142
+ const { intLoadStack, loadingMessage } = store2((state) => ({
143
+ intLoadStack: state.app.intLoadStack,
144
+ loadingMessage: state.app.loadingMessage
145
+ }));
146
+ return {
147
+ isLoading: intLoadStack.size > 0,
148
+ message: loadingMessage
149
+ };
150
+ };
151
+ const useErrorStore2 = (requestKey) => {
152
+ const error = store2((state) => state.app.errorStack.get(requestKey));
153
+ useEffect(() => {
154
+ if (error) {
155
+ return () => {
156
+ clearError2(requestKey);
157
+ };
158
+ }
159
+ }, [error, requestKey]);
160
+ return error;
161
+ };
162
+ const useModalStore2 = () => {
163
+ return store2((state) => state.app.modal);
164
+ };
165
+ const setOptions = (opts) => {
166
+ options = __spreadValues(__spreadValues({}, options), opts);
167
+ };
168
+ return {
169
+ startLoading: startLoading2,
170
+ endLoading: endLoading2,
171
+ setError: setError2,
172
+ getError: getError2,
173
+ clearError: clearError2,
174
+ openModal: openModal2,
175
+ closeModal: closeModal2,
176
+ checkIsLoading,
177
+ useLoadStore: useLoadStore2,
178
+ useInterruptiveLoadStore: useInterruptiveLoadStore2,
179
+ useErrorStore: useErrorStore2,
180
+ useModalStore: useModalStore2,
181
+ setOptions
182
+ };
183
+ };
184
+
185
+ // actions/auth.action.ts
186
+ import { produce as produce2 } from "immer";
187
+ var initAuthActions = (store2, authService2) => {
188
+ const requestLogin2 = async (entityType, payload) => {
189
+ const { data } = await authService2.requestLoginMagicLink(
190
+ entityType,
191
+ payload
192
+ );
193
+ return data;
194
+ };
195
+ const useProfile2 = () => {
196
+ const profile = store2((state) => state.auth.profile);
197
+ return profile;
198
+ };
199
+ const getProfile2 = async () => {
200
+ try {
201
+ const {
202
+ data: _a
203
+ } = await authService2.getSessionProfile(), _b = _a, { displayName, email, accountId } = _b, rest = __objRest(_b, ["displayName", "email", "accountId"]);
204
+ store2.setState(
205
+ produce2((state) => {
206
+ state.auth.profile = __spreadValues({ displayName, email, accountId }, rest);
207
+ state.auth.isUnauthorized = false;
208
+ state.auth.isLoading = false;
209
+ }),
210
+ void 0,
211
+ "mr/auth/get-profile"
212
+ );
213
+ } catch (e) {
214
+ store2.setState(
215
+ produce2((state) => {
216
+ state.auth.profile = {};
217
+ state.auth.isUnauthorized = true;
218
+ state.auth.isLoading = false;
219
+ }),
220
+ void 0,
221
+ "mr/auth/get-profile"
222
+ );
223
+ }
224
+ };
225
+ const useIsUnauthorized2 = () => {
226
+ const { isUnauthorized, profile, isLoading } = store2((state) => state.auth);
227
+ return { isUnauthorized: isUnauthorized && !profile.accountId, isLoading };
228
+ };
229
+ const setIsUnauthorized2 = (isUnauthorized) => store2.setState(
230
+ produce2((state) => {
231
+ state.auth.isUnauthorized = isUnauthorized;
232
+ })
233
+ );
234
+ const logout2 = () => {
235
+ authService2.logout();
236
+ store2.setState(
237
+ produce2((state) => {
238
+ state.auth.isUnauthorized = true;
239
+ state.auth.profile = {};
240
+ })
241
+ );
242
+ };
243
+ return {
244
+ requestLogin: requestLogin2,
245
+ useProfile: useProfile2,
246
+ getProfile: getProfile2,
247
+ useIsUnauthorized: useIsUnauthorized2,
248
+ setIsUnauthorized: setIsUnauthorized2,
249
+ logout: logout2
250
+ };
251
+ };
252
+
253
+ // actions/config.action.ts
254
+ import { produce as produce3 } from "immer";
255
+ var initConfigActions = (store2) => {
256
+ const setConfig2 = (config) => {
257
+ const entityMaps = Object.keys(config).reduce(
258
+ (acc, entity) => {
259
+ acc[entity] = {
260
+ dataMap: /* @__PURE__ */ new Map(),
261
+ isFirstFetched: false,
262
+ lastKey: ""
263
+ };
264
+ return acc;
265
+ },
266
+ {}
267
+ );
268
+ store2.setState(
269
+ produce3((state) => {
270
+ state.config = config;
271
+ state.entity = entityMaps;
272
+ })
273
+ );
274
+ };
275
+ const getConfig2 = () => {
276
+ return store2.getState().config;
277
+ };
278
+ const useConfig2 = () => {
279
+ const config = store2((state) => state.config);
280
+ return config;
281
+ };
282
+ return {
283
+ setConfig: setConfig2,
284
+ getConfig: getConfig2,
285
+ useConfig: useConfig2
286
+ };
287
+ };
288
+
289
+ // actions/core.action.ts
290
+ import { produce as produce4 } from "immer";
291
+ import { useEffect as useEffect2, useState as useState2 } from "react";
292
+
293
+ // lib/entity.ts
294
+ var constructLocal = (entityType, entityId, data) => {
295
+ return {
296
+ entityType,
297
+ entityId,
298
+ data,
299
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
300
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
301
+ };
302
+ };
303
+ var constructMutual = (byEntityType, byEntityId2, entityType, entityId, mutualData, data) => {
304
+ return {
305
+ mutualId: `${byEntityId2}-${entityId}`,
306
+ byEntityType,
307
+ byEntityId: byEntityId2,
308
+ entityType,
309
+ entityId,
310
+ mutualData,
311
+ data,
312
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
313
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
314
+ mutualUpdatedAt: (/* @__PURE__ */ new Date()).toISOString()
315
+ };
316
+ };
317
+ var flipMutual = (mutual) => {
318
+ return __spreadProps(__spreadValues({}, mutual), {
319
+ entityId: mutual.byEntityId,
320
+ entityType: mutual.byEntityType,
321
+ byEntityId: mutual.entityId,
322
+ byEntityType: mutual.entityType
323
+ });
324
+ };
325
+ var byEntityId = (a, b) => {
326
+ if (b.entityId < a.entityId) return -1;
327
+ return 1;
328
+ };
329
+
330
+ // lib/utils.ts
331
+ var convertToMap = (data, mapKey) => {
332
+ const map = /* @__PURE__ */ new Map();
333
+ for (const i in data) {
334
+ map.set(data[i][mapKey], data[i]);
335
+ }
336
+ return map;
337
+ };
338
+ var getMutualStateKey = (byEntity, byEntityId2, entity, entityId, chainEntityQuery) => {
339
+ return `${byEntity}/${byEntityId2}/${entity}${entityId ? `/${entityId}` : ""}${chainEntityQuery ? `?${chainEntityQuery}` : ""}`;
340
+ };
341
+ var getTagStateKey = (entityType, tagName, params = {}) => {
342
+ const _a = params, { lastKey } = _a, stateParams = __objRest(_a, ["lastKey"]);
343
+ const paramsKey = Object.keys(stateParams).map((key) => `${key}:${stateParams[key]}`).join("/");
344
+ return `${entityType}/${tagName}/${paramsKey}`;
345
+ };
346
+ var getUniqueFieldStateKey = (fieldName, fieldValue) => {
347
+ return `${fieldName}/${fieldValue}`;
348
+ };
349
+ var getEntityRequestKey = (mode, entityType, entityId) => {
350
+ return `entity/${entityType}/${mode}${entityId ? `/${entityId}` : ""}`;
351
+ };
352
+ var getMutualRequestKey = (mode, byEntityType, entityType, byEntityId2, entityId, chainEntityQuery) => {
353
+ return `mutual/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId, chainEntityQuery)}/${mode}`;
354
+ };
355
+ var getTagRequestKey = (mode, entityType, tagName, params = {}) => {
356
+ return `tag/${getTagStateKey(entityType, tagName, params)}/${mode}`;
357
+ };
358
+ var getUniqueFieldRequestKey = (entityType, fieldName, value) => {
359
+ return `entity/${entityType}/unique/${getUniqueFieldStateKey(fieldName, value)}`;
360
+ };
361
+
362
+ // actions/core.action.ts
363
+ var defaultOnError = (error) => {
364
+ if (!(error instanceof Error) && error.status >= 500) {
365
+ throw error;
366
+ }
367
+ };
368
+ var initCoreActions = (monoriseStore, appActions, coreService2) => {
369
+ const { makeEntityService, makeMutualService } = coreService2;
370
+ const { checkIsLoading, getError: getError2, useLoadStore: useLoadStore2, useErrorStore: useErrorStore2 } = appActions;
371
+ const listEntities = async (entityType, params = {}, opts = {}) => {
372
+ var _a, _b;
373
+ const store2 = monoriseStore.getState();
374
+ const entityState = (_a = store2.entity[entityType]) != null ? _a : {};
375
+ const { isFirstFetched } = entityState;
376
+ const entityService = makeEntityService(entityType);
377
+ const { skRange } = params;
378
+ const requestKey = getEntityRequestKey("list", entityType);
379
+ const isLoading = checkIsLoading(requestKey);
380
+ const error = getError2(requestKey);
381
+ const onError = (_b = opts.onError) != null ? _b : defaultOnError;
382
+ if (error) {
383
+ onError(error);
384
+ return;
385
+ }
386
+ if (!opts.forceFetch && (isFirstFetched && !params.skRange || isLoading)) {
387
+ return;
388
+ }
389
+ try {
390
+ const { data: result } = await entityService.listEntities(
391
+ __spreadProps(__spreadValues({}, (params == null ? void 0 : params.all) ? {} : { limit: 20 }), {
392
+ start: skRange == null ? void 0 : skRange.start,
393
+ end: skRange == null ? void 0 : skRange.end
394
+ }),
395
+ opts
396
+ );
397
+ const newEntityMap = convertToMap(
398
+ result.data,
399
+ "entityId"
400
+ );
401
+ const mergedMap = new Map([
402
+ ...newEntityMap,
403
+ ...store2.entity[entityType].dataMap
404
+ ]);
405
+ monoriseStore.setState(
406
+ produce4((state) => {
407
+ state.entity[entityType] = {
408
+ dataMap: params.skRange ? newEntityMap : mergedMap,
409
+ isFirstFetched: true,
410
+ lastKey: result.lastKey
411
+ };
412
+ }),
413
+ void 0,
414
+ `mr/entity/list/${entityType}`
415
+ );
416
+ } catch (err) {
417
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
418
+ return { error: error2 };
419
+ }
420
+ };
421
+ const listMoreEntities2 = async (entityType, opts = {}) => {
422
+ var _a;
423
+ const requestKey = getEntityRequestKey("list", entityType);
424
+ const store2 = monoriseStore.getState();
425
+ const entityState = store2.entity[entityType];
426
+ const { dataMap, lastKey } = entityState;
427
+ const entityService = makeEntityService(entityType);
428
+ const error = getError2(requestKey);
429
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
430
+ if (!lastKey) {
431
+ return;
432
+ }
433
+ if (error) {
434
+ onError(error);
435
+ return;
436
+ }
437
+ try {
438
+ const { data: result } = await entityService.listEntities(
439
+ {
440
+ limit: 20,
441
+ lastKey
442
+ },
443
+ opts
444
+ );
445
+ monoriseStore.setState(
446
+ produce4((state) => {
447
+ state.entity[entityType].isFirstFetched = true;
448
+ state.entity[entityType].lastKey = result.lastKey;
449
+ for (const i in result.data) {
450
+ state.entity[entityType].dataMap.set(
451
+ result.data[i].entityId,
452
+ result.data[i]
453
+ );
454
+ }
455
+ }),
456
+ void 0,
457
+ `mr/entity/list-more/${entityType}`
458
+ );
459
+ } catch (err) {
460
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
461
+ return { error: error2 };
462
+ }
463
+ };
464
+ const searchEntities = async (entityType, query, opts = {}) => {
465
+ var _a;
466
+ const requestKey = getEntityRequestKey("search", entityType);
467
+ const entityService = makeEntityService(entityType);
468
+ const error = getError2(requestKey);
469
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
470
+ if (error) {
471
+ onError(error);
472
+ return;
473
+ }
474
+ try {
475
+ const { data: result } = await entityService.searchEntities(query, opts);
476
+ monoriseStore.setState(
477
+ produce4((state) => {
478
+ state.entity[entityType].searchResults = result.data;
479
+ for (const i in result.data) {
480
+ state.entity[entityType].dataMap.set(
481
+ result.data[i].entityId,
482
+ result.data[i]
483
+ );
484
+ }
485
+ })
486
+ );
487
+ } catch (err) {
488
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
489
+ return { error: error2 };
490
+ }
491
+ };
492
+ const listEntitiesByTag = async (entityType, tagName, opts = {}) => {
493
+ var _a;
494
+ const tagKey = getTagStateKey(entityType, tagName, opts.params);
495
+ const state = monoriseStore.getState();
496
+ const tagState = state.tag[tagKey] || {};
497
+ const { isFirstFetched, dataMap } = tagState;
498
+ const entityService = makeEntityService(entityType);
499
+ const { forceFetch } = opts;
500
+ const requestKey = getTagRequestKey(
501
+ "list",
502
+ entityType,
503
+ tagName,
504
+ opts.params
505
+ );
506
+ const isLoading = checkIsLoading(requestKey);
507
+ const error = getError2(requestKey);
508
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
509
+ if (!forceFetch) {
510
+ if (error) {
511
+ onError(error);
512
+ return { error };
513
+ }
514
+ if (isFirstFetched || isLoading) {
515
+ return {
516
+ data: {
517
+ entities: Array.from((dataMap == null ? void 0 : dataMap.values()) || []),
518
+ lastKey: null
519
+ }
520
+ };
521
+ }
522
+ }
523
+ try {
524
+ const { data } = await entityService.listEntitiesByTag(tagName, __spreadProps(__spreadValues({}, opts), {
525
+ requestKey
526
+ }));
527
+ const { entities, lastKey } = data;
528
+ monoriseStore.setState(
529
+ produce4((state2) => {
530
+ for (const entity of entities) {
531
+ state2.entity[entityType].dataMap.set(entity.entityId, entity);
532
+ }
533
+ })
534
+ );
535
+ monoriseStore.setState(
536
+ produce4((state2) => {
537
+ if (!state2.tag[tagKey]) {
538
+ state2.tag[tagKey] = {
539
+ dataMap: /* @__PURE__ */ new Map(),
540
+ isFirstFetched: false
541
+ };
542
+ }
543
+ for (const entity of entities) {
544
+ state2.tag[tagKey].dataMap.set(entity.entityId, entity);
545
+ }
546
+ state2.tag[tagKey].isFirstFetched = true;
547
+ state2.tag[tagKey].lastKey = lastKey;
548
+ })
549
+ );
550
+ return { data };
551
+ } catch (err) {
552
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
553
+ return { error: error2 };
554
+ }
555
+ };
556
+ const getEntity2 = async (entityType, id, opts = {}) => {
557
+ var _a;
558
+ const store2 = monoriseStore.getState();
559
+ const entityState = store2.entity[entityType];
560
+ const { dataMap } = entityState;
561
+ const entityService = makeEntityService(entityType);
562
+ let entity = dataMap.get(id);
563
+ const requestKey = getEntityRequestKey("get", entityType, id);
564
+ const isLoading = checkIsLoading(requestKey);
565
+ const error = getError2(requestKey);
566
+ const { forceFetch } = opts;
567
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
568
+ if (!forceFetch) {
569
+ if (error) {
570
+ onError == null ? void 0 : onError(error);
571
+ return { error };
572
+ }
573
+ if (entity) {
574
+ return { data: entity };
575
+ }
576
+ if (isLoading) {
577
+ return {};
578
+ }
579
+ }
580
+ try {
581
+ ({ data: entity } = await entityService.getEntity(id, opts));
582
+ monoriseStore.setState(
583
+ produce4((state) => {
584
+ state.entity[entityType].dataMap.set(entity == null ? void 0 : entity.entityId, entity);
585
+ }),
586
+ void 0,
587
+ `mr/entity/get/${entityType}/${id}`
588
+ );
589
+ return { data: entity };
590
+ } catch (err) {
591
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
592
+ return { error: error2 };
593
+ }
594
+ };
595
+ const getEntityByUniqueField = async (entityType, fieldName, value, opts = {}) => {
596
+ var _a;
597
+ const store2 = monoriseStore.getState();
598
+ const entityState = store2.entity[entityType];
599
+ const { dataMap } = entityState;
600
+ const entityService = makeEntityService(entityType);
601
+ const stateKey = getUniqueFieldStateKey(fieldName, value);
602
+ let entity = dataMap.get(stateKey);
603
+ const requestKey = getUniqueFieldRequestKey(entityType, fieldName, value);
604
+ const isLoading = checkIsLoading(requestKey);
605
+ const error = getError2(requestKey);
606
+ const { forceFetch } = opts;
607
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
608
+ if (!forceFetch) {
609
+ if (error) {
610
+ onError == null ? void 0 : onError(error);
611
+ return { error };
612
+ }
613
+ if (entity) {
614
+ return { data: entity };
615
+ }
616
+ if (isLoading) {
617
+ return {};
618
+ }
619
+ }
620
+ try {
621
+ ({ data: entity } = await entityService.getEntityByUniqueField(
622
+ fieldName,
623
+ value,
624
+ opts
625
+ ));
626
+ monoriseStore.setState(
627
+ produce4((state) => {
628
+ state.entity[entityType].dataMap.set(entity == null ? void 0 : entity.entityId, entity);
629
+ state.entity[entityType].dataMap.set(`${stateKey}`, entity);
630
+ }),
631
+ void 0,
632
+ `mr/entity/unique/${entityType}/${stateKey}`
633
+ );
634
+ return { data: entity };
635
+ } catch (err) {
636
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
637
+ return { error: error2 };
638
+ }
639
+ };
640
+ const createEntity2 = async (entityType, entity, opts = {}) => {
641
+ var _a;
642
+ const entityService = makeEntityService(entityType);
643
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
644
+ try {
645
+ const { data } = await entityService.createEntity(entity, opts);
646
+ monoriseStore.setState(
647
+ produce4((state) => {
648
+ var _a2, _b;
649
+ state.entity[entityType].dataMap.set(data.entityId, data);
650
+ const tagConfigs = (_a2 = state.config[entityType]) == null ? void 0 : _a2.tags;
651
+ if (tagConfigs) {
652
+ for (const tagConfig of tagConfigs) {
653
+ const { name, processor } = tagConfig;
654
+ const processorResults = processor(
655
+ data
656
+ );
657
+ for (const tagKey of Object.keys(state.tag)) {
658
+ const [tagEntityType, tagName, ...paramParts] = tagKey.split("/");
659
+ if (tagEntityType !== entityType || tagName !== name) {
660
+ continue;
661
+ }
662
+ if (!((_b = state.tag[tagKey]) == null ? void 0 : _b.isFirstFetched)) {
663
+ continue;
664
+ }
665
+ const keyParams = {};
666
+ for (const part of paramParts) {
667
+ const colonIdx = part.indexOf(":");
668
+ if (colonIdx > 0) {
669
+ keyParams[part.substring(0, colonIdx)] = part.substring(colonIdx + 1);
670
+ }
671
+ }
672
+ if (keyParams.query) {
673
+ continue;
674
+ }
675
+ const matches = processorResults.some((result) => {
676
+ if (keyParams.group && result.group !== keyParams.group) {
677
+ return false;
678
+ }
679
+ if (keyParams.start && (!result.sortValue || result.sortValue < keyParams.start)) {
680
+ return false;
681
+ }
682
+ if (keyParams.end && (!result.sortValue || result.sortValue > keyParams.end)) {
683
+ return false;
684
+ }
685
+ return true;
686
+ });
687
+ if (matches) {
688
+ state.tag[tagKey].dataMap.set(data.entityId, data);
689
+ }
690
+ }
691
+ }
692
+ }
693
+ }),
694
+ void 0,
695
+ `mr/entity/create/${entityType}`
696
+ );
697
+ return { data };
698
+ } catch (err) {
699
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
700
+ onError == null ? void 0 : onError(error);
701
+ return { error };
702
+ }
703
+ };
704
+ const upsertEntity2 = async (entityType, id, entity, opts = {}) => {
705
+ var _a;
706
+ const entityService = makeEntityService(entityType);
707
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
708
+ try {
709
+ const { data } = await entityService.upsertEntity(id, entity, opts);
710
+ monoriseStore.setState(
711
+ produce4((state) => {
712
+ state.entity[entityType].dataMap.set(data.entityId, data);
713
+ }),
714
+ void 0,
715
+ `mr/entity/upsert/${entityType}/${id}`
716
+ );
717
+ return { data };
718
+ } catch (err) {
719
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
720
+ onError(error);
721
+ return { error };
722
+ }
723
+ };
724
+ const editEntity2 = async (entityType, id, entity, opts = {}) => {
725
+ var _a;
726
+ const entityService = makeEntityService(entityType);
727
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
728
+ try {
729
+ const { data } = await entityService.editEntity(id, entity, opts);
730
+ monoriseStore.setState(
731
+ produce4((state) => {
732
+ var _a2, _b;
733
+ state.entity[entityType].dataMap.set(data.entityId, data);
734
+ for (const key of Object.keys(state.mutual)) {
735
+ const [_byEntity, _byId, _entityType] = key.split("/");
736
+ if (_entityType === entityType) {
737
+ const mutual = state.mutual[key].dataMap.get(id);
738
+ if (mutual) {
739
+ state.mutual[key].dataMap = new Map(
740
+ state.mutual[key].dataMap
741
+ ).set(id, __spreadProps(__spreadValues({}, mutual), { data: data.data }));
742
+ }
743
+ }
744
+ }
745
+ for (const key of Object.keys(state.mutual)) {
746
+ const [_byEntity, _byId] = key.split("/");
747
+ if (_byEntity === entityType && _byId === id) {
748
+ const newDataMap = new Map(state.mutual[key].dataMap);
749
+ for (const [entryId, mutual] of newDataMap) {
750
+ newDataMap.set(entryId, __spreadProps(__spreadValues({}, mutual), { data: data.data }));
751
+ }
752
+ state.mutual[key].dataMap = newDataMap;
753
+ }
754
+ }
755
+ for (const tagKey of Object.keys(state.tag)) {
756
+ const [tagEntityType] = tagKey.split("/");
757
+ if (tagEntityType === entityType) {
758
+ if ((_b = (_a2 = state.tag[tagKey]) == null ? void 0 : _a2.dataMap) == null ? void 0 : _b.has(id)) {
759
+ state.tag[tagKey].dataMap.set(id, data);
760
+ }
761
+ }
762
+ }
763
+ }),
764
+ void 0,
765
+ `mr/entity/edit/${entityType}/${id}`
766
+ );
767
+ return { data };
768
+ } catch (err) {
769
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
770
+ onError(error);
771
+ return { error };
772
+ }
773
+ };
774
+ const deleteEntity2 = async (entityType, id, opts = {}) => {
775
+ var _a;
776
+ const entityService = makeEntityService(entityType);
777
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
778
+ try {
779
+ await entityService.deleteEntity(id, opts);
780
+ deleteLocalMutualsByEntity(entityType, id);
781
+ monoriseStore.setState(
782
+ produce4((state) => {
783
+ var _a2, _b;
784
+ state.entity[entityType].dataMap.delete(id);
785
+ for (const key of Object.keys(state.mutual)) {
786
+ const [_byEntity, _byId, _entityType] = key.split("/");
787
+ if (_entityType === entityType) {
788
+ state.mutual[key].dataMap.delete(id);
789
+ }
790
+ }
791
+ for (const tagKey of Object.keys(state.tag)) {
792
+ const [tagEntityType] = tagKey.split("/");
793
+ if (tagEntityType === entityType) {
794
+ (_b = (_a2 = state.tag[tagKey]) == null ? void 0 : _a2.dataMap) == null ? void 0 : _b.delete(id);
795
+ }
796
+ }
797
+ }),
798
+ void 0,
799
+ `mr/entity/delete/${entityType}/${id}`
800
+ );
801
+ return { data: { entityId: id } };
802
+ } catch (err) {
803
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
804
+ onError(error);
805
+ return { error };
806
+ }
807
+ };
808
+ const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
809
+ var _a, _b, _c;
810
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
811
+ const selfKey = (_b = opts.stateKey) != null ? _b : getMutualStateKey(
812
+ byEntityType,
813
+ id,
814
+ entityType,
815
+ void 0,
816
+ chainEntityQuery
817
+ );
818
+ const mutualService = makeMutualService(byEntityType, entityType);
819
+ const store2 = monoriseStore.getState();
820
+ const mutualState = store2.mutual[selfKey] || {};
821
+ const { isFirstFetched } = mutualState;
822
+ const requestKey = getMutualRequestKey(
823
+ "list",
824
+ byEntityType,
825
+ entityType,
826
+ id,
827
+ void 0,
828
+ chainEntityQuery
829
+ );
830
+ const isLoading = checkIsLoading(requestKey);
831
+ const error = getError2(requestKey);
832
+ const { forceFetch } = opts;
833
+ if (!forceFetch) {
834
+ if (error) {
835
+ onError(error);
836
+ return { error };
837
+ }
838
+ if (isFirstFetched || isLoading) {
839
+ return {
840
+ data: {
841
+ entities: Array.from(((_c = mutualState.dataMap) == null ? void 0 : _c.values()) || []),
842
+ lastKey: mutualState.lastKey
843
+ }
844
+ };
845
+ }
846
+ }
847
+ try {
848
+ const { data } = await mutualService.listEntitiesByEntity(
849
+ id,
850
+ opts,
851
+ chainEntityQuery
852
+ );
853
+ const { entities, lastKey } = data;
854
+ const newEntityDataMap = /* @__PURE__ */ new Map();
855
+ for (const i in entities) {
856
+ newEntityDataMap.set(entities[i].entityId, entities[i]);
857
+ }
858
+ monoriseStore.setState(
859
+ produce4((state) => {
860
+ var _a2;
861
+ for (const [key, value] of newEntityDataMap) {
862
+ (_a2 = state.entity[entityType]) == null ? void 0 : _a2.dataMap.set(key, value);
863
+ }
864
+ const existing = state.mutual[selfKey];
865
+ if (existing) {
866
+ const incoming = convertToMap(entities, "entityId");
867
+ for (const [k, v] of incoming) {
868
+ existing.dataMap.set(k, v);
869
+ }
870
+ existing.isFirstFetched = true;
871
+ existing.lastKey = lastKey;
872
+ } else {
873
+ state.mutual[selfKey] = {
874
+ dataMap: convertToMap(entities, "entityId"),
875
+ isFirstFetched: true,
876
+ lastKey
877
+ };
878
+ }
879
+ }),
880
+ void 0,
881
+ `mr/mutual/list/${selfKey}`
882
+ );
883
+ return { data };
884
+ } catch (err) {
885
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
886
+ return { error: error2 };
887
+ }
888
+ };
889
+ const getMutual2 = async (byEntityType, entityType, byEntityId2, entityId, opts = {}) => {
890
+ var _a, _b, _c, _d;
891
+ const selfKey = getMutualStateKey(byEntityType, byEntityId2, entityType);
892
+ const mutualService = makeMutualService(byEntityType, entityType);
893
+ const store2 = monoriseStore.getState();
894
+ const mutualState = store2.mutual[selfKey] || {};
895
+ const requestKey = getMutualRequestKey(
896
+ "get",
897
+ byEntityType,
898
+ entityType,
899
+ byEntityId2,
900
+ entityId
901
+ );
902
+ const isLoading = checkIsLoading(requestKey);
903
+ const error = getError2(requestKey);
904
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
905
+ if (error) {
906
+ onError == null ? void 0 : onError(error);
907
+ return { error };
908
+ }
909
+ if (!byEntityId2 || isLoading) {
910
+ return {
911
+ data: (_b = mutualState.dataMap) == null ? void 0 : _b.get(entityId)
912
+ };
913
+ }
914
+ if ((_c = mutualState.dataMap) == null ? void 0 : _c.get(entityId)) {
915
+ return {
916
+ data: mutualState.dataMap.get(entityId)
917
+ };
918
+ }
919
+ let mutual;
920
+ let hasRequestFailed = false;
921
+ try {
922
+ ({ data: mutual } = await mutualService.getMutual(
923
+ byEntityId2,
924
+ entityId,
925
+ opts
926
+ ));
927
+ } catch (err) {
928
+ if (!opts.defaultMutualData) {
929
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
930
+ return { error: error2 };
931
+ }
932
+ hasRequestFailed = true;
933
+ mutual = {
934
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
935
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
936
+ mutualId: `${byEntityId2}-${entityId}`,
937
+ mutualUpdatedAt: (/* @__PURE__ */ new Date()).toISOString(),
938
+ byEntityType,
939
+ byEntityId: byEntityId2,
940
+ entityType,
941
+ entityId,
942
+ mutualData: opts.defaultMutualData
943
+ };
944
+ }
945
+ const newMutualDataMap = new Map((_d = store2.mutual[selfKey]) == null ? void 0 : _d.dataMap);
946
+ newMutualDataMap.set(mutual.entityId, mutual);
947
+ if (!hasRequestFailed) {
948
+ const entityState = store2.entity[entityType];
949
+ const { dataMap: entityDataMap } = entityState;
950
+ const newEntityDataMap = new Map(entityDataMap);
951
+ newEntityDataMap.set(mutual.entityId, mutual);
952
+ monoriseStore.setState(
953
+ produce4((state) => {
954
+ state.mutual[selfKey] = __spreadProps(__spreadValues({}, state.mutual[selfKey] || {}), {
955
+ dataMap: newMutualDataMap
956
+ });
957
+ state.entity[entityType].dataMap = newEntityDataMap;
958
+ }),
959
+ void 0,
960
+ `mr/mutual/get/${getMutualStateKey(
961
+ byEntityType,
962
+ byEntityId2,
963
+ entityType,
964
+ entityId
965
+ )}`
966
+ );
967
+ }
968
+ return { data: mutual };
969
+ };
970
+ const createMutual2 = async (byEntityType, entityType, byEntityId2, entityId, payload = {}, opts = {}) => {
971
+ var _a;
972
+ const mutualService = makeMutualService(byEntityType, entityType);
973
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
974
+ try {
975
+ const { data: mutual } = await mutualService.createMutual(
976
+ byEntityId2,
977
+ entityId,
978
+ payload,
979
+ opts
980
+ );
981
+ monoriseStore.setState(
982
+ produce4((state) => {
983
+ var _a2, _b;
984
+ const bySide = getMutualStateKey(
985
+ byEntityType,
986
+ byEntityId2,
987
+ entityType
988
+ );
989
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
990
+ if (!state.mutual[bySide]) {
991
+ state.mutual[bySide] = {
992
+ dataMap: /* @__PURE__ */ new Map()
993
+ };
994
+ }
995
+ state.mutual[bySide].dataMap = new Map(
996
+ (_a2 = state.mutual[bySide]) == null ? void 0 : _a2.dataMap
997
+ ).set(mutual.entityId, mutual);
998
+ if (!state.mutual[side]) {
999
+ state.mutual[side] = {
1000
+ dataMap: /* @__PURE__ */ new Map()
1001
+ };
1002
+ }
1003
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1004
+ mutual.byEntityId,
1005
+ flipMutual(mutual)
1006
+ );
1007
+ }),
1008
+ void 0,
1009
+ `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1010
+ );
1011
+ return { data: mutual };
1012
+ } catch (err) {
1013
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
1014
+ onError(error);
1015
+ return { error };
1016
+ }
1017
+ };
1018
+ const createLocalMutual2 = async (byEntityType, entityType, byEntityId2, entityId, mutualData, data) => {
1019
+ const mutual = constructMutual(
1020
+ byEntityType,
1021
+ byEntityId2,
1022
+ entityType,
1023
+ entityId,
1024
+ mutualData,
1025
+ data
1026
+ );
1027
+ monoriseStore.setState(
1028
+ produce4((state) => {
1029
+ var _a, _b;
1030
+ const bySide = getMutualStateKey(byEntityType, byEntityId2, entityType);
1031
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
1032
+ if (!state.mutual[bySide]) {
1033
+ state.mutual[bySide] = {
1034
+ dataMap: /* @__PURE__ */ new Map()
1035
+ };
1036
+ }
1037
+ state.mutual[bySide].dataMap = new Map(
1038
+ (_a = state.mutual[bySide]) == null ? void 0 : _a.dataMap
1039
+ ).set(mutual.entityId, mutual);
1040
+ if (!state.mutual[side]) {
1041
+ state.mutual[side] = {
1042
+ dataMap: /* @__PURE__ */ new Map()
1043
+ };
1044
+ }
1045
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1046
+ mutual.byEntityId,
1047
+ flipMutual(mutual)
1048
+ );
1049
+ }),
1050
+ void 0,
1051
+ `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1052
+ );
1053
+ };
1054
+ const updateLocalEntity2 = async (entityType, entityId, data = {}) => {
1055
+ const createdEntity = constructLocal(entityType, entityId, data);
1056
+ monoriseStore.setState(
1057
+ produce4((state) => {
1058
+ state.entity[entityType].dataMap.set(entityId, createdEntity);
1059
+ }),
1060
+ void 0,
1061
+ `mr/entity/local-update/${entityType}/${entityId}`
1062
+ );
1063
+ };
1064
+ const upsertLocalMutual2 = async (byEntityType, entityType, byEntityId2, entityId, mutualData, data = {}) => {
1065
+ const mutual = constructMutual(
1066
+ byEntityType,
1067
+ byEntityId2,
1068
+ entityType,
1069
+ entityId,
1070
+ mutualData,
1071
+ data
1072
+ );
1073
+ monoriseStore.setState(
1074
+ produce4((state) => {
1075
+ var _a, _b;
1076
+ const bySide = getMutualStateKey(byEntityType, byEntityId2, entityType);
1077
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
1078
+ if (!state.mutual[bySide]) {
1079
+ state.mutual[bySide] = {
1080
+ dataMap: /* @__PURE__ */ new Map()
1081
+ };
1082
+ }
1083
+ state.mutual[bySide].dataMap = new Map(
1084
+ (_a = state.mutual[bySide]) == null ? void 0 : _a.dataMap
1085
+ ).set(entityId, mutual);
1086
+ if (!state.mutual[side]) {
1087
+ state.mutual[side] = {
1088
+ dataMap: /* @__PURE__ */ new Map()
1089
+ };
1090
+ }
1091
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1092
+ byEntityId2,
1093
+ flipMutual(mutual)
1094
+ );
1095
+ }),
1096
+ void 0,
1097
+ `mr/mutual/local-update/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1098
+ );
1099
+ };
1100
+ const editMutual2 = async (byEntityType, entityType, byEntityId2, entityId, payload = {}, opts = {}) => {
1101
+ var _a;
1102
+ const mutualService = makeMutualService(byEntityType, entityType);
1103
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
1104
+ try {
1105
+ const { data: mutual } = await mutualService.editMutual(
1106
+ byEntityId2,
1107
+ entityId,
1108
+ payload,
1109
+ opts
1110
+ );
1111
+ monoriseStore.setState(
1112
+ produce4((state) => {
1113
+ var _a2, _b;
1114
+ const bySide = getMutualStateKey(
1115
+ byEntityType,
1116
+ byEntityId2,
1117
+ entityType
1118
+ );
1119
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
1120
+ if (!state.mutual[bySide]) {
1121
+ state.mutual[bySide] = {
1122
+ dataMap: /* @__PURE__ */ new Map()
1123
+ };
1124
+ }
1125
+ state.mutual[bySide].dataMap = new Map(
1126
+ (_a2 = state.mutual[bySide]) == null ? void 0 : _a2.dataMap
1127
+ ).set(mutual.entityId, mutual);
1128
+ if (!state.mutual[side]) {
1129
+ state.mutual[side] = {
1130
+ dataMap: /* @__PURE__ */ new Map()
1131
+ };
1132
+ }
1133
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1134
+ mutual.byEntityId,
1135
+ flipMutual(mutual)
1136
+ );
1137
+ }),
1138
+ void 0,
1139
+ `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1140
+ );
1141
+ return { data: mutual };
1142
+ } catch (err) {
1143
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
1144
+ onError(error);
1145
+ return { error };
1146
+ }
1147
+ };
1148
+ const deleteMutual2 = async (byEntityType, entityType, byEntityId2, entityId = "", opts = {}) => {
1149
+ var _a;
1150
+ const mutualService = makeMutualService(byEntityType, entityType);
1151
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
1152
+ try {
1153
+ const { data } = await mutualService.deleteMutual(
1154
+ byEntityId2,
1155
+ entityId,
1156
+ opts
1157
+ );
1158
+ monoriseStore.setState(
1159
+ produce4((state) => {
1160
+ const bySide = getMutualStateKey(
1161
+ byEntityType,
1162
+ byEntityId2,
1163
+ entityType
1164
+ );
1165
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
1166
+ state.mutual[bySide].dataMap.delete(data.entityId);
1167
+ if (state.mutual[side]) {
1168
+ state.mutual[side].dataMap.delete(data.byEntityId);
1169
+ }
1170
+ }),
1171
+ void 0,
1172
+ `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1173
+ );
1174
+ return { data };
1175
+ } catch (err) {
1176
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
1177
+ onError(error);
1178
+ return { error };
1179
+ }
1180
+ };
1181
+ const deleteLocalMutual2 = (byEntityType, entityType, byEntityId2, entityId) => {
1182
+ monoriseStore.setState(
1183
+ produce4((state) => {
1184
+ var _a, _b;
1185
+ const bySide = getMutualStateKey(byEntityType, byEntityId2, entityType);
1186
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
1187
+ const bySideDataMap = new Map((_a = state.mutual[bySide]) == null ? void 0 : _a.dataMap);
1188
+ const sideDataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap);
1189
+ bySideDataMap.delete(entityId);
1190
+ sideDataMap.delete(byEntityId2);
1191
+ if (state.mutual[bySide]) {
1192
+ state.mutual[bySide].dataMap.delete(entityId);
1193
+ }
1194
+ if (state.mutual[side]) {
1195
+ state.mutual[side].dataMap.delete(byEntityId2);
1196
+ }
1197
+ }),
1198
+ void 0,
1199
+ `mr/mutual/local-delete/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`
1200
+ );
1201
+ };
1202
+ const deleteLocalMutualsByEntity = (entityType, id) => {
1203
+ const store2 = monoriseStore.getState();
1204
+ const entityConfig = store2.config;
1205
+ let mutuals = [];
1206
+ for (const i of Object.keys(entityConfig)) {
1207
+ const mutualState = store2.mutual[`${entityType}/${id}/${i}`];
1208
+ mutuals = [
1209
+ ...mutuals,
1210
+ ...Array.from((mutualState == null ? void 0 : mutualState.dataMap.values()) || [])
1211
+ ];
1212
+ }
1213
+ const updatedState = mutuals.reduce(
1214
+ (acc, mutual) => {
1215
+ var _a;
1216
+ const side = getMutualStateKey(
1217
+ mutual.entityType,
1218
+ mutual.entityId,
1219
+ mutual.byEntityType
1220
+ );
1221
+ const dataMap = new Map((_a = store2.mutual[side]) == null ? void 0 : _a.dataMap);
1222
+ dataMap.delete(id);
1223
+ acc[side] = __spreadProps(__spreadValues({}, store2.mutual[side]), {
1224
+ dataMap
40
1225
  });
1226
+ return acc;
1227
+ },
1228
+ {}
1229
+ );
1230
+ monoriseStore.setState(
1231
+ produce4((state) => {
1232
+ for (const key in updatedState) {
1233
+ state.mutual[key] = updatedState[key];
1234
+ }
1235
+ }),
1236
+ void 0,
1237
+ `mr/mutual/local-delete-by-entity/${entityType}/${id}`
1238
+ );
1239
+ };
1240
+ const useEntity2 = (entityType, id, opts = {}) => {
1241
+ const dataMap = monoriseStore(
1242
+ (state) => {
1243
+ var _a;
1244
+ return ((_a = state.entity[entityType]) == null ? void 0 : _a.dataMap) || /* @__PURE__ */ new Map();
1245
+ }
1246
+ );
1247
+ const isFirstFetched = monoriseStore(
1248
+ (state) => {
1249
+ var _a;
1250
+ return (_a = state.entity[entityType]) == null ? void 0 : _a.isFirstFetched;
1251
+ }
1252
+ );
1253
+ const requestKey = getEntityRequestKey("get", entityType, id);
1254
+ const isLoading = useLoadStore2(requestKey);
1255
+ const error = useErrorStore2(requestKey);
1256
+ useEffect2(() => {
1257
+ if (id) {
1258
+ getEntity2(entityType, id, opts);
1259
+ }
1260
+ }, [id, entityType, opts]);
1261
+ return {
1262
+ entity: id ? dataMap.get(id) : void 0,
1263
+ isLoading,
1264
+ error,
1265
+ requestKey,
1266
+ isFirstFetched,
1267
+ refetch: async () => {
1268
+ if (id) {
1269
+ const { data, error: fetchError } = await getEntity2(entityType, id, __spreadProps(__spreadValues({}, opts), {
1270
+ forceFetch: true
1271
+ }));
1272
+ if (fetchError) {
1273
+ }
1274
+ return data;
1275
+ }
1276
+ }
1277
+ };
1278
+ };
1279
+ const useEntityByUniqueField2 = (entityType, fieldName, value, opts = {}) => {
1280
+ const dataMap = monoriseStore(
1281
+ (state) => {
1282
+ var _a;
1283
+ return ((_a = state.entity[entityType]) == null ? void 0 : _a.dataMap) || /* @__PURE__ */ new Map();
1284
+ }
1285
+ );
1286
+ const isFirstFetched = monoriseStore(
1287
+ (state) => {
1288
+ var _a;
1289
+ return (_a = state.entity[entityType]) == null ? void 0 : _a.isFirstFetched;
1290
+ }
1291
+ );
1292
+ const stateKey = getUniqueFieldStateKey(fieldName, value || "");
1293
+ const requestKey = getUniqueFieldRequestKey(
1294
+ entityType,
1295
+ fieldName,
1296
+ value || ""
1297
+ );
1298
+ const isLoading = useLoadStore2(requestKey);
1299
+ const error = useErrorStore2(requestKey);
1300
+ useEffect2(() => {
1301
+ if (value) {
1302
+ getEntityByUniqueField(entityType, fieldName, value, opts);
1303
+ }
1304
+ }, [fieldName, value, entityType, opts]);
1305
+ return {
1306
+ entity: value ? dataMap.get(`${stateKey}`) : void 0,
1307
+ isLoading,
1308
+ error,
1309
+ requestKey,
1310
+ isFirstFetched,
1311
+ refetch: async () => {
1312
+ if (value) {
1313
+ const { data, error: fetchError } = await getEntityByUniqueField(
1314
+ entityType,
1315
+ fieldName,
1316
+ value,
1317
+ __spreadProps(__spreadValues({}, opts), {
1318
+ forceFetch: true
1319
+ })
1320
+ );
1321
+ if (fetchError) {
1322
+ }
1323
+ return data;
1324
+ }
1325
+ }
1326
+ };
1327
+ };
1328
+ const useEntities2 = (entityType, params = {}, opts = {}) => {
1329
+ const requestKey = getEntityRequestKey("list", entityType);
1330
+ const isListing = useLoadStore2(requestKey);
1331
+ const error = useErrorStore2(requestKey);
1332
+ const state = monoriseStore((state2) => state2.entity[entityType]);
1333
+ const { dataMap, searchResults, isFirstFetched, lastKey } = state != null ? state : {
1334
+ dataMap: /* @__PURE__ */ new Map()
1335
+ };
1336
+ const [entities, setEntities] = useState2();
1337
+ const [query, setQuery] = useState2("");
1338
+ const [skRange, setBetween] = useState2(params.skRange);
1339
+ const [all, setAll] = useState2(params.all);
1340
+ const [isSearching, setIsSearching] = useState2(false);
1341
+ const isLoading = isListing || isSearching;
1342
+ useEffect2(() => {
1343
+ if ((params == null ? void 0 : params.skRange) && (skRange == null ? void 0 : skRange.start) !== params.skRange.start && (skRange == null ? void 0 : skRange.end) !== params.skRange.end) {
1344
+ setBetween(params.skRange);
1345
+ }
1346
+ }, [skRange == null ? void 0 : skRange.end, skRange == null ? void 0 : skRange.start, params.skRange]);
1347
+ useEffect2(() => {
1348
+ if ((params == null ? void 0 : params.all) !== all) {
1349
+ setAll(params.all);
1350
+ }
1351
+ }, [all, params.all]);
1352
+ useEffect2(() => {
1353
+ if (!isFirstFetched) {
1354
+ listEntities(entityType, { skRange, all }, opts);
1355
+ }
1356
+ }, [all, entityType, skRange, opts, isFirstFetched]);
1357
+ useEffect2(() => {
1358
+ var _a;
1359
+ let queryTimeout;
1360
+ if (query == null ? void 0 : query.length) {
1361
+ setIsSearching(true);
1362
+ queryTimeout = setTimeout(async () => {
1363
+ await searchEntities(entityType, query);
1364
+ setIsSearching(false);
1365
+ }, (_a = opts.searchInterval) != null ? _a : 700);
1366
+ }
1367
+ return () => queryTimeout && clearTimeout(queryTimeout);
1368
+ }, [entityType, query]);
1369
+ useEffect2(() => {
1370
+ if (!query && dataMap.size !== (entities == null ? void 0 : entities.length)) {
1371
+ setIsSearching(false);
1372
+ setEntities(
1373
+ Array.from(dataMap.values()).sort(byEntityId)
1374
+ );
1375
+ }
1376
+ if (query) {
1377
+ setEntities(searchResults);
1378
+ }
1379
+ }, [
1380
+ dataMap,
1381
+ dataMap.size,
1382
+ entities == null ? void 0 : entities.length,
1383
+ query,
1384
+ searchResults,
1385
+ searchResults == null ? void 0 : searchResults.length
1386
+ ]);
1387
+ const handleQueryChange = (event) => {
1388
+ setQuery(event.target.value);
1389
+ };
1390
+ const searchField = {
1391
+ value: query,
1392
+ onChange: handleQueryChange
1393
+ };
1394
+ return {
1395
+ entities,
1396
+ entitiesMap: dataMap,
1397
+ searchField,
1398
+ isLoading,
1399
+ error,
1400
+ requestKey,
1401
+ isFirstFetched,
1402
+ lastKey,
1403
+ refetch: async () => {
1404
+ if (entityType && query) {
1405
+ await searchEntities(entityType, query);
1406
+ return;
1407
+ }
1408
+ await listEntities(
1409
+ entityType,
1410
+ { skRange, all },
1411
+ __spreadProps(__spreadValues({}, opts), {
1412
+ forceFetch: true
1413
+ })
1414
+ );
1415
+ },
1416
+ listMore: async () => {
1417
+ if (!lastKey) return;
1418
+ await listMoreEntities2(entityType, __spreadProps(__spreadValues({}, opts), {
1419
+ forceFetch: true
1420
+ }));
1421
+ }
1422
+ };
1423
+ };
1424
+ const useMutual2 = (byEntityType, entityType, byId, id, opts = {}) => {
1425
+ const stateKey = getMutualStateKey(byEntityType, byId, entityType);
1426
+ const state = monoriseStore((state2) => state2.mutual[stateKey]);
1427
+ const requestKey = getMutualRequestKey(
1428
+ "get",
1429
+ byEntityType,
1430
+ entityType,
1431
+ byId,
1432
+ id
1433
+ );
1434
+ const isLoading = useLoadStore2(requestKey);
1435
+ const error = useErrorStore2(requestKey);
1436
+ const { dataMap } = state || {
1437
+ dataMap: /* @__PURE__ */ new Map()
1438
+ };
1439
+ useEffect2(() => {
1440
+ if (!dataMap.get(id)) {
1441
+ getMutual2(byEntityType, entityType, byId, id, opts);
1442
+ }
1443
+ }, [byEntityType, byId, entityType, id, opts, dataMap]);
1444
+ return {
1445
+ mutual: dataMap.get(id),
1446
+ isLoading,
1447
+ error,
1448
+ requestKey
1449
+ };
1450
+ };
1451
+ const useMutuals2 = (byEntityType, entityType, byId, opts = {}, chainEntityQuery) => {
1452
+ const stateKey = getMutualStateKey(
1453
+ byEntityType,
1454
+ byId || "",
1455
+ entityType,
1456
+ void 0,
1457
+ chainEntityQuery
1458
+ );
1459
+ const state = monoriseStore((state2) => state2.mutual[stateKey]);
1460
+ const { dataMap, isFirstFetched, lastKey } = state || {
1461
+ dataMap: /* @__PURE__ */ new Map()
1462
+ };
1463
+ const [mutuals, setMutuals] = useState2([]);
1464
+ const requestKey = getMutualRequestKey(
1465
+ "list",
1466
+ byEntityType,
1467
+ entityType,
1468
+ byId || "",
1469
+ void 0,
1470
+ chainEntityQuery
1471
+ );
1472
+ const isLoading = useLoadStore2(requestKey);
1473
+ const error = useErrorStore2(requestKey);
1474
+ useEffect2(() => {
1475
+ if (!isFirstFetched && byEntityType && entityType && byId) {
1476
+ listEntitiesByEntity(
1477
+ byEntityType,
1478
+ entityType,
1479
+ byId,
1480
+ __spreadProps(__spreadValues({}, opts), { stateKey }),
1481
+ chainEntityQuery
1482
+ );
1483
+ }
1484
+ }, [
1485
+ isFirstFetched,
1486
+ byEntityType,
1487
+ byId,
1488
+ entityType,
1489
+ opts,
1490
+ chainEntityQuery,
1491
+ stateKey,
1492
+ opts == null ? void 0 : opts.forceFetch,
1493
+ opts == null ? void 0 : opts.noData
1494
+ ]);
1495
+ useEffect2(() => {
1496
+ const dataMapArray = Array.from(dataMap.values());
1497
+ if (dataMap.size !== (mutuals == null ? void 0 : mutuals.length) || dataMapArray.some(
1498
+ (item, index) => JSON.stringify(item) !== JSON.stringify(mutuals[index])
1499
+ )) {
1500
+ setMutuals(dataMapArray);
1501
+ }
1502
+ }, [dataMap, dataMap.size, mutuals == null ? void 0 : mutuals.length]);
1503
+ return __spreadValues({
1504
+ mutuals,
1505
+ mutualsMap: dataMap,
1506
+ isLoading,
1507
+ requestKey,
1508
+ error,
1509
+ isFirstFetched,
1510
+ lastKey
1511
+ }, lastKey && {
1512
+ listMore: () => {
1513
+ if (byEntityType && entityType && byId) {
1514
+ listEntitiesByEntity(
1515
+ byEntityType,
1516
+ entityType,
1517
+ byId,
1518
+ __spreadProps(__spreadValues({}, opts), {
1519
+ forceFetch: true,
1520
+ params: __spreadProps(__spreadValues({}, opts.params), { lastKey }),
1521
+ stateKey
1522
+ }),
1523
+ chainEntityQuery
1524
+ );
1525
+ }
1526
+ }
1527
+ });
1528
+ };
1529
+ const useTaggedEntities2 = (entityType, tagName, opts = {}) => {
1530
+ const { params } = opts || {};
1531
+ const stateKey = getTagStateKey(entityType, tagName, params);
1532
+ const state = monoriseStore((state2) => state2.tag[stateKey]);
1533
+ const { dataMap, isFirstFetched, lastKey } = state || {
1534
+ dataMap: /* @__PURE__ */ new Map()
1535
+ };
1536
+ const [entities, setEntities] = useState2([]);
1537
+ const requestKey = getTagRequestKey("list", entityType, tagName, params);
1538
+ const isLoading = useLoadStore2(requestKey);
1539
+ const error = useErrorStore2(requestKey);
1540
+ useEffect2(() => {
1541
+ if (entityType && tagName && Object.keys(params).length > 0) {
1542
+ listEntitiesByTag(entityType, tagName, opts);
1543
+ }
1544
+ }, [entityType, opts, tagName, params, opts == null ? void 0 : opts.forceFetch]);
1545
+ useEffect2(() => {
1546
+ const dataMapArray = Array.from(dataMap.values());
1547
+ if (dataMap.size !== (entities == null ? void 0 : entities.length) || dataMapArray.some(
1548
+ (item, index) => JSON.stringify(item) !== JSON.stringify(entities[index])
1549
+ )) {
1550
+ setEntities(dataMapArray);
1551
+ }
1552
+ }, [dataMap, dataMap.size, entities == null ? void 0 : entities.length]);
1553
+ return {
1554
+ entities,
1555
+ entitiesMap: dataMap,
1556
+ isLoading,
1557
+ requestKey,
1558
+ error,
1559
+ isFirstFetched,
1560
+ lastKey,
1561
+ refetch: async () => {
1562
+ if (entityType && tagName && (params == null ? void 0 : params.group)) {
1563
+ return await listEntitiesByTag(entityType, tagName, __spreadProps(__spreadValues({}, opts), {
1564
+ forceFetch: true
1565
+ }));
1566
+ }
1567
+ },
1568
+ listMore: async () => {
1569
+ if (entityType && tagName && (params == null ? void 0 : params.group)) {
1570
+ return await listEntitiesByTag(entityType, tagName, __spreadProps(__spreadValues({}, opts), {
1571
+ forceFetch: true,
1572
+ params: __spreadProps(__spreadValues({}, params), { lastKey })
1573
+ }));
1574
+ }
1575
+ }
41
1576
  };
42
- return Object.assign(Object.assign(Object.assign(Object.assign({ config,
43
- store,
44
- axios,
45
- authService,
46
- filestoreService,
47
- coreService }, configActions), appActions), authActions), coreActions);
48
- };
49
- const Monorise = initMonorise();
50
- const { store, axios, authService, filestoreService, coreService, setConfig, getConfig, useConfig, startLoading, endLoading, setError, getError, clearError, openModal, closeModal, useLoadStore, useInterruptiveLoadStore, useErrorStore, useModalStore, requestLogin, useProfile, getProfile, useIsUnauthorized, setIsUnauthorized, logout, listMoreEntities, createEntity, upsertEntity, editEntity, updateLocalEntity, deleteEntity, getMutual, createMutual, createLocalMutual, upsertLocalMutual, editMutual, deleteMutual, deleteLocalMutual, useEntity, useEntityByUniqueField, useEntities, useMutual, useMutuals, useTaggedEntities, useEntityState, getEntity, updateLocalTaggedEntity, deleteLocalTaggedEntity, } = Monorise;
51
- export { store, axios, authService, filestoreService, coreService, setConfig, getConfig, useConfig, startLoading, endLoading, setError, getError, clearError, openModal, closeModal, useLoadStore, useInterruptiveLoadStore, useErrorStore, useModalStore, requestLogin, useProfile, getProfile, useIsUnauthorized, setIsUnauthorized, logout, listMoreEntities, createEntity, upsertEntity, editEntity, updateLocalEntity, deleteEntity, getMutual, createMutual, createLocalMutual, upsertLocalMutual, editMutual, deleteMutual, deleteLocalMutual, useEntity, useEntityByUniqueField, useEntities, useMutual, useMutuals, useTaggedEntities, useEntityState, getEntityRequestKey, getMutualRequestKey, getTagRequestKey, getUniqueFieldRequestKey, getEntity, updateLocalTaggedEntity, deleteLocalTaggedEntity, };
52
- export default Monorise;
1577
+ };
1578
+ const useEntityState2 = (entityType) => {
1579
+ return monoriseStore((state) => state.entity[entityType]);
1580
+ };
1581
+ const updateLocalTaggedEntity2 = (entityType, entityId, tagName, data = {}, params) => {
1582
+ const tagKey = getTagStateKey(
1583
+ entityType,
1584
+ tagName,
1585
+ params
1586
+ );
1587
+ monoriseStore.setState(
1588
+ produce4((state) => {
1589
+ var _a, _b;
1590
+ const entity = (_b = (_a = state.tag[tagKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.get(entityId);
1591
+ if (entity) {
1592
+ state.tag[tagKey].dataMap.set(entityId, __spreadProps(__spreadValues({}, entity), {
1593
+ data: __spreadValues(__spreadValues({}, entity.data), data)
1594
+ }));
1595
+ }
1596
+ }),
1597
+ void 0,
1598
+ `mr/tag/local-update/${entityType}/${entityId}`
1599
+ );
1600
+ };
1601
+ const deleteLocalTaggedEntity2 = (entityType, entityId, tagName, params) => {
1602
+ const tagKey = getTagStateKey(
1603
+ entityType,
1604
+ tagName,
1605
+ params
1606
+ );
1607
+ monoriseStore.setState(
1608
+ produce4((state) => {
1609
+ var _a, _b;
1610
+ if ((_b = (_a = state.tag[tagKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.has(entityId)) {
1611
+ state.tag[tagKey].dataMap.delete(entityId);
1612
+ }
1613
+ }),
1614
+ void 0,
1615
+ `mr/tag/local-delete/${entityType}/${entityId}`
1616
+ );
1617
+ };
1618
+ return {
1619
+ listMoreEntities: listMoreEntities2,
1620
+ createEntity: createEntity2,
1621
+ upsertEntity: upsertEntity2,
1622
+ getEntity: getEntity2,
1623
+ editEntity: editEntity2,
1624
+ deleteEntity: deleteEntity2,
1625
+ getMutual: getMutual2,
1626
+ updateLocalEntity: updateLocalEntity2,
1627
+ createMutual: createMutual2,
1628
+ createLocalMutual: createLocalMutual2,
1629
+ upsertLocalMutual: upsertLocalMutual2,
1630
+ editMutual: editMutual2,
1631
+ deleteMutual: deleteMutual2,
1632
+ deleteLocalMutual: deleteLocalMutual2,
1633
+ useEntity: useEntity2,
1634
+ useEntityByUniqueField: useEntityByUniqueField2,
1635
+ useEntities: useEntities2,
1636
+ useMutual: useMutual2,
1637
+ useMutuals: useMutuals2,
1638
+ useTaggedEntities: useTaggedEntities2,
1639
+ useEntityState: useEntityState2,
1640
+ updateLocalTaggedEntity: updateLocalTaggedEntity2,
1641
+ deleteLocalTaggedEntity: deleteLocalTaggedEntity2
1642
+ };
1643
+ };
1644
+
1645
+ // lib/api.ts
1646
+ import request from "axios";
1647
+ var initAxiosInterceptor = (store2, appActions) => {
1648
+ const { startLoading: startLoading2 } = appActions;
1649
+ const axiosInstance = request.create();
1650
+ function makeRequest(url, config, data) {
1651
+ const { requestKey, isInterruptive = false, feedback } = config;
1652
+ const { ongoingRequests } = store2.getState().app;
1653
+ if (ongoingRequests.has(requestKey)) {
1654
+ return ongoingRequests.get(requestKey);
1655
+ }
1656
+ const promise = axiosInstance.request(__spreadProps(__spreadValues({}, config), {
1657
+ url,
1658
+ data,
1659
+ headers: __spreadProps(__spreadValues({}, config.headers || {}), {
1660
+ "Mr-Interruptive": String(isInterruptive)
1661
+ })
1662
+ }));
1663
+ startLoading2({
1664
+ requestKey,
1665
+ isInterruptive,
1666
+ message: feedback == null ? void 0 : feedback.loading,
1667
+ request: promise
1668
+ });
1669
+ return promise;
1670
+ }
1671
+ const axios2 = __spreadProps(__spreadValues({}, axiosInstance), {
1672
+ post: (url, data, config) => makeRequest(url, __spreadProps(__spreadValues({}, config), { method: "POST" }), data),
1673
+ put: (url, data, config) => makeRequest(url, __spreadProps(__spreadValues({}, config), { method: "PUT" }), data),
1674
+ patch: (url, data, config) => makeRequest(url, __spreadProps(__spreadValues({}, config), { method: "PATCH" }), data),
1675
+ delete: (url, config) => makeRequest(url, __spreadProps(__spreadValues({}, config), { method: "DELETE" })),
1676
+ get: (url, config) => makeRequest(url, __spreadProps(__spreadValues({}, config), { method: "GET" }))
1677
+ });
1678
+ return axios2;
1679
+ };
1680
+ var injectAxiosInterceptor = (appActions, authActions, axios2, opts) => {
1681
+ let options = opts || {};
1682
+ const { endLoading: endLoading2, setError: setError2, clearError: clearError2 } = appActions;
1683
+ const { setIsUnauthorized: setIsUnauthorized2 } = authActions;
1684
+ const unknownError = {
1685
+ status: 500,
1686
+ code: "UNKNOWN_EXCEPTION",
1687
+ message: "Ops, something doesn't seems right"
1688
+ };
1689
+ axios2.interceptors.response.use(
1690
+ (response) => {
1691
+ var _a;
1692
+ const { requestKey, isInterruptive, feedback } = response.config;
1693
+ endLoading2({ requestKey, isInterruptive });
1694
+ clearError2(requestKey);
1695
+ if (feedback == null ? void 0 : feedback.success) {
1696
+ (_a = options.onApiSuccess) == null ? void 0 : _a.call(options, response, feedback.success);
1697
+ }
1698
+ return Promise.resolve(response);
1699
+ },
1700
+ (error) => {
1701
+ var _a, _b, _c, _d, _e;
1702
+ const { requestKey, isInterruptive } = error.config;
1703
+ endLoading2({ requestKey, isInterruptive });
1704
+ const appError = ((_a = error.response) == null ? void 0 : _a.data) ? {
1705
+ code: (_b = error.response) == null ? void 0 : _b.statusText,
1706
+ status: (_c = error.response) == null ? void 0 : _c.status,
1707
+ message: (_d = error.response) == null ? void 0 : _d.data
1708
+ } : unknownError;
1709
+ setError2({ requestKey, error: appError });
1710
+ if (((_e = error.response) == null ? void 0 : _e.status) === 401) {
1711
+ setIsUnauthorized2(true);
1712
+ }
1713
+ return Promise.reject(error);
1714
+ }
1715
+ );
1716
+ const setOptions = (opts2) => {
1717
+ options = __spreadValues(__spreadValues({}, options), opts2);
1718
+ };
1719
+ return { setOptions };
1720
+ };
1721
+
1722
+ // services/auth.service.ts
1723
+ var initAuthService = (axios2) => {
1724
+ let options = {};
1725
+ const { apiBaseUrl = "/api/auth" } = options;
1726
+ const requestLoginMagicLink = (entityType, payload) => {
1727
+ return axios2.post(
1728
+ `${apiBaseUrl}/${entityType}`,
1729
+ {
1730
+ body: __spreadValues({}, payload)
1731
+ },
1732
+ {
1733
+ requestKey: `auth/${entityType}/request-login`,
1734
+ isInterruptive: true,
1735
+ feedback: {
1736
+ loading: "Requesting login link"
1737
+ }
1738
+ }
1739
+ );
1740
+ };
1741
+ const getSessionProfile = () => axios2.get("/session", {
1742
+ requestKey: "auth/session"
1743
+ });
1744
+ const logout2 = () => axios2.get("/logout", {
1745
+ requestKey: "auth/logout"
1746
+ });
1747
+ const setOptions = (opts) => {
1748
+ options = __spreadValues(__spreadValues({}, options), opts);
1749
+ };
1750
+ return {
1751
+ requestLoginMagicLink,
1752
+ getSessionProfile,
1753
+ logout: logout2,
1754
+ setOptions
1755
+ };
1756
+ };
1757
+ var auth_service_default = initAuthService;
1758
+
1759
+ // services/core.service.ts
1760
+ var ENTITY_API_BASE_URL = "/api/core/entity";
1761
+ var MUTUAL_API_BASE_URL = "/api/core/mutual";
1762
+ var TAG_API_BASE_URL = "/api/core/tag";
1763
+ var initCoreService = (monoriseStore, axios2, opts) => {
1764
+ let options = opts || {};
1765
+ const listEntities = (entityType, payload, opts2 = {}) => {
1766
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1767
+ return axios2.get(opts2.customUrl || `${entityApiBaseUrl}/${entityType}`, {
1768
+ requestKey: getEntityRequestKey("list", entityType),
1769
+ params: payload != null ? payload : void 0,
1770
+ isInterruptive: opts2.isInterruptive,
1771
+ feedback: opts2.feedback
1772
+ });
1773
+ };
1774
+ const searchEntities = (entityType, query, opts2 = {}) => {
1775
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1776
+ return axios2.get(
1777
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}`,
1778
+ {
1779
+ requestKey: getEntityRequestKey("search", entityType),
1780
+ params: { query },
1781
+ isInterruptive: opts2.isInterruptive,
1782
+ feedback: opts2.feedback
1783
+ }
1784
+ );
1785
+ };
1786
+ const listEntitiesByTag = (entityType, tagName, opts2 = {}) => {
1787
+ const { tagApiBaseUrl = TAG_API_BASE_URL } = options;
1788
+ return axios2.get(
1789
+ opts2.customUrl || `${tagApiBaseUrl}/${entityType}/${tagName}`,
1790
+ {
1791
+ requestKey: opts2.requestKey || getTagRequestKey("list", entityType, tagName, opts2.params),
1792
+ params: opts2.params,
1793
+ isInterruptive: opts2.isInterruptive,
1794
+ feedback: opts2.feedback
1795
+ }
1796
+ );
1797
+ };
1798
+ const getEntity2 = (entityType, id, opts2 = {}) => {
1799
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1800
+ return axios2.get(
1801
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
1802
+ {
1803
+ requestKey: getEntityRequestKey("get", entityType, id),
1804
+ isInterruptive: opts2.isInterruptive,
1805
+ feedback: __spreadValues({
1806
+ loading: `Retrieving ${entityType}`
1807
+ }, opts2.feedback || {})
1808
+ }
1809
+ );
1810
+ };
1811
+ const getEntityByUniqueField = (entityType, fieldName, value, opts2 = {}) => {
1812
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1813
+ return axios2.get(
1814
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}/unique/${getUniqueFieldStateKey(fieldName, value)}`,
1815
+ {
1816
+ requestKey: getUniqueFieldRequestKey(entityType, fieldName, value),
1817
+ isInterruptive: opts2.isInterruptive,
1818
+ feedback: __spreadValues({
1819
+ loading: `Retrieving ${entityType}`
1820
+ }, opts2.feedback || {})
1821
+ }
1822
+ );
1823
+ };
1824
+ const createEntity2 = (entityType, values, opts2 = {}) => {
1825
+ var _a;
1826
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1827
+ const entityConfig = monoriseStore.getState().config;
1828
+ return axios2.post(
1829
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}`,
1830
+ values,
1831
+ {
1832
+ requestKey: opts2.requestKey || getEntityRequestKey("create", entityType),
1833
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1834
+ feedback: __spreadValues({
1835
+ loading: `Creating ${entityConfig[entityType].displayName}`,
1836
+ success: `${entityConfig[entityType].displayName} created`
1837
+ }, opts2.feedback || {})
1838
+ }
1839
+ );
1840
+ };
1841
+ const upsertEntity2 = (entityType, id, values, opts2 = {}) => {
1842
+ var _a;
1843
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1844
+ const entityConfig = monoriseStore.getState().config;
1845
+ return axios2.put(
1846
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
1847
+ values,
1848
+ {
1849
+ requestKey: getEntityRequestKey("upsert", entityType, id),
1850
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1851
+ feedback: __spreadValues({
1852
+ loading: `Updating ${entityConfig[entityType].displayName}`,
1853
+ success: `${entityConfig[entityType].displayName} updated`
1854
+ }, opts2.feedback || {})
1855
+ }
1856
+ );
1857
+ };
1858
+ const editEntity2 = (entityType, id, values, opts2 = {}) => {
1859
+ var _a;
1860
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1861
+ const entityConfig = monoriseStore.getState().config;
1862
+ return axios2.patch(
1863
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
1864
+ values,
1865
+ {
1866
+ requestKey: getEntityRequestKey("edit", entityType, id),
1867
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1868
+ feedback: __spreadValues({
1869
+ loading: `Updating ${entityConfig[entityType].displayName}`,
1870
+ success: `${entityConfig[entityType].displayName} updated`
1871
+ }, opts2.feedback || {})
1872
+ }
1873
+ );
1874
+ };
1875
+ const deleteEntity2 = (entityType, id, opts2 = {}) => {
1876
+ var _a;
1877
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
1878
+ const entityConfig = monoriseStore.getState().config;
1879
+ return axios2.delete(
1880
+ opts2.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
1881
+ {
1882
+ requestKey: getEntityRequestKey("delete", entityType, id),
1883
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1884
+ feedback: __spreadValues({
1885
+ loading: `Deleting ${entityConfig[entityType].displayName}`,
1886
+ success: `${entityConfig[entityType].displayName} deleted`
1887
+ }, opts2.feedback || {})
1888
+ }
1889
+ );
1890
+ };
1891
+ const listEntitiesByEntity = (byEntityType, entityType, byEntityId2, opts2 = {}, chainEntityQuery) => {
1892
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
1893
+ return axios2.get(
1894
+ opts2.customUrl || `${mutualApiBaseUrl}/${byEntityType}/${byEntityId2}/${entityType}`,
1895
+ {
1896
+ requestKey: getMutualRequestKey(
1897
+ "list",
1898
+ byEntityType,
1899
+ entityType,
1900
+ byEntityId2,
1901
+ void 0,
1902
+ chainEntityQuery
1903
+ ),
1904
+ isInterruptive: opts2.isInterruptive,
1905
+ feedback: opts2.feedback,
1906
+ params: __spreadValues(__spreadValues({
1907
+ chainEntityQuery,
1908
+ limit: opts2.limit
1909
+ }, opts2.params), opts2.noData && { projection: "no-data" })
1910
+ }
1911
+ );
1912
+ };
1913
+ const getMutual2 = (byEntityType, entityType, byEntityId2, entityId, opts2 = {}) => {
1914
+ var _a;
1915
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
1916
+ return axios2.get(
1917
+ opts2.customUrl || `${mutualApiBaseUrl}/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`,
1918
+ {
1919
+ requestKey: getMutualRequestKey(
1920
+ "get",
1921
+ byEntityType,
1922
+ entityType,
1923
+ byEntityId2,
1924
+ entityId
1925
+ ),
1926
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : false,
1927
+ feedback: opts2.feedback
1928
+ }
1929
+ );
1930
+ };
1931
+ const createMutual2 = (byEntityType, entityType, byEntityId2, entityId, payload, opts2 = {}) => {
1932
+ var _a;
1933
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
1934
+ return axios2.post(
1935
+ opts2.customUrl || `${mutualApiBaseUrl}/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`,
1936
+ payload,
1937
+ {
1938
+ requestKey: getMutualRequestKey(
1939
+ "create",
1940
+ byEntityType,
1941
+ entityType,
1942
+ byEntityId2
1943
+ ),
1944
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1945
+ feedback: __spreadValues({
1946
+ loading: "Creating linkage",
1947
+ success: "Linkage created"
1948
+ }, opts2.feedback || {})
1949
+ }
1950
+ );
1951
+ };
1952
+ const editMutual2 = (byEntityType, entityType, byEntityId2, entityId, payload, opts2 = {}) => {
1953
+ var _a;
1954
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
1955
+ return axios2.patch(
1956
+ opts2.customUrl || `${mutualApiBaseUrl}/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`,
1957
+ payload,
1958
+ {
1959
+ requestKey: getMutualRequestKey(
1960
+ "update",
1961
+ byEntityType,
1962
+ entityType,
1963
+ byEntityId2,
1964
+ entityId
1965
+ ),
1966
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1967
+ feedback: __spreadValues({
1968
+ loading: "Updating linkage",
1969
+ success: "Linkage updated"
1970
+ }, opts2.feedback || {})
1971
+ }
1972
+ );
1973
+ };
1974
+ const deleteMutual2 = (byEntityType, entityType, byEntityId2, entityId, opts2 = {}) => {
1975
+ var _a;
1976
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
1977
+ return axios2.delete(
1978
+ opts2.customUrl || `${mutualApiBaseUrl}/${getMutualStateKey(byEntityType, byEntityId2, entityType, entityId)}`,
1979
+ {
1980
+ requestKey: getMutualRequestKey(
1981
+ "delete",
1982
+ byEntityType,
1983
+ entityType,
1984
+ byEntityId2,
1985
+ entityId
1986
+ ),
1987
+ isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
1988
+ feedback: __spreadValues({
1989
+ loading: "Removing linkage",
1990
+ success: "Linkage removed"
1991
+ }, opts2.feedback || {})
1992
+ }
1993
+ );
1994
+ };
1995
+ const makeEntityService = (entityType) => ({
1996
+ listEntities: (payload, opts2 = {}) => listEntities(entityType, payload, opts2),
1997
+ searchEntities: (query, opts2 = {}) => searchEntities(entityType, query, opts2),
1998
+ listEntitiesByTag: (tagName, opts2 = {}) => listEntitiesByTag(entityType, tagName, opts2),
1999
+ getEntity: (id, opts2 = {}) => getEntity2(entityType, id, opts2),
2000
+ getEntityByUniqueField: (fieldName, value, opts2 = {}) => getEntityByUniqueField(entityType, fieldName, value, opts2),
2001
+ createEntity: (values, opts2 = {}) => createEntity2(entityType, values, opts2),
2002
+ upsertEntity: (id, values, opts2 = {}) => upsertEntity2(entityType, id, values, opts2),
2003
+ editEntity: (id, values, opts2 = {}) => editEntity2(entityType, id, values, opts2),
2004
+ updateEntity: (id, values, opts2 = {}) => editEntity2(entityType, id, values, opts2),
2005
+ deleteEntity: (id, opts2 = {}) => deleteEntity2(entityType, id, opts2)
2006
+ });
2007
+ const makeMutualService = (byEntityType, entityType) => ({
2008
+ listEntitiesByEntity: (byEntityId2, opts2 = {}, chainEntityQuery) => listEntitiesByEntity(
2009
+ byEntityType,
2010
+ entityType,
2011
+ byEntityId2,
2012
+ opts2,
2013
+ chainEntityQuery
2014
+ ),
2015
+ getMutual: (byEntityId2, entityId, opts2 = {}) => getMutual2(byEntityType, entityType, byEntityId2, entityId, opts2),
2016
+ createMutual: (byEntityId2, entityId, payload, opts2 = {}) => createMutual2(
2017
+ byEntityType,
2018
+ entityType,
2019
+ byEntityId2,
2020
+ entityId,
2021
+ payload,
2022
+ opts2
2023
+ ),
2024
+ editMutual: (byEntityId2, entityId, payload, opts2 = {}) => editMutual2(byEntityType, entityType, byEntityId2, entityId, payload, opts2),
2025
+ deleteMutual: (byEntityId2, entityId, opts2 = {}) => deleteMutual2(byEntityType, entityType, byEntityId2, entityId, opts2)
2026
+ });
2027
+ const setOptions = (opts2) => {
2028
+ options = __spreadValues(__spreadValues({}, options), opts2);
2029
+ };
2030
+ return {
2031
+ makeEntityService,
2032
+ makeMutualService,
2033
+ setOptions
2034
+ };
2035
+ };
2036
+ var core_service_default = initCoreService;
2037
+
2038
+ // services/filestore.service.ts
2039
+ var initFilestoreService = (axios2) => {
2040
+ let options = {};
2041
+ const { apiBaseUrl = "/api/file" } = options;
2042
+ const uploadFile = async ({
2043
+ file,
2044
+ scope,
2045
+ directory,
2046
+ name,
2047
+ disableLoading,
2048
+ onProgress
2049
+ }) => {
2050
+ const { data } = await axios2.get(`${apiBaseUrl}/upload`, {
2051
+ requestKey: `filestore/presign/file/${name}`,
2052
+ isInterruptive: !disableLoading,
2053
+ feedback: {
2054
+ loading: "Preparing upload"
2055
+ },
2056
+ params: {
2057
+ scope,
2058
+ directory,
2059
+ filename: name
2060
+ }
2061
+ });
2062
+ return axios2.put(data.url, file, {
2063
+ requestKey: `filestore/upload/${name}`,
2064
+ isInterruptive: !disableLoading,
2065
+ feedback: {
2066
+ loading: "Uploading file"
2067
+ },
2068
+ headers: {
2069
+ "Content-Type": file.type,
2070
+ "Content-Disposition": "inline"
2071
+ },
2072
+ onUploadProgress: (progressEvent) => {
2073
+ if (onProgress && progressEvent.total) {
2074
+ const progress = Math.round(
2075
+ progressEvent.loaded / progressEvent.total * 100
2076
+ );
2077
+ onProgress(progress);
2078
+ }
2079
+ }
2080
+ });
2081
+ };
2082
+ const deleteFiles = async (keys) => {
2083
+ console.log("Deleting files", keys);
2084
+ const { data } = await axios2.post(
2085
+ `${apiBaseUrl}/delete`,
2086
+ {
2087
+ keys
2088
+ },
2089
+ {
2090
+ requestKey: "filestore/delete"
2091
+ }
2092
+ );
2093
+ return data;
2094
+ };
2095
+ const uploadVideo = async ({
2096
+ file,
2097
+ scope,
2098
+ directory,
2099
+ name
2100
+ }) => {
2101
+ const { data: presign } = await axios2.get(`${apiBaseUrl}/upload`, {
2102
+ requestKey: `filestore/presign/video/${name}`,
2103
+ isInterruptive: true,
2104
+ feedback: {
2105
+ loading: "Preparing upload"
2106
+ },
2107
+ params: {
2108
+ scope,
2109
+ directory,
2110
+ filename: name
2111
+ }
2112
+ });
2113
+ await axios2.put(presign.url, file, {
2114
+ requestKey: `filestore/upload/video/${name}`,
2115
+ isInterruptive: true,
2116
+ feedback: {
2117
+ loading: "Uploading video"
2118
+ },
2119
+ headers: {
2120
+ "Content-Type": file.type
2121
+ }
2122
+ });
2123
+ const { data } = await axios2.post(
2124
+ `${apiBaseUrl}/video/convert`,
2125
+ {
2126
+ scope,
2127
+ directory,
2128
+ filename: name
2129
+ },
2130
+ {
2131
+ requestKey: "filestore/convert/video",
2132
+ isInterruptive: true,
2133
+ feedback: {
2134
+ loading: "Triggering video conversion"
2135
+ }
2136
+ }
2137
+ );
2138
+ return data;
2139
+ };
2140
+ const uploadAudio = async ({
2141
+ file,
2142
+ scope,
2143
+ directory,
2144
+ name
2145
+ }) => {
2146
+ const { data: presign } = await axios2.get(`${apiBaseUrl}/upload`, {
2147
+ requestKey: `filestore/presign/audio/${name}`,
2148
+ isInterruptive: true,
2149
+ feedback: {
2150
+ loading: "Preparing upload"
2151
+ },
2152
+ params: {
2153
+ scope,
2154
+ directory,
2155
+ filename: name
2156
+ }
2157
+ });
2158
+ await axios2.put(presign.url, file, {
2159
+ requestKey: `filestore/upload/audio/${name}`,
2160
+ isInterruptive: true,
2161
+ feedback: {
2162
+ loading: "Uploading audio"
2163
+ },
2164
+ headers: {
2165
+ "Content-Type": file.type
2166
+ }
2167
+ });
2168
+ const { data } = await axios2.post(
2169
+ `${apiBaseUrl}/audio/convert`,
2170
+ {
2171
+ scope,
2172
+ directory,
2173
+ filename: name
2174
+ },
2175
+ {
2176
+ requestKey: "filestore/convert/audio",
2177
+ isInterruptive: true,
2178
+ feedback: {
2179
+ loading: "Triggering audio conversion"
2180
+ }
2181
+ }
2182
+ );
2183
+ return data;
2184
+ };
2185
+ const getJobStatus = async (id) => {
2186
+ const { data } = await axios2.get(`${apiBaseUrl}/job/${id}`, {
2187
+ requestKey: `filestore/get-job/${id}`
2188
+ });
2189
+ return data;
2190
+ };
2191
+ const setOptions = (opts) => {
2192
+ options = __spreadValues(__spreadValues({}, options), opts);
2193
+ };
2194
+ return {
2195
+ uploadFile,
2196
+ deleteFiles,
2197
+ uploadVideo,
2198
+ uploadAudio,
2199
+ getJobStatus,
2200
+ setOptions
2201
+ };
2202
+ };
2203
+ var filestore_service_default = initFilestoreService;
2204
+
2205
+ // store/monorise.store.ts
2206
+ import { enableMapSet } from "immer";
2207
+ import { create } from "zustand";
2208
+ import { devtools } from "zustand/middleware";
2209
+ enableMapSet();
2210
+ var configureDevtools = (storeName) => {
2211
+ var _a;
2212
+ return {
2213
+ name: `${storeName} Store`,
2214
+ anonymousActionType: `${storeName.toLowerCase()}/action`,
2215
+ enabled: !["prod", "dev"].includes((_a = process.env.NEXT_PUBLIC_ENVIRONMENT) != null ? _a : ""),
2216
+ serialize: {
2217
+ options: {
2218
+ map: true
2219
+ }
2220
+ }
2221
+ };
2222
+ };
2223
+ var initMonoriseStore = () => {
2224
+ let options = {};
2225
+ const { modals = {} } = options;
2226
+ const monoriseStore = create(
2227
+ devtools(
2228
+ () => ({
2229
+ app: {
2230
+ ongoingRequests: /* @__PURE__ */ new Map(),
2231
+ loadStack: /* @__PURE__ */ new Map(),
2232
+ intLoadStack: /* @__PURE__ */ new Map(),
2233
+ loadingMessage: "",
2234
+ errorStack: /* @__PURE__ */ new Map(),
2235
+ modal: {
2236
+ name: null
2237
+ }
2238
+ },
2239
+ config: {},
2240
+ entity: {},
2241
+ mutual: {},
2242
+ tag: {},
2243
+ auth: {
2244
+ isUnauthorized: false,
2245
+ profile: {
2246
+ displayName: "",
2247
+ email: "",
2248
+ accountId: ""
2249
+ },
2250
+ isLoading: true
2251
+ }
2252
+ }),
2253
+ configureDevtools("monorise")
2254
+ )
2255
+ );
2256
+ const setOptions = (opts) => {
2257
+ options = __spreadValues(__spreadValues({}, options), opts);
2258
+ };
2259
+ return { setOptions, monoriseStore };
2260
+ };
2261
+
2262
+ // index.ts
2263
+ var initMonorise = () => {
2264
+ const { monoriseStore: store2, setOptions: setMonoriseOptions } = initMonoriseStore();
2265
+ const appActions = initAppActions(store2);
2266
+ const configActions = initConfigActions(store2);
2267
+ const axios2 = initAxiosInterceptor(store2, appActions);
2268
+ const authService2 = auth_service_default(axios2);
2269
+ const filestoreService2 = filestore_service_default(axios2);
2270
+ const coreService2 = core_service_default(store2, axios2);
2271
+ const authActions = initAuthActions(store2, authService2);
2272
+ const coreActions = initCoreActions(store2, appActions, coreService2);
2273
+ const axiosInterceptor = injectAxiosInterceptor(
2274
+ appActions,
2275
+ authActions,
2276
+ axios2
2277
+ );
2278
+ const config = (opts) => {
2279
+ setMonoriseOptions({
2280
+ modals: opts.modals
2281
+ });
2282
+ configActions.setConfig(opts.entityConfig);
2283
+ authService2.setOptions({
2284
+ apiBaseUrl: opts.authBaseUrl
2285
+ });
2286
+ filestoreService2.setOptions({
2287
+ apiBaseUrl: opts.filestoreBaseUrl
2288
+ });
2289
+ coreService2.setOptions({
2290
+ entityApiBaseUrl: opts.entityBaseUrl,
2291
+ mutualApiBaseUrl: opts.mutualBaseUrl,
2292
+ tagApiBaseUrl: opts.tagBaseUrl
2293
+ });
2294
+ axiosInterceptor.setOptions({
2295
+ onApiSuccess: opts.onApiSuccess
2296
+ });
2297
+ };
2298
+ return __spreadValues(__spreadValues(__spreadValues(__spreadValues({
2299
+ config,
2300
+ store: store2,
2301
+ axios: axios2,
2302
+ authService: authService2,
2303
+ filestoreService: filestoreService2,
2304
+ coreService: coreService2
2305
+ }, configActions), appActions), authActions), coreActions);
2306
+ };
2307
+ var Monorise = initMonorise();
2308
+ var {
2309
+ store,
2310
+ axios,
2311
+ authService,
2312
+ filestoreService,
2313
+ coreService,
2314
+ setConfig,
2315
+ getConfig,
2316
+ useConfig,
2317
+ startLoading,
2318
+ endLoading,
2319
+ setError,
2320
+ getError,
2321
+ clearError,
2322
+ openModal,
2323
+ closeModal,
2324
+ useLoadStore,
2325
+ useInterruptiveLoadStore,
2326
+ useErrorStore,
2327
+ useModalStore,
2328
+ requestLogin,
2329
+ useProfile,
2330
+ getProfile,
2331
+ useIsUnauthorized,
2332
+ setIsUnauthorized,
2333
+ logout,
2334
+ listMoreEntities,
2335
+ createEntity,
2336
+ upsertEntity,
2337
+ editEntity,
2338
+ updateLocalEntity,
2339
+ deleteEntity,
2340
+ getMutual,
2341
+ createMutual,
2342
+ createLocalMutual,
2343
+ upsertLocalMutual,
2344
+ editMutual,
2345
+ deleteMutual,
2346
+ deleteLocalMutual,
2347
+ useEntity,
2348
+ useEntityByUniqueField,
2349
+ useEntities,
2350
+ useMutual,
2351
+ useMutuals,
2352
+ useTaggedEntities,
2353
+ useEntityState,
2354
+ getEntity,
2355
+ updateLocalTaggedEntity,
2356
+ deleteLocalTaggedEntity
2357
+ } = Monorise;
2358
+ var index_default = Monorise;
2359
+ export {
2360
+ authService,
2361
+ axios,
2362
+ clearError,
2363
+ closeModal,
2364
+ coreService,
2365
+ createEntity,
2366
+ createLocalMutual,
2367
+ createMutual,
2368
+ index_default as default,
2369
+ deleteEntity,
2370
+ deleteLocalMutual,
2371
+ deleteLocalTaggedEntity,
2372
+ deleteMutual,
2373
+ editEntity,
2374
+ editMutual,
2375
+ endLoading,
2376
+ filestoreService,
2377
+ getConfig,
2378
+ getEntity,
2379
+ getEntityRequestKey,
2380
+ getError,
2381
+ getMutual,
2382
+ getMutualRequestKey,
2383
+ getProfile,
2384
+ getTagRequestKey,
2385
+ getUniqueFieldRequestKey,
2386
+ listMoreEntities,
2387
+ logout,
2388
+ openModal,
2389
+ requestLogin,
2390
+ setConfig,
2391
+ setError,
2392
+ setIsUnauthorized,
2393
+ startLoading,
2394
+ store,
2395
+ updateLocalEntity,
2396
+ updateLocalTaggedEntity,
2397
+ upsertEntity,
2398
+ upsertLocalMutual,
2399
+ useConfig,
2400
+ useEntities,
2401
+ useEntity,
2402
+ useEntityByUniqueField,
2403
+ useEntityState,
2404
+ useErrorStore,
2405
+ useInterruptiveLoadStore,
2406
+ useIsUnauthorized,
2407
+ useLoadStore,
2408
+ useModalStore,
2409
+ useMutual,
2410
+ useMutuals,
2411
+ useProfile,
2412
+ useTaggedEntities
2413
+ };
53
2414
  //# sourceMappingURL=index.js.map