@emailmaker/filemanager 0.10.103 → 1.1.70

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 (37) hide show
  1. package/.build-meta.json +5 -0
  2. package/assets/image-editor/fonts/Rubik-Black.woff2 +0 -0
  3. package/assets/image-editor/fonts/Rubik-Bold.woff2 +0 -0
  4. package/assets/image-editor/fonts/Rubik-Light.woff2 +0 -0
  5. package/assets/image-editor/fonts/Rubik-Regular.woff2 +0 -0
  6. package/assets/image-editor/fonts/caveat-v23-cyrillic_latin-regular.woff2 +0 -0
  7. package/assets/image-editor/fonts/commissioner-v24-cyrillic_latin-regular.woff2 +0 -0
  8. package/assets/image-editor/fonts/inter-v20-cyrillic_latin-regular.woff2 +0 -0
  9. package/assets/image-editor/fonts/oswald-v57-cyrillic_latin-regular.woff2 +0 -0
  10. package/assets/image-editor/fonts/pacifico-v23-cyrillic_latin-regular.woff2 +0 -0
  11. package/assets/image-editor/fonts/playfair-display-v40-cyrillic_latin-regular.woff2 +0 -0
  12. package/components/PixieEditor/PixieEditor.d.ts +1 -2
  13. package/components/PixieEditor/index.d.ts +4 -1
  14. package/core.d.ts +21 -0
  15. package/core.esm.js +2727 -0
  16. package/core.js +2 -0
  17. package/{file-manager-IconsTab-Cv51TWYV.js → file-manager-IconsTab-pUKiId5k.js} +527 -519
  18. package/{file-manager-ImageCodeGenerator-DroVucLL.js → file-manager-ImageCodeGenerator-sUlzbDys.js} +118 -118
  19. package/{file-manager-JsBarcode-Cji_rnaG.js → file-manager-JsBarcode-CyaffT5b.js} +28 -29
  20. package/file-manager-PixieEditor-Doy__jUq.js +597 -0
  21. package/{file-manager-browser-CweGvoKm.js → file-manager-browser-DDyRNDQk.js} +4 -4
  22. package/{file-manager-hooks-Dg-OcR_x.js → file-manager-hooks-vqT-4jeg.js} +2593 -2397
  23. package/{file-manager-rolldown-runtime-DtPi1Y-2.js → file-manager-rolldown-runtime-DAVfCfog.js} +11 -4
  24. package/file-manager-urls-DN9gaYil.js +37 -0
  25. package/{file-manager-useMediaListEngine-ByGmaaib.js → file-manager-useMediaListEngine-BitrHuUM.js} +6 -6
  26. package/file-manager.css +1 -1
  27. package/file-manager.d.ts +2 -20
  28. package/file-manager.esm.js +2382 -3049
  29. package/file-manager.js +11 -11
  30. package/notification.d.ts +4 -4
  31. package/package.json +24 -1
  32. package/utils/iconCssVars.d.ts +12 -0
  33. package/utils/pixieEditorConfig.d.ts +38 -0
  34. package/utils/pixieEditorConfig.test.d.ts +1 -0
  35. package/utils/svgToPng.test.d.ts +1 -0
  36. package/utils/urlNormalization.d.ts +9 -0
  37. package/utils/urlNormalization.test.d.ts +1 -0
package/core.esm.js ADDED
@@ -0,0 +1,2727 @@
1
+ //#region packages/file-manager/src/shared/fileManagerApiError.ts
2
+ var e = class e extends Error {
3
+ constructor(t, n, r, i) {
4
+ super(t), this.code = n, this.status = r, this.details = i, this.name = "FileManagerApiError", Error.captureStackTrace && Error.captureStackTrace(this, e);
5
+ }
6
+ };
7
+ //#endregion
8
+ //#region packages/file-manager/src/shared/mapHttpStatusToErrorCode.ts
9
+ function t(e, t = "BackendError") {
10
+ switch (e) {
11
+ case 400: return "ValidationError";
12
+ case 401: return "AuthenticationFailed";
13
+ case 403: return "AuthorizationFailed";
14
+ case 404: return "FileNotFound";
15
+ case 413: return "PayloadTooLarge";
16
+ case 415: return "UnsupportedMediaType";
17
+ case 429: return "RateLimitExceeded";
18
+ case 500: return "BackendError";
19
+ case 503: return "ServiceUnavailable";
20
+ case 504: return "Timeout";
21
+ default: return t;
22
+ }
23
+ }
24
+ //#endregion
25
+ //#region packages/file-manager/src/helpers/urls.ts
26
+ function n(e, t) {
27
+ return e ? t ? `${e.replace(/\/+$/, "")}/${t.replace(/^\/+/, "")}` : e : t;
28
+ }
29
+ function r(e) {
30
+ let t = typeof __webpack_public_path__ < "u" ? __webpack_public_path__ : "";
31
+ if (e && /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(e)) return e;
32
+ let r = n(t, e || "");
33
+ return r && !r.startsWith("/") && !/^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(r) && (r = "/" + r), r;
34
+ }
35
+ function i(e, t) {
36
+ if (/^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(t)) return t;
37
+ let i = n(r(e), t || "");
38
+ return i && !i.startsWith("/") && !/^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(i) ? "/" + i : i;
39
+ }
40
+ //#endregion
41
+ //#region packages/file-manager/src/adapters/FailedRequestCache.ts
42
+ var a = "", o = class {
43
+ constructor() {
44
+ this.map = /* @__PURE__ */ new Map();
45
+ }
46
+ static childrenViewKey(e, t) {
47
+ return `v${a}${e}${a}${t}`;
48
+ }
49
+ static childrenCountKey(e) {
50
+ return `c${a}${e}`;
51
+ }
52
+ getFreshError(e, t) {
53
+ let n = this.map.get(e);
54
+ return n ? t !== Infinity && Date.now() - n.updatedAt > t ? (this.map.delete(e), null) : n.error : null;
55
+ }
56
+ setError(e, t) {
57
+ this.map.set(e, {
58
+ error: t,
59
+ updatedAt: Date.now()
60
+ });
61
+ }
62
+ delete(e) {
63
+ this.map.delete(e);
64
+ }
65
+ clearChildrenViewErrorsForParent(e) {
66
+ let t = `v${a}${e}${a}`;
67
+ for (let e of [...this.map.keys()]) e.startsWith(t) && this.map.delete(e);
68
+ }
69
+ clearAll() {
70
+ this.map.clear();
71
+ }
72
+ }, s = class {
73
+ constructor() {
74
+ this.deletingIds = /* @__PURE__ */ new Set();
75
+ }
76
+ beginDeleting(e) {
77
+ for (let t of e) t && this.deletingIds.add(t);
78
+ }
79
+ endDeleting(e) {
80
+ for (let t of e) t && this.deletingIds.delete(t);
81
+ }
82
+ isDeleting(e) {
83
+ return !!e && this.deletingIds.has(e);
84
+ }
85
+ };
86
+ //#endregion
87
+ //#region packages/file-manager/src/adapters/cacheViewStrategy.ts
88
+ function c(e) {
89
+ return {
90
+ sortBy: e?.sortBy ?? "name",
91
+ sortOrder: e?.sortOrder == null ? e?.sortBy != null && e.sortBy !== "" ? "desc" : "asc" : e.sortOrder === "desc" ? "desc" : "asc"
92
+ };
93
+ }
94
+ function l(e, t, n) {
95
+ let { sortBy: r, sortOrder: i } = c(n), a = 0;
96
+ switch (r) {
97
+ case "name":
98
+ a = (e.name || "").localeCompare(t.name || "");
99
+ break;
100
+ case "date":
101
+ a = new Date(e.date || 0).getTime() - new Date(t.date || 0).getTime();
102
+ break;
103
+ case "size":
104
+ a = (Number(e.size) || 0) - (Number(t.size) || 0);
105
+ break;
106
+ case "type":
107
+ a = (e.extension || e.type || "").toLowerCase().localeCompare((t.extension || t.type || "").toLowerCase());
108
+ break;
109
+ default: a = (e.name || "").localeCompare(t.name || "");
110
+ }
111
+ return i === "desc" ? -a : a;
112
+ }
113
+ function u(e) {
114
+ return e != null && String(e).trim() !== "";
115
+ }
116
+ function d(e, t) {
117
+ let { sortBy: n } = c(t);
118
+ switch (n) {
119
+ case "date": return u(e.date) ? !Number.isNaN(new Date(e.date).getTime()) : !1;
120
+ case "size": return e.size != null && Number.isFinite(Number(e.size));
121
+ case "type": return u(e.extension) || u(e.type);
122
+ default: return u(e.name);
123
+ }
124
+ }
125
+ function f(e, t) {
126
+ let n = typeof t?.search == "string" ? t.search.trim() : "";
127
+ if (!n) return !0;
128
+ let r = n.toLowerCase();
129
+ return (e.name?.toLowerCase().includes(r) ?? !1) || (e.type?.toLowerCase().includes(r) ?? !1) || (e.extension?.toLowerCase().includes(r) ?? !1);
130
+ }
131
+ var p = l, m = f, h = d;
132
+ function g(e = {}) {
133
+ return {
134
+ ...e,
135
+ comparator: e.comparator ?? p,
136
+ filterMatch: e.filterMatch ?? m,
137
+ hasSortValue: e.hasSortValue ?? h
138
+ };
139
+ }
140
+ //#endregion
141
+ //#region packages/file-manager/src/adapters/systemFolderPath.ts
142
+ var _ = "Trash";
143
+ function v(e) {
144
+ return e.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
145
+ }
146
+ function y(e, t, n = _) {
147
+ let r = v(e);
148
+ if (!r) return [];
149
+ let i = [e, r];
150
+ if (r === v(t)) {
151
+ i.push(t);
152
+ let e = v(t);
153
+ e === "Trash" || e.toLowerCase() === "trash" ? i.push(n, _, _.toLowerCase()) : n !== "Trash" && v(n) !== e && i.push(n);
154
+ }
155
+ return Array.from(new Set(i.map(v).filter(Boolean)));
156
+ }
157
+ function b(e) {
158
+ let t = /* @__PURE__ */ new Map();
159
+ for (let [n, r] of Object.entries(e)) r.id && t.set(v(n), r);
160
+ return (e) => {
161
+ let n = t.get(v(e));
162
+ if (n) return {
163
+ id: n.id,
164
+ name: n.name ?? n.id,
165
+ folderId: n.folderId ?? null,
166
+ isFolder: !0
167
+ };
168
+ };
169
+ }
170
+ //#endregion
171
+ //#region packages/file-manager/src/utils/urlNormalization.ts
172
+ var x = /^(utm_[a-z0-9_]+|fbclid|gclid|igshid|mc_cid|mc_eid)$/i;
173
+ function S(e) {
174
+ try {
175
+ return decodeURIComponent(e.replace(/\+/g, " "));
176
+ } catch {
177
+ return e;
178
+ }
179
+ }
180
+ function C(e) {
181
+ let t = new URL(e.trim());
182
+ t.hash = "", t.hostname = t.hostname.toLowerCase(), (t.protocol === "https:" && t.port === "443" || t.protocol === "http:" && t.port === "80") && (t.port = "");
183
+ let n = (t.search.startsWith("?") ? t.search.slice(1) : t.search).split("&").filter(Boolean).map((e) => {
184
+ let t = e.indexOf("="), n = t === -1 ? e : e.slice(0, t), r = t === -1 ? "" : e.slice(t + 1);
185
+ return {
186
+ pair: e,
187
+ key: S(n),
188
+ value: S(r)
189
+ };
190
+ }).filter(({ key: e }) => !x.test(e)).sort((e, t) => {
191
+ let n = e.key.localeCompare(t.key);
192
+ return n === 0 ? e.value.localeCompare(t.value) : n;
193
+ });
194
+ return t.search = n.length ? `?${n.map(({ pair: e }) => e).join("&")}` : "", t.toString();
195
+ }
196
+ function w(e) {
197
+ try {
198
+ return C(e);
199
+ } catch {
200
+ return e.trim();
201
+ }
202
+ }
203
+ //#endregion
204
+ //#region packages/file-manager/src/adapters/TreeCache.ts
205
+ var T = "", E = 1800 * 1e3;
206
+ function D(e) {
207
+ return e == null || e === "" ? T : e;
208
+ }
209
+ function O(e) {
210
+ if (e == null) return Infinity;
211
+ let t = Number(e);
212
+ return Number.isFinite(t) && t >= 0 ? t : Infinity;
213
+ }
214
+ function k(e) {
215
+ return !e || typeof e.search != "string" ? !1 : e.search.trim().length > 0;
216
+ }
217
+ function A(e) {
218
+ if (!e) return null;
219
+ let t = e.folderId;
220
+ return t == null ? null : String(t);
221
+ }
222
+ function j(e) {
223
+ return !!e && !!e.isFolder;
224
+ }
225
+ function M() {
226
+ if (typeof globalThis?.crypto?.randomUUID == "function") return globalThis.crypto.randomUUID();
227
+ try {
228
+ let e = globalThis.crypto || globalThis.msCrypto;
229
+ if (e && typeof e.getRandomValues == "function") {
230
+ let t = /* @__PURE__ */ new Uint8Array(16);
231
+ e.getRandomValues(t);
232
+ let n = "";
233
+ for (let e = 0; e < t.length; e++) n += t[e].toString(16).padStart(2, "0");
234
+ return `id_${n}`;
235
+ }
236
+ } catch {}
237
+ return `id_fallback_${Date.now().toString(36)}_${Math.floor(Math.random() * (2 ** 53 - 1)).toString(36).slice(0, 8)}`;
238
+ }
239
+ function N(e) {
240
+ if (!e || typeof e != "object") return !1;
241
+ let t = e;
242
+ return t.code === "path_folder_not_found" || t.code === "folder_not_found" || t.code === "FolderNotFound" || t.data?.code === "path_folder_not_found" || t.data?.code === "folder_not_found" || t.status === 404 || t.data?.data?.status === 404;
243
+ }
244
+ function P(e) {
245
+ let t = 2166136261;
246
+ for (let n = 0; n < e.length; n++) t ^= e.charCodeAt(n), t = t + ((t << 1) + (t << 4) + (t << 7) + (t << 8) + (t << 24)) >>> 0;
247
+ return t.toString(36);
248
+ }
249
+ function F(e) {
250
+ if (!e || typeof e != "object") return String(e ?? "");
251
+ let t = e, n = [], r = {}, i = [
252
+ "page",
253
+ "pageSize",
254
+ "sortBy",
255
+ "sortOrder",
256
+ "search",
257
+ "itemType",
258
+ "limit"
259
+ ];
260
+ for (let e of Object.keys(t).sort()) {
261
+ let a = t[e];
262
+ i.includes(e) ? n.push(`${e}=${String(a ?? "")}`) : r[e] = a;
263
+ }
264
+ let a = Object.keys(r).length ? JSON.stringify(r) : "", o = n.join("|"), s = a ? P(a) : "";
265
+ return `${o}${s ? `|${s}` : ""}`;
266
+ }
267
+ var I = class e {
268
+ constructor() {
269
+ this.entities = /* @__PURE__ */ new Map(), this.childrenByParent = /* @__PURE__ */ new Map(), this.maxCacheSize = 1e4;
270
+ }
271
+ now() {
272
+ return Date.now();
273
+ }
274
+ checkCacheSize() {
275
+ if (this.entities.size > this.maxCacheSize) {
276
+ let e = Array.from(this.entities.entries()).sort((e, t) => e[1].updatedAt - t[1].updatedAt), t = Math.floor(this.maxCacheSize * .1);
277
+ for (let n = 0; n < t && n < e.length; n++) {
278
+ let [t, r] = e[n];
279
+ this.deleteEntityById(t);
280
+ }
281
+ }
282
+ }
283
+ getEntity(e) {
284
+ return this.entities.get(e)?.value;
285
+ }
286
+ getRecord(e) {
287
+ return this.entities.get(e);
288
+ }
289
+ getFileSystemItem(e) {
290
+ let t = this.getEntity(e);
291
+ return t && !j(t) ? t : void 0;
292
+ }
293
+ getFolder(e) {
294
+ let t = this.getEntity(e);
295
+ return t && j(t) ? t : void 0;
296
+ }
297
+ getChildrenIds(e, t = {}) {
298
+ let n = D(e), r = this.childrenByParent.get(n);
299
+ return r ? t.onlyFileSystemItems ? Array.from(r.files) : t.onlyFolders ? Array.from(r.folders) : [...Array.from(r.folders), ...Array.from(r.files)] : [];
300
+ }
301
+ searchByUrl(e) {
302
+ let t = w(e);
303
+ for (let n of this.entities.values()) {
304
+ let r = n.value.url;
305
+ if (r && (r === e || w(r) === t)) return n.value;
306
+ }
307
+ return null;
308
+ }
309
+ upsertEntity(e) {
310
+ let t = this.entities.get(e.id) ?? null, n = t ? t.value : null, r = n ? A(n) : null, i = A(e), a = n ? j(n) : !1, o = j(e), s;
311
+ if (n && j(n) && o) {
312
+ let t = n.childrenCount, r = e.childrenCount;
313
+ t !== void 0 && r === void 0 && (s = t);
314
+ }
315
+ if (n && (r !== i || a !== o)) {
316
+ let t = D(r), n = this.childrenByParent.get(t);
317
+ n && (a ? n.folders.delete(e.id) : n.files.delete(e.id), n.files.size === 0 && n.folders.size === 0 && this.childrenByParent.delete(t));
318
+ }
319
+ let c = e;
320
+ s !== void 0 && (c = {
321
+ ...e,
322
+ childrenCount: s
323
+ }), this.entities.set(e.id, {
324
+ value: { ...c },
325
+ updatedAt: this.now()
326
+ }), this.checkCacheSize();
327
+ let l = D(i), u = this.childrenByParent.get(l);
328
+ u || (u = {
329
+ files: /* @__PURE__ */ new Set(),
330
+ folders: /* @__PURE__ */ new Set()
331
+ }, this.childrenByParent.set(l, u)), o ? u.folders.add(e.id) : u.files.add(e.id);
332
+ }
333
+ updateServerChildrenCount(e, t) {
334
+ if (e == null || t == null || Number.isNaN(t)) return;
335
+ let n = this.entities.get(e);
336
+ if (!n || !j(n.value)) return;
337
+ let r = {
338
+ ...n.value,
339
+ childrenCount: t
340
+ };
341
+ this.entities.set(e, {
342
+ value: r,
343
+ updatedAt: this.now()
344
+ });
345
+ }
346
+ deleteEntityById(e) {
347
+ let t = this.entities.get(e) ?? null;
348
+ if (!t) return {
349
+ entity: null,
350
+ parentId: null
351
+ };
352
+ let n = t.value, r = A(n), i = D(r), a = this.childrenByParent.get(i);
353
+ return a && (j(n) ? a.folders.delete(e) : a.files.delete(e), a.files.size === 0 && a.folders.size === 0 && this.childrenByParent.delete(i)), this.entities.delete(e), {
354
+ entity: { ...n },
355
+ parentId: r
356
+ };
357
+ }
358
+ deleteFolderRecursive(e) {
359
+ let t = this.entities.get(e);
360
+ if (!t || !j(t.value)) return {
361
+ deletedFolderRecords: [],
362
+ deletedFileSystemItemRecords: []
363
+ };
364
+ let n = [{
365
+ id: e,
366
+ parentId: A(t.value)
367
+ }], r = [], i = [];
368
+ for (; n.length;) {
369
+ let e = n.shift();
370
+ r.push(e);
371
+ let t = this.childrenByParent.get(D(e.id));
372
+ if (t) {
373
+ for (let e of Array.from(t.folders)) {
374
+ let t = this.entities.get(e);
375
+ t && n.push({
376
+ id: e,
377
+ parentId: A(t.value)
378
+ });
379
+ }
380
+ for (let e of Array.from(t.files)) i.push(e);
381
+ }
382
+ }
383
+ let a = [];
384
+ for (let e of i) {
385
+ let t = this.entities.get(e);
386
+ if (!t) continue;
387
+ let n = t.value, r = D(A(n)), i = this.childrenByParent.get(r);
388
+ i && (i.files.delete(e), i.files.size === 0 && i.folders.size === 0 && this.childrenByParent.delete(r)), this.entities.delete(e), a.push({ ...n });
389
+ }
390
+ let o = [];
391
+ for (let e = r.length - 1; e >= 0; e--) {
392
+ let { id: t, parentId: n } = r[e], i = this.entities.get(t);
393
+ if (!i) continue;
394
+ let a = i.value, s = D(a.id);
395
+ this.childrenByParent.delete(s);
396
+ let c = D(n), l = this.childrenByParent.get(c);
397
+ l && (l.folders.delete(t), l.files.size === 0 && l.folders.size === 0 && this.childrenByParent.delete(c)), this.entities.delete(t), o.push({ ...a });
398
+ }
399
+ return {
400
+ deletedFolderRecords: o,
401
+ deletedFileSystemItemRecords: a
402
+ };
403
+ }
404
+ moveEntityById(e, t) {
405
+ let n = this.entities.get(e) ?? null;
406
+ if (!n) return null;
407
+ let r = n.value, i = D(A(r)), a = this.childrenByParent.get(i);
408
+ a && (j(r) ? a.folders.delete(e) : a.files.delete(e), a.files.size === 0 && a.folders.size === 0 && this.childrenByParent.delete(i));
409
+ let o = {
410
+ ...r,
411
+ folderId: t
412
+ };
413
+ if (j(r) && j(o)) {
414
+ let e = r.childrenCount;
415
+ e !== void 0 && (o.childrenCount = e);
416
+ }
417
+ this.entities.set(e, {
418
+ value: o,
419
+ updatedAt: this.now()
420
+ });
421
+ let s = D(t), c = this.childrenByParent.get(s);
422
+ return c || (c = {
423
+ files: /* @__PURE__ */ new Set(),
424
+ folders: /* @__PURE__ */ new Set()
425
+ }, this.childrenByParent.set(s, c)), j(o) ? c.folders.add(e) : c.files.add(e), { ...o };
426
+ }
427
+ copyFileSystemItem(e, t, n) {
428
+ let r = this.entities.get(e) ?? null;
429
+ if (!r || j(r.value)) return null;
430
+ let i = {
431
+ ...r.value,
432
+ id: t,
433
+ folderId: n
434
+ };
435
+ return this.upsertEntity(i), { ...i };
436
+ }
437
+ copyFolderShallow(e, t, n) {
438
+ let r = this.entities.get(e) ?? null;
439
+ if (!r || !j(r.value)) return null;
440
+ let i = {
441
+ ...r.value,
442
+ id: t,
443
+ folderId: n
444
+ };
445
+ return this.upsertEntity(i), { ...i };
446
+ }
447
+ isEntityStale(e, t) {
448
+ let n = this.entities.get(e) ?? null;
449
+ return !n || t !== Infinity && this.now() - n.updatedAt > t;
450
+ }
451
+ touch(e, t) {
452
+ let n = this.entities.get(e);
453
+ n && (n.updatedAt = t ?? this.now());
454
+ }
455
+ getEntitiesByIds(e) {
456
+ return e.map((e) => this.entities.get(e)?.value ?? null);
457
+ }
458
+ searchByName(e, t) {
459
+ if (!e || e.trim() === "") return [];
460
+ let n = t?.parentId, r = t?.recursive ?? !1, i = t?.itemType ?? "all", a = t?.caseSensitive ?? !1, o = t?.limit ?? Infinity, s = a ? e.trim() : e.trim().toLowerCase(), c = [], l = null;
461
+ if (n !== void 0) {
462
+ l = /* @__PURE__ */ new Set();
463
+ let e = D(n);
464
+ if (l.add(e), r) {
465
+ let t = [e], n = /* @__PURE__ */ new Set();
466
+ for (; t.length > 0;) {
467
+ let e = t.shift();
468
+ if (n.has(e)) continue;
469
+ n.add(e);
470
+ let r = this.childrenByParent.get(e);
471
+ if (r) for (let e of r.folders) l.add(e), t.push(e);
472
+ }
473
+ }
474
+ }
475
+ for (let [, e] of this.entities) {
476
+ if (c.length >= o) break;
477
+ let t = e.value;
478
+ if (l !== null) {
479
+ let e = D(A(t));
480
+ if (!l.has(e)) continue;
481
+ }
482
+ if (i === "file" && j(t) || i === "folder" && !j(t)) continue;
483
+ let n = t.name ?? "";
484
+ (a ? n : n.toLowerCase()).includes(s) && c.push({ ...t });
485
+ }
486
+ return c;
487
+ }
488
+ toJSON() {
489
+ return {
490
+ entities: Array.from(this.entities.entries()),
491
+ childrenByParent: Array.from(this.childrenByParent.entries()).map(([e, t]) => [e, {
492
+ files: Array.from(t.files),
493
+ folders: Array.from(t.folders)
494
+ }])
495
+ };
496
+ }
497
+ static fromJSON(t) {
498
+ let n = new e();
499
+ for (let [e, r] of t.entities ?? []) n.entities.set(e, {
500
+ value: r.value,
501
+ updatedAt: r.updatedAt
502
+ });
503
+ for (let [e, r] of t.childrenByParent ?? []) n.childrenByParent.set(e, {
504
+ files: new Set(r.files),
505
+ folders: new Set(r.folders)
506
+ });
507
+ return n;
508
+ }
509
+ }, L = class e {
510
+ constructor(e = {}, t = E, n, r, i = new o(), a = N) {
511
+ this.views = /* @__PURE__ */ new Map(), this.maxViewsPerParent = 50;
512
+ let s = g(e);
513
+ this.strategy = {
514
+ allowSortedInsert: s.allowSortedInsert ?? !0,
515
+ allowSearchInsert: s.allowSearchInsert ?? !0,
516
+ invalidateOnPaginationFull: s.invalidateOnPaginationFull ?? !0,
517
+ preloadPagesBefore: s.preloadPagesBefore ?? 0,
518
+ preloadPagesAfter: s.preloadPagesAfter ?? 1,
519
+ comparator: s.comparator,
520
+ filterMatch: s.filterMatch,
521
+ hasSortValue: s.hasSortValue
522
+ }, this.ttl = t, this.errorTtl = n ?? t, this.entityTtl = r ?? t, this.failedRequests = i, this.isFolderNotFoundError = a;
523
+ }
524
+ setServerChildrenCountHandler(e) {
525
+ this.serverChildrenCountHandler = e;
526
+ }
527
+ setChildrenCountInvalidator(e) {
528
+ this.childrenCountInvalidator = e;
529
+ }
530
+ now() {
531
+ return Date.now();
532
+ }
533
+ parentKey(e) {
534
+ return D(e ?? null);
535
+ }
536
+ isExpired(e) {
537
+ return e == null || this.ttl !== Infinity && this.now() - e > this.ttl;
538
+ }
539
+ areEntitiesStale(e, t) {
540
+ return !e || e.some((e) => !t.getEntity(e) || t.isEntityStale(e, this.entityTtl));
541
+ }
542
+ isEntityTypeCompatible(e, t) {
543
+ return !!(!t || t === "all" || t === "file" && !j(e) || t === "folder" && j(e));
544
+ }
545
+ markViewStaleForReload(e) {
546
+ e.view.meta = {
547
+ ...e.view.meta ?? {},
548
+ stale: !0
549
+ }, e.view.updatedAt = this.now(), e.view.loadId = M(), e.view.abortController && (e.view.abortController.abort(), e.view.abortController = void 0);
550
+ }
551
+ cleanupOldViews(e) {
552
+ if (e.size <= this.maxViewsPerParent) return;
553
+ let t = Array.from(e.entries()).sort((e, t) => (t[1].view.updatedAt || 0) - (e[1].view.updatedAt || 0));
554
+ for (; e.size > this.maxViewsPerParent && t.length > 0;) {
555
+ let [n] = t.pop();
556
+ e.delete(n);
557
+ }
558
+ }
559
+ async getOrCreateChildren(e, t, n, r) {
560
+ let i = this.parentKey(e), a = F(t ?? {}), s = this.views.get(i);
561
+ s || (s = /* @__PURE__ */ new Map(), this.views.set(i, s));
562
+ let c = o.childrenViewKey(i, a), l = s.get(a);
563
+ l || (l = {
564
+ params: t,
565
+ view: {
566
+ promise: null,
567
+ value: void 0,
568
+ meta: void 0,
569
+ updatedAt: 0,
570
+ loadId: null
571
+ }
572
+ }, s.set(a, l), this.cleanupOldViews(s));
573
+ let u = this.failedRequests.getFreshError(c, this.errorTtl);
574
+ if (u != null) throw u;
575
+ if (l.view.value && !this.isExpired(l.view.updatedAt) && !this.areEntitiesStale(l.view.value, r) && !l.view.meta?.stale) {
576
+ let e = l.view.value.map((e) => r.getEntity(e)).filter(Boolean), t = l.params?.itemType || "all";
577
+ return {
578
+ items: e.filter((e) => this.isEntityTypeCompatible(e, t)),
579
+ ...l.view.meta ?? {}
580
+ };
581
+ }
582
+ if (l.view.promise) return l.view.promise;
583
+ let d = M();
584
+ l.view.loadId = d;
585
+ let f = (async () => {
586
+ try {
587
+ let a = await n(e, t), o = k(t), s = o ? {
588
+ ...a,
589
+ total: void 0
590
+ } : a, u = this.views.get(i)?.get(F(t));
591
+ if (!u || u.view.loadId !== d) {
592
+ let e = /* @__PURE__ */ Error("StaleLoad");
593
+ throw e.name = "StaleLoad", e;
594
+ }
595
+ for (let e of s.items) r.upsertEntity(e);
596
+ l.view.value = s.items.map((e) => e.id);
597
+ let { items: f, ...p } = s, m = { ...p };
598
+ l.view.meta = m, l.view.meta && (l.view.meta.stale = !1), this.failedRequests.delete(c), l.view.updatedAt = this.now(), !o && typeof m.total == "number" && (this.serverChildrenCountHandler ? this.serverChildrenCountHandler(e ?? null, m.total, r) : r.updateServerChildrenCount(e, m.total));
599
+ let h = this.strategy.preloadPagesAfter ?? 0, g = this.strategy.preloadPagesBefore ?? 0, _ = O(t?.pageSize);
600
+ if ((h > 0 || g > 0) && Number.isFinite(_)) {
601
+ let i = e, a = { ...t }, o = typeof m.totalPages == "number" ? m.totalPages : 1, s = t?.page ?? 1, c = _, l = n, u = d;
602
+ setTimeout(() => {
603
+ this.preloadPagesAsync(i, a, s, o, c, h, g, r, m, l, u);
604
+ }, 0);
605
+ }
606
+ return s;
607
+ } catch (e) {
608
+ if (e?.name === "StaleLoad") throw this.failedRequests.delete(c), l.view.value = void 0, l.view.updatedAt = this.now(), e;
609
+ if (this.isFolderNotFoundError(e)) return this.failedRequests.delete(c), l.view.value = [], l.view.meta = {
610
+ total: 0,
611
+ totalPages: 0
612
+ }, l.view.updatedAt = this.now(), {
613
+ items: [],
614
+ total: 0,
615
+ totalPages: 0
616
+ };
617
+ throw this.failedRequests.setError(c, e), l.view.value = void 0, l.view.updatedAt = this.now(), e;
618
+ } finally {
619
+ l.view.promise = null;
620
+ }
621
+ })();
622
+ return l.view.promise = f, f;
623
+ }
624
+ async preloadPagesAsync(e, t, n, r, i, a, o, s, c, l, u) {
625
+ try {
626
+ let i = this.parentKey(e), c = this.views.get(i);
627
+ if (!c) return;
628
+ let d = c.get(F(t));
629
+ if (!d || u != null && d.view.loadId !== u) return;
630
+ let f = c.get(F(t));
631
+ if (!f?.view.meta) return;
632
+ let p = typeof f.view.meta.totalPages == "number" ? f.view.meta.totalPages : 1;
633
+ if (p !== r) return;
634
+ let m = async (n) => {
635
+ let r = {
636
+ ...t,
637
+ page: n
638
+ };
639
+ try {
640
+ let a = await l(e, r), o = this.views.get(i);
641
+ if (!o) return;
642
+ let c = o.get(F(t));
643
+ if (!c || u != null && c.view.loadId !== u || c.view.loadId !== u) return;
644
+ for (let e of a.items) s.upsertEntity(e);
645
+ let d = F(r), f = o.get(d);
646
+ if (f || (f = {
647
+ params: r,
648
+ view: {
649
+ promise: null,
650
+ value: void 0,
651
+ meta: void 0,
652
+ updatedAt: 0,
653
+ loadId: c.view.loadId
654
+ }
655
+ }, o.set(d, f), this.cleanupOldViews(o)), !f.view.value || this.isExpired(f.view.updatedAt)) {
656
+ f.view.value = a.items.map((e) => e.id);
657
+ let { items: e, ...t } = a;
658
+ f.view.meta = {
659
+ ...t,
660
+ page: n,
661
+ totalPages: p
662
+ }, f.view.updatedAt = this.now();
663
+ }
664
+ } catch (e) {
665
+ console.debug("Preload failed silently:", e);
666
+ }
667
+ };
668
+ if (a > 0 && n < p) for (let e = 1; e <= Math.min(a, p - n); e++) {
669
+ let r = n + e, i = F({
670
+ ...t,
671
+ page: r
672
+ }), a = c.get(i);
673
+ (!a?.view.value || this.isExpired(a.view.updatedAt)) && m(r);
674
+ }
675
+ if (o > 0 && n > 1) for (let e = 1; e <= Math.min(o, n - 1); e++) {
676
+ let r = n - e, i = F({
677
+ ...t,
678
+ page: r
679
+ }), a = c.get(i);
680
+ (!a?.view.value || this.isExpired(a.view.updatedAt)) && m(r);
681
+ }
682
+ } catch (e) {
683
+ console.debug("Preload process failed:", e);
684
+ }
685
+ }
686
+ removeEmptyPageIfNeeded(e, t, n, r, i) {
687
+ if (t.view.value && t.view.value.length === 0 && n < r) {
688
+ let t = F({
689
+ ...i,
690
+ page: n
691
+ });
692
+ e.delete(t);
693
+ }
694
+ }
695
+ updateTotalPagesForAllViews(e, t) {
696
+ for (let [, n] of e) n.view.meta && (n.view.meta.totalPages = t);
697
+ }
698
+ isParamsSame(e, t) {
699
+ let n = Object.keys(e ?? {}).filter((e) => e !== "page").sort(), r = Object.keys(t ?? {}).filter((e) => e !== "page").sort();
700
+ if (n.length !== r.length) return !1;
701
+ for (let i = 0; i < n.length; i++) {
702
+ let a = n[i];
703
+ if (a !== r[i] || e[a] !== t[a]) return !1;
704
+ }
705
+ return !0;
706
+ }
707
+ syncTotalsInSameViews(e, t) {
708
+ let n = t.view.meta;
709
+ if (!n || typeof n.total != "number") return;
710
+ let r = Number(n.total), i = O(t.params?.pageSize), a = Number.isFinite(i) && i > 0 ? Math.max(1, Math.ceil(r / i)) : void 0;
711
+ for (let [, n] of e) this.isParamsSame(n.params ?? {}, t.params ?? {}) && (n.view.meta || (n.view.meta = {}), n.view.meta.total = r, a !== void 0 && (n.view.meta.totalPages = a));
712
+ }
713
+ cascadeShiftPages(e, t, n, r, i, a, o) {
714
+ let s = t, c = n, l = !1, u = 0;
715
+ for (; s.view.value && s.view.value.length < r && c < i && u < 100;) {
716
+ u++;
717
+ let t = F({
718
+ ...o,
719
+ page: c + 1
720
+ }), n = e.get(t);
721
+ if (!n?.view.value || this.isExpired(n.view.updatedAt)) {
722
+ s.view.meta = {
723
+ ...s.view.meta ?? {},
724
+ stale: !0
725
+ };
726
+ break;
727
+ }
728
+ let i = r - s.view.value.length, d = n.view.value.slice(0, i).filter((e) => a.getEntity(e));
729
+ if (d.length === 0) {
730
+ s.view.meta = {
731
+ ...s.view.meta ?? {},
732
+ stale: !0
733
+ };
734
+ break;
735
+ }
736
+ if (s.view.value = [...s.view.value, ...d], s.view.updatedAt = Date.now(), n.view.value = n.view.value.slice(d.length), n.view.updatedAt = Date.now(), n.view.value.length === 0) {
737
+ let t = c + 1, r = n.view.meta;
738
+ if (t < (r && typeof r.totalPages == "number" ? r.totalPages : 1)) {
739
+ let n = F({
740
+ ...o,
741
+ page: t
742
+ });
743
+ e.delete(n), l = !0;
744
+ }
745
+ break;
746
+ }
747
+ s = n, c++;
748
+ }
749
+ return (!s.view.value || s.view.value.length < r) && c >= i && (s.view.meta = {
750
+ ...s.view.meta ?? {},
751
+ stale: !0
752
+ }), l;
753
+ }
754
+ cascadeShiftToNextPage(e, t, n, r, i, a, o) {
755
+ if (!t.view.value || t.view.value.length <= r) return !1;
756
+ if (n - (o.page || 1) >= 10) return t.view.meta = {
757
+ ...t.view.meta ?? {},
758
+ stale: !0
759
+ }, !1;
760
+ let s = {
761
+ ...o,
762
+ page: n + 1
763
+ }, c = F(s), l = e.get(c), u = !1;
764
+ if (!l?.view.value || this.isExpired(l.view.updatedAt)) if (n >= i) {
765
+ l = {
766
+ params: s,
767
+ view: {
768
+ promise: null,
769
+ value: void 0,
770
+ meta: void 0,
771
+ updatedAt: 0,
772
+ loadId: null
773
+ }
774
+ }, e.set(c, l), this.cleanupOldViews(e);
775
+ let r = t.view.meta;
776
+ r && (l.view.meta = {
777
+ ...r,
778
+ page: n + 1
779
+ }), u = !0;
780
+ } else return t.view.meta = {
781
+ ...t.view.meta ?? {},
782
+ stale: !0
783
+ }, !1;
784
+ let d = t.view.value.slice(r).filter((e) => a.getEntity(e));
785
+ if (d.length === 0) return u && e.delete(c), !1;
786
+ t.view.value = t.view.value.slice(0, r), t.view.updatedAt = Date.now(), l.view.value = [...d, ...l.view.value ?? []], l.view.updatedAt = Date.now();
787
+ let f = l.view.meta && typeof l.view.meta.totalPages == "number" ? l.view.meta.totalPages : i;
788
+ if (!u && this.cascadeShiftToNextPage(e, l, n + 1, r, f, a, o)) return !0;
789
+ if (!l.view.value || l.view.value.length === 0) {
790
+ let t = n + 1, r = l.view.meta, i = r && typeof r.totalPages == "number" ? r.totalPages : 1;
791
+ this.removeEmptyPageIfNeeded(e, l, t, i, o);
792
+ }
793
+ return u;
794
+ }
795
+ canCompareInSortedView(e, t) {
796
+ let n = e.params ?? {};
797
+ return !n.sortBy && !n.sortOrder || !this.strategy.hasSortValue || this.strategy.hasSortValue(t, e.params);
798
+ }
799
+ tryInsertSorted(e, t, n) {
800
+ let r = this.strategy.comparator;
801
+ if (!r || !e.view.value) return !1;
802
+ let i = e.params?.itemType || "all";
803
+ if (!this.isEntityTypeCompatible(t, i) || !this.canCompareInSortedView(e, t)) return !1;
804
+ let a = e.view.value.map((e) => n.getEntity(e));
805
+ if (a.some((e) => !e)) return !1;
806
+ if (a.length === 0) {
807
+ let n = e.params ?? {};
808
+ if (n.sortBy || n.sortOrder) {
809
+ let n = r(t, t, e.params);
810
+ if (typeof n != "number" || Number.isNaN(n)) return !1;
811
+ }
812
+ return e.view.value.includes(t.id) ? (e.view.updatedAt = this.now(), !0) : (e.view.value = [t.id], e.view.updatedAt = this.now(), !0);
813
+ }
814
+ let o = 0, s = a.length;
815
+ for (; o < s;) {
816
+ let n = Math.floor((o + s) / 2), i = r(t, a[n], e.params);
817
+ if (typeof i != "number" || Number.isNaN(i)) return !1;
818
+ i >= 0 ? o = n + 1 : s = n;
819
+ }
820
+ return e.view.value.includes(t.id) ? (e.view.updatedAt = this.now(), !0) : (e.view.value = [
821
+ ...e.view.value.slice(0, o),
822
+ t.id,
823
+ ...e.view.value.slice(o)
824
+ ], e.view.updatedAt = this.now(), !0);
825
+ }
826
+ updateChildrenCountForOperation(e, t, n, r) {
827
+ let i = A(t);
828
+ switch (e) {
829
+ case "delete":
830
+ this.adjustChildrenCount(r, i, -1), this.childrenCountInvalidator?.(i);
831
+ break;
832
+ case "insert":
833
+ case "copy":
834
+ this.adjustChildrenCount(r, i, 1), this.childrenCountInvalidator?.(i);
835
+ break;
836
+ case "move":
837
+ this.adjustChildrenCount(r, n ?? null, -1), this.adjustChildrenCount(r, i, 1), this.childrenCountInvalidator?.(n ?? null), this.childrenCountInvalidator?.(i);
838
+ break;
839
+ }
840
+ }
841
+ adjustChildrenCount(e, t, n) {
842
+ if (!t) return;
843
+ let r = e.getFolder(t);
844
+ if (r && typeof r.childrenCount == "number") {
845
+ let i = Math.max(0, r.childrenCount + n);
846
+ e.updateServerChildrenCount(t, i);
847
+ }
848
+ }
849
+ smartInvalidateChildrenViews(e, t, n, r, i, a) {
850
+ if (!t) return;
851
+ this.updateChildrenCountForOperation(n, t, i, r);
852
+ let o = this.parentKey(e), s = this.views.get(o);
853
+ if (s) for (let [, e] of s) {
854
+ let a = e.params ?? {}, o = a?.page ?? 1, c = O(a?.pageSize), l = Number.isFinite(c), u = !!(a?.sortBy || a?.sortOrder), d = !!a?.search, f = a?.itemType || "all";
855
+ if (this.isEntityTypeCompatible(t, f)) {
856
+ if (d && this.strategy.filterMatch && !this.strategy.filterMatch(t, e.params)) {
857
+ e.view.value?.includes(t.id) && (e.view.value = e.view.value.filter((e) => e !== t.id), e.view.meta && typeof e.view.meta.total == "number" && (e.view.meta.total = Math.max(0, e.view.meta.total - 1)), this.markViewStaleForReload(e));
858
+ continue;
859
+ }
860
+ if (!e.view.value) {
861
+ e.view.meta && (e.view.meta.stale = !0), e.view.loadId = M(), e.view.abortController && (e.view.abortController.abort(), e.view.abortController = void 0);
862
+ continue;
863
+ }
864
+ if (e.view.value.includes(t.id), this.shouldInvalidateCompletely(e, n, t, u, l, o)) {
865
+ this.markViewStaleForReload(e);
866
+ continue;
867
+ }
868
+ switch (n) {
869
+ case "delete":
870
+ this.handleDelete(s, e, o, c, l, t, r);
871
+ break;
872
+ case "insert":
873
+ case "copy":
874
+ this.handleInsert(s, e, o, c, l, u, t, r);
875
+ break;
876
+ case "update":
877
+ this.handleUpdate(s, e, o, c, l, u, d, t, r);
878
+ break;
879
+ case "move":
880
+ this.handleMove(s, e, o, c, l, u, t, i, r);
881
+ break;
882
+ }
883
+ }
884
+ }
885
+ }
886
+ shouldInvalidateCompletely(e, t, n, r, i, a) {
887
+ return !!(i && a > 1 && (t === "insert" || t === "delete" || t === "move") || this.isExpired(e.view.updatedAt));
888
+ }
889
+ tryReinsertEntity(e, t, n, r, i) {
890
+ let a = e.params?.itemType || "all";
891
+ if (!this.isEntityTypeCompatible(t, a)) return !1;
892
+ let o = e.params ?? {};
893
+ return this.strategy.filterMatch(t, o) ? this.tryInsertSorted(e, t, n) ? !0 : o.sortBy || o.sortOrder ? !1 : !r || e.view.value.length < i ? (e.view.value.push(t.id), !0) : !1 : !1;
894
+ }
895
+ handleDelete(e, t, n, r, i, a, o) {
896
+ let s = t.view.value.includes(a.id);
897
+ if (s && (t.view.value = t.view.value.filter((e) => e !== a.id)), t.view.meta && typeof t.view.meta.total == "number") {
898
+ if (t.view.meta.total = Math.max(0, t.view.meta.total - 1), i && Number.isFinite(r) && typeof t.view.meta.total == "number") {
899
+ let e = r;
900
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
901
+ }
902
+ this.syncTotalsInSameViews(e, t);
903
+ }
904
+ if (t.view.updatedAt = Date.now(), s && i && t.view.value.length < r && o) {
905
+ let i = t.view.meta, a = i && typeof i.totalPages == "number" ? i.totalPages : 1;
906
+ if (this.cascadeShiftPages(e, t, n, r, a, o, t.params ?? {}) && t.view.meta && typeof t.view.meta.totalPages == "number") {
907
+ let n = Math.max(1, t.view.meta.totalPages - 1);
908
+ t.view.meta.totalPages = n, this.updateTotalPagesForAllViews(e, n);
909
+ }
910
+ }
911
+ t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
912
+ }
913
+ handleInsert(e, t, n, r, i, a, o, s) {
914
+ if (!t.view.value.includes(o.id)) {
915
+ if (a && this.strategy.allowSortedInsert && this.strategy.comparator && s) if (this.tryInsertSorted(t, o, s)) {
916
+ if (t.view.meta && typeof t.view.meta.total == "number") {
917
+ if (t.view.meta.total += 1, i && Number.isFinite(r)) {
918
+ let e = r;
919
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
920
+ }
921
+ this.syncTotalsInSameViews(e, t);
922
+ }
923
+ if (t.view.updatedAt = Date.now(), i && (t.view.value?.length ?? 0) > r && s) {
924
+ let i = t.view.meta, a = i && typeof i.totalPages == "number" ? i.totalPages : 1;
925
+ if (this.cascadeShiftToNextPage(e, t, n, r, a, s, t.params ?? {}) && t.view.meta && typeof t.view.meta.totalPages == "number") {
926
+ let n = t.view.meta.totalPages + 1;
927
+ t.view.meta.totalPages = n, this.updateTotalPagesForAllViews(e, n);
928
+ }
929
+ }
930
+ } else this.markViewStaleForReload(t);
931
+ else if (!i || t.view.value.length < r) {
932
+ if (t.view.value.push(o.id), t.view.meta && typeof t.view.meta.total == "number") {
933
+ if (t.view.meta.total += 1, i && Number.isFinite(r)) {
934
+ let e = r;
935
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
936
+ }
937
+ this.syncTotalsInSameViews(e, t);
938
+ }
939
+ t.view.updatedAt = Date.now();
940
+ } else this.markViewStaleForReload(t);
941
+ t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
942
+ }
943
+ }
944
+ handleUpdate(e, t, n, r, i, a, o, s, c) {
945
+ let l = t.view.value.includes(s.id), u = this.isEntityTypeCompatible(s, t.params?.itemType || "all");
946
+ if (l && !u) {
947
+ if (t.view.value = t.view.value.filter((e) => e !== s.id), t.view.meta && typeof t.view.meta.total == "number" && (t.view.meta.total = Math.max(0, t.view.meta.total - 1), i && Number.isFinite(r) && typeof t.view.meta.total == "number")) {
948
+ let e = r;
949
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
950
+ }
951
+ if (t.view.updatedAt = Date.now(), i && t.view.value.length < r && c) {
952
+ let i = t.view.meta, a = i && typeof i.totalPages == "number" ? i.totalPages : 1;
953
+ if (this.cascadeShiftPages(e, t, n, r, a, c, t.params ?? {}) && t.view.meta && typeof t.view.meta.totalPages == "number") {
954
+ let n = Math.max(1, t.view.meta.totalPages - 1);
955
+ t.view.meta.totalPages = n, this.updateTotalPagesForAllViews(e, n);
956
+ }
957
+ }
958
+ t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
959
+ return;
960
+ }
961
+ if (l && (a || o)) {
962
+ t.view.value = t.view.value.filter((e) => e !== s.id), this.tryReinsertEntity(t, s, c, i, r) ? (t.view.updatedAt = Date.now(), t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0)) : this.markViewStaleForReload(t);
963
+ return;
964
+ }
965
+ if (l) {
966
+ t.view.updatedAt = Date.now(), t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
967
+ return;
968
+ }
969
+ if (u && !o) {
970
+ if (a && this.strategy.allowSortedInsert && this.strategy.comparator && c) if (this.tryInsertSorted(t, s, c)) {
971
+ if (t.view.meta && typeof t.view.meta.total == "number") {
972
+ if (t.view.meta.total += 1, i && Number.isFinite(r)) {
973
+ let e = r;
974
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
975
+ }
976
+ this.syncTotalsInSameViews(e, t);
977
+ }
978
+ t.view.updatedAt = Date.now();
979
+ } else this.markViewStaleForReload(t);
980
+ else if (!i || t.view.value.length < r) {
981
+ if (t.view.value.push(s.id), t.view.meta && typeof t.view.meta.total == "number") {
982
+ if (t.view.meta.total += 1, i && Number.isFinite(r)) {
983
+ let e = r;
984
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
985
+ }
986
+ this.syncTotalsInSameViews(e, t);
987
+ }
988
+ t.view.updatedAt = Date.now();
989
+ } else this.markViewStaleForReload(t);
990
+ t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
991
+ }
992
+ }
993
+ handleMove(e, t, n, r, i, a, o, s, c) {
994
+ if (t.view.value.includes(o.id)) {
995
+ if (t.view.value = t.view.value.filter((e) => e !== o.id), t.view.meta && typeof t.view.meta.total == "number" && (t.view.meta.total = Math.max(0, t.view.meta.total - 1), i && Number.isFinite(r) && typeof t.view.meta.total == "number")) {
996
+ let e = r;
997
+ e > 0 && (t.view.meta.totalPages = Math.max(1, Math.ceil(t.view.meta.total / e)));
998
+ }
999
+ if (t.view.updatedAt = Date.now(), i && t.view.value.length < r && c) {
1000
+ let i = t.view.meta, a = i && typeof i.totalPages == "number" ? i.totalPages : 1;
1001
+ if (this.cascadeShiftPages(e, t, n, r, a, c, t.params ?? {}) && t.view.meta && typeof t.view.meta.totalPages == "number") {
1002
+ let n = Math.max(1, t.view.meta.totalPages - 1);
1003
+ t.view.meta.totalPages = n, this.updateTotalPagesForAllViews(e, n);
1004
+ }
1005
+ }
1006
+ this.syncTotalsInSameViews(e, t);
1007
+ }
1008
+ t.view.loadId = M(), t.view.abortController && (t.view.abortController.abort(), t.view.abortController = void 0);
1009
+ }
1010
+ forceInvalidateChildrenViews(e) {
1011
+ let t = this.parentKey(e ?? null);
1012
+ this.failedRequests.clearChildrenViewErrorsForParent(t);
1013
+ let n = this.views.get(t);
1014
+ if (n) for (let [, e] of n) e.view.value = void 0, e.view.meta = void 0, e.view.promise = null, e.view.updatedAt = 0, e.view.loadId = M(), e.view.abortController && (e.view.abortController.abort(), e.view.abortController = void 0);
1015
+ }
1016
+ getKnownTotal(e) {
1017
+ let t = this.parentKey(e ?? null), n = this.views.get(t);
1018
+ if (!n) return;
1019
+ let r = null;
1020
+ for (let [, e] of n) {
1021
+ let t = e.view.meta;
1022
+ t && typeof t.total == "number" && (!r || (e.view.updatedAt ?? 0) > r.updatedAt) && (r = {
1023
+ total: Number(t.total),
1024
+ updatedAt: e.view.updatedAt ?? 0
1025
+ });
1026
+ }
1027
+ return r ? r.total : void 0;
1028
+ }
1029
+ toJSON() {
1030
+ return {
1031
+ views: Array.from(this.views.entries()).map(([e, t]) => [e, Array.from(t.entries())]),
1032
+ ttl: this.ttl,
1033
+ errorTtl: this.errorTtl,
1034
+ entityTtl: this.entityTtl,
1035
+ strategy: {
1036
+ allowSortedInsert: this.strategy.allowSortedInsert,
1037
+ allowSearchInsert: this.strategy.allowSearchInsert,
1038
+ invalidateOnPaginationFull: this.strategy.invalidateOnPaginationFull,
1039
+ preloadPagesBefore: this.strategy.preloadPagesBefore,
1040
+ preloadPagesAfter: this.strategy.preloadPagesAfter
1041
+ }
1042
+ };
1043
+ }
1044
+ static fromJSON(t, n) {
1045
+ let r = n?.ttl ?? t?.ttl ?? 18e5, i = n?.errorTtl ?? t?.errorTtl ?? r, a = n?.failedRequests ?? new o(), s = new e({}, r, i, void 0, a, n?.isFolderNotFoundError);
1046
+ for (let [e, n] of t.views ?? []) {
1047
+ let t = /* @__PURE__ */ new Map();
1048
+ for (let [e, r] of n) {
1049
+ let n = r;
1050
+ n?.view && "error" in n.view && delete n.view.error, t.set(e, n);
1051
+ }
1052
+ s.views.set(e, t);
1053
+ }
1054
+ return s;
1055
+ }
1056
+ }, R = class e {
1057
+ invalidateChildrenCountCache(e) {
1058
+ let t = D(e ?? "");
1059
+ this.childrenCountPromises.delete(t), this.failedRequests.delete(o.childrenCountKey(t));
1060
+ }
1061
+ constructor(e = {}, t = E, n, r) {
1062
+ this.childrenCountPromises = /* @__PURE__ */ new Map(), this.isFolderNotFoundError = r?.isFolderNotFoundError ?? N, this.failedRequests = new o(), this.ttl = r?.ttl ?? t, this.errorTtl = r?.errorTtl ?? n ?? this.ttl, this.entities = new I(), this.views = new L(e, this.ttl, this.errorTtl, void 0, this.failedRequests, this.isFolderNotFoundError), this.views.setServerChildrenCountHandler((e, t, n) => {
1063
+ this.handleServerReportedChildrenCount(e, t, n ?? this.entities);
1064
+ }), this.views.setChildrenCountInvalidator((e) => {
1065
+ this.invalidateChildrenCountCache(e);
1066
+ });
1067
+ }
1068
+ getEntity(e) {
1069
+ return this.entities.getEntity(e) ?? null;
1070
+ }
1071
+ getFileSystemItem(e) {
1072
+ return this.entities.getFileSystemItem(e) ?? null;
1073
+ }
1074
+ getFolder(e) {
1075
+ return this.entities.getFolder(e) ?? null;
1076
+ }
1077
+ async getOrCreateChildren(e, t, n) {
1078
+ return this.views.getOrCreateChildren(e, t, n, this.entities);
1079
+ }
1080
+ upsertEntity(e) {
1081
+ let t = this.entities.getEntity(e.id), n = t ? A(t) : null, r = A(e), i = t ? j(t) : !1, a = j(e);
1082
+ if (this.entities.upsertEntity(e), t) {
1083
+ if (n !== r || i !== a) {
1084
+ this.views.smartInvalidateChildrenViews(n, t, "delete", this.entities), this.views.smartInvalidateChildrenViews(r, e, "insert", this.entities);
1085
+ return;
1086
+ }
1087
+ this.views.smartInvalidateChildrenViews(r, e, "update", this.entities, void 0, t);
1088
+ } else this.views.smartInvalidateChildrenViews(r, e, "insert", this.entities);
1089
+ }
1090
+ isAncestor(e, t) {
1091
+ let n = t, r = /* @__PURE__ */ new Set();
1092
+ for (; n && !r.has(n);) {
1093
+ if (r.add(n), D(n) === D(e)) return !0;
1094
+ let t = this.entities.getEntity(n);
1095
+ if (!t) break;
1096
+ n = A(t), n === null && (n = T);
1097
+ }
1098
+ return !1;
1099
+ }
1100
+ moveEntity(e, t) {
1101
+ let n = this.entities.getEntity(e);
1102
+ if (!n || j(n) && t !== null && this.isAncestor(e, t)) return null;
1103
+ let r = A(n), i = this.entities.moveEntityById(e, t);
1104
+ return i ? (this.views.smartInvalidateChildrenViews(r, n, "delete", this.entities), this.views.smartInvalidateChildrenViews(t, i, "insert", this.entities), i) : null;
1105
+ }
1106
+ copyEntity(e, t, n, r) {
1107
+ let i = this.entities.getEntity(e);
1108
+ if (!i) return null;
1109
+ let a = null;
1110
+ if (j(i)) {
1111
+ let n = [e], i = [];
1112
+ for (; n.length;) {
1113
+ let e = n.shift();
1114
+ i.push(e);
1115
+ let t = this.entities.getChildrenIds(e, { onlyFolders: !0 });
1116
+ for (let e of t) n.push(e);
1117
+ }
1118
+ let o = /* @__PURE__ */ new Map();
1119
+ for (let e of i) o.set(e, M());
1120
+ for (let n of i) {
1121
+ let i = this.entities.getFolder(n), a = o.get(n), s = A(i), c = s === null ? t : o.get(s) ?? null, l = {
1122
+ ...i,
1123
+ ...n === e ? r : void 0,
1124
+ id: a,
1125
+ folderId: (n === e ? r?.folderId : void 0) ?? c
1126
+ };
1127
+ this.entities.upsertEntity(l);
1128
+ }
1129
+ for (let e of i) {
1130
+ let t = o.get(e), n = this.entities.getChildrenIds(e, { onlyFileSystemItems: !0 });
1131
+ for (let e of n) {
1132
+ let n = this.entities.getFileSystemItem(e);
1133
+ if (!n) continue;
1134
+ let r = M(), i = {
1135
+ ...n,
1136
+ id: r,
1137
+ folderId: t
1138
+ };
1139
+ this.entities.upsertEntity(i);
1140
+ }
1141
+ }
1142
+ a = this.entities.getEntity(o.get(e)) ?? null;
1143
+ } else {
1144
+ let i = n ?? M();
1145
+ if (a = this.entities.copyFileSystemItem(e, i, t ?? null), a && r) {
1146
+ let e = {
1147
+ ...a,
1148
+ ...r,
1149
+ id: i,
1150
+ folderId: r.folderId ?? t ?? null
1151
+ };
1152
+ this.entities.upsertEntity(e), a = e;
1153
+ }
1154
+ }
1155
+ return a && this.views.smartInvalidateChildrenViews(t, a, "copy", this.entities), a;
1156
+ }
1157
+ searchByUrl(e) {
1158
+ return this.entities.searchByUrl(e) ?? null;
1159
+ }
1160
+ deleteEntity(e) {
1161
+ let t = this.entities.getEntity(e);
1162
+ if (!t) return null;
1163
+ if (j(t)) {
1164
+ let { deletedFolderRecords: n, deletedFileSystemItemRecords: r } = this.entities.deleteFolderRecursive(e);
1165
+ for (let e of r) this.views.smartInvalidateChildrenViews(e.folderId, e, "delete", this.entities);
1166
+ for (let e of n) this.views.smartInvalidateChildrenViews(e.folderId, e, "delete", this.entities);
1167
+ for (let e of n) this.views.forceInvalidateChildrenViews(e.id);
1168
+ return t;
1169
+ } else {
1170
+ let { entity: t, parentId: n } = this.entities.deleteEntityById(e);
1171
+ return t && this.views.smartInvalidateChildrenViews(n, t, "delete", this.entities), t;
1172
+ }
1173
+ }
1174
+ deleteFolderRecursive(e) {
1175
+ let { deletedFolderRecords: t, deletedFileSystemItemRecords: n } = this.entities.deleteFolderRecursive(e);
1176
+ for (let e of n) this.views.smartInvalidateChildrenViews(e.folderId, e, "delete", this.entities);
1177
+ for (let e of t) this.views.smartInvalidateChildrenViews(e.folderId, e, "delete", this.entities);
1178
+ for (let e of t) this.views.forceInvalidateChildrenViews(e.id);
1179
+ return {
1180
+ deletedFileSystemItemIds: n.map((e) => e.id),
1181
+ deletedFolderIds: t.map((e) => e.id)
1182
+ };
1183
+ }
1184
+ forceInvalidate(e) {
1185
+ let t = this.entities.getEntity(e);
1186
+ if (!t) {
1187
+ this.views.forceInvalidateChildrenViews(e ?? null), this.invalidateChildrenCountCache(e);
1188
+ return;
1189
+ }
1190
+ let n = /* @__PURE__ */ new Set();
1191
+ if (j(t)) {
1192
+ let t = [e];
1193
+ for (; t.length;) {
1194
+ let e = t.pop();
1195
+ if (n.has(e)) continue;
1196
+ n.add(e), this.views.forceInvalidateChildrenViews(e), this.invalidateChildrenCountCache(e);
1197
+ let r = this.entities.getChildrenIds(e, { onlyFolders: !0 });
1198
+ for (let e of r) t.push(e);
1199
+ }
1200
+ } else {
1201
+ let e = t.folderId ?? null;
1202
+ this.views.forceInvalidateChildrenViews(e), this.invalidateChildrenCountCache(e);
1203
+ }
1204
+ }
1205
+ invalidateListingViews(e) {
1206
+ this.views.forceInvalidateChildrenViews(e ?? null);
1207
+ }
1208
+ getChildrenIds(e) {
1209
+ return this.entities.getChildrenIds(e);
1210
+ }
1211
+ searchByName(e, t) {
1212
+ return this.entities.searchByName(e, t);
1213
+ }
1214
+ async getChildrenCount(e = "", t) {
1215
+ let n = D(e), r = o.childrenCountKey(n), i = this.failedRequests.getFreshError(r, this.errorTtl);
1216
+ if (i != null) return Promise.reject(i);
1217
+ let a = this.childrenCountPromises.get(n);
1218
+ if (a) return a;
1219
+ let s = (async () => {
1220
+ try {
1221
+ let n = this.entities.getFolder(e);
1222
+ if (n && typeof n.childrenCount == "number") return this.failedRequests.delete(r), n.childrenCount;
1223
+ let i = this.views.getKnownTotal(e);
1224
+ if (typeof i == "number") return n && this.handleServerReportedChildrenCount(e, i, this.entities), this.failedRequests.delete(r), i;
1225
+ if (t) {
1226
+ let n = await t(e);
1227
+ return this.entities.getFolder(e) && this.handleServerReportedChildrenCount(e, n, this.entities), this.failedRequests.delete(r), n;
1228
+ }
1229
+ return this.failedRequests.delete(r), this.entities.getChildrenIds(e).length;
1230
+ } catch (e) {
1231
+ if (this.childrenCountPromises.delete(n), this.isFolderNotFoundError(e)) return this.failedRequests.delete(r), 0;
1232
+ throw this.failedRequests.setError(r, e), e;
1233
+ } finally {
1234
+ this.childrenCountPromises.delete(n);
1235
+ }
1236
+ })();
1237
+ return this.childrenCountPromises.set(n, s), s;
1238
+ }
1239
+ toJSON() {
1240
+ return {
1241
+ entities: this.entities.toJSON(),
1242
+ views: this.views.toJSON(),
1243
+ ttl: this.ttl,
1244
+ errorTtl: this.errorTtl
1245
+ };
1246
+ }
1247
+ static fromJSON(t, n, r) {
1248
+ let i = r?.ttl ?? t?.ttl ?? 18e5, a = r?.errorTtl ?? t?.errorTtl ?? i, o = new e(n ?? {}, i, a, r), s = I.fromJSON(t?.entities ?? {}), c = L.fromJSON(t?.views ?? {}, {
1249
+ failedRequests: o.failedRequests,
1250
+ ttl: i,
1251
+ errorTtl: a,
1252
+ isFolderNotFoundError: r?.isFolderNotFoundError
1253
+ });
1254
+ if (n) {
1255
+ let e = g(n);
1256
+ c.strategy = {
1257
+ allowSortedInsert: e.allowSortedInsert ?? !0,
1258
+ allowSearchInsert: e.allowSearchInsert ?? !0,
1259
+ invalidateOnPaginationFull: e.invalidateOnPaginationFull ?? !0,
1260
+ preloadPagesBefore: e.preloadPagesBefore ?? 0,
1261
+ preloadPagesAfter: e.preloadPagesAfter ?? 1,
1262
+ comparator: e.comparator,
1263
+ filterMatch: e.filterMatch
1264
+ };
1265
+ }
1266
+ return o.entities = s, o.views = c, c.setServerChildrenCountHandler((e, t, n) => {
1267
+ o.handleServerReportedChildrenCount(e, t, n ?? s);
1268
+ }), c.setChildrenCountInvalidator((e) => {
1269
+ o.invalidateChildrenCountCache(e);
1270
+ }), o;
1271
+ }
1272
+ handleServerReportedChildrenCount(e, t, n) {
1273
+ this.checkAndResolveChildrenCountConflict(e, t, n), this.invalidateChildrenCountCache(e);
1274
+ }
1275
+ checkAndResolveChildrenCountConflict(e, t, n) {
1276
+ if (!n || t === void 0) return;
1277
+ let r = e ?? null;
1278
+ if (!r) {
1279
+ n.updateServerChildrenCount(r, t);
1280
+ return;
1281
+ }
1282
+ let i = n.getFolder(r)?.childrenCount;
1283
+ i !== void 0 && i !== t && (console.warn(`Conflict: folder "${r}" has childrenCount=${i} but server says total=${t}`), this.views.forceInvalidateChildrenViews(r)), n.updateServerChildrenCount(r, t);
1284
+ }
1285
+ }, ee = "", te = "Trash", ne = "Edited", z = 1e4, B = 1e4, V = 25, H = 10;
1286
+ function U(e, t) {
1287
+ let n = e ?? {}, r = Array.isArray(n.detailed) ? n.detailed.map((e) => ({
1288
+ requestId: e.requestId ?? e.id,
1289
+ success: !!e.success,
1290
+ data: t(e.data),
1291
+ error: e.error
1292
+ })) : void 0;
1293
+ return {
1294
+ success: !!n.success,
1295
+ error: n.error,
1296
+ detailed: r
1297
+ };
1298
+ }
1299
+ function W(e) {
1300
+ return e == null ? "" : String(e);
1301
+ }
1302
+ function G(e) {
1303
+ return {
1304
+ ...e,
1305
+ name: W(e.name)
1306
+ };
1307
+ }
1308
+ function K(e) {
1309
+ return (e ?? []).map(G);
1310
+ }
1311
+ function q(e) {
1312
+ return !e || typeof e != "object" || !("name" in e) ? e : {
1313
+ ...e,
1314
+ name: W(e.name)
1315
+ };
1316
+ }
1317
+ function J(e) {
1318
+ return e ? e.replaceAll("\\", "/").split("/").filter(Boolean) : [];
1319
+ }
1320
+ for (var re = class {
1321
+ _getTrashPath() {
1322
+ return this._pathOptions.trashPath || te;
1323
+ }
1324
+ _getPathLookupCandidates(e) {
1325
+ return y(e, this._getTrashPath(), _);
1326
+ }
1327
+ _folderFromResolvedPath(t, n) {
1328
+ if (t.isFolder !== !0) throw new e(t.isFolder === !1 ? "Path '%s' resolves to a file, not a folder".replace("%s", n) : "Path '%s' resolved to an item that is not marked as a folder".replace("%s", n), "ValidationError");
1329
+ if (!t.id) throw new e("Path '%s' resolved to a folder without id".replace("%s", n), "ValidationError");
1330
+ let r = {
1331
+ id: t.id,
1332
+ name: t.name || t.id,
1333
+ parentId: t.folderId
1334
+ };
1335
+ return this._cache.upsertEntity(G({
1336
+ id: r.id,
1337
+ name: r.name,
1338
+ folderId: r.parentId ?? null,
1339
+ isFolder: !0
1340
+ })), r;
1341
+ }
1342
+ async _resolveFolderByPath(e) {
1343
+ if (typeof this._dataProvider.findItemByPath == "function") for (let t of this._getPathLookupCandidates(e)) {
1344
+ let e = await this._dataProvider.findItemByPath(t);
1345
+ if (e) return this._folderFromResolvedPath(e, t);
1346
+ }
1347
+ }
1348
+ async _findOrCreateFolderByPath(t) {
1349
+ let n = J(t);
1350
+ if (n.length > H) throw new e("Path depth exceeds maximum limit of %s".replace("%s", String(H)), "ValidationError");
1351
+ let r = await this._resolveFolderByPath(t);
1352
+ if (r) return r;
1353
+ let i;
1354
+ for (let r of n) {
1355
+ let { files: n } = await this.getFiles({
1356
+ folderId: i?.id || "",
1357
+ itemType: "folder"
1358
+ }), a = n.find((e) => e.name === r);
1359
+ if (a) {
1360
+ if (!a.isFolder) throw new e("Path component '%s' in '%s' is not a folder".replace("%s", r).replace("%s", t), "ValidationError");
1361
+ i = {
1362
+ id: a.id,
1363
+ name: a.name,
1364
+ parentId: a.folderId
1365
+ };
1366
+ } else {
1367
+ if (typeof this._dataProvider.createFolder != "function") throw new e("Create folder operation is not supported by data provider", "UnsupportedOperation");
1368
+ i = await this.createFolder({
1369
+ name: r,
1370
+ parentId: i?.id
1371
+ });
1372
+ }
1373
+ }
1374
+ return i;
1375
+ }
1376
+ async getTrashFolder() {
1377
+ let t = await this._findOrCreateFolderByPath(this._getTrashPath());
1378
+ if (!t) throw new e("Trash folder path '%s' could not be resolved".replace("%s", this._getTrashPath()), "FolderNotFound");
1379
+ return t;
1380
+ }
1381
+ async getTempFolder() {
1382
+ let t = this._pathOptions.tempPath ?? ne;
1383
+ if (t) try {
1384
+ return await this._findOrCreateFolderByPath(t);
1385
+ } catch (t) {
1386
+ if (t instanceof e && t.code === "ValidationError") throw t;
1387
+ console.warn("getTempFolder: temp folder unavailable, falling back to root", t);
1388
+ return;
1389
+ }
1390
+ }
1391
+ constructor(e, t = {}, n, r = {}) {
1392
+ this._dataProvider = e, this._options = t, this._pathOptions = n, this._blobFetchForceReloadOnceIds = /* @__PURE__ */ new Set(), this._mutationRegistry = new s(), this._mapItem = async ({ childrenCount: e, ...t }) => {
1393
+ let n = G(t);
1394
+ if (n.isFolder) return {
1395
+ ...n,
1396
+ childCount: e,
1397
+ url: "",
1398
+ thumbnailUrl: ""
1399
+ };
1400
+ let [r, i] = await Promise.all([this._getUrl(n), this._getThumbnailUrl(n)]);
1401
+ return {
1402
+ ...n,
1403
+ childCount: 0,
1404
+ url: r,
1405
+ thumbnailUrl: i
1406
+ };
1407
+ }, this._config = r, this._useSoftDelete = this._pathOptions.useSoftDelete, this._rootId = this._pathOptions.rootId || ee, this._enableTrash = this._options.enableTrash === void 0 || this._options.enableTrash, this._validateDuplicates = this._dataProvider.validateDuplicates, this._cache = new R(t.cacheStrategy || {}, t.cacheStrategy?.ttl, t.cacheStrategy?.errorTtl), this.setController({
1408
+ invalidateItemCache: (e) => {
1409
+ this._cache.forceInvalidate(e);
1410
+ },
1411
+ getCachedItem: (e) => this._cache.getEntity(e)
1412
+ });
1413
+ let i = Object.getPrototypeOf(this);
1414
+ for (let e of Object.getOwnPropertyNames(i)) {
1415
+ let t = Object.getOwnPropertyDescriptor(i, e);
1416
+ e !== "constructor" && typeof t?.value == "function" && (this[e] = t.value.bind(this));
1417
+ }
1418
+ }
1419
+ setController(e) {
1420
+ typeof this._dataProvider.setController == "function" && this._dataProvider.setController(e);
1421
+ }
1422
+ async clearCache(e) {
1423
+ let t = e ?? this._rootId;
1424
+ this._cache.forceInvalidate(t), this._cache.invalidateListingViews(t), typeof this._dataProvider.clearCache == "function" && await this._dataProvider.clearCache(e);
1425
+ }
1426
+ markNextFileBlobFetchBypassHttpCache(e) {
1427
+ this._blobFetchForceReloadOnceIds.add(this.normalizeId(e));
1428
+ }
1429
+ normalizeId(e) {
1430
+ return !e || e === "root" || e === "folders" ? this._rootId : e;
1431
+ }
1432
+ _isDeletingId(e) {
1433
+ return this._mutationRegistry.isDeleting(this.normalizeId(e));
1434
+ }
1435
+ _collectDeletingIds(e) {
1436
+ let t = this.normalizeId(e), n = /* @__PURE__ */ new Set([t]);
1437
+ if (!this._cache.getEntity(t)?.isFolder) return Array.from(n);
1438
+ let r = [t];
1439
+ for (; r.length > 0;) {
1440
+ let e = r.pop(), t = this._cache.getChildrenIds(e);
1441
+ for (let e of t) n.has(e) || (n.add(e), this._cache.getEntity(e)?.isFolder && r.push(e));
1442
+ }
1443
+ return Array.from(n);
1444
+ }
1445
+ _emptyFilesResult(e, t) {
1446
+ return {
1447
+ files: [],
1448
+ pagination: {
1449
+ ...typeof e == "number" ? { page: e } : {},
1450
+ ...typeof t == "number" ? { limit: t } : {},
1451
+ total: 0,
1452
+ totalPages: 1,
1453
+ hasNext: !1,
1454
+ hasPrev: !1
1455
+ }
1456
+ };
1457
+ }
1458
+ _validateName(t, n = !1) {
1459
+ if (!t || t.trim().length === 0) throw new e(n ? "Folder name cannot be empty" : "File name cannot be empty", n ? "FolderInvalidName" : "FileInvalidName");
1460
+ if (/[/\\:*?"<>|]/.test(t)) {
1461
+ let r = n ? "FolderInvalidName" : "FileInvalidName";
1462
+ throw new e(n ? "Folder name contains invalid characters: %s".replace("%s", t) : "File name contains invalid characters: %s".replace("%s", t), r);
1463
+ }
1464
+ }
1465
+ _validateTargetFolderType(t) {
1466
+ let n = this.normalizeId(t);
1467
+ if (n === this._rootId) return;
1468
+ let r = this._cache.getEntity(n);
1469
+ if (r && !r.isFolder) throw new e("Target item with id '%s' is not a folder".replace("%s", t), "ValidationError");
1470
+ }
1471
+ _checkNameExistsInCache(e, t, n, r) {
1472
+ let i = this.normalizeId(e), a = this._cache.getChildrenIds(i);
1473
+ for (let e of a) {
1474
+ let i = this._cache.getEntity(e);
1475
+ if (i && i.name === t && !!i.isFolder === n && (!r || i.id !== r)) return !0;
1476
+ }
1477
+ return !1;
1478
+ }
1479
+ async _getUrl(e) {
1480
+ if (e) {
1481
+ if (e.url) return e.url;
1482
+ try {
1483
+ return typeof this._dataProvider?.getUrl == "function" && this._dataProvider.getUrl(e) || void 0;
1484
+ } catch (e) {
1485
+ console.log(e);
1486
+ }
1487
+ }
1488
+ }
1489
+ async _getThumbnailUrl(e) {
1490
+ if (e) {
1491
+ if (e.thumbnailUrl) return e.thumbnailUrl;
1492
+ try {
1493
+ return typeof this._dataProvider?.getThumbnailUrl == "function" && this._dataProvider.getThumbnailUrl(e) || void 0;
1494
+ } catch (e) {
1495
+ console.log(e);
1496
+ }
1497
+ }
1498
+ }
1499
+ _isAncestor(e, t) {
1500
+ if (!t) return !1;
1501
+ let n = this.normalizeId(t), r = this.normalizeId(e);
1502
+ if (n === r) return !0;
1503
+ let i = this._cache.getEntity(n), a = /* @__PURE__ */ new Set();
1504
+ for (; i && i.folderId && !a.has(i.id);) {
1505
+ a.add(i.id);
1506
+ let e = this.normalizeId(i.folderId);
1507
+ if (e === r) return !0;
1508
+ if (i = this._cache.getEntity(e), !i || !i.isFolder) break;
1509
+ }
1510
+ return !1;
1511
+ }
1512
+ async getFile(e, t) {
1513
+ return this._isDeletingId(e) ? null : this._cache.searchByName(t, {
1514
+ parentId: this.normalizeId(e),
1515
+ caseSensitive: !0,
1516
+ recursive: !1,
1517
+ itemType: "file"
1518
+ })[0] || (await this.getFiles({
1519
+ folderId: e,
1520
+ itemType: "file",
1521
+ search: t
1522
+ })).files[0];
1523
+ }
1524
+ async getFileById(e) {
1525
+ let t = this.normalizeId(e);
1526
+ if (this._isDeletingId(t)) return null;
1527
+ let n = this._cache.getFileSystemItem(t);
1528
+ if (n && !n.isFolder) return await this._mapItem(n);
1529
+ if (!this._dataProvider.pathMode) return null;
1530
+ let r = await this._getFileByPathStyleId(t);
1531
+ if (r && !r.isFolder) {
1532
+ let e = G(r);
1533
+ return this._cache.upsertEntity(e), await this._mapItem(e);
1534
+ }
1535
+ return null;
1536
+ }
1537
+ async _getFileByPathStyleId(e) {
1538
+ let t = e.replace(/\\/g, "/").replace(/^\/+/, ""), n = t.lastIndexOf("/"), r = n < 0 ? "" : t.slice(0, n), i = n < 0 ? t : t.slice(n + 1);
1539
+ return i ? this.getFile(r, i) : null;
1540
+ }
1541
+ async getFileUrl(e, t) {
1542
+ let n = await this.getFile(e ?? "", t);
1543
+ if (n) return await this._getUrl(n) || await this._getThumbnailUrl(n) || n.thumbnail;
1544
+ }
1545
+ async getFiles({ folderId: e, search: t, sortBy: n, sortOrder: r, page: i, limit: a, itemType: o = "all", noFolder: s = !1 }) {
1546
+ s && (e = (await this.getTempFolder())?.id);
1547
+ let c = this.normalizeId(e ?? this._rootId), l = this._options.enablePagination === !1, u = l ? 1 : i, d = l ? z : a, f = this._options.clientSideFilter, p = this._options.clientSideSorting, m = this._options.clientSidePaging, h = m ? Math.max(1, i ?? 1) : void 0, g = m ? typeof a == "number" && Number.isFinite(a) && a > 0 ? a : V : void 0, _ = this._emptyFilesResult(m ? h : u, m ? g : d);
1548
+ if (this._isDeletingId(c)) return _;
1549
+ let v = this._options.unifiedCatalogFetch !== !1 && l && (o === "folder" || o === "file"), y = v ? "all" : o, b = {
1550
+ folderId: c,
1551
+ itemType: y
1552
+ };
1553
+ f || (b.search = t), p || (b.sortBy = n ?? (y === "folder" ? void 0 : "date"), b.sortOrder = r ?? (y === "folder" ? void 0 : "desc")), m || (b.page = u, b.limit = v ? B : d);
1554
+ let x = v ? B : m ? void 0 : d, S = {
1555
+ folderId: c,
1556
+ itemType: y,
1557
+ search: f ? void 0 : t,
1558
+ sortBy: p ? void 0 : b.sortBy,
1559
+ sortOrder: p ? void 0 : b.sortOrder,
1560
+ page: m ? void 0 : u,
1561
+ pageSize: x
1562
+ }, C;
1563
+ try {
1564
+ C = await this._cache.getOrCreateChildren(c, S, async (e, t) => {
1565
+ let n = t.pageSize, r = n != null && Number.isFinite(Number(n)) ? Number(n) : z, i = await this._dataProvider.getItems({
1566
+ ...t,
1567
+ folderId: e,
1568
+ limit: r
1569
+ }), a = K(i.items);
1570
+ return m ? {
1571
+ items: a,
1572
+ total: typeof i.pagination?.total == "number" ? i.pagination.total : a.length
1573
+ } : {
1574
+ items: a,
1575
+ ...i.pagination
1576
+ };
1577
+ });
1578
+ } catch (e) {
1579
+ if (this._isDeletingId(c)) return _;
1580
+ throw e;
1581
+ }
1582
+ let w = C.items;
1583
+ if (v && (o === "folder" ? w = w.filter((e) => e.isFolder) : o === "file" && (w = w.filter((e) => !e.isFolder))), f) {
1584
+ if (typeof this._options.clientSideFilter == "function") w = this._options.clientSideFilter(w, t);
1585
+ else if (t) {
1586
+ let e = t.toLowerCase();
1587
+ w = w.filter((t) => t.name?.toLowerCase().includes(e));
1588
+ }
1589
+ }
1590
+ p && (w = typeof this._options.clientSideSorting == "function" ? this._options.clientSideSorting(w, n, r) : this._defaultClientSideSort(w, n, r));
1591
+ let T;
1592
+ T = m || f || v ? w.length : C.total ?? w.length;
1593
+ let E = w;
1594
+ if (m) if (typeof this._options.clientSidePaging == "function") E = this._options.clientSidePaging(w, h, g);
1595
+ else {
1596
+ let e = (h - 1) * g, t = e + g;
1597
+ E = w.slice(e, t);
1598
+ }
1599
+ if (typeof this._dataProvider.getUrl == "function" || typeof this._dataProvider.getThumbnailUrl == "function") {
1600
+ let e = E.map(async (e) => {
1601
+ if (e.isFolder) return e;
1602
+ let [t, n] = await Promise.all([e.url ? Promise.resolve(e.url) : typeof this._dataProvider.getUrl == "function" ? this._dataProvider.getUrl(e) : Promise.resolve(void 0), e.thumbnailUrl ? Promise.resolve(e.thumbnailUrl) : typeof this._dataProvider.getThumbnailUrl == "function" ? this._dataProvider.getThumbnailUrl(e) : Promise.resolve(void 0)]);
1603
+ return {
1604
+ ...e,
1605
+ url: t,
1606
+ thumbnailUrl: n
1607
+ };
1608
+ });
1609
+ E = await Promise.all(e);
1610
+ }
1611
+ let D = m ? {
1612
+ page: h,
1613
+ limit: g,
1614
+ total: T,
1615
+ totalPages: Math.max(1, Math.ceil(T / g)),
1616
+ hasNext: h * g < T,
1617
+ hasPrev: h > 1
1618
+ } : {
1619
+ page: u,
1620
+ limit: d,
1621
+ total: T,
1622
+ ...!m && C.totalPages && { totalPages: C.totalPages },
1623
+ ...!m && C.hasNextPage && { hasNextPage: C.hasNextPage }
1624
+ };
1625
+ return {
1626
+ files: await Promise.all(E.map(this._mapItem)),
1627
+ pagination: D
1628
+ };
1629
+ }
1630
+ _defaultClientSideSort(e, t, n) {
1631
+ return e.sort((e, r) => l(e, r, {
1632
+ sortBy: t,
1633
+ sortOrder: n
1634
+ }));
1635
+ }
1636
+ async getFileData(t, n) {
1637
+ let r = this.normalizeId(typeof t == "string" ? t : t.id), i = n;
1638
+ this._blobFetchForceReloadOnceIds.delete(r) && (i = {
1639
+ ...n,
1640
+ forceReload: !0
1641
+ });
1642
+ let a = this._cache.getEntity(r) || (typeof t == "string" ? {
1643
+ id: r,
1644
+ name: "",
1645
+ folderId: this._rootId,
1646
+ isFolder: !1
1647
+ } : {
1648
+ ...t,
1649
+ childrenCount: t.childCount
1650
+ });
1651
+ if (typeof this._dataProvider.getFileData == "function") return await this._dataProvider.getFileData(a, i);
1652
+ if (!a.url && typeof this._dataProvider.getUrl != "function") throw new e("Cannot read file", "FileReadError");
1653
+ let o = a.url || (typeof this._dataProvider.getUrl == "function" ? await this._dataProvider.getUrl(a) : "");
1654
+ if (o) return (await (this._config.fetchWithProxy ?? fetch)(o, i?.forceReload ? { cache: "no-store" } : void 0)).blob();
1655
+ throw new e("Unable to get file data", "FileReadError");
1656
+ }
1657
+ async findFileByUrl(e) {
1658
+ let t = this._cache.searchByUrl(e);
1659
+ if (t) return t;
1660
+ if (typeof this._dataProvider.findFileByUrl == "function") try {
1661
+ let t = await this._dataProvider.findFileByUrl(e);
1662
+ if (t) {
1663
+ let e = G(t);
1664
+ return this._cache.upsertEntity(e), e;
1665
+ }
1666
+ } catch (e) {
1667
+ console.log(e);
1668
+ }
1669
+ return null;
1670
+ }
1671
+ async getChildrenCount(e) {
1672
+ if (!e) return 0;
1673
+ let t = this.normalizeId(e ?? this._rootId);
1674
+ return this._isDeletingId(t) ? 0 : this._cache.getChildrenCount(t, async () => {
1675
+ try {
1676
+ if (this._options.clientSidePaging) {
1677
+ let e = await this.getFiles({
1678
+ folderId: t,
1679
+ itemType: "all",
1680
+ page: 1,
1681
+ limit: V
1682
+ });
1683
+ return e.pagination.total ?? e.files.length;
1684
+ }
1685
+ let e = await this._dataProvider.getItems({
1686
+ folderId: t,
1687
+ limit: 0,
1688
+ itemType: "all"
1689
+ });
1690
+ return e.pagination === void 0 ? this._options.enablePagination === !1 ? e.items.length : 0 : e.pagination.total || 0;
1691
+ } catch (e) {
1692
+ if (this._isDeletingId(t) || this._isPathFolderNotFoundError(e)) return 0;
1693
+ throw e;
1694
+ }
1695
+ });
1696
+ }
1697
+ _isPathFolderNotFoundError(e) {
1698
+ if (!e || typeof e != "object") return !1;
1699
+ let t = e;
1700
+ return t.code === "path_folder_not_found" || t.data?.code === "path_folder_not_found" || t.status === 404 || t.data?.data?.status === 404;
1701
+ }
1702
+ async _uploadFile(t) {
1703
+ let n = this.normalizeId(t.folderId ?? this._rootId);
1704
+ if (typeof this._dataProvider.uploadFile != "function") throw new e("Upload file operation is not supported by data provider", "UnsupportedOperation");
1705
+ if (this._validateName(t.name, !1), this._validateDuplicates && this._checkNameExistsInCache(n, t.name, !1)) throw new e("File '%s' already exists in target folder".replace("%s", t.name), "FileAlreadyExists");
1706
+ let r = await this._dataProvider.uploadFile({
1707
+ ...t,
1708
+ folderId: n
1709
+ }), i = G({
1710
+ name: t.name,
1711
+ size: t.size,
1712
+ ...r,
1713
+ folderId: r.folderId ?? n
1714
+ });
1715
+ return this._cache.upsertEntity(i), i;
1716
+ }
1717
+ async uploadFiles(t) {
1718
+ let n = t.items.some((e) => e.noFolder) ? await this.getTempFolder() : null;
1719
+ t = {
1720
+ ...t,
1721
+ items: t.items?.map((e) => ({
1722
+ ...e,
1723
+ folderId: e.noFolder ? n?.id : e.folderId
1724
+ }))
1725
+ };
1726
+ for (let e of t.items) e.folderId && (t.folderId = e.folderId);
1727
+ let r = t.items || [], i = t.folderId ?? this._rootId, a = this.normalizeId(i);
1728
+ if (this._validateDuplicates) {
1729
+ for (let t of r) if (t.name) {
1730
+ this._validateName(t.name, !1);
1731
+ let n = this.normalizeId(t.folderId ?? a);
1732
+ if (this._checkNameExistsInCache(n, t.name, !1)) throw new e("File '%s' already exists in target folder".replace("%s", t.name), "FileAlreadyExists");
1733
+ }
1734
+ }
1735
+ let o = {
1736
+ detailed: Array(r.length),
1737
+ success: !0
1738
+ };
1739
+ if (typeof this._dataProvider.uploadFiles == "function") {
1740
+ let e = r.map((e) => ({
1741
+ ...e,
1742
+ folderId: this.normalizeId(e.folderId ?? a)
1743
+ })), t = await this._dataProvider.uploadFiles({
1744
+ folderId: a,
1745
+ items: e
1746
+ });
1747
+ o = U(t, (e) => {
1748
+ if (e) return {
1749
+ id: e.id,
1750
+ name: W(e.name),
1751
+ folderId: e.folderId,
1752
+ url: e.url,
1753
+ thumbnailUrl: e.thumbnailUrl,
1754
+ thumbnail: e.thumbnail
1755
+ };
1756
+ });
1757
+ let n = t?.detailed;
1758
+ if (o.detailed && o.detailed.length) {
1759
+ for (let e = 0; e < o.detailed.length; e++) {
1760
+ let t = o.detailed[e];
1761
+ if (t && t.success) {
1762
+ let r = t.data, i = r?.id ?? t.requestId, o = n?.[e]?.data, s = G({
1763
+ ...o,
1764
+ ...r,
1765
+ id: i,
1766
+ folderId: r?.folderId ?? o?.folderId ?? a,
1767
+ isFolder: !1
1768
+ });
1769
+ this._cache.upsertEntity(s), t.requestId = i;
1770
+ }
1771
+ }
1772
+ o.success === void 0 && (o.success = !o.detailed.some((e) => !e.success));
1773
+ } else o.success = !o.error && !!o.success;
1774
+ } else if (typeof this._dataProvider.uploadFile == "function") o.detailed = await Promise.all(r.map(async (e) => {
1775
+ let t = this.normalizeId(e.folderId ?? a);
1776
+ try {
1777
+ let n = await this._uploadFile({
1778
+ ...e,
1779
+ folderId: t
1780
+ });
1781
+ return {
1782
+ requestId: n.id,
1783
+ data: n,
1784
+ success: !0
1785
+ };
1786
+ } catch (t) {
1787
+ return {
1788
+ requestId: e?.id,
1789
+ success: !1,
1790
+ error: t
1791
+ };
1792
+ }
1793
+ })), o.success = !o.detailed.some((e) => !e.success);
1794
+ else throw new e("Upload files is not supported by data provider", "UnsupportedOperation");
1795
+ if (o.success && this._enableTrash) try {
1796
+ this._cache.forceInvalidate(a);
1797
+ } catch {}
1798
+ return o;
1799
+ }
1800
+ async deleteFile(e) {
1801
+ let t = this.normalizeId(e), n = [t];
1802
+ this._mutationRegistry.beginDeleting(n);
1803
+ try {
1804
+ if (this._useSoftDelete && this._enableTrash) {
1805
+ let e = await this.getTrashFolder();
1806
+ return await this.moveItem({
1807
+ itemId: t,
1808
+ targetFolderId: e.id,
1809
+ isFolder: !1
1810
+ });
1811
+ }
1812
+ return await this.hardDeleteItem(t);
1813
+ } finally {
1814
+ this._mutationRegistry.endDeleting(n);
1815
+ }
1816
+ }
1817
+ async deleteItems(e, t) {
1818
+ let n = Array.from(new Set(e.flatMap((e) => this._collectDeletingIds(this.normalizeId(e)))));
1819
+ this._mutationRegistry.beginDeleting(n);
1820
+ try {
1821
+ if (this._useSoftDelete && this._enableTrash && t.permanent !== !0) {
1822
+ let t = await this.getTrashFolder();
1823
+ return await this.moveItems(t.id, e);
1824
+ }
1825
+ return await this.hardDeleteItems(e, t);
1826
+ } finally {
1827
+ this._mutationRegistry.endDeleting(n);
1828
+ }
1829
+ }
1830
+ async hardDeleteItems(t, n) {
1831
+ let r = {
1832
+ success: !0,
1833
+ detailed: []
1834
+ }, i = n.permanent ? null : await this.getTrashFolder();
1835
+ if (typeof this._dataProvider.deleteItems == "function") {
1836
+ if (r = U(await this._dataProvider.deleteItems(t.map((e) => this._cache.getEntity(e)), n), (e) => e), r.success) {
1837
+ let e = r.detailed || t.map((e) => ({
1838
+ requestId: e,
1839
+ success: !0,
1840
+ error: void 0
1841
+ }));
1842
+ for (let t of e) t.success && (i ? this._cache.moveEntity(t.requestId, i.id) : this._cache.deleteEntity(t.requestId));
1843
+ }
1844
+ } else typeof this._dataProvider.deleteItem == "function" && (r.detailed = await Promise.all(t.map(async (t) => {
1845
+ let r = this._cache.getEntity(t);
1846
+ if (!r) return {
1847
+ requestId: t,
1848
+ success: !1,
1849
+ error: new e("Item with id '%s' not found".replace("%s", t), "FileNotFound")
1850
+ };
1851
+ try {
1852
+ return await this._dataProvider.deleteItem(r, n), i ? this._cache.moveEntity(t, i.id) : this._cache.deleteEntity(t), {
1853
+ requestId: t,
1854
+ success: !0
1855
+ };
1856
+ } catch (e) {
1857
+ return {
1858
+ requestId: t,
1859
+ error: e,
1860
+ success: !1
1861
+ };
1862
+ }
1863
+ })), r.success = !r.detailed.some((e) => !e.success));
1864
+ if (r.success && i) try {
1865
+ this._cache.forceInvalidate(i.id);
1866
+ } catch {}
1867
+ return r;
1868
+ }
1869
+ async hardDeleteItem(t) {
1870
+ let n = this._cache.getEntity(t);
1871
+ if (!n) throw new e("Item with id '%s' not found in cache".replace("%s", t), "FileNotFound");
1872
+ if (typeof this._dataProvider.deleteItem != "function") throw new e("Delete item operation is not supported by data provider", "DeleteNotAllowed");
1873
+ if (await this._dataProvider.deleteItem(n, { permanent: !0 }), this._cache.deleteEntity(t), this._enableTrash) try {
1874
+ let e = await this.getTrashFolder();
1875
+ e && this._cache.forceInvalidate(e.id);
1876
+ } catch {}
1877
+ return !0;
1878
+ }
1879
+ async cleanTrash() {
1880
+ if (!this._enableTrash) throw new e("Trash is not enabled", "UnsupportedOperation");
1881
+ if (typeof this._dataProvider.cleanTrash == "function") {
1882
+ let e = await this._dataProvider.cleanTrash();
1883
+ try {
1884
+ let e = await this.getTrashFolder();
1885
+ e && this._cache.forceInvalidate(e.id);
1886
+ } catch {}
1887
+ return e;
1888
+ }
1889
+ throw new e("Clean trash is not supported by data provider", "UnsupportedOperation");
1890
+ }
1891
+ async createFolder({ name: t, parentId: n, is_force_create: r }) {
1892
+ let i = this.normalizeId(n ?? this._rootId);
1893
+ if (this._validateName(t, !0), this._validateDuplicates && this._checkNameExistsInCache(i, t, !0)) throw new e("Folder '%s' already exists in parent folder".replace("%s", t), "FolderAlreadyExists");
1894
+ let a = await this._dataProvider.createFolder(t, i, r), o = G({
1895
+ id: a.id,
1896
+ folderId: i,
1897
+ name: a.name,
1898
+ isFolder: !0,
1899
+ childrenCount: a.childrenCount
1900
+ });
1901
+ return this._cache.upsertEntity(o), {
1902
+ ...a,
1903
+ name: o.name
1904
+ };
1905
+ }
1906
+ async deleteFolder(e) {
1907
+ let t = this.normalizeId(e), n = this._collectDeletingIds(t);
1908
+ this._mutationRegistry.beginDeleting(n);
1909
+ try {
1910
+ if (this._useSoftDelete && this._enableTrash) try {
1911
+ let e = await this.getTrashFolder();
1912
+ return await this.moveItem({
1913
+ itemId: t,
1914
+ targetFolderId: e.id,
1915
+ isFolder: !0
1916
+ }) ? (this._cache.deleteFolderRecursive(t), !0) : !1;
1917
+ } catch (e) {
1918
+ return console.error("Error moving folder to trash:", e), await this.hardDeleteItem(t);
1919
+ }
1920
+ return await this.hardDeleteItem(t);
1921
+ } finally {
1922
+ this._mutationRegistry.endDeleting(n);
1923
+ }
1924
+ }
1925
+ async renameFolder(t, n) {
1926
+ if (typeof this._dataProvider.renameItem != "function") throw new e("Rename folder is not supported", "RenameNotAllowed");
1927
+ let r = this.normalizeId(t), i = this._cache.getEntity(r);
1928
+ if (!i) throw new e("Folder with id '%s' not found in cache".replace("%s", t), "FolderNotFound");
1929
+ if (this._validateName(n, !0), this._validateDuplicates) {
1930
+ let t = this.normalizeId(i.folderId);
1931
+ if (this._checkNameExistsInCache(t, n, !0, r)) throw new e("Folder '%s' already exists in parent folder".replace("%s", n), "FolderAlreadyExists");
1932
+ }
1933
+ let a = q(await this._dataProvider.renameItem(i, n));
1934
+ return this._cache.upsertEntity(G({
1935
+ ...this._cache.getEntity(a.id),
1936
+ ...a
1937
+ })), this._cache.getEntity(a.id);
1938
+ }
1939
+ async renameFile(t, n) {
1940
+ if (typeof this._dataProvider.renameItem != "function") throw new e("Rename file is not supported", "RenameNotAllowed");
1941
+ let r = this._cache.getEntity(t);
1942
+ if (!r) throw new e("File with id '%s' not found in cache".replace("%s", t), "FileNotFound");
1943
+ let i = this.normalizeId(t);
1944
+ if (this._validateName(n, !1), this._validateDuplicates) {
1945
+ let t = this.normalizeId(r.folderId);
1946
+ if (this._checkNameExistsInCache(t, n, !1, i)) throw new e("File '%s' already exists in parent folder".replace("%s", n), "FileAlreadyExists");
1947
+ }
1948
+ let a = q(await this._dataProvider.renameItem(r, n));
1949
+ return a && this._cache.upsertEntity(G({
1950
+ ...this._cache.getEntity(i),
1951
+ ...a
1952
+ })), this._cache.getEntity(i);
1953
+ }
1954
+ async moveItem({ itemId: t, targetFolderId: n, isFolder: r }) {
1955
+ if (typeof this._dataProvider.moveItem != "function") throw new e("Move item operation is not supported by data provider", "MoveNotAllowed");
1956
+ let i = this.normalizeId(n);
1957
+ this._validateTargetFolderType(n);
1958
+ let a = this._cache.getEntity(t);
1959
+ if (!a) {
1960
+ let e = q(await this._dataProvider.moveItem({
1961
+ id: t,
1962
+ folderId: i,
1963
+ isFolder: r
1964
+ }, i));
1965
+ if (e.id === t) this._cache.upsertEntity(G({
1966
+ ...e,
1967
+ folderId: i
1968
+ }));
1969
+ else {
1970
+ try {
1971
+ this._cache.deleteEntity(t);
1972
+ } catch {}
1973
+ this._cache.upsertEntity(G({
1974
+ ...e,
1975
+ folderId: i
1976
+ }));
1977
+ }
1978
+ return !0;
1979
+ }
1980
+ if (a.isFolder) {
1981
+ if (t === i) throw new e("Cannot move folder into itself", "MoveIntoSelf");
1982
+ if (this._isAncestor(t, i)) throw new e("Cannot move folder into its descendant", "MoveIntoDescendant");
1983
+ }
1984
+ if (this._validateDuplicates && i !== this.normalizeId(a.folderId) && a.name && this._checkNameExistsInCache(i, a.name, !!a.isFolder, t)) {
1985
+ let t = a.isFolder ? "FolderAlreadyExists" : "FileAlreadyExists";
1986
+ throw new e(`${a.isFolder ? "Folder" : "File"} '${a.name}' already exists in target folder`, t);
1987
+ }
1988
+ let o = q(await this._dataProvider.moveItem(a, i));
1989
+ if (o.id === t) {
1990
+ let e = this._cache.moveEntity(t, i);
1991
+ return this._cache.upsertEntity(G({
1992
+ ...e,
1993
+ ...o
1994
+ })), !0;
1995
+ } else {
1996
+ let e = this._cache.deleteEntity(t);
1997
+ return this._cache.upsertEntity(G({
1998
+ ...e,
1999
+ ...o,
2000
+ folderId: i
2001
+ })), !0;
2002
+ }
2003
+ }
2004
+ async moveItems(t, n) {
2005
+ let r = this.normalizeId(t);
2006
+ this._validateTargetFolderType(t);
2007
+ let i = n.map((e) => this._cache.getEntity(e));
2008
+ for (let t of i) if (t) {
2009
+ if (t.isFolder) {
2010
+ if (t.id === r) throw new e("Cannot move folder '%s' into itself".replace("%s", t.name), "MoveIntoSelf");
2011
+ if (this._isAncestor(t.id, r)) throw new e("Cannot move folder '%s' into its descendant".replace("%s", t.name), "MoveIntoDescendant");
2012
+ }
2013
+ if (this._validateDuplicates && r !== this.normalizeId(t.folderId) && t.name && this._checkNameExistsInCache(r, t.name, !!t.isFolder, t.id)) {
2014
+ let n = t.isFolder ? "FolderAlreadyExists" : "FileAlreadyExists";
2015
+ throw new e(`${t.isFolder ? "Folder" : "File"} '${t.name}' already exists in target folder`, n);
2016
+ }
2017
+ }
2018
+ let a = {
2019
+ detailed: Array(i.length),
2020
+ success: !0
2021
+ };
2022
+ if (typeof this._dataProvider.moveItems == "function") a = U(await this._dataProvider.moveItems(i, r), (e) => e);
2023
+ else if (typeof this._dataProvider.moveItem == "function") a.detailed = await Promise.all(n.map(async (e, t) => {
2024
+ let n = i[t] || {
2025
+ id: e,
2026
+ folderId: null,
2027
+ isFolder: !1,
2028
+ name: e
2029
+ };
2030
+ try {
2031
+ return {
2032
+ requestId: e,
2033
+ data: q(await this._dataProvider.moveItem(n, r)),
2034
+ success: !0
2035
+ };
2036
+ } catch (t) {
2037
+ return {
2038
+ requestId: e,
2039
+ success: !1,
2040
+ error: t
2041
+ };
2042
+ }
2043
+ }));
2044
+ else throw new e("Move items is not supported", "MoveNotAllowed");
2045
+ if (a.success) {
2046
+ let e = a.detailed || n.map((e) => ({
2047
+ requestId: e,
2048
+ success: !0,
2049
+ data: void 0
2050
+ }));
2051
+ for (let t of e) if (t.success) if (!t.data || t.data.id === t.requestId) this._cache.moveEntity(t.requestId, r), t.data && this._cache.upsertEntity(G({
2052
+ ...this._cache.getEntity(t.requestId),
2053
+ ...t.data,
2054
+ folderId: r
2055
+ }));
2056
+ else {
2057
+ let e = this._cache.deleteEntity(t.requestId);
2058
+ this._cache.upsertEntity(G({
2059
+ ...e,
2060
+ ...t.data,
2061
+ folderId: r
2062
+ }));
2063
+ }
2064
+ return a;
2065
+ } else return {
2066
+ success: !1,
2067
+ error: a.error || new e("Move items failed", "MoveNotAllowed")
2068
+ };
2069
+ }
2070
+ async copyItem({ itemId: t, targetFolderId: n }) {
2071
+ if (typeof this._dataProvider.copyItem != "function") throw new e("Copy item is not supported", "CopyNotAllowed");
2072
+ let r = this.normalizeId(n);
2073
+ this._validateTargetFolderType(n);
2074
+ let i = this._cache.getEntity(t);
2075
+ if (!i) {
2076
+ let e = q(await this._dataProvider.copyItem({
2077
+ id: t,
2078
+ folderId: r
2079
+ }, r));
2080
+ return this._cache.copyEntity(t, r, e.id), this._cache.upsertEntity(G({
2081
+ ...e,
2082
+ folderId: r
2083
+ })), !0;
2084
+ }
2085
+ if (this._validateDuplicates && i.name) {
2086
+ let n = r === this.normalizeId(i.folderId) ? t : void 0;
2087
+ if (this._checkNameExistsInCache(r, i.name, !!i.isFolder, n)) {
2088
+ let t = i.isFolder ? "FolderAlreadyExists" : "FileAlreadyExists";
2089
+ throw new e(`${i.isFolder ? "Folder" : "File"} '${i.name}' already exists in target folder`, t);
2090
+ }
2091
+ }
2092
+ let a = q(await this._dataProvider.copyItem(i, r));
2093
+ return this._cache.copyEntity(t, r, a.id, {
2094
+ ...a,
2095
+ folderId: r
2096
+ }) || this._cache.upsertEntity(G({
2097
+ ...a,
2098
+ folderId: r
2099
+ })), !0;
2100
+ }
2101
+ async copyItems(t, n) {
2102
+ let r = this.normalizeId(t);
2103
+ this._validateTargetFolderType(t);
2104
+ let i = n.map((e) => typeof e == "string" ? { id: e } : e), a = i.map((e) => e.id), o = a.map((e, t) => {
2105
+ let n = this._cache.getEntity(e), r = i[t]?.newName;
2106
+ return n ? r ? {
2107
+ ...n,
2108
+ name: r
2109
+ } : n : void 0;
2110
+ });
2111
+ if (this._validateDuplicates) for (let t = 0; t < o.length; t++) {
2112
+ let n = o[t];
2113
+ if (n && n.name) {
2114
+ let i = r === this.normalizeId(n.folderId) ? a[t] : void 0;
2115
+ if (this._checkNameExistsInCache(r, n.name, !!n.isFolder, i)) {
2116
+ let t = n.isFolder ? "FolderAlreadyExists" : "FileAlreadyExists";
2117
+ throw new e(`${n.isFolder ? "Folder" : "File"} '${n.name}' already exists in target folder`, t);
2118
+ }
2119
+ }
2120
+ }
2121
+ let s = {
2122
+ detailed: Array(o.length),
2123
+ success: !0
2124
+ };
2125
+ if (typeof this._dataProvider.copyItems == "function") s = U(await this._dataProvider.copyItems(o, r), (e) => e);
2126
+ else if (typeof this._dataProvider.copyItem == "function") s.detailed = await Promise.all(a.map(async (e, t) => {
2127
+ let n = o[t] || {
2128
+ id: e,
2129
+ folderId: null,
2130
+ isFolder: !1
2131
+ };
2132
+ try {
2133
+ return {
2134
+ requestId: e,
2135
+ data: q(await this._dataProvider.copyItem(n, r)),
2136
+ success: !0
2137
+ };
2138
+ } catch (t) {
2139
+ return {
2140
+ requestId: e,
2141
+ success: !1,
2142
+ error: t
2143
+ };
2144
+ }
2145
+ }));
2146
+ else throw new e("Copy items is not supported", "CopyNotAllowed");
2147
+ if (s.success && s.detailed) for (let e = 0, t = s.detailed.length; e < t; e++) {
2148
+ let t = s.detailed[e];
2149
+ if (t.success) if (t.data?.id) {
2150
+ let e = t.data.folderId || r;
2151
+ !this._cache.copyEntity(t.requestId, e, t.data.id, {
2152
+ ...t.data,
2153
+ folderId: e
2154
+ }) && t.data && this._cache.upsertEntity(G({
2155
+ ...t.data,
2156
+ folderId: e
2157
+ }));
2158
+ } else console.error("batchResult in copy method must have ids");
2159
+ }
2160
+ else if (!s.detailed) throw new e("batchResult in copy method must have detailed results", "ValidationError");
2161
+ return s;
2162
+ }
2163
+ async uploadFileByUrl(e) {
2164
+ if (e.noFolder) {
2165
+ let t = await this.getTempFolder();
2166
+ e = {
2167
+ ...e,
2168
+ folderId: t?.id
2169
+ };
2170
+ }
2171
+ let t = this.normalizeId(e.folderId ?? this._rootId);
2172
+ try {
2173
+ let n = G({
2174
+ ...await this._dataProvider.uploadFileByUrl(e.url, t, { eventSource: e.eventSource }),
2175
+ folderId: t
2176
+ });
2177
+ return this._cache.upsertEntity(n), n;
2178
+ } catch (e) {
2179
+ throw console.error("uploadFileByUrl error:", e), e;
2180
+ }
2181
+ }
2182
+ async updateFile(t, n) {
2183
+ if (typeof this._dataProvider.updateFile != "function") throw new e("Update file is not supported", "UnsupportedOperation");
2184
+ let r = this.normalizeId(t), i = this._cache.getEntity(r);
2185
+ if (!i) throw new e("File with id '%s' not found in cache".replace("%s", t), "FileNotFound");
2186
+ if (n.name && n.name !== i.name && this._validateName(n.name, !1), this._validateDuplicates && n.name && n.name !== i.name) {
2187
+ let t = this.normalizeId(i.folderId);
2188
+ if (this._checkNameExistsInCache(t, n.name, !1, r)) throw new e("File '%s' already exists in parent folder".replace("%s", n.name), "FileAlreadyExists");
2189
+ }
2190
+ let a = q(await this._dataProvider.updateFile(i, { ...n }));
2191
+ return this._cache.upsertEntity(G({
2192
+ ...this._cache.getEntity(r),
2193
+ ...a,
2194
+ ...n
2195
+ })), this._cache.getEntity(r);
2196
+ }
2197
+ }, Y = [], X = 0; X < 256; ++X) Y.push((X + 256).toString(16).slice(1));
2198
+ function ie(e, t = 0) {
2199
+ return (Y[e[t + 0]] + Y[e[t + 1]] + Y[e[t + 2]] + Y[e[t + 3]] + "-" + Y[e[t + 4]] + Y[e[t + 5]] + "-" + Y[e[t + 6]] + Y[e[t + 7]] + "-" + Y[e[t + 8]] + Y[e[t + 9]] + "-" + Y[e[t + 10]] + Y[e[t + 11]] + Y[e[t + 12]] + Y[e[t + 13]] + Y[e[t + 14]] + Y[e[t + 15]]).toLowerCase();
2200
+ }
2201
+ //#endregion
2202
+ //#region packages/file-manager/node_modules/uuid/dist/esm-browser/rng.js
2203
+ var Z, ae = /* @__PURE__ */ new Uint8Array(16);
2204
+ function oe() {
2205
+ if (!Z && (Z = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !Z)) throw Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
2206
+ return Z(ae);
2207
+ }
2208
+ var Q = { randomUUID: typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto) };
2209
+ //#endregion
2210
+ //#region packages/file-manager/node_modules/uuid/dist/esm-browser/v4.js
2211
+ function se(e, t, n) {
2212
+ if (Q.randomUUID && !t && !e) return Q.randomUUID();
2213
+ e ||= {};
2214
+ var r = e.random || (e.rng || oe)();
2215
+ if (r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, t) {
2216
+ n ||= 0;
2217
+ for (var i = 0; i < 16; ++i) t[n + i] = r[i];
2218
+ return t;
2219
+ }
2220
+ return ie(r);
2221
+ }
2222
+ //#endregion
2223
+ //#region packages/file-manager/src/adapters/PathAwareStorageAdapter.ts
2224
+ var ce = "", le = "Trash", ue = class {
2225
+ constructor(e, t = ce, n = _, r = le, i = !0) {
2226
+ this._inner = e, this.rootPath = t, this.trashId = n, this.trashPath = r, this.ensureFolderTrailingSlash = i, this._pathToId = /* @__PURE__ */ new Map(), this._idToPath = /* @__PURE__ */ new Map(), this.validateDuplicates = this._inner.validateDuplicates ?? !1, typeof e.getUrl == "function" && (this.getUrl = (t) => e.getUrl({
2227
+ ...t,
2228
+ id: this.pathForId(t.id, !1),
2229
+ folderId: t.folderId ? this.pathForId(t.folderId, !0) : this.rootPath
2230
+ })), typeof e.getThumbnailUrl == "function" && (this.getThumbnailUrl = (t) => e.getThumbnailUrl({
2231
+ ...t,
2232
+ id: this.pathForId(t.id, !1),
2233
+ folderId: t.folderId ? this.pathForId(t.folderId, !0) : this.rootPath
2234
+ }));
2235
+ let a = this;
2236
+ typeof this._inner.deleteItems != "function" && (a.deleteItems = void 0), typeof this._inner.deleteItem != "function" && (a.deleteItem = void 0), typeof this._inner.moveItems == "function" ? a.moveItem = void 0 : a.moveItems = void 0, typeof this._inner.copyItems == "function" ? a.copyItem = void 0 : a.copyItems = void 0, typeof this._inner.uploadFiles == "function" ? a.uploadFile = void 0 : a.uploadFiles = void 0, typeof this._inner.clearCache != "function" && (a.clearCache = void 0), this._getOrGenerateId(this.rootPath);
2237
+ }
2238
+ setController(e) {
2239
+ typeof this._inner.setController == "function" && this._inner.setController({
2240
+ invalidateItemCache: (t) => {
2241
+ let n = t.replace(/^\/+|\/+$/g, ""), r = this._pathToId.get(n);
2242
+ r && e.invalidateItemCache(r);
2243
+ },
2244
+ getCachedItem: (t) => {
2245
+ let n = t.replace(/^\/+|\/+$/g, ""), r = this._pathToId.get(n);
2246
+ if (r) {
2247
+ let t = e.getCachedItem(r);
2248
+ if (t) return {
2249
+ ...t,
2250
+ id: this.pathForId(t.id, t.isFolder ?? !1),
2251
+ folderId: this.pathForId(t.folderId ?? "", t.isFolder ?? !1)
2252
+ };
2253
+ }
2254
+ }
2255
+ });
2256
+ }
2257
+ clearCache(e) {
2258
+ if (typeof this._inner.clearCache == "function") return this._inner.clearCache(e);
2259
+ }
2260
+ findItemByPath(e) {
2261
+ if (typeof this._inner.findItemByPath != "function") return;
2262
+ let t = (e, t, n) => {
2263
+ if (!e) return;
2264
+ if (e.isFolder !== !0) return e;
2265
+ let r = v(v(n) === v(this.trashPath) ? this.trashPath : t), i = e.folderId != null && e.folderId !== "" ? v(e.folderId) : this.rootPath;
2266
+ return {
2267
+ ...e,
2268
+ id: this._getOrGenerateId(r),
2269
+ name: e.name,
2270
+ folderId: i === v(this.rootPath) ? this.rootPath || "" : this._getOrGenerateId(i),
2271
+ isFolder: !0
2272
+ };
2273
+ }, n = y(e, this.trashPath, this.trashId);
2274
+ return (async () => {
2275
+ for (let r of n) {
2276
+ let n = await Promise.resolve(this._inner.findItemByPath(r)), i = t(n, r, e);
2277
+ if (i) return i;
2278
+ }
2279
+ })();
2280
+ }
2281
+ _getOrGenerateId(e) {
2282
+ if (e === this.rootPath) return "";
2283
+ let t = v(e);
2284
+ if (t === v(this.trashPath)) return this.trashId;
2285
+ if (this._pathToId.has(t)) return this._pathToId.get(t);
2286
+ let n = se();
2287
+ return this._pathToId.set(t, n), this._idToPath.set(n, t), n;
2288
+ }
2289
+ pathForId(t, n, r) {
2290
+ if (t === "" || t === void 0) return this.rootPath;
2291
+ if (t === this.trashId) return this.trashPath;
2292
+ let i = this._idToPath.get(t);
2293
+ if (!i) {
2294
+ if (r) return `unknown folder ${t}`;
2295
+ throw new e(`Path for id '${t}' not found in mapping`, "FileNotFound");
2296
+ }
2297
+ let a = 0;
2298
+ for (; a < this.rootPath.length && this.rootPath[a] === "/";) a++;
2299
+ if (a) {
2300
+ let e = 0;
2301
+ for (; e < i.length && i[e] === "/";) e++;
2302
+ let t = i.slice(e);
2303
+ i = "/".repeat(a) + t;
2304
+ }
2305
+ return n && this.ensureFolderTrailingSlash && !i.endsWith("/") && (i += "/"), i;
2306
+ }
2307
+ _updateIdMapping(e, t) {
2308
+ let n = t.replace(/^\/+|\/+$/g, ""), r = this._idToPath.get(e);
2309
+ r && this._pathToId.delete(r), this._idToPath.set(e, n), this._pathToId.set(n, e);
2310
+ }
2311
+ async getItems(e) {
2312
+ let t = e.folderId ? this.pathForId(e.folderId, !0) : this.rootPath, n = await this._inner.getItems({
2313
+ ...e,
2314
+ folderId: t
2315
+ }), r = n.items.map((e) => {
2316
+ let t = this._getOrGenerateId(e.id), n = e.folderId ? this._getOrGenerateId(e.folderId) : this._getOrGenerateId(this.rootPath);
2317
+ return {
2318
+ ...e,
2319
+ id: t,
2320
+ folderId: n
2321
+ };
2322
+ });
2323
+ return {
2324
+ ...n,
2325
+ items: r
2326
+ };
2327
+ }
2328
+ async createFolder(e, t, n) {
2329
+ let r = t ? this.pathForId(t, !0) : this.rootPath, i = await this._inner.createFolder(e, r, n), a = this._getOrGenerateId(i.id), o = i.folderId ? this._getOrGenerateId(i.folderId) : this._getOrGenerateId(this.rootPath);
2330
+ return {
2331
+ ...i,
2332
+ id: a,
2333
+ folderId: o
2334
+ };
2335
+ }
2336
+ async renameItem(t, n) {
2337
+ if (typeof this._inner.renameItem != "function") throw new e("Rename item is not supported by inner storage", "RenameNotAllowed");
2338
+ let r = this.pathForId(t.id, t.isFolder ?? !1), i = r, { ...a } = await this._inner.renameItem({
2339
+ ...t,
2340
+ id: r,
2341
+ folderId: this.pathForId(t.folderId ?? "", !0)
2342
+ }, n);
2343
+ return this._updateIdMapping(t.id, a.id), t.isFolder && this.remapCachedFolderContents(t.id, i, a.id), a.folderId && (a = {
2344
+ ...a,
2345
+ folderId: this._getOrGenerateId(a.folderId)
2346
+ }), {
2347
+ ...a,
2348
+ id: this._getOrGenerateId(a.id)
2349
+ };
2350
+ }
2351
+ async deleteItem(t, n) {
2352
+ if (typeof this._inner.deleteItem != "function") throw new e("Delete item is not supported by inner storage", "DeleteNotAllowed");
2353
+ let r = this.pathForId(t.id, t.isFolder ?? !1);
2354
+ await this._inner.deleteItem({
2355
+ ...t,
2356
+ id: r,
2357
+ folderId: this.pathForId(t.folderId ?? "", !0)
2358
+ }, n), this.recursivelyRemoveMappings(t.id, r);
2359
+ }
2360
+ async uploadFile(t) {
2361
+ if (typeof this._inner.uploadFile != "function") throw new e("Upload file is not supported by inner storage", "UnsupportedOperation");
2362
+ let n = t.folderId ? this.pathForId(t.folderId, !0) : this.rootPath, r = await this._inner.uploadFile({
2363
+ ...t,
2364
+ folderId: n
2365
+ }), i = this._getOrGenerateId(r.id), a = r.folderId ? this._getOrGenerateId(r.folderId) : this._getOrGenerateId(this.rootPath);
2366
+ return {
2367
+ ...r,
2368
+ id: i,
2369
+ folderId: a
2370
+ };
2371
+ }
2372
+ async uploadFiles(e) {
2373
+ let t = e.folderId, n = e.items?.map((e) => e.folderId) ?? [], r = e.folderId ? this.pathForId(e.folderId, !0) : this.rootPath, i = {
2374
+ ...e,
2375
+ folderId: r,
2376
+ items: e.items?.map((e) => ({
2377
+ ...e,
2378
+ folderId: e.folderId ? this.pathForId(e.folderId ?? "", !0) : r
2379
+ })) ?? []
2380
+ }, a = await this._inner.uploadFiles(i);
2381
+ return {
2382
+ ...a,
2383
+ detailed: a.detailed?.map((e, r) => {
2384
+ if (!e?.data) return e;
2385
+ let i = e.data.id ? this._getOrGenerateId(e.data.id) : "", a = e.data.folderId ? this._getOrGenerateId(e.data.folderId) : n[r] ?? t ?? this._getOrGenerateId(this.rootPath);
2386
+ return {
2387
+ ...e,
2388
+ data: {
2389
+ ...e.data,
2390
+ id: i,
2391
+ folderId: a
2392
+ }
2393
+ };
2394
+ })
2395
+ };
2396
+ }
2397
+ async findFileByUrl(e, t) {
2398
+ let n = t ? this.pathForId(t, !0) : this.rootPath;
2399
+ if (typeof this._inner.findFileByUrl == "function") {
2400
+ let t = await this._inner.findFileByUrl(e, n);
2401
+ if (!t) return;
2402
+ let r = this._getOrGenerateId(t.id), i = t.folderId ? this._getOrGenerateId(t.folderId) : this._getOrGenerateId(this.rootPath);
2403
+ return {
2404
+ ...t,
2405
+ id: r,
2406
+ folderId: i
2407
+ };
2408
+ }
2409
+ }
2410
+ async uploadFileByUrl(e, t, n) {
2411
+ let r = t ? this.pathForId(t, !0) : this.rootPath, i = await this._inner.uploadFileByUrl(e, r, n), a = this._getOrGenerateId(i.id), o = i.folderId ? this._getOrGenerateId(i.folderId) : this._getOrGenerateId(this.rootPath);
2412
+ return {
2413
+ ...i,
2414
+ id: a,
2415
+ folderId: o
2416
+ };
2417
+ }
2418
+ async moveItem(t, n) {
2419
+ if (typeof this._inner.moveItem != "function") throw new e("Move item is not supported by inner storage", "MoveNotAllowed");
2420
+ let r = this.pathForId(t.id, t.isFolder ?? !1), i = n ? this.pathForId(n, !0) : this.rootPath, a = await this._inner.moveItem({
2421
+ ...t,
2422
+ id: r,
2423
+ folderId: this.pathForId(t.folderId ?? "", !0)
2424
+ }, i);
2425
+ this._updateIdMapping(t.id, a.id), t.isFolder && this.remapCachedFolderContents(t.id, r, a.id);
2426
+ let o = this._getOrGenerateId(a.id), s = a.folderId ? this._getOrGenerateId(a.folderId) : n;
2427
+ return {
2428
+ ...a,
2429
+ id: o,
2430
+ folderId: s
2431
+ };
2432
+ }
2433
+ async moveItems(t, n) {
2434
+ if (typeof this._inner.moveItems != "function") throw new e("Move items is not supported by inner storage", "MoveNotAllowed");
2435
+ let r = n ? this.pathForId(n, !0) : this.rootPath, i = t.map((e) => ({
2436
+ ...e,
2437
+ id: this.pathForId(e.id, e.isFolder ?? !1),
2438
+ folderId: e.folderId ? this.pathForId(e.folderId ?? "", !0) : this.rootPath
2439
+ })), a = await this._inner.moveItems(i, r), o = [];
2440
+ if (a.detailed && a.detailed.length > 0) {
2441
+ let e = /* @__PURE__ */ new Map();
2442
+ for (let n = 0; n < a.detailed.length; n++) {
2443
+ let r = a.detailed[n], i = r.requestId || (n < t.length ? t[n].id : void 0);
2444
+ i && e.set(i, r);
2445
+ }
2446
+ for (let r of t) {
2447
+ let i = e.get(r.id) || (a.detailed.length === t.length ? a.detailed[t.indexOf(r)] : void 0);
2448
+ if (i?.success && i.data) {
2449
+ if (this._updateIdMapping(r.id, i.data.id), r.isFolder) {
2450
+ let e = this.pathForId(r.id, r.isFolder ?? !1);
2451
+ this.remapCachedFolderContents(r.id, e, i.data.id);
2452
+ }
2453
+ o.push({
2454
+ requestId: r.id,
2455
+ success: !0,
2456
+ data: {
2457
+ ...i.data,
2458
+ id: this._getOrGenerateId(i.data.id),
2459
+ folderId: i.data.folderId ? this._getOrGenerateId(i.data.folderId) : n || this._getOrGenerateId(this.rootPath)
2460
+ }
2461
+ });
2462
+ } else o.push({
2463
+ requestId: r.id,
2464
+ success: !1,
2465
+ error: i?.error || {
2466
+ message: "Move operation failed - no detailed result",
2467
+ code: "MoveNotAllowed"
2468
+ }
2469
+ });
2470
+ }
2471
+ } else for (let e of t) o.push({
2472
+ requestId: e.id,
2473
+ success: !1,
2474
+ error: {
2475
+ message: "Move operation failed - no detailed results",
2476
+ code: "MoveNotAllowed"
2477
+ }
2478
+ });
2479
+ return {
2480
+ success: a.success,
2481
+ detailed: o,
2482
+ error: a.error
2483
+ };
2484
+ }
2485
+ async copyItems(t, n) {
2486
+ if (typeof this._inner.copyItems != "function") throw new e("Copy items is not supported by inner storage", "CopyNotAllowed");
2487
+ let r = n ? this.pathForId(n, !0) : this.rootPath, i = t.map((e) => ({
2488
+ ...e,
2489
+ id: this.pathForId(e.id, e.isFolder ?? !1),
2490
+ folderId: e.folderId ? this.pathForId(e.folderId ?? "", !0) : this.rootPath
2491
+ })), a = await this._inner.copyItems(i, r), o = [];
2492
+ if (a.detailed && a.detailed.length > 0) {
2493
+ let e = /* @__PURE__ */ new Map();
2494
+ for (let n = 0; n < a.detailed.length; n++) {
2495
+ let r = a.detailed[n], i = r.requestId || (n < t.length ? t[n].id : void 0);
2496
+ i && e.set(i, r);
2497
+ }
2498
+ for (let r of t) {
2499
+ let i = e.get(r.id) || (a.detailed.length === t.length ? a.detailed[t.indexOf(r)] : void 0);
2500
+ if (i?.success && i.data?.id) {
2501
+ let e = this._getOrGenerateId(i.data.id);
2502
+ o.push({
2503
+ requestId: r.id,
2504
+ success: !0,
2505
+ data: {
2506
+ ...i.data,
2507
+ id: e,
2508
+ folderId: i.data.folderId ? this._getOrGenerateId(i.data.folderId) : n || this._getOrGenerateId(this.rootPath)
2509
+ }
2510
+ });
2511
+ } else {
2512
+ let e = i ? i.success ? i.data ? i.data.id ? {
2513
+ message: "Unknown copy error",
2514
+ code: "CopyNotAllowed"
2515
+ } : {
2516
+ message: "No ID in copied item data",
2517
+ code: "CopyNotAllowed"
2518
+ } : {
2519
+ message: "No data returned from copy operation",
2520
+ code: "CopyNotAllowed"
2521
+ } : i.error || {
2522
+ message: "Copy operation failed",
2523
+ code: "CopyNotAllowed"
2524
+ } : {
2525
+ message: "No detailed result from copy operation",
2526
+ code: "CopyNotAllowed"
2527
+ };
2528
+ o.push({
2529
+ requestId: r.id,
2530
+ success: !1,
2531
+ error: e
2532
+ });
2533
+ }
2534
+ }
2535
+ } else for (let e of t) o.push({
2536
+ requestId: e.id,
2537
+ success: !1,
2538
+ error: {
2539
+ message: "No detailed results from copy operation",
2540
+ code: "CopyNotAllowed"
2541
+ }
2542
+ });
2543
+ let s = o.every((e) => e.success);
2544
+ return {
2545
+ success: s,
2546
+ detailed: o,
2547
+ error: s ? void 0 : a.error || {
2548
+ message: "Copy operation completed with errors",
2549
+ code: "CopyNotAllowed"
2550
+ }
2551
+ };
2552
+ }
2553
+ async deleteItems(t, n) {
2554
+ if (typeof this._inner.deleteItems != "function") throw new e("Delete items is not supported by inner storage", "DeleteNotAllowed");
2555
+ let r = t.map((e) => ({
2556
+ ...e,
2557
+ id: this.pathForId(e.id, e.isFolder ?? !1),
2558
+ folderId: e.folderId ? this.pathForId(e.folderId ?? "", !0) : this.rootPath
2559
+ })), i = await this._inner.deleteItems(r, n), a = [];
2560
+ if (i.detailed && i.detailed.length > 0) {
2561
+ let e = /* @__PURE__ */ new Map();
2562
+ for (let n = 0; n < i.detailed.length; n++) {
2563
+ let r = i.detailed[n], a = r.requestId || (n < t.length ? t[n].id : void 0);
2564
+ a && e.set(a, r);
2565
+ }
2566
+ for (let r of t) {
2567
+ let o = e.get(r.id) || (i.detailed.length === t.length ? i.detailed[t.indexOf(r)] : void 0);
2568
+ if (o?.success) {
2569
+ let e = this.pathForId(r.id, r.isFolder ?? !1);
2570
+ if (n.permanent) this.recursivelyRemoveMappings(r.id, e);
2571
+ else if (this._updateIdMapping(r.id, this.trashPath + "/" + e + (r.isFolder ? "/" : "")), r.isFolder) {
2572
+ let t = this.pathForId(r.id, r.isFolder ?? !1);
2573
+ this.remapCachedFolderContents(r.id, t, this.trashPath + "/" + e + (r.isFolder ? "/" : ""));
2574
+ }
2575
+ a.push({
2576
+ requestId: r.id,
2577
+ success: !0
2578
+ });
2579
+ } else a.push({
2580
+ requestId: r.id,
2581
+ success: !1,
2582
+ error: o?.error || {
2583
+ message: "Delete operation failed",
2584
+ code: "DeleteNotAllowed"
2585
+ }
2586
+ });
2587
+ }
2588
+ } else for (let e of t) if (i.success) {
2589
+ let t = this.pathForId(e.id, e.isFolder ?? !1);
2590
+ if (n.permanent) this.recursivelyRemoveMappings(e.id, t);
2591
+ else if (this._updateIdMapping(e.id, this.trashPath + "/" + t + (e.isFolder ? "/" : "")), e.isFolder) {
2592
+ let n = this.pathForId(e.id, e.isFolder ?? !1);
2593
+ this.remapCachedFolderContents(e.id, n, this.trashPath + "/" + t + (e.isFolder ? "/" : ""));
2594
+ }
2595
+ a.push({
2596
+ requestId: e.id,
2597
+ success: !0
2598
+ });
2599
+ } else a.push({
2600
+ requestId: e.id,
2601
+ success: !1,
2602
+ error: i.error || {
2603
+ message: "Delete operation failed",
2604
+ code: "DeleteNotAllowed"
2605
+ }
2606
+ });
2607
+ let o = a.every((e) => e.success);
2608
+ return {
2609
+ success: o,
2610
+ detailed: a,
2611
+ error: o ? void 0 : i.error || {
2612
+ message: "Some items failed to delete",
2613
+ code: "DeleteNotAllowed"
2614
+ }
2615
+ };
2616
+ }
2617
+ remapCachedFolderContents(e, t, n) {
2618
+ let r = t.replace(/\/+$/, ""), i = n.replace(/\/+$/, ""), a = [];
2619
+ for (let [e, t] of this._idToPath.entries()) {
2620
+ let n = t.replace(/\/+$/, "");
2621
+ n !== r && n.startsWith(r + "/") && a.push({
2622
+ id: e,
2623
+ oldPath: n
2624
+ });
2625
+ }
2626
+ for (let { id: e, oldPath: t } of a) {
2627
+ let n = i + t.substring(r.length);
2628
+ this._updateIdMapping(e, n);
2629
+ }
2630
+ }
2631
+ recursivelyRemoveMappings(e, t) {
2632
+ let n = t.replace(/\/+$/, ""), r = [], i = [];
2633
+ for (let [e, t] of this._idToPath.entries()) {
2634
+ let a = t.replace(/\/+$/, "");
2635
+ (a === n || a.startsWith(n + "/")) && (r.push(e), i.push(a));
2636
+ }
2637
+ for (let e = 0; e < r.length; e++) this._idToPath.delete(r[e]), this._pathToId.delete(i[e]);
2638
+ }
2639
+ async copyItem(t, n) {
2640
+ if (typeof this._inner.copyItem != "function") throw new e("Copy item is not supported by inner storage", "CopyNotAllowed");
2641
+ let r = this.pathForId(t.id, t.isFolder ?? !1), i = n ? this.pathForId(n, !0) : this.rootPath, a = await this._inner.copyItem({
2642
+ ...t,
2643
+ id: r,
2644
+ folderId: this.pathForId(t.folderId ?? "", !0)
2645
+ }, i), o = this._getOrGenerateId(a.id), s = a.folderId ? this._getOrGenerateId(a.folderId) : n ?? "";
2646
+ return {
2647
+ ...a,
2648
+ id: o,
2649
+ folderId: s
2650
+ };
2651
+ }
2652
+ async updateFile(t, n) {
2653
+ if (typeof this._inner.updateFile != "function") throw new e("Update file is not supported by inner storage", "UnsupportedOperation");
2654
+ let r = this.pathForId(t.id, !1), { ...i } = await this._inner.updateFile({
2655
+ ...t,
2656
+ id: r,
2657
+ folderId: this.pathForId(t.folderId ?? "", !0)
2658
+ }, n);
2659
+ return this._updateIdMapping(t.id, i.id), i.folderId && (i = {
2660
+ ...i,
2661
+ folderId: this._getOrGenerateId(i.folderId)
2662
+ }), {
2663
+ ...i,
2664
+ id: this._getOrGenerateId(i.id)
2665
+ };
2666
+ }
2667
+ async getFileData(t, n) {
2668
+ let r = this.pathForId(t.id, !1);
2669
+ if (typeof this._inner.getFileData == "function") return this._inner.getFileData({
2670
+ ...t,
2671
+ id: r,
2672
+ folderId: this.pathForId(t.folderId ?? "", !0)
2673
+ }, n);
2674
+ let i = t.url || (typeof this.getUrl == "function" ? await this.getUrl(t) : void 0);
2675
+ if (!i) throw new e("Cannot get file data: no url and no inner.getFileData", "FileReadError");
2676
+ return fetch(i, n?.forceReload ? { cache: "no-store" } : void 0).then((e) => e.blob());
2677
+ }
2678
+ async cleanTrash() {
2679
+ if (typeof this._inner.cleanTrash != "function") throw new e("Clean trash is not supported by inner storage", "UnsupportedOperation");
2680
+ return await this._inner.cleanTrash();
2681
+ }
2682
+ };
2683
+ //#endregion
2684
+ //#region packages/file-manager/src/utils/directResourceFetchError.ts
2685
+ function de(e) {
2686
+ return typeof e != "object" || !e ? !1 : "name" in e && e.name === "DirectResourceFetchError" ? !("final" in e) || e.final !== !1 : "final" in e && e.final === !0;
2687
+ }
2688
+ //#endregion
2689
+ //#region packages/file-manager/src/utils/remoteFileFetchPipeline.ts
2690
+ function $(t) {
2691
+ if (de(t) || t instanceof e) throw t;
2692
+ }
2693
+ async function fe(e, t, n, r, i) {
2694
+ let { fetchWithProxy: a, handleFileProxy: o, handleProxyUrl: s } = t, c = {
2695
+ mode: "cors",
2696
+ ...i?.fetchInit
2697
+ }, l;
2698
+ if (o) try {
2699
+ let t = await Promise.resolve(o(e));
2700
+ if (t) return r(t);
2701
+ } catch (e) {
2702
+ $(e), l = e, console.warn("handleFileProxy failed, falling back to next strategy", e);
2703
+ }
2704
+ if (s) try {
2705
+ let t = await Promise.resolve(s(e));
2706
+ if (t && t !== e) return r(await n(t));
2707
+ } catch (e) {
2708
+ $(e), l = e, console.warn("handleProxyUrl fetch failed, falling back to next strategy", e);
2709
+ }
2710
+ if (a) try {
2711
+ let t = await Promise.resolve(a(e, c, { intent: "downloadBlob" }));
2712
+ if (!t.ok) throw Error(`HTTP ${t.status}`);
2713
+ return r(await t.blob());
2714
+ } catch (e) {
2715
+ $(e), l = e, console.warn("fetchWithProxy failed", e);
2716
+ }
2717
+ if (!a) try {
2718
+ return r(await n(e));
2719
+ } catch (e) {
2720
+ $(e), l = e;
2721
+ }
2722
+ throw l instanceof Error ? l : /* @__PURE__ */ Error("Failed to download file from the URL");
2723
+ }
2724
+ //#endregion
2725
+ export { E as DEFAULT_FILE_MANAGER_CACHE_TTL_MS, re as DataSourceProxyBase, o as FailedRequestCache, e as FileManagerApiError, s as MutationRegistry, ue as PathAwareStorageAdapter, _ as TRASH_FOLDER_ID, R as TreeCache, L as ViewCacheManager, i as buildAssetsPath, l as compareFileSystemItems, b as createSystemFolderByPathResolver, m as defaultChildrenViewFilterMatch, p as defaultFileSystemItemComparator, h as defaultHasSortValue, N as defaultIsFolderNotFoundError, fe as fetchRemoteImageBlobByUrl, r as getAssetsBase, y as getTrashFolderLookupCandidates, d as hasComparableSortValue, n as joinUrls, t as mapHttpStatusToErrorCode, f as matchesChildrenViewFilter, v as normalizeSystemFolderPath, c as resolveSortParams, g as resolveViewStrategyOptions };
2726
+
2727
+ //# sourceMappingURL=core.esm.js.map