@embedpdf-editor/vue3-chapter-viewer 1.0.7-beta.7 → 1.0.7-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -68,10 +68,10 @@ function ke$1(e14) {
68
68
  function De$1(e14) {
69
69
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
70
70
  }
71
- function Ee$1(e14, t2) {
71
+ function Ue$1(e14, t2) {
72
72
  return `${e14}#s${t2}`;
73
73
  }
74
- function Ue$1(e14) {
74
+ function Ee$1(e14) {
75
75
  const t2 = (function(e15) {
76
76
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
77
77
  const t3 = e15.source;
@@ -87,7 +87,7 @@ function Ue$1(e14) {
87
87
  for (let i2 = 0; i2 < n9; i2++) {
88
88
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
89
89
  if (n10 >= s2) break;
90
- a2.push({ index: i2, url: (r2 == null ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Ee$1(e14.chapterId, i2) });
90
+ a2.push({ index: i2, url: (r2 == null ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Ue$1(e14.chapterId, i2) });
91
91
  }
92
92
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
93
93
  }
@@ -99,18 +99,18 @@ let Ne$1 = class Ne {
99
99
  return this.cb(e14, t2);
100
100
  }
101
101
  };
102
- const Ge$1 = "chapter-manager", Re$1 = { id: Ge$1, name: "Chapter Manager Plugin", version: "0.1.0", provides: ["chapter-manager"], requires: ["document-manager"], optional: [], defaultConfig: { manifest: { chapters: [] }, prefetchChapters: 1, maxConcurrentSegmentLoads: 1, unloadTimeoutMs: 6e4, autoActivateOnLoad: true } };
103
- function _e$1(e14) {
102
+ const ze$1 = "chapter-manager", Ge$1 = { id: ze$1, name: "Chapter Manager Plugin", version: "0.1.0", provides: ["chapter-manager"], requires: ["document-manager"], optional: [], defaultConfig: { manifest: { chapters: [] }, prefetchChapters: 1, maxConcurrentSegmentLoads: 1, unloadTimeoutMs: 6e4, autoActivateOnLoad: true } };
103
+ function Be$1(e14) {
104
104
  const [t2, s2] = e14.localPageRange;
105
105
  return s2 - t2 + 1;
106
106
  }
107
- function Be$1(e14) {
107
+ function _e$1(e14) {
108
108
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
109
109
  }
110
110
  function We$1(e14) {
111
111
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
112
112
  const t2 = e14.source;
113
- return Be$1(t2) ? t2.segmentPageThreshold : null;
113
+ return _e$1(t2) ? t2.segmentPageThreshold : null;
114
114
  }
115
115
  function qe$1(e14, t2) {
116
116
  return `${e14}#s${t2}`;
@@ -127,7 +127,7 @@ function Ke$1(e14, t2) {
127
127
  function je$1(e14) {
128
128
  const t2 = We$1(e14);
129
129
  if (!t2) return null;
130
- const s2 = _e$1(e14), n9 = Ke$1(s2, t2), r2 = Be$1(e14.source) ? e14.source.urls : void 0, a2 = [];
130
+ const s2 = Be$1(e14), n9 = Ke$1(s2, t2), r2 = _e$1(e14.source) ? e14.source.urls : void 0, a2 = [];
131
131
  for (let i2 = 0; i2 < n9; i2++) {
132
132
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
133
133
  if (n10 >= s2) break;
@@ -155,10 +155,10 @@ function Ye$1(e14) {
155
155
  var t2;
156
156
  const s2 = We$1(e14);
157
157
  if (!s2) return [];
158
- const n9 = [], { chapterId: r2 } = e14, a2 = _e$1(e14);
158
+ const n9 = [], { chapterId: r2 } = e14, a2 = Be$1(e14);
159
159
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
160
160
  const i2 = e14.source;
161
- if (!Be$1(i2)) return n9;
161
+ if (!_e$1(i2)) return n9;
162
162
  0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
163
163
  const o2 = Ke$1(a2, s2);
164
164
  i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
@@ -245,7 +245,7 @@ function tt$1(e14, t2 = Qe$1) {
245
245
  }
246
246
  const st$1 = class e extends BasePlugin {
247
247
  constructor(e14, t2) {
248
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Qe$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
248
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Qe$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
249
249
  }
250
250
  async initialize(e14) {
251
251
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Qe$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -380,7 +380,19 @@ const st$1 = class e extends BasePlugin {
380
380
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
381
381
  }
382
382
  clearVisibleSegmentBacklog() {
383
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
383
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
384
+ }
385
+ chapterNeedsResolvedUrls(e14) {
386
+ var t2;
387
+ const s2 = e14.source;
388
+ return !_e$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
389
+ }
390
+ ensureVisibleChapterUrls(e14) {
391
+ const { chapterId: t2 } = e14;
392
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
393
+ }).finally(() => {
394
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
395
+ })), false));
384
396
  }
385
397
  segmentStartGlobalIndex(e14, t2) {
386
398
  var s2;
@@ -401,11 +413,18 @@ const st$1 = class e extends BasePlugin {
401
413
  }
402
414
  pumpVisibleSegmentBacklog() {
403
415
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
416
+ let t2 = 0;
404
417
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
405
418
  const e15 = this.visibleSegmentBacklog.shift();
406
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
407
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
408
- })));
419
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
420
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
421
+ const s2 = this.findChapter(e15.chapterId);
422
+ if (s2) {
423
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
424
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
425
+ });
426
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
427
+ }
409
428
  }
410
429
  }
411
430
  prepareChapterForSegmentLoad(e14) {
@@ -529,7 +548,7 @@ const st$1 = class e extends BasePlugin {
529
548
  if (n9) return n9;
530
549
  const r2 = (async () => {
531
550
  const s3 = e14.source;
532
- if (Be$1(s3)) {
551
+ if (_e$1(s3)) {
533
552
  const e15 = [...s3.urls];
534
553
  return this.chapterUrlsCache.set(t2, e15), e15;
535
554
  }
@@ -557,7 +576,7 @@ const st$1 = class e extends BasePlugin {
557
576
  async resolvePdfPayload(t2, s2) {
558
577
  const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = We$1(t2);
559
578
  if (a2) {
560
- const n10 = Ke$1(_e$1(t2), a2);
579
+ const n10 = Ke$1(Be$1(t2), a2);
561
580
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
562
581
  const r3 = await this.resolveChapterUrls(t2);
563
582
  if (r3.length !== n10) {
@@ -568,7 +587,7 @@ const st$1 = class e extends BasePlugin {
568
587
  }
569
588
  if (0 !== s2) throw new Error(`Chapter "${t2.chapterId}" is not segmented`);
570
589
  if (n9) {
571
- if (Be$1(n9)) {
590
+ if (_e$1(n9)) {
572
591
  const e14 = await this.resolveChapterUrls(t2);
573
592
  return this.openPayloadFromUrl(t2, s2, e14);
574
593
  }
@@ -655,7 +674,7 @@ const st$1 = class e extends BasePlugin {
655
674
  };
656
675
  st$1.id = "chapter-manager";
657
676
  let nt$1 = st$1;
658
- const rt$1 = { manifest: Re$1, create: (e14) => new nt$1(Ge$1, e14), reducer: (e14) => e14, initialState: {} }, at$1 = "chapter-scroll", it$1 = { id: at$1, name: "Chapter Scroll Plugin", version: "0.1.0", provides: ["chapter-scroll"], requires: ["chapter-manager"], optional: [], defaultConfig: { placeholderPageHeight: 1200, placeholderPageWidth: 900, bufferSize: 2, pageGap: 20 } };
677
+ const rt$1 = { manifest: Ge$1, create: (e14) => new nt$1(ze$1, e14), reducer: (e14) => e14, initialState: {} }, at$1 = "chapter-scroll", it$1 = { id: at$1, name: "Chapter Scroll Plugin", version: "0.1.0", provides: ["chapter-scroll"], requires: ["chapter-manager"], optional: [], defaultConfig: { placeholderPageHeight: 1200, placeholderPageWidth: 900, bufferSize: 2, pageGap: 20 } };
659
678
  function ot$1(e14) {
660
679
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
661
680
  }
@@ -839,7 +858,7 @@ function Mt$1(e14, t2 = Ct$1) {
839
858
  }
840
859
  const xt$1 = class e2 extends BasePlugin {
841
860
  constructor(e14, t2) {
842
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Ct$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
861
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Ct$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
843
862
  }
844
863
  async initialize(e14) {
845
864
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Ct$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -974,7 +993,19 @@ const xt$1 = class e2 extends BasePlugin {
974
993
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
975
994
  }
976
995
  clearVisibleSegmentBacklog() {
977
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
996
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
997
+ }
998
+ chapterNeedsResolvedUrls(e14) {
999
+ var t2;
1000
+ const s2 = e14.source;
1001
+ return !pt$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
1002
+ }
1003
+ ensureVisibleChapterUrls(e14) {
1004
+ const { chapterId: t2 } = e14;
1005
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
1006
+ }).finally(() => {
1007
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
1008
+ })), false));
978
1009
  }
979
1010
  segmentStartGlobalIndex(e14, t2) {
980
1011
  var s2;
@@ -995,11 +1026,18 @@ const xt$1 = class e2 extends BasePlugin {
995
1026
  }
996
1027
  pumpVisibleSegmentBacklog() {
997
1028
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
1029
+ let t2 = 0;
998
1030
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
999
1031
  const e15 = this.visibleSegmentBacklog.shift();
1000
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1001
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1002
- })));
1032
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1033
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1034
+ const s2 = this.findChapter(e15.chapterId);
1035
+ if (s2) {
1036
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1037
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1038
+ });
1039
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1040
+ }
1003
1041
  }
1004
1042
  }
1005
1043
  prepareChapterForSegmentLoad(e14) {
@@ -1476,19 +1514,19 @@ const kt$1 = class kt extends BasePlugin {
1476
1514
  };
1477
1515
  kt$1.id = "chapter-scroll";
1478
1516
  let Dt$1 = kt$1;
1479
- const Et$1 = { manifest: it$1, create: (e14) => new Dt$1(at$1, e14), reducer: (e14) => e14, initialState: {} }, Ut$1 = "note", Tt$1 = { id: Ut$1, name: "Note Plugin", version: "0.1.0", provides: ["note"], requires: ["selection", "chapter-scroll", "chapter-manager"], optional: [], defaultConfig: { callbacks: { onCreateNote: async () => null, onActivateNote: () => {
1517
+ const Ut$1 = { manifest: it$1, create: (e14) => new Dt$1(at$1, e14), reducer: (e14) => e14, initialState: {} }, Et$1 = "note", Vt$1 = { id: Et$1, name: "Note Plugin", version: "0.1.0", provides: ["note"], requires: ["selection", "chapter-scroll", "chapter-manager"], optional: [], defaultConfig: { callbacks: { onCreateNote: async () => null, onActivateNote: () => {
1480
1518
  } }, markerSize: 18 } };
1481
1519
  function Nt$1(e14) {
1482
1520
  const [t2, s2] = e14.localPageRange;
1483
1521
  return s2 - t2 + 1;
1484
1522
  }
1485
- function Ft$1(e14) {
1523
+ function Tt$1(e14) {
1486
1524
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
1487
1525
  }
1488
- function Vt$1(e14) {
1526
+ function Ft$1(e14) {
1489
1527
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
1490
1528
  const t2 = e14.source;
1491
- return Ft$1(t2) ? t2.segmentPageThreshold : null;
1529
+ return Tt$1(t2) ? t2.segmentPageThreshold : null;
1492
1530
  }
1493
1531
  function At$1(e14, t2) {
1494
1532
  return `${e14}#s${t2}`;
@@ -1499,13 +1537,13 @@ function Ot$1(e14) {
1499
1537
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
1500
1538
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
1501
1539
  }
1502
- function zt$1(e14, t2) {
1540
+ function Rt$1(e14, t2) {
1503
1541
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
1504
1542
  }
1505
- function Gt$1(e14) {
1506
- const t2 = Vt$1(e14);
1543
+ function zt$1(e14) {
1544
+ const t2 = Ft$1(e14);
1507
1545
  if (!t2) return null;
1508
- const s2 = Nt$1(e14), n9 = zt$1(s2, t2), r2 = Ft$1(e14.source) ? e14.source.urls : void 0, a2 = [];
1546
+ const s2 = Nt$1(e14), n9 = Rt$1(s2, t2), r2 = Tt$1(e14.source) ? e14.source.urls : void 0, a2 = [];
1509
1547
  for (let i2 = 0; i2 < n9; i2++) {
1510
1548
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
1511
1549
  if (n10 >= s2) break;
@@ -1513,8 +1551,8 @@ function Gt$1(e14) {
1513
1551
  }
1514
1552
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
1515
1553
  }
1516
- function Rt$1(e14, t2) {
1517
- const s2 = Gt$1(e14);
1554
+ function Gt$1(e14, t2) {
1555
+ const s2 = zt$1(e14);
1518
1556
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
1519
1557
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
1520
1558
  if (!n9) {
@@ -1523,22 +1561,22 @@ function Rt$1(e14, t2) {
1523
1561
  }
1524
1562
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
1525
1563
  }
1526
- function _t$1(e14, t2) {
1527
- const s2 = Gt$1(e14);
1564
+ function Bt$1(e14, t2) {
1565
+ const s2 = zt$1(e14);
1528
1566
  if (!s2) return 0;
1529
1567
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
1530
1568
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
1531
1569
  }
1532
- function Bt$1(e14) {
1570
+ function _t$1(e14) {
1533
1571
  var t2;
1534
- const s2 = Vt$1(e14);
1572
+ const s2 = Ft$1(e14);
1535
1573
  if (!s2) return [];
1536
1574
  const n9 = [], { chapterId: r2 } = e14, a2 = Nt$1(e14);
1537
1575
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
1538
1576
  const i2 = e14.source;
1539
- if (!Ft$1(i2)) return n9;
1577
+ if (!Tt$1(i2)) return n9;
1540
1578
  0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
1541
- const o2 = zt$1(a2, s2);
1579
+ const o2 = Rt$1(a2, s2);
1542
1580
  i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
1543
1581
  for (let l = 0; l < i2.urls.length; l++) (null == (t2 = i2.urls[l]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${l}] is empty`);
1544
1582
  return n9;
@@ -1623,7 +1661,7 @@ function Kt$1(e14, t2 = Wt$1) {
1623
1661
  }
1624
1662
  const jt$1 = class e3 extends BasePlugin {
1625
1663
  constructor(e14, t2) {
1626
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Wt$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
1664
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Wt$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
1627
1665
  }
1628
1666
  async initialize(e14) {
1629
1667
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Wt$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -1645,10 +1683,10 @@ const jt$1 = class e3 extends BasePlugin {
1645
1683
  t2 && (this.overlapStrategy = t2), this.setManifestInternal(e14);
1646
1684
  }, getManifest: () => this.manifest, getVirtualPageMap: () => this.virtualPageMap, setVisibleGlobalPages: (e14) => this.handleVisibleChange(e14), visibleRangeNeedsSegmentLoad: (e14) => this.visibleRangeNeedsSegmentLoad(e14), shouldShowChapterLoadError: (e14, t2) => this.shouldShowChapterLoadError(e14, t2), ensureChapterLoaded: (e14) => this.ensureChapterLoaded(e14), ensureSegmentLoaded: (e14, t2) => this.ensureSegmentLoaded(e14, t2), ensureAllSegmentsLoaded: (e14) => this.ensureAllSegmentsLoaded(e14), resolvePageDocument: (e14, t2) => {
1647
1685
  const s2 = this.findChapter(e14);
1648
- return s2 ? Rt$1(s2, t2) : null;
1686
+ return s2 ? Gt$1(s2, t2) : null;
1649
1687
  }, getSegmentPlan: (e14) => {
1650
1688
  const t2 = this.findChapter(e14);
1651
- return t2 ? Gt$1(t2) : null;
1689
+ return t2 ? zt$1(t2) : null;
1652
1690
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
1653
1691
  }
1654
1692
  destroy() {
@@ -1680,7 +1718,7 @@ const jt$1 = class e3 extends BasePlugin {
1680
1718
  s3.has(n10.chapterId) && t3.push(`Duplicate chapterId: ${n10.chapterId}`), s3.add(n10.chapterId);
1681
1719
  const [e15, r3] = n10.globalPageRange, [a3, i2] = n10.localPageRange;
1682
1720
  if (e15 > r3 && t3.push(`${n10.chapterId}: invalid globalPageRange [${e15},${r3}]`), a3 > i2 && t3.push(`${n10.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e15 !== i2 - a3 && t3.push(`${n10.chapterId}: page-count mismatch (global ${r3 - e15 + 1} vs local ${i2 - a3 + 1})`), n10.ownedGlobalPages) for (const s4 of n10.ownedGlobalPages) (s4 < e15 || s4 > r3) && t3.push(`${n10.chapterId}: ownedGlobalPages contains ${s4} outside range`);
1683
- t3.push(...Bt$1(n10));
1721
+ t3.push(..._t$1(n10));
1684
1722
  }
1685
1723
  return t3;
1686
1724
  })(s2);
@@ -1705,7 +1743,7 @@ const jt$1 = class e3 extends BasePlugin {
1705
1743
  }
1706
1744
  getSegmentPlanForChapter(e14) {
1707
1745
  const t2 = this.findChapter(e14);
1708
- return t2 ? Gt$1(t2) : null;
1746
+ return t2 ? zt$1(t2) : null;
1709
1747
  }
1710
1748
  anySegmentOpen(e14) {
1711
1749
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -1749,7 +1787,7 @@ const jt$1 = class e3 extends BasePlugin {
1749
1787
  if (!e15) continue;
1750
1788
  const s2 = this.findChapter(e15.chapterId);
1751
1789
  if (!s2) continue;
1752
- const n9 = _t$1(s2, e15.localPageIndex);
1790
+ const n9 = Bt$1(s2, e15.localPageIndex);
1753
1791
  if (!this.isSegmentDocumentOpen(e15.chapterId, n9)) return true;
1754
1792
  }
1755
1793
  return false;
@@ -1758,14 +1796,26 @@ const jt$1 = class e3 extends BasePlugin {
1758
1796
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
1759
1797
  }
1760
1798
  clearVisibleSegmentBacklog() {
1761
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
1799
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
1800
+ }
1801
+ chapterNeedsResolvedUrls(e14) {
1802
+ var t2;
1803
+ const s2 = e14.source;
1804
+ return !Tt$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
1805
+ }
1806
+ ensureVisibleChapterUrls(e14) {
1807
+ const { chapterId: t2 } = e14;
1808
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
1809
+ }).finally(() => {
1810
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
1811
+ })), false));
1762
1812
  }
1763
1813
  segmentStartGlobalIndex(e14, t2) {
1764
1814
  var s2;
1765
1815
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
1766
1816
  const n9 = this.findChapter(e14);
1767
1817
  if (!n9) return Number.MAX_SAFE_INTEGER;
1768
- const r2 = Gt$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
1818
+ const r2 = zt$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
1769
1819
  if (i2) return i2.globalPageIndex;
1770
1820
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
1771
1821
  }
@@ -1779,11 +1829,18 @@ const jt$1 = class e3 extends BasePlugin {
1779
1829
  }
1780
1830
  pumpVisibleSegmentBacklog() {
1781
1831
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
1832
+ let t2 = 0;
1782
1833
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
1783
1834
  const e15 = this.visibleSegmentBacklog.shift();
1784
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1785
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1786
- })));
1835
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1836
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1837
+ const s2 = this.findChapter(e15.chapterId);
1838
+ if (s2) {
1839
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1840
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1841
+ });
1842
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1843
+ }
1787
1844
  }
1788
1845
  }
1789
1846
  prepareChapterForSegmentLoad(e14) {
@@ -1800,7 +1857,7 @@ const jt$1 = class e3 extends BasePlugin {
1800
1857
  if (t3 < 0) return;
1801
1858
  const s3 = this.findChapter(e15);
1802
1859
  if (!s3) return;
1803
- const n10 = Gt$1(s3);
1860
+ const n10 = zt$1(s3);
1804
1861
  if (n10 && t3 >= n10.segments.length) return;
1805
1862
  const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
1806
1863
  (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
@@ -1811,7 +1868,7 @@ const jt$1 = class e3 extends BasePlugin {
1811
1868
  a2.add(e15.chapterId);
1812
1869
  const t3 = this.findChapter(e15.chapterId);
1813
1870
  if (!t3) continue;
1814
- const s3 = _t$1(t3, e15.localPageIndex);
1871
+ const s3 = Bt$1(t3, e15.localPageIndex);
1815
1872
  o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
1816
1873
  });
1817
1874
  }
@@ -1820,7 +1877,7 @@ const jt$1 = class e3 extends BasePlugin {
1820
1877
  if (l) {
1821
1878
  const e15 = this.findChapter(l.chapterId);
1822
1879
  if (e15) {
1823
- const t3 = _t$1(e15, l.localPageIndex);
1880
+ const t3 = Bt$1(e15, l.localPageIndex);
1824
1881
  o2(l.chapterId, t3 + 1);
1825
1882
  }
1826
1883
  }
@@ -1832,7 +1889,7 @@ const jt$1 = class e3 extends BasePlugin {
1832
1889
  if (this.scheduleVisibleSegmentLoads(h2), l) {
1833
1890
  const e15 = this.findChapter(l.chapterId);
1834
1891
  if (e15) {
1835
- const t3 = _t$1(e15, l.localPageIndex);
1892
+ const t3 = Bt$1(e15, l.localPageIndex);
1836
1893
  this.isSegmentDocumentOpen(l.chapterId, t3) || (this.prepareChapterForSegmentLoad(l.chapterId), this.loadSegmentNow(l.chapterId, t3).finally(() => {
1837
1894
  this.pumpVisibleSegmentBacklog();
1838
1895
  }));
@@ -1841,7 +1898,7 @@ const jt$1 = class e3 extends BasePlugin {
1841
1898
  if (this.config.autoActivateOnLoad ?? 1) {
1842
1899
  const e15 = this.virtualPageMap.atIndex(r2);
1843
1900
  if (e15) {
1844
- const t3 = Rt$1(this.findChapter(e15.chapterId), e15.localPageIndex);
1901
+ const t3 = Gt$1(this.findChapter(e15.chapterId), e15.localPageIndex);
1845
1902
  if (this.documentManager.provides().isDocumentOpen(t3.documentId)) {
1846
1903
  this.documentManager.provides().getActiveDocumentId() !== t3.documentId && this.documentManager.provides().setActiveDocument(t3.documentId);
1847
1904
  }
@@ -1877,7 +1934,7 @@ const jt$1 = class e3 extends BasePlugin {
1877
1934
  this.prepareChapterForSegmentLoad(e14);
1878
1935
  const s2 = this.findChapter(e14);
1879
1936
  if (!s2) return Promise.resolve("error");
1880
- const n9 = Gt$1(s2);
1937
+ const n9 = zt$1(s2);
1881
1938
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
1882
1939
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
1883
1940
  const r2 = n9.segments[t2], a2 = r2.documentId;
@@ -1907,7 +1964,7 @@ const jt$1 = class e3 extends BasePlugin {
1907
1964
  if (n9) return n9;
1908
1965
  const r2 = (async () => {
1909
1966
  const s3 = e14.source;
1910
- if (Ft$1(s3)) {
1967
+ if (Tt$1(s3)) {
1911
1968
  const e15 = [...s3.urls];
1912
1969
  return this.chapterUrlsCache.set(t2, e15), e15;
1913
1970
  }
@@ -1933,9 +1990,9 @@ const jt$1 = class e3 extends BasePlugin {
1933
1990
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
1934
1991
  }
1935
1992
  async resolvePdfPayload(t2, s2) {
1936
- const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Vt$1(t2);
1993
+ const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Ft$1(t2);
1937
1994
  if (a2) {
1938
- const n10 = zt$1(Nt$1(t2), a2);
1995
+ const n10 = Rt$1(Nt$1(t2), a2);
1939
1996
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
1940
1997
  const r3 = await this.resolveChapterUrls(t2);
1941
1998
  if (r3.length !== n10) {
@@ -1946,7 +2003,7 @@ const jt$1 = class e3 extends BasePlugin {
1946
2003
  }
1947
2004
  if (0 !== s2) throw new Error(`Chapter "${t2.chapterId}" is not segmented`);
1948
2005
  if (n9) {
1949
- if (Ft$1(n9)) {
2006
+ if (Tt$1(n9)) {
1950
2007
  const e14 = await this.resolveChapterUrls(t2);
1951
2008
  return this.openPayloadFromUrl(t2, s2, e14);
1952
2009
  }
@@ -2015,7 +2072,7 @@ const jt$1 = class e3 extends BasePlugin {
2015
2072
  });
2016
2073
  }
2017
2074
  closeChapter(e14) {
2018
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Gt$1(t2) : null, r2 = (e15) => {
2075
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? zt$1(t2) : null, r2 = (e15) => {
2019
2076
  if (s2.isDocumentOpen(e15)) {
2020
2077
  s2.closeDocument(e15).wait(() => {
2021
2078
  }, () => {
@@ -2216,7 +2273,7 @@ function gs(e14, t2 = cs$1) {
2216
2273
  }
2217
2274
  const ms = class e4 extends BasePlugin {
2218
2275
  constructor(e14, t2) {
2219
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = cs$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
2276
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = cs$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
2220
2277
  }
2221
2278
  async initialize(e14) {
2222
2279
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? cs$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -2351,7 +2408,19 @@ const ms = class e4 extends BasePlugin {
2351
2408
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
2352
2409
  }
2353
2410
  clearVisibleSegmentBacklog() {
2354
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
2411
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
2412
+ }
2413
+ chapterNeedsResolvedUrls(e14) {
2414
+ var t2;
2415
+ const s2 = e14.source;
2416
+ return !ss$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
2417
+ }
2418
+ ensureVisibleChapterUrls(e14) {
2419
+ const { chapterId: t2 } = e14;
2420
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
2421
+ }).finally(() => {
2422
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
2423
+ })), false));
2355
2424
  }
2356
2425
  segmentStartGlobalIndex(e14, t2) {
2357
2426
  var s2;
@@ -2372,11 +2441,18 @@ const ms = class e4 extends BasePlugin {
2372
2441
  }
2373
2442
  pumpVisibleSegmentBacklog() {
2374
2443
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
2444
+ let t2 = 0;
2375
2445
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
2376
2446
  const e15 = this.visibleSegmentBacklog.shift();
2377
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
2378
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
2379
- })));
2447
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
2448
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
2449
+ const s2 = this.findChapter(e15.chapterId);
2450
+ if (s2) {
2451
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
2452
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
2453
+ });
2454
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
2455
+ }
2380
2456
  }
2381
2457
  }
2382
2458
  prepareChapterForSegmentLoad(e14) {
@@ -2967,7 +3043,7 @@ function Cs(e14) {
2967
3043
  const t2 = e14[e14.length - 1];
2968
3044
  return { x: t2.origin.x + t2.size.width, y: t2.origin.y + t2.size.height };
2969
3045
  }
2970
- const ys = { manifest: Tt$1, create: (e14) => new Ss(Ut$1, e14), reducer: (e14) => e14, initialState: {} }, ws = "paragraph-bookmark", Ms = { id: ws, name: "Paragraph Bookmark Plugin", version: "0.1.0", provides: ["paragraph-bookmark"], requires: ["chapter-scroll", "chapter-manager"], optional: [], defaultConfig: {} };
3046
+ const ys = { manifest: Vt$1, create: (e14) => new Ss(Et$1, e14), reducer: (e14) => e14, initialState: {} }, ws = "paragraph-bookmark", Ms = { id: ws, name: "Paragraph Bookmark Plugin", version: "0.1.0", provides: ["paragraph-bookmark"], requires: ["chapter-scroll", "chapter-manager"], optional: [], defaultConfig: {} };
2971
3047
  function xs(e14) {
2972
3048
  const [t2, s2] = e14.localPageRange;
2973
3049
  return s2 - t2 + 1;
@@ -2980,31 +3056,31 @@ function $s$1(e14) {
2980
3056
  const t2 = e14.source;
2981
3057
  return Ls(t2) ? t2.segmentPageThreshold : null;
2982
3058
  }
2983
- function ks$1(e14, t2) {
3059
+ function ks(e14, t2) {
2984
3060
  return `${e14}#s${t2}`;
2985
3061
  }
2986
- function Ds(e14) {
3062
+ function Ds$1(e14) {
2987
3063
  const t2 = e14.lastIndexOf("#s");
2988
3064
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
2989
3065
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
2990
3066
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
2991
3067
  }
2992
- function Es$1(e14, t2) {
3068
+ function Us$1(e14, t2) {
2993
3069
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
2994
3070
  }
2995
- function Us$1(e14) {
3071
+ function Es$1(e14) {
2996
3072
  const t2 = $s$1(e14);
2997
3073
  if (!t2) return null;
2998
- const s2 = xs(e14), n9 = Es$1(s2, t2), r2 = Ls(e14.source) ? e14.source.urls : void 0, a2 = [];
3074
+ const s2 = xs(e14), n9 = Us$1(s2, t2), r2 = Ls(e14.source) ? e14.source.urls : void 0, a2 = [];
2999
3075
  for (let i2 = 0; i2 < n9; i2++) {
3000
3076
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
3001
3077
  if (n10 >= s2) break;
3002
- a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: ks$1(e14.chapterId, i2) });
3078
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: ks(e14.chapterId, i2) });
3003
3079
  }
3004
3080
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
3005
3081
  }
3006
- function Ts$1(e14, t2) {
3007
- const s2 = Us$1(e14);
3082
+ function Vs$1(e14, t2) {
3083
+ const s2 = Es$1(e14);
3008
3084
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
3009
3085
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
3010
3086
  if (!n9) {
@@ -3014,12 +3090,12 @@ function Ts$1(e14, t2) {
3014
3090
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
3015
3091
  }
3016
3092
  function Ns$1(e14, t2) {
3017
- const s2 = Us$1(e14);
3093
+ const s2 = Es$1(e14);
3018
3094
  if (!s2) return 0;
3019
3095
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
3020
3096
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
3021
3097
  }
3022
- function Fs$1(e14) {
3098
+ function Ts$1(e14) {
3023
3099
  var t2;
3024
3100
  const s2 = $s$1(e14);
3025
3101
  if (!s2) return [];
@@ -3028,12 +3104,12 @@ function Fs$1(e14) {
3028
3104
  const i2 = e14.source;
3029
3105
  if (!Ls(i2)) return n9;
3030
3106
  0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
3031
- const o2 = Es$1(a2, s2);
3107
+ const o2 = Us$1(a2, s2);
3032
3108
  i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
3033
3109
  for (let l = 0; l < i2.urls.length; l++) (null == (t2 = i2.urls[l]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${l}] is empty`);
3034
3110
  return n9;
3035
3111
  }
3036
- const Vs$1 = { kind: "first-wins" };
3112
+ const Fs$1 = { kind: "first-wins" };
3037
3113
  function As$1(e14, t2, s2) {
3038
3114
  if (1 === t2.length) return t2[0].chapterId;
3039
3115
  switch (s2.kind) {
@@ -3086,8 +3162,8 @@ let Os$1 = class Os {
3086
3162
  return s2 ? this._pages.indexOf(s2) : -1;
3087
3163
  }
3088
3164
  };
3089
- function zs$1(e14, t2 = Vs$1) {
3090
- const s2 = (function(e15, t3 = Vs$1) {
3165
+ function Rs$1(e14, t2 = Fs$1) {
3166
+ const s2 = (function(e15, t3 = Fs$1) {
3091
3167
  const s3 = /* @__PURE__ */ new Map();
3092
3168
  for (const r3 of e15.chapters) {
3093
3169
  const [e16, t4] = r3.globalPageRange;
@@ -3111,19 +3187,19 @@ function zs$1(e14, t2 = Vs$1) {
3111
3187
  }
3112
3188
  return new Os$1(a2);
3113
3189
  }
3114
- const Gs$1 = class e6 extends BasePlugin {
3190
+ const zs$1 = class e6 extends BasePlugin {
3115
3191
  constructor(e14, t2) {
3116
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Vs$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3192
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Fs$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3117
3193
  }
3118
3194
  async initialize(e14) {
3119
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Vs$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
3120
- const { chapterId: t2 } = Ds(e15.id);
3195
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Fs$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
3196
+ const { chapterId: t2 } = Ds$1(e15.id);
3121
3197
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
3122
3198
  }), this.documentManager.provides().onDocumentClosed((e15) => {
3123
- const { chapterId: t2 } = Ds(e15);
3199
+ const { chapterId: t2 } = Ds$1(e15);
3124
3200
  this.findChapter(t2) && this.syncChapterStatusFromDocuments(t2);
3125
3201
  }), this.documentManager.provides().onDocumentError((e15) => {
3126
- const { chapterId: t2 } = Ds(e15.documentId);
3202
+ const { chapterId: t2 } = Ds$1(e15.documentId);
3127
3203
  this.findChapter(t2) && this.handleDocumentError(e15.documentId, t2, e15.code, e15.message);
3128
3204
  })), this.setManifestInternal(e14.manifest), e14.unloadTimeoutMs && e14.unloadTimeoutMs > 0) {
3129
3205
  const t2 = Math.max(5e3, Math.floor(e14.unloadTimeoutMs / 4));
@@ -3135,10 +3211,10 @@ const Gs$1 = class e6 extends BasePlugin {
3135
3211
  t2 && (this.overlapStrategy = t2), this.setManifestInternal(e14);
3136
3212
  }, getManifest: () => this.manifest, getVirtualPageMap: () => this.virtualPageMap, setVisibleGlobalPages: (e14) => this.handleVisibleChange(e14), visibleRangeNeedsSegmentLoad: (e14) => this.visibleRangeNeedsSegmentLoad(e14), shouldShowChapterLoadError: (e14, t2) => this.shouldShowChapterLoadError(e14, t2), ensureChapterLoaded: (e14) => this.ensureChapterLoaded(e14), ensureSegmentLoaded: (e14, t2) => this.ensureSegmentLoaded(e14, t2), ensureAllSegmentsLoaded: (e14) => this.ensureAllSegmentsLoaded(e14), resolvePageDocument: (e14, t2) => {
3137
3213
  const s2 = this.findChapter(e14);
3138
- return s2 ? Ts$1(s2, t2) : null;
3214
+ return s2 ? Vs$1(s2, t2) : null;
3139
3215
  }, getSegmentPlan: (e14) => {
3140
3216
  const t2 = this.findChapter(e14);
3141
- return t2 ? Us$1(t2) : null;
3217
+ return t2 ? Es$1(t2) : null;
3142
3218
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
3143
3219
  }
3144
3220
  destroy() {
@@ -3170,7 +3246,7 @@ const Gs$1 = class e6 extends BasePlugin {
3170
3246
  s3.has(n10.chapterId) && t3.push(`Duplicate chapterId: ${n10.chapterId}`), s3.add(n10.chapterId);
3171
3247
  const [e15, r3] = n10.globalPageRange, [a3, i2] = n10.localPageRange;
3172
3248
  if (e15 > r3 && t3.push(`${n10.chapterId}: invalid globalPageRange [${e15},${r3}]`), a3 > i2 && t3.push(`${n10.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e15 !== i2 - a3 && t3.push(`${n10.chapterId}: page-count mismatch (global ${r3 - e15 + 1} vs local ${i2 - a3 + 1})`), n10.ownedGlobalPages) for (const s4 of n10.ownedGlobalPages) (s4 < e15 || s4 > r3) && t3.push(`${n10.chapterId}: ownedGlobalPages contains ${s4} outside range`);
3173
- t3.push(...Fs$1(n10));
3249
+ t3.push(...Ts$1(n10));
3174
3250
  }
3175
3251
  return t3;
3176
3252
  })(s2);
@@ -3178,7 +3254,7 @@ const Gs$1 = class e6 extends BasePlugin {
3178
3254
  const r2 = new Set(this.chapterStatus.keys()), a2 = new Set(s2.chapters.map((e14) => e14.chapterId));
3179
3255
  for (const e14 of r2) a2.has(e14) || (this.closeChapter(e14), this.chapterStatus.delete(e14), this.chapterErrorMessages.delete(e14), this.chapterUrlsCache.delete(e14), this.pendingChapterUrlsPromises.delete(e14));
3180
3256
  for (const e14 of s2.chapters) this.chapterStatus.has(e14.chapterId) || this.chapterStatus.set(e14.chapterId, "idle");
3181
- this.manifest = s2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = zs$1(s2, this.overlapStrategy), this.manifestChange$.emit({ manifest: s2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
3257
+ this.manifest = s2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = Rs$1(s2, this.overlapStrategy), this.manifestChange$.emit({ manifest: s2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
3182
3258
  }
3183
3259
  eagerPrefetchFromManifest() {
3184
3260
  const e14 = this.config.prefetchChapters ?? 0;
@@ -3195,7 +3271,7 @@ const Gs$1 = class e6 extends BasePlugin {
3195
3271
  }
3196
3272
  getSegmentPlanForChapter(e14) {
3197
3273
  const t2 = this.findChapter(e14);
3198
- return t2 ? Us$1(t2) : null;
3274
+ return t2 ? Es$1(t2) : null;
3199
3275
  }
3200
3276
  anySegmentOpen(e14) {
3201
3277
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -3248,14 +3324,26 @@ const Gs$1 = class e6 extends BasePlugin {
3248
3324
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
3249
3325
  }
3250
3326
  clearVisibleSegmentBacklog() {
3251
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
3327
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
3328
+ }
3329
+ chapterNeedsResolvedUrls(e14) {
3330
+ var t2;
3331
+ const s2 = e14.source;
3332
+ return !Ls(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
3333
+ }
3334
+ ensureVisibleChapterUrls(e14) {
3335
+ const { chapterId: t2 } = e14;
3336
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
3337
+ }).finally(() => {
3338
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
3339
+ })), false));
3252
3340
  }
3253
3341
  segmentStartGlobalIndex(e14, t2) {
3254
3342
  var s2;
3255
3343
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
3256
3344
  const n9 = this.findChapter(e14);
3257
3345
  if (!n9) return Number.MAX_SAFE_INTEGER;
3258
- const r2 = Us$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
3346
+ const r2 = Es$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
3259
3347
  if (i2) return i2.globalPageIndex;
3260
3348
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
3261
3349
  }
@@ -3269,11 +3357,18 @@ const Gs$1 = class e6 extends BasePlugin {
3269
3357
  }
3270
3358
  pumpVisibleSegmentBacklog() {
3271
3359
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
3360
+ let t2 = 0;
3272
3361
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
3273
3362
  const e15 = this.visibleSegmentBacklog.shift();
3274
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3275
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3276
- })));
3363
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3364
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3365
+ const s2 = this.findChapter(e15.chapterId);
3366
+ if (s2) {
3367
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3368
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3369
+ });
3370
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3371
+ }
3277
3372
  }
3278
3373
  }
3279
3374
  prepareChapterForSegmentLoad(e14) {
@@ -3290,7 +3385,7 @@ const Gs$1 = class e6 extends BasePlugin {
3290
3385
  if (t3 < 0) return;
3291
3386
  const s3 = this.findChapter(e15);
3292
3387
  if (!s3) return;
3293
- const n10 = Us$1(s3);
3388
+ const n10 = Es$1(s3);
3294
3389
  if (n10 && t3 >= n10.segments.length) return;
3295
3390
  const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
3296
3391
  (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
@@ -3331,7 +3426,7 @@ const Gs$1 = class e6 extends BasePlugin {
3331
3426
  if (this.config.autoActivateOnLoad ?? 1) {
3332
3427
  const e15 = this.virtualPageMap.atIndex(r2);
3333
3428
  if (e15) {
3334
- const t3 = Ts$1(this.findChapter(e15.chapterId), e15.localPageIndex);
3429
+ const t3 = Vs$1(this.findChapter(e15.chapterId), e15.localPageIndex);
3335
3430
  if (this.documentManager.provides().isDocumentOpen(t3.documentId)) {
3336
3431
  this.documentManager.provides().getActiveDocumentId() !== t3.documentId && this.documentManager.provides().setActiveDocument(t3.documentId);
3337
3432
  }
@@ -3367,7 +3462,7 @@ const Gs$1 = class e6 extends BasePlugin {
3367
3462
  this.prepareChapterForSegmentLoad(e14);
3368
3463
  const s2 = this.findChapter(e14);
3369
3464
  if (!s2) return Promise.resolve("error");
3370
- const n9 = Us$1(s2);
3465
+ const n9 = Es$1(s2);
3371
3466
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
3372
3467
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
3373
3468
  const r2 = n9.segments[t2], a2 = r2.documentId;
@@ -3425,7 +3520,7 @@ const Gs$1 = class e6 extends BasePlugin {
3425
3520
  async resolvePdfPayload(t2, s2) {
3426
3521
  const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = $s$1(t2);
3427
3522
  if (a2) {
3428
- const n10 = Es$1(xs(t2), a2);
3523
+ const n10 = Us$1(xs(t2), a2);
3429
3524
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
3430
3525
  const r3 = await this.resolveChapterUrls(t2);
3431
3526
  if (r3.length !== n10) {
@@ -3484,7 +3579,7 @@ const Gs$1 = class e6 extends BasePlugin {
3484
3579
  async handleDocumentError(t2, s2, n9, r2) {
3485
3580
  if (this.pendingSegmentLoadPromises.has(t2)) return;
3486
3581
  if (n9 !== PdfErrorCode.Password) {
3487
- const e14 = this.getSegmentPlanForChapter(s2), { segmentIndex: n10 } = Ds(t2);
3582
+ const e14 = this.getSegmentPlanForChapter(s2), { segmentIndex: n10 } = Ds$1(t2);
3488
3583
  return void (e14 && 0 !== n10 && null !== n10 || this.updateStatus(s2, "error"));
3489
3584
  }
3490
3585
  if (!this.passwordProvider) return void this.updateStatus(s2, "password-required");
@@ -3505,7 +3600,7 @@ const Gs$1 = class e6 extends BasePlugin {
3505
3600
  });
3506
3601
  }
3507
3602
  closeChapter(e14) {
3508
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Us$1(t2) : null, r2 = (e15) => {
3603
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Es$1(t2) : null, r2 = (e15) => {
3509
3604
  if (s2.isDocumentOpen(e15)) {
3510
3605
  s2.closeDocument(e15).wait(() => {
3511
3606
  }, () => {
@@ -3521,12 +3616,12 @@ const Gs$1 = class e6 extends BasePlugin {
3521
3616
  "error" === t2 && s2 ? this.chapterErrorMessages.set(e14, s2) : "loading" !== t2 && "loaded" !== t2 || this.chapterErrorMessages.delete(e14), n9 !== t2 && (this.chapterStatus.set(e14, t2), this.statusChange$.emit({ chapterId: e14, status: t2, ..."error" === t2 && s2 ? { error: { message: s2 } } : {} }));
3522
3617
  }
3523
3618
  };
3524
- Gs$1.id = "chapter-manager";
3525
- let Rs$1 = Gs$1;
3526
- function _s$1(e14) {
3619
+ zs$1.id = "chapter-manager";
3620
+ let Gs$1 = zs$1;
3621
+ function Bs$1(e14) {
3527
3622
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
3528
3623
  }
3529
- function Bs$1(e14, t2) {
3624
+ function _s$1(e14, t2) {
3530
3625
  return `${e14}#s${t2}`;
3531
3626
  }
3532
3627
  function Ws$1(e14) {
@@ -3539,7 +3634,7 @@ function qs$1(e14) {
3539
3634
  const t2 = (function(e15) {
3540
3635
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
3541
3636
  const t3 = e15.source;
3542
- return _s$1(t3) ? t3.segmentPageThreshold : null;
3637
+ return Bs$1(t3) ? t3.segmentPageThreshold : null;
3543
3638
  })(e14);
3544
3639
  if (!t2) return null;
3545
3640
  const s2 = (function(e15) {
@@ -3547,11 +3642,11 @@ function qs$1(e14) {
3547
3642
  return s3 - t3 + 1;
3548
3643
  })(e14), n9 = (function(e15, t3) {
3549
3644
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
3550
- })(s2, t2), r2 = _s$1(e14.source) ? e14.source.urls : void 0, a2 = [];
3645
+ })(s2, t2), r2 = Bs$1(e14.source) ? e14.source.urls : void 0, a2 = [];
3551
3646
  for (let i2 = 0; i2 < n9; i2++) {
3552
3647
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
3553
3648
  if (n10 >= s2) break;
3554
- a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Bs$1(e14.chapterId, i2) });
3649
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: _s$1(e14.chapterId, i2) });
3555
3650
  }
3556
3651
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
3557
3652
  }
@@ -3706,7 +3801,7 @@ function on(e14, t2 = nn) {
3706
3801
  }
3707
3802
  const ln = class e7 extends BasePlugin {
3708
3803
  constructor(e14, t2) {
3709
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = nn, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3804
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = nn, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3710
3805
  }
3711
3806
  async initialize(e14) {
3712
3807
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? nn, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -3841,7 +3936,19 @@ const ln = class e7 extends BasePlugin {
3841
3936
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
3842
3937
  }
3843
3938
  clearVisibleSegmentBacklog() {
3844
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
3939
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
3940
+ }
3941
+ chapterNeedsResolvedUrls(e14) {
3942
+ var t2;
3943
+ const s2 = e14.source;
3944
+ return !js$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
3945
+ }
3946
+ ensureVisibleChapterUrls(e14) {
3947
+ const { chapterId: t2 } = e14;
3948
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
3949
+ }).finally(() => {
3950
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
3951
+ })), false));
3845
3952
  }
3846
3953
  segmentStartGlobalIndex(e14, t2) {
3847
3954
  var s2;
@@ -3862,11 +3969,18 @@ const ln = class e7 extends BasePlugin {
3862
3969
  }
3863
3970
  pumpVisibleSegmentBacklog() {
3864
3971
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
3972
+ let t2 = 0;
3865
3973
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
3866
3974
  const e15 = this.visibleSegmentBacklog.shift();
3867
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3868
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3869
- })));
3975
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3976
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3977
+ const s2 = this.findChapter(e15.chapterId);
3978
+ if (s2) {
3979
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3980
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3981
+ });
3982
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3983
+ }
3870
3984
  }
3871
3985
  }
3872
3986
  prepareChapterForSegmentLoad(e14) {
@@ -4390,7 +4504,7 @@ async function Pn(e14, t2, s2) {
4390
4504
  }
4391
4505
  const In = class e8 extends BasePlugin {
4392
4506
  constructor(e14, t2) {
4393
- super(e14, t2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(un.id), this.chapterManager = this.registry.getPlugin(Rs$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4507
+ super(e14, t2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(un.id), this.chapterManager = this.registry.getPlugin(Gs$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4394
4508
  }
4395
4509
  async initialize(t2) {
4396
4510
  var s2;
@@ -4551,7 +4665,7 @@ function Ln(e14) {
4551
4665
  return { id: "chapter-editor-main-toolbar", position: { placement: "top", slot: "main", order: 0 }, permanent: true, items: [{ type: "spacer", id: "main-spacer-left", flex: true }, { type: "group", id: "mode-group", alignment: "start", gap: 2, items: [...e16.has("view") ? [t4("view", "View", "mode:view")] : [], ...e16.has("annotate") ? [t4("annotate", "Annotate", "mode:annotate")] : [], ...e16.has("shapes") ? [t4("shapes", "Shapes", "mode:shapes")] : [], ...e16.has("insert") ? [t4("insert", "Insert", "mode:insert")] : [], ...e16.has("form") ? [t4("form", "Form", "mode:form")] : [], ...e16.has("redact") ? [t4("redact", "Redact", "mode:redact")] : []] }, { type: "spacer", id: "main-spacer-right", flex: true }] };
4552
4666
  })(o3);
4553
4667
  return c2 && (d2["chapter-editor-main-toolbar"] = c2), (null == (s3 = e15.extend) ? void 0 : s3.toolbars) && Object.assign(d2, e15.extend.toolbars), { schema: { id: "chapter-pdf-editor-ui", version: "1.0.0", toolbars: d2, menus: (null == (n10 = e15.extend) ? void 0 : n10.menus) ?? {}, sidebars: (null == (r3 = e15.extend) ? void 0 : r3.sidebars) ?? {}, modals: (null == (a3 = e15.extend) ? void 0 : a3.modals) ?? {}, selectionMenus: (null == (i3 = e15.extend) ? void 0 : i3.selectionMenus) ?? {} }, operationRegistry: l };
4554
- })({ ...t2, enabledModes: s2 }), i2 = { ...n9 ? xn() : {}, ...n9 ? $n(a2.list()) : {}, ...e14.commands ?? {} }, o2 = [createPluginRegistration(DocumentManagerPluginPackage, { initialDocuments: [] }), createPluginRegistration(rt$1, { manifest: e14.manifest, overlapStrategy: e14.overlapStrategy, passwordProvider: e14.passwordProvider, chapterPdfLoader: e14.chapterPdfLoader, prefetchChapters: e14.prefetchChapters, maxConcurrentSegmentLoads: e14.maxConcurrentSegmentLoads, unloadTimeoutMs: e14.unloadTimeoutMs }), createPluginRegistration(Et$1, { placeholderPageHeight: e14.placeholderPageHeight, placeholderPageWidth: e14.placeholderPageWidth }), createPluginRegistration(InteractionManagerPluginPackage), createPluginRegistration(RenderPluginPackage), createPluginRegistration(RotatePluginPackage), createPluginRegistration(SelectionPluginPackage, { marquee: { enabled: false }, toleranceFactor: 1.2, minSelectionDragDistance: 4 }), createPluginRegistration(HistoryPluginPackage), createPluginRegistration(AnnotationPluginPackage), createPluginRegistration(RedactionPluginPackage), createPluginRegistration(FormPluginPackage), createPluginRegistration(StampPluginPackage, false === e14.loadDefaultStampLibrary ? { manifests: [] } : void 0), createPluginRegistration(SignaturePluginPackage), ...Object.keys(i2).length > 0 ? [createPluginRegistration(CommandsPluginPackage, { commands: i2 })] : [], ...n9 ? [createPluginRegistration(I18nPluginPackage), createPluginRegistration(UIPluginPackage, { schema: r2 })] : []];
4668
+ })({ ...t2, enabledModes: s2 }), i2 = { ...n9 ? xn() : {}, ...n9 ? $n(a2.list()) : {}, ...e14.commands ?? {} }, o2 = [createPluginRegistration(DocumentManagerPluginPackage, { initialDocuments: [] }), createPluginRegistration(rt$1, { manifest: e14.manifest, overlapStrategy: e14.overlapStrategy, passwordProvider: e14.passwordProvider, chapterPdfLoader: e14.chapterPdfLoader, prefetchChapters: e14.prefetchChapters, maxConcurrentSegmentLoads: e14.maxConcurrentSegmentLoads, unloadTimeoutMs: e14.unloadTimeoutMs }), createPluginRegistration(Ut$1, { placeholderPageHeight: e14.placeholderPageHeight, placeholderPageWidth: e14.placeholderPageWidth }), createPluginRegistration(InteractionManagerPluginPackage), createPluginRegistration(RenderPluginPackage), createPluginRegistration(RotatePluginPackage), createPluginRegistration(SelectionPluginPackage, { marquee: { enabled: false }, toleranceFactor: 1.2, minSelectionDragDistance: 4 }), createPluginRegistration(HistoryPluginPackage), createPluginRegistration(AnnotationPluginPackage), createPluginRegistration(RedactionPluginPackage), createPluginRegistration(FormPluginPackage), createPluginRegistration(StampPluginPackage, false === e14.loadDefaultStampLibrary ? { manifests: [] } : void 0), createPluginRegistration(SignaturePluginPackage), ...Object.keys(i2).length > 0 ? [createPluginRegistration(CommandsPluginPackage, { commands: i2 })] : [], ...n9 ? [createPluginRegistration(I18nPluginPackage), createPluginRegistration(UIPluginPackage, { schema: r2 })] : []];
4555
4669
  return e14.notes && o2.push(createPluginRegistration(ys, { callbacks: e14.notes.callbacks, ui: (_b = (_a2 = e14.features) == null ? void 0 : _a2.notes) == null ? void 0 : _b.marker })), e14.bookmarks && o2.push(createPluginRegistration(Sn, { callbacks: e14.bookmarks.callbacks, ui: (_d = (_c = e14.features) == null ? void 0 : _c.bookmarks) == null ? void 0 : _d.marker, hover: (_f = (_e2 = e14.features) == null ? void 0 : _e2.bookmarks) == null ? void 0 : _f.hover })), { plugins: o2, uiSchema: r2, operationRegistry: a2 };
4556
4670
  }
4557
4671
  function $n(e14) {
@@ -4563,24 +4677,24 @@ function $n(e14) {
4563
4677
  return t2;
4564
4678
  }
4565
4679
  const kn = ke$1(), Dn = {};
4566
- function En(e14) {
4680
+ function Un(e14) {
4567
4681
  return Ln({ ...e14, toolbar: e14.toolbar ?? { enabledModes: [] }, features: ke$1(e14.features) });
4568
4682
  }
4569
- const Un = { overlapStrategy: { kind: "first-wins" }, passwordProvider: new Ne$1(async () => null), prefetchChapters: 1, maxConcurrentSegmentLoads: 1, loadDefaultStampLibrary: false, unloadTimeoutMs: 12e4, toolbar: { enabledModes: [] } };
4570
- function Tn(e14) {
4683
+ const En = { overlapStrategy: { kind: "first-wins" }, passwordProvider: new Ne$1(async () => null), prefetchChapters: 1, maxConcurrentSegmentLoads: 1, loadDefaultStampLibrary: false, unloadTimeoutMs: 12e4, toolbar: { enabledModes: [] } };
4684
+ function Vn(e14) {
4571
4685
  return (function(e15) {
4572
4686
  return "callbacks" in e15.notes;
4573
4687
  })(e14) ? { manifest: e14.manifest, chapterPdfLoader: e14.chapterPdfLoader, notes: e14.notes.callbacks, bookmarks: e14.bookmarks.callbacks } : e14;
4574
4688
  }
4575
4689
  function Nn(e14) {
4576
- return { ...Un, manifest: e14.manifest, chapterPdfLoader: e14.chapterPdfLoader, overlapStrategy: e14.overlapStrategy ?? Un.overlapStrategy, prefetchChapters: e14.prefetchChapters ?? Un.prefetchChapters, maxConcurrentSegmentLoads: e14.maxConcurrentSegmentLoads ?? Un.maxConcurrentSegmentLoads, unloadTimeoutMs: e14.unloadTimeoutMs ?? Un.unloadTimeoutMs, notes: { callbacks: e14.notes }, bookmarks: { callbacks: e14.bookmarks }, features: ke$1(e14.features) };
4690
+ return { ...En, manifest: e14.manifest, chapterPdfLoader: e14.chapterPdfLoader, overlapStrategy: e14.overlapStrategy ?? En.overlapStrategy, prefetchChapters: e14.prefetchChapters ?? En.prefetchChapters, maxConcurrentSegmentLoads: e14.maxConcurrentSegmentLoads ?? En.maxConcurrentSegmentLoads, unloadTimeoutMs: e14.unloadTimeoutMs ?? En.unloadTimeoutMs, notes: { callbacks: e14.notes }, bookmarks: { callbacks: e14.bookmarks }, features: ke$1(e14.features) };
4577
4691
  }
4578
- function Fn(e14) {
4579
- const t2 = Nn(Tn(e14)), { plugins: s2 } = En(t2);
4692
+ function Tn(e14) {
4693
+ const t2 = Nn(Vn(e14)), { plugins: s2 } = Un(t2);
4580
4694
  return { plugins: s2, features: t2.features, editorOptions: t2 };
4581
4695
  }
4582
- function Vn(e14) {
4583
- const { features: t2, ...s2 } = Nn(Tn(e14));
4696
+ function Fn(e14) {
4697
+ const { features: t2, ...s2 } = Nn(Vn(e14));
4584
4698
  return s2;
4585
4699
  }
4586
4700
  function An(e14, t2 = 0.5, s2 = 3) {
@@ -4596,17 +4710,17 @@ function On(e14, t2) {
4596
4710
  }
4597
4711
  return s2 <= 0 ? 1 : t2 / s2;
4598
4712
  }
4599
- function zn(e14, t2) {
4713
+ function Rn(e14, t2) {
4600
4714
  return e14 <= 0 ? t2 ?? 0 : null == t2 || t2 <= 0 ? e14 : Math.min(t2, e14);
4601
4715
  }
4602
- function Gn(e14, t2, s2, n9) {
4603
- const r2 = zn(t2, s2);
4716
+ function zn(e14, t2, s2, n9) {
4717
+ const r2 = Rn(t2, s2);
4604
4718
  if (r2 <= 0) return n9;
4605
4719
  const a2 = On(e14, r2);
4606
4720
  return Math.min(n9, a2);
4607
4721
  }
4608
- function Rn(e14, t2, s2) {
4609
- const n9 = Ue$1(t2);
4722
+ function Gn(e14, t2, s2) {
4723
+ const n9 = Ee$1(t2);
4610
4724
  if (n9) {
4611
4725
  for (const t3 of n9.segments) {
4612
4726
  const n10 = e14.getDocument(t3.documentId);
@@ -4617,10 +4731,10 @@ function Rn(e14, t2, s2) {
4617
4731
  const r2 = e14.getDocument(t2.chapterId);
4618
4732
  r2 && s2(r2, t2.chapterId);
4619
4733
  }
4620
- function _n(e14) {
4734
+ function Bn(e14) {
4621
4735
  return e14 instanceof HTMLElement ? e14.clientWidth : 0;
4622
4736
  }
4623
- function Bn(a2, i2, o2) {
4737
+ function _n(a2, i2, o2) {
4624
4738
  const l = isRef(a2) ? a2 : computed(() => a2), { registry: h2 } = useRegistry(), { provides: d2 } = useCapability(DocumentManagerPlugin.id), { provides: c2 } = useCapability(nt$1.id), u2 = ref(P2()), p2 = ref(u2.value), g2 = ref(0), m2 = ref(false), f2 = () => {
4625
4739
  var _a2;
4626
4740
  return (_a2 = i2.value) == null ? void 0 : _a2.pageWidth;
@@ -4634,8 +4748,8 @@ function Bn(a2, i2, o2) {
4634
4748
  return (function(e14, t2, s2, n9, r2) {
4635
4749
  if (!e14 || s2 <= 0) return r2;
4636
4750
  let a3 = r2;
4637
- for (const i3 of t2.chapters) Rn(e14, i3, (e15) => {
4638
- a3 = Math.min(a3, Gn(e15, s2, n9, r2));
4751
+ for (const i3 of t2.chapters) Gn(e14, i3, (e15) => {
4752
+ a3 = Math.min(a3, zn(e15, s2, n9, r2));
4639
4753
  });
4640
4754
  return a3;
4641
4755
  })(d2.value, l.value, g2.value, f2(), ((_a2 = i2.value) == null ? void 0 : _a2.max) ?? 3);
@@ -4652,7 +4766,7 @@ function Bn(a2, i2, o2) {
4652
4766
  const i3 = An(s3, n10, r2);
4653
4767
  a3.dispatchToCore(setDefaultScale(i3));
4654
4768
  for (const o3 of t3.chapters) {
4655
- const e16 = Ue$1(o3);
4769
+ const e16 = Ee$1(o3);
4656
4770
  if (e16) for (const t4 of e16.segments) a3.getState().core.documents[t4.documentId] && a3.dispatchToCore(setScale(i3, t4.documentId));
4657
4771
  else a3.getState().core.documents[o3.chapterId] && a3.dispatchToCore(setScale(i3, o3.chapterId));
4658
4772
  }
@@ -4662,7 +4776,7 @@ function Bn(a2, i2, o2) {
4662
4776
  if (!d2.value || !c2.value) return;
4663
4777
  const t2 = c2.value.resolvePageDocument(e14, 0), s2 = d2.value.getDocument(t2.documentId);
4664
4778
  if (!s2) return;
4665
- const n9 = zn(g2.value, f2());
4779
+ const n9 = Rn(g2.value, f2());
4666
4780
  n9 <= 0 || C2(On(s2, n9));
4667
4781
  }
4668
4782
  function x(e14) {
@@ -4675,10 +4789,10 @@ function Bn(a2, i2, o2) {
4675
4789
  };
4676
4790
  let s2 = null;
4677
4791
  const n9 = new ResizeObserver(() => {
4678
- t2(_n(s2));
4792
+ t2(Bn(s2));
4679
4793
  }), r2 = () => {
4680
4794
  const r3 = e15.querySelector("[data-chapter-scroll-viewport]");
4681
- r3 !== s2 ? (s2 && n9.unobserve(s2), s2 = r3, s2 ? (n9.observe(s2), t2(_n(s2))) : t2(0)) : t2(_n(s2));
4795
+ r3 !== s2 ? (s2 && n9.unobserve(s2), s2 = r3, s2 ? (n9.observe(s2), t2(Bn(s2))) : t2(0)) : t2(Bn(s2));
4682
4796
  };
4683
4797
  r2();
4684
4798
  const a3 = new MutationObserver(r2);
@@ -4767,7 +4881,7 @@ function Kn(e14) {
4767
4881
  }), l = computed(() => {
4768
4882
  var _a2;
4769
4883
  return (_a2 = e14.value) == null ? void 0 : _a2.zoom;
4770
- }), { scale: h2, setScale: d2, zoomEnabled: c2, minZoom: u2, maxZoom: p2 } = Bn(o2, l, a2);
4884
+ }), { scale: h2, setScale: d2, zoomEnabled: c2, minZoom: u2, maxZoom: p2 } = _n(o2, l, a2);
4771
4885
  return (function(e15, t2) {
4772
4886
  let s2 = null;
4773
4887
  watch([e15, t2], () => {
@@ -4960,7 +5074,7 @@ function ur(e14, t2 = hr) {
4960
5074
  }
4961
5075
  const pr = class e9 extends BasePlugin {
4962
5076
  constructor(e14, t2) {
4963
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = hr, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
5077
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = hr, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4964
5078
  }
4965
5079
  async initialize(e14) {
4966
5080
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? hr, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -5095,7 +5209,19 @@ const pr = class e9 extends BasePlugin {
5095
5209
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
5096
5210
  }
5097
5211
  clearVisibleSegmentBacklog() {
5098
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
5212
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
5213
+ }
5214
+ chapterNeedsResolvedUrls(e14) {
5215
+ var t2;
5216
+ const s2 = e14.source;
5217
+ return !er(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
5218
+ }
5219
+ ensureVisibleChapterUrls(e14) {
5220
+ const { chapterId: t2 } = e14;
5221
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
5222
+ }).finally(() => {
5223
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
5224
+ })), false));
5099
5225
  }
5100
5226
  segmentStartGlobalIndex(e14, t2) {
5101
5227
  var s2;
@@ -5116,11 +5242,18 @@ const pr = class e9 extends BasePlugin {
5116
5242
  }
5117
5243
  pumpVisibleSegmentBacklog() {
5118
5244
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
5245
+ let t2 = 0;
5119
5246
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
5120
5247
  const e15 = this.visibleSegmentBacklog.shift();
5121
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
5122
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
5123
- })));
5248
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
5249
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
5250
+ const s2 = this.findChapter(e15.chapterId);
5251
+ if (s2) {
5252
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
5253
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
5254
+ });
5255
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
5256
+ }
5124
5257
  }
5125
5258
  }
5126
5259
  prepareChapterForSegmentLoad(e14) {
@@ -5685,8 +5818,8 @@ function Dr(e14) {
5685
5818
  for (let l = 0; l < i2.urls.length; l++) (null == (t2 = i2.urls[l]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${l}] is empty`);
5686
5819
  return n9;
5687
5820
  }
5688
- const Er = { kind: "first-wins" };
5689
- function Ur(e14, t2, s2) {
5821
+ const Ur = { kind: "first-wins" };
5822
+ function Er(e14, t2, s2) {
5690
5823
  if (1 === t2.length) return t2[0].chapterId;
5691
5824
  switch (s2.kind) {
5692
5825
  case "first-wins":
@@ -5704,7 +5837,7 @@ function Ur(e14, t2, s2) {
5704
5837
  return s2.resolve(e14, t2);
5705
5838
  }
5706
5839
  }
5707
- let Tr = class {
5840
+ let Vr = class {
5708
5841
  constructor(e14) {
5709
5842
  this._pages = e14, this._byChapter = /* @__PURE__ */ new Map(), this._byGlobalNumber = /* @__PURE__ */ new Map();
5710
5843
  for (const t2 of e14) {
@@ -5738,8 +5871,8 @@ let Tr = class {
5738
5871
  return s2 ? this._pages.indexOf(s2) : -1;
5739
5872
  }
5740
5873
  };
5741
- function Nr(e14, t2 = Er) {
5742
- const s2 = (function(e15, t3 = Er) {
5874
+ function Nr(e14, t2 = Ur) {
5875
+ const s2 = (function(e15, t3 = Ur) {
5743
5876
  const s3 = /* @__PURE__ */ new Map();
5744
5877
  for (const r3 of e15.chapters) {
5745
5878
  const [e16, t4] = r3.globalPageRange;
@@ -5749,7 +5882,7 @@ function Nr(e14, t2 = Er) {
5749
5882
  }
5750
5883
  }
5751
5884
  const n10 = /* @__PURE__ */ new Map();
5752
- for (const [r3, a3] of s3) n10.set(r3, Ur(r3, a3, t3));
5885
+ for (const [r3, a3] of s3) n10.set(r3, Er(r3, a3, t3));
5753
5886
  return n10;
5754
5887
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
5755
5888
  for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
@@ -5761,14 +5894,14 @@ function Nr(e14, t2 = Er) {
5761
5894
  const [r3] = t3.globalPageRange, [l] = t3.localPageRange, h2 = l + (o2 - r3);
5762
5895
  a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: h2 });
5763
5896
  }
5764
- return new Tr(a2);
5897
+ return new Vr(a2);
5765
5898
  }
5766
- const Fr = class e10 extends BasePlugin {
5899
+ const Tr = class e10 extends BasePlugin {
5767
5900
  constructor(e14, t2) {
5768
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Er, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
5901
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Ur, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
5769
5902
  }
5770
5903
  async initialize(e14) {
5771
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Er, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
5904
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Ur, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
5772
5905
  const { chapterId: t2 } = Mr(e15.id);
5773
5906
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
5774
5907
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -5900,7 +6033,19 @@ const Fr = class e10 extends BasePlugin {
5900
6033
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
5901
6034
  }
5902
6035
  clearVisibleSegmentBacklog() {
5903
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
6036
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
6037
+ }
6038
+ chapterNeedsResolvedUrls(e14) {
6039
+ var t2;
6040
+ const s2 = e14.source;
6041
+ return !Cr(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
6042
+ }
6043
+ ensureVisibleChapterUrls(e14) {
6044
+ const { chapterId: t2 } = e14;
6045
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
6046
+ }).finally(() => {
6047
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
6048
+ })), false));
5904
6049
  }
5905
6050
  segmentStartGlobalIndex(e14, t2) {
5906
6051
  var s2;
@@ -5921,11 +6066,18 @@ const Fr = class e10 extends BasePlugin {
5921
6066
  }
5922
6067
  pumpVisibleSegmentBacklog() {
5923
6068
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
6069
+ let t2 = 0;
5924
6070
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
5925
6071
  const e15 = this.visibleSegmentBacklog.shift();
5926
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
5927
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
5928
- })));
6072
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6073
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6074
+ const s2 = this.findChapter(e15.chapterId);
6075
+ if (s2) {
6076
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
6077
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
6078
+ });
6079
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6080
+ }
5929
6081
  }
5930
6082
  }
5931
6083
  prepareChapterForSegmentLoad(e14) {
@@ -6173,21 +6325,21 @@ const Fr = class e10 extends BasePlugin {
6173
6325
  "error" === t2 && s2 ? this.chapterErrorMessages.set(e14, s2) : "loading" !== t2 && "loaded" !== t2 || this.chapterErrorMessages.delete(e14), n9 !== t2 && (this.chapterStatus.set(e14, t2), this.statusChange$.emit({ chapterId: e14, status: t2, ..."error" === t2 && s2 ? { error: { message: s2 } } : {} }));
6174
6326
  }
6175
6327
  };
6176
- Fr.id = "chapter-manager";
6177
- let Vr = Fr;
6328
+ Tr.id = "chapter-manager";
6329
+ let Fr = Tr;
6178
6330
  function Ar(e14) {
6179
6331
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
6180
6332
  }
6181
6333
  function Or(e14, t2) {
6182
6334
  return `${e14}#s${t2}`;
6183
6335
  }
6184
- function zr(e14) {
6336
+ function Rr(e14) {
6185
6337
  const t2 = e14.lastIndexOf("#s");
6186
6338
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
6187
6339
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
6188
6340
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
6189
6341
  }
6190
- function Gr(e14) {
6342
+ function zr(e14) {
6191
6343
  const t2 = (function(e15) {
6192
6344
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
6193
6345
  const t3 = e15.source;
@@ -6207,23 +6359,23 @@ function Gr(e14) {
6207
6359
  }
6208
6360
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
6209
6361
  }
6210
- function Rr(e14, t2) {
6211
- const s2 = Gr(e14);
6362
+ function Gr(e14, t2) {
6363
+ const s2 = zr(e14);
6212
6364
  if (!s2) return 0;
6213
6365
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
6214
6366
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
6215
6367
  }
6216
- function _r(e14) {
6368
+ function Br(e14) {
6217
6369
  const [t2, s2] = e14.localPageRange;
6218
6370
  return s2 - t2 + 1;
6219
6371
  }
6220
- function Br(e14) {
6372
+ function _r(e14) {
6221
6373
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
6222
6374
  }
6223
6375
  function Wr(e14) {
6224
6376
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
6225
6377
  const t2 = e14.source;
6226
- return Br(t2) ? t2.segmentPageThreshold : null;
6378
+ return _r(t2) ? t2.segmentPageThreshold : null;
6227
6379
  }
6228
6380
  function qr(e14, t2) {
6229
6381
  return `${e14}#s${t2}`;
@@ -6240,7 +6392,7 @@ function Kr(e14, t2) {
6240
6392
  function jr(e14) {
6241
6393
  const t2 = Wr(e14);
6242
6394
  if (!t2) return null;
6243
- const s2 = _r(e14), n9 = Kr(s2, t2), r2 = Br(e14.source) ? e14.source.urls : void 0, a2 = [];
6395
+ const s2 = Br(e14), n9 = Kr(s2, t2), r2 = _r(e14.source) ? e14.source.urls : void 0, a2 = [];
6244
6396
  for (let i2 = 0; i2 < n9; i2++) {
6245
6397
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
6246
6398
  if (n10 >= s2) break;
@@ -6268,10 +6420,10 @@ function Yr(e14) {
6268
6420
  var t2;
6269
6421
  const s2 = Wr(e14);
6270
6422
  if (!s2) return [];
6271
- const n9 = [], { chapterId: r2 } = e14, a2 = _r(e14);
6423
+ const n9 = [], { chapterId: r2 } = e14, a2 = Br(e14);
6272
6424
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
6273
6425
  const i2 = e14.source;
6274
- if (!Br(i2)) return n9;
6426
+ if (!_r(i2)) return n9;
6275
6427
  0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
6276
6428
  const o2 = Kr(a2, s2);
6277
6429
  i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
@@ -6358,7 +6510,7 @@ function ta(e14, t2 = Qr) {
6358
6510
  }
6359
6511
  const sa = class e11 extends BasePlugin {
6360
6512
  constructor(e14, t2) {
6361
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Qr, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
6513
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Qr, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
6362
6514
  }
6363
6515
  async initialize(e14) {
6364
6516
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Qr, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -6493,7 +6645,19 @@ const sa = class e11 extends BasePlugin {
6493
6645
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
6494
6646
  }
6495
6647
  clearVisibleSegmentBacklog() {
6496
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
6648
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
6649
+ }
6650
+ chapterNeedsResolvedUrls(e14) {
6651
+ var t2;
6652
+ const s2 = e14.source;
6653
+ return !_r(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
6654
+ }
6655
+ ensureVisibleChapterUrls(e14) {
6656
+ const { chapterId: t2 } = e14;
6657
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
6658
+ }).finally(() => {
6659
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
6660
+ })), false));
6497
6661
  }
6498
6662
  segmentStartGlobalIndex(e14, t2) {
6499
6663
  var s2;
@@ -6514,11 +6678,18 @@ const sa = class e11 extends BasePlugin {
6514
6678
  }
6515
6679
  pumpVisibleSegmentBacklog() {
6516
6680
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
6681
+ let t2 = 0;
6517
6682
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
6518
6683
  const e15 = this.visibleSegmentBacklog.shift();
6519
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
6520
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
6521
- })));
6684
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6685
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6686
+ const s2 = this.findChapter(e15.chapterId);
6687
+ if (s2) {
6688
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
6689
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
6690
+ });
6691
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6692
+ }
6522
6693
  }
6523
6694
  }
6524
6695
  prepareChapterForSegmentLoad(e14) {
@@ -6642,7 +6813,7 @@ const sa = class e11 extends BasePlugin {
6642
6813
  if (n9) return n9;
6643
6814
  const r2 = (async () => {
6644
6815
  const s3 = e14.source;
6645
- if (Br(s3)) {
6816
+ if (_r(s3)) {
6646
6817
  const e15 = [...s3.urls];
6647
6818
  return this.chapterUrlsCache.set(t2, e15), e15;
6648
6819
  }
@@ -6670,7 +6841,7 @@ const sa = class e11 extends BasePlugin {
6670
6841
  async resolvePdfPayload(t2, s2) {
6671
6842
  const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Wr(t2);
6672
6843
  if (a2) {
6673
- const n10 = Kr(_r(t2), a2);
6844
+ const n10 = Kr(Br(t2), a2);
6674
6845
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
6675
6846
  const r3 = await this.resolveChapterUrls(t2);
6676
6847
  if (r3.length !== n10) {
@@ -6681,7 +6852,7 @@ const sa = class e11 extends BasePlugin {
6681
6852
  }
6682
6853
  if (0 !== s2) throw new Error(`Chapter "${t2.chapterId}" is not segmented`);
6683
6854
  if (n9) {
6684
- if (Br(n9)) {
6855
+ if (_r(n9)) {
6685
6856
  const e14 = await this.resolveChapterUrls(t2);
6686
6857
  return this.openPayloadFromUrl(t2, s2, e14);
6687
6858
  }
@@ -6794,7 +6965,7 @@ const aa = class extends BasePlugin {
6794
6965
  isChapterPagePlaceholder(e14, t2) {
6795
6966
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14);
6796
6967
  if (!n9) return true;
6797
- const r2 = Rr(n9, t2);
6968
+ const r2 = Gr(n9, t2);
6798
6969
  return !s2.isSegmentLoaded(e14, r2);
6799
6970
  }
6800
6971
  refreshLayout() {
@@ -6815,14 +6986,14 @@ const aa = class extends BasePlugin {
6815
6986
  }
6816
6987
  onDocumentLoaded(e14) {
6817
6988
  var t2;
6818
- const { chapterId: s2 } = zr(e14);
6989
+ const { chapterId: s2 } = Rr(e14);
6819
6990
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
6820
6991
  const n9 = this.documentManager.provides();
6821
6992
  n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
6822
6993
  }
6823
6994
  onDocumentClosed(e14) {
6824
6995
  var t2;
6825
- const { chapterId: s2 } = zr(e14);
6996
+ const { chapterId: s2 } = Rr(e14);
6826
6997
  0 !== ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0) && (this.dropChapterSizes(s2), this.refreshLayout());
6827
6998
  }
6828
6999
  buildCapability() {
@@ -6874,7 +7045,7 @@ const aa = class extends BasePlugin {
6874
7045
  if (!this.virtualPageMap) return;
6875
7046
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14), r2 = this.virtualPageMap.pagesOfChapter(e14);
6876
7047
  if (!n9 || 0 === r2.length) return;
6877
- const a2 = Gr(n9), i2 = this.documentManager.provides(), o2 = [], l = (e15, t3, s3, n10) => {
7048
+ const a2 = zr(n9), i2 = this.documentManager.provides(), o2 = [], l = (e15, t3, s3, n10) => {
6878
7049
  var r3;
6879
7050
  const a3 = i2.getDocument(e15);
6880
7051
  if (!(null == (r3 = null == a3 ? void 0 : a3.pages) ? void 0 : r3.length)) return false;
@@ -6896,7 +7067,7 @@ const aa = class extends BasePlugin {
6896
7067
  onScaleChanged(e14) {
6897
7068
  const t2 = this.coreState.core.documents[e14];
6898
7069
  if (!t2 || "loaded" !== t2.status) return;
6899
- const { chapterId: s2 } = zr(e14);
7070
+ const { chapterId: s2 } = Rr(e14);
6900
7071
  this.chapterManager.provides().getChapter(s2) && this.harvestChapterSizes(s2);
6901
7072
  }
6902
7073
  dropChapterSizes(e14) {
@@ -6982,7 +7153,7 @@ const aa = class extends BasePlugin {
6982
7153
  if (s2 < 0) return;
6983
7154
  const n9 = this.chapterManager.provides(), r2 = n9.getChapter(e14), a2 = this.virtualPageMap.atIndex(s2);
6984
7155
  if (r2 && a2) {
6985
- const t3 = Rr(r2, a2.localPageIndex);
7156
+ const t3 = Gr(r2, a2.localPageIndex);
6986
7157
  n9.ensureSegmentLoaded(e14, t3);
6987
7158
  } else n9.ensureChapterLoaded(e14);
6988
7159
  this.scrollToIndex(s2, t2);
@@ -6997,7 +7168,7 @@ aa.id = "chapter-scroll";
6997
7168
  let ia = aa;
6998
7169
  const oa = class e12 extends BasePlugin {
6999
7170
  constructor(e14, t2) {
7000
- super(e14, t2), this.notesChange$ = createBehaviorEmitter([]), this.noteActivated$ = createEmitter(), this.notes = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(ia.id), this.chapterManager = this.registry.getPlugin(Vr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7171
+ super(e14, t2), this.notesChange$ = createBehaviorEmitter([]), this.noteActivated$ = createEmitter(), this.notes = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(ia.id), this.chapterManager = this.registry.getPlugin(Fr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7001
7172
  }
7002
7173
  async initialize(t2) {
7003
7174
  if (this.config = t2, t2.callbacks.loadNotes) try {
@@ -7376,14 +7547,14 @@ const Da = /* @__PURE__ */ defineComponent({ __name: "HoverBookmarkLayer", props
7376
7547
  var _a2;
7377
7548
  return unref(u2) && e14.pdfPageWidth && e14.pdfPageHeight ? (openBlock(), createElementBlock("div", { key: 0, ref_key: "rootRef", ref: p2, style: { position: "absolute", inset: "0", zIndex: 14, pointerEvents: "none" } }, [D2.value && S.value && !$2.value ? (openBlock(), createElementBlock("div", { key: 0, style: normalizeStyle({ position: "absolute", left: `${D2.value.zoneLeft}px`, top: `${D2.value.zoneTop}px`, width: `${D2.value.size + 20}px`, height: `${(((_a2 = S.value[S.value.length - 1]) == null ? void 0 : _a2.size.height) ?? 0) * (e14.cssPageHeight / e14.pdfPageHeight) + D2.value.size + 12}px`, pointerEvents: "auto" }), onMouseenter: s2[0] || (s2[0] = (e15) => I2.value = S.value), onMouseleave: s2[1] || (s2[1] = (e15) => I2.value = null) }, [createElementVNode("button", { type: "button", title: "添加书签", style: normalizeStyle({ position: "absolute", left: D2.value.iconLeft - D2.value.zoneLeft + "px", top: D2.value.iconTop - D2.value.zoneTop + "px", width: `${D2.value.size}px`, height: `${D2.value.size}px`, cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center", background: "#3b82f6", color: "#fff", border: "none", borderRadius: "4px", boxShadow: "0 2px 8px rgba(37, 99, 235, 0.45)", fontSize: "12px", padding: 0 }), onClick: withModifiers(k2, ["stop"]) }, " 🔖 ", 4)], 36)) : createCommentVNode("", true)], 512)) : createCommentVNode("", true);
7378
7549
  };
7379
- } }), Ea = { key: 0, width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Ua = { key: 1, width: "24", height: "24", viewBox: "0 0 28 28", fill: "none", "aria-hidden": "" }, Ta = { key: 0, x: "5", y: "4", width: "18", height: "20", rx: "3", fill: "#fde047" }, Na = { key: 1, d: "M6 22h16", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Fa = { key: 2, d: "M6 22c2-2 3-2 5 0s3 2 5 0 3-2 5 0", stroke: "#dc2626", "stroke-width": "2", "stroke-linecap": "round", fill: "none" }, Va = { key: 3, d: "M5 14h18", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Aa = { key: 2, width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Oa = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarIcons", props: { kind: {} }, setup: (e14) => (t2, s2) => "copy" === e14.kind ? (openBlock(), createElementBlock("svg", Ea, [...s2[0] || (s2[0] = [createElementVNode("rect", { x: "8.5", y: "8.5", width: "11", height: "13", rx: "2.25", fill: "#f1f5f9", stroke: "#94a3b8", "stroke-width": "1.35" }, null, -1), createElementVNode("rect", { x: "4.5", y: "3.5", width: "11", height: "13", rx: "2.25", fill: "#ffffff", stroke: "#334155", "stroke-width": "1.5" }, null, -1), createElementVNode("path", { d: "M7.5 7h5.5a1.5 1.5 0 0 1 1.5 1.5V14", stroke: "#64748b", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) : "note" !== e14.kind ? (openBlock(), createElementBlock("svg", Ua, ["highlight" === e14.kind ? (openBlock(), createElementBlock("rect", Ta)) : createCommentVNode("", true), s2[1] || (s2[1] = createElementVNode("text", { x: "14", y: "19", "text-anchor": "middle", "font-size": "15", "font-weight": "700", "font-family": "system-ui, -apple-system, sans-serif", fill: "#0f172a" }, " A ", -1)), "underline" === e14.kind ? (openBlock(), createElementBlock("path", Na)) : "squiggly" === e14.kind ? (openBlock(), createElementBlock("path", Fa)) : "strikeout" === e14.kind ? (openBlock(), createElementBlock("path", Va)) : createCommentVNode("", true)])) : (openBlock(), createElementBlock("svg", Aa, [...s2[2] || (s2[2] = [createElementVNode("path", { d: "M8 3h8l4 4v14H8V3z", fill: "#fef3c7", stroke: "#d97706", "stroke-width": "1.2", "stroke-linejoin": "round" }, null, -1), createElementVNode("path", { d: "M16 3v4h4", stroke: "#d97706", "stroke-width": "1.2" }, null, -1), createElementVNode("path", { d: "M10 12h6M10 16h4", stroke: "#92400e", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) }), za = ["title", "aria-label", "onMouseenter", "onClick"], Ga = ["title", "onMouseenter", "onClick"], Ra = { style: { "font-size": "11px", "font-weight": "600" } }, _a = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarCard", props: { placementAbove: { type: Boolean }, anchorHeight: {}, hiddenBuiltinActions: {}, extraActions: {}, renderCopyIcon: { type: Function } }, emits: ["action"], setup(e14, { emit: n9 }) {
7550
+ } }), Ua = { key: 0, width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Ea = { key: 1, width: "24", height: "24", viewBox: "0 0 28 28", fill: "none", "aria-hidden": "" }, Va = { key: 0, x: "5", y: "4", width: "18", height: "20", rx: "3", fill: "#fde047" }, Na = { key: 1, d: "M6 22h16", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Ta = { key: 2, d: "M6 22c2-2 3-2 5 0s3 2 5 0 3-2 5 0", stroke: "#dc2626", "stroke-width": "2", "stroke-linecap": "round", fill: "none" }, Fa = { key: 3, d: "M5 14h18", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Aa = { key: 2, width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Oa = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarIcons", props: { kind: {} }, setup: (e14) => (t2, s2) => "copy" === e14.kind ? (openBlock(), createElementBlock("svg", Ua, [...s2[0] || (s2[0] = [createElementVNode("rect", { x: "8.5", y: "8.5", width: "11", height: "13", rx: "2.25", fill: "#f1f5f9", stroke: "#94a3b8", "stroke-width": "1.35" }, null, -1), createElementVNode("rect", { x: "4.5", y: "3.5", width: "11", height: "13", rx: "2.25", fill: "#ffffff", stroke: "#334155", "stroke-width": "1.5" }, null, -1), createElementVNode("path", { d: "M7.5 7h5.5a1.5 1.5 0 0 1 1.5 1.5V14", stroke: "#64748b", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) : "note" !== e14.kind ? (openBlock(), createElementBlock("svg", Ea, ["highlight" === e14.kind ? (openBlock(), createElementBlock("rect", Va)) : createCommentVNode("", true), s2[1] || (s2[1] = createElementVNode("text", { x: "14", y: "19", "text-anchor": "middle", "font-size": "15", "font-weight": "700", "font-family": "system-ui, -apple-system, sans-serif", fill: "#0f172a" }, " A ", -1)), "underline" === e14.kind ? (openBlock(), createElementBlock("path", Na)) : "squiggly" === e14.kind ? (openBlock(), createElementBlock("path", Ta)) : "strikeout" === e14.kind ? (openBlock(), createElementBlock("path", Fa)) : createCommentVNode("", true)])) : (openBlock(), createElementBlock("svg", Aa, [...s2[2] || (s2[2] = [createElementVNode("path", { d: "M8 3h8l4 4v14H8V3z", fill: "#fef3c7", stroke: "#d97706", "stroke-width": "1.2", "stroke-linejoin": "round" }, null, -1), createElementVNode("path", { d: "M16 3v4h4", stroke: "#d97706", "stroke-width": "1.2" }, null, -1), createElementVNode("path", { d: "M10 12h6M10 16h4", stroke: "#92400e", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) }), Ra = ["title", "aria-label", "onMouseenter", "onClick"], za = ["title", "onMouseenter", "onClick"], Ga = { style: { "font-size": "11px", "font-weight": "600" } }, Ba = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarCard", props: { placementAbove: { type: Boolean }, anchorHeight: {}, hiddenBuiltinActions: {}, extraActions: {}, renderCopyIcon: { type: Function } }, emits: ["action"], setup(e14, { emit: n9 }) {
7380
7551
  const r2 = e14, a2 = n9, i2 = [{ id: "highlight", label: "高亮背景" }, { id: "underline", label: "下划线" }, { id: "squiggly", label: "波浪线" }, { id: "strikeout", label: "删除线" }], d2 = computed(() => new Set(r2.hiddenBuiltinActions ?? [])), g2 = computed(() => !d2.value.has("copy")), f2 = computed(() => i2.filter((e15) => !d2.value.has(e15.id))), v = computed(() => !d2.value.has("note")), I2 = computed(() => [...r2.extraActions ?? []].sort((e15, t2) => (e15.order ?? 0) - (t2.order ?? 0))), b2 = computed(() => f2.value.length > 0 || I2.value.length > 0), w2 = ref(null), M2 = computed(() => ({ position: "absolute", pointerEvents: "auto", left: "50%", transform: "translateX(-50%)", top: r2.placementAbove ? void 0 : `${r2.anchorHeight + 10}px`, bottom: r2.placementAbove ? `${r2.anchorHeight + 10}px` : void 0, display: "flex", alignItems: "center", gap: "2px", padding: "4px 6px", borderRadius: "12px", background: "rgba(255, 255, 255, 0.96)", border: "1px solid rgba(15, 23, 42, 0.06)", boxShadow: "0 2px 8px rgba(15, 23, 42, 0.08), 0 12px 32px rgba(15, 23, 42, 0.1)", backdropFilter: "blur(10px)", WebkitBackdropFilter: "blur(10px)" }));
7381
7552
  function x(e15, t2 = false) {
7382
7553
  return { display: "flex", alignItems: "center", justifyContent: "center", width: "32px", height: "32px", padding: 0, margin: 0, border: "none", borderRadius: "8px", background: w2.value === e15 ? t2 ? "#fffbeb" : "#f1f5f9" : "transparent", color: t2 ? "#b45309" : "#334155", cursor: "pointer", flexShrink: 0, WebkitAppearance: "none", appearance: "none" };
7383
7554
  }
7384
7555
  const L2 = { width: "1px", height: "20px", margin: "0 4px", background: "rgba(15, 23, 42, 0.1)", flexShrink: 0 };
7385
- return (t2, s2) => (openBlock(), createElementBlock("div", { role: "toolbar", "aria-label": "选区工具", style: normalizeStyle(M2.value) }, [g2.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createElementVNode("button", { type: "button", title: "复制", "aria-label": "复制", style: normalizeStyle(x("copy")), onMouseenter: s2[0] || (s2[0] = (e15) => w2.value = "copy"), onMouseleave: s2[1] || (s2[1] = (e15) => w2.value = null), onClick: s2[2] || (s2[2] = (e15) => a2("action", "copy")) }, [e14.renderCopyIcon ? (openBlock(), createBlock(resolveDynamicComponent(e14.renderCopyIcon), { key: 0 })) : (openBlock(), createBlock(Oa, { key: 1, kind: "copy" }))], 36), b2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true)], 64)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(f2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, "aria-label": e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[3] || (s2[3] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createVNode(Oa, { kind: e15.id }, null, 8, ["kind"])], 44, za))), 128)), (openBlock(true), createElementBlock(Fragment, null, renderList(I2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[4] || (s2[4] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createElementVNode("span", Ra, toDisplayString(e15.label.slice(0, 2)), 1)], 44, Ga))), 128)), v.value ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [b2.value || g2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true), createElementVNode("button", { type: "button", title: "添加笔记", "aria-label": "添加笔记", style: normalizeStyle(x("note", true)), onMouseenter: s2[5] || (s2[5] = (e15) => w2.value = "note"), onMouseleave: s2[6] || (s2[6] = (e15) => w2.value = null), onClick: s2[7] || (s2[7] = (e15) => a2("action", "note")) }, [createVNode(Oa, { kind: "note" })], 36)], 64)) : createCommentVNode("", true)], 4));
7386
- } }), Ba = { highlight: "highlight", underline: "underline", squiggly: "squiggly", strikeout: "strikeout" };
7556
+ return (t2, s2) => (openBlock(), createElementBlock("div", { role: "toolbar", "aria-label": "选区工具", style: normalizeStyle(M2.value) }, [g2.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createElementVNode("button", { type: "button", title: "复制", "aria-label": "复制", style: normalizeStyle(x("copy")), onMouseenter: s2[0] || (s2[0] = (e15) => w2.value = "copy"), onMouseleave: s2[1] || (s2[1] = (e15) => w2.value = null), onClick: s2[2] || (s2[2] = (e15) => a2("action", "copy")) }, [e14.renderCopyIcon ? (openBlock(), createBlock(resolveDynamicComponent(e14.renderCopyIcon), { key: 0 })) : (openBlock(), createBlock(Oa, { key: 1, kind: "copy" }))], 36), b2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true)], 64)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(f2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, "aria-label": e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[3] || (s2[3] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createVNode(Oa, { kind: e15.id }, null, 8, ["kind"])], 44, Ra))), 128)), (openBlock(true), createElementBlock(Fragment, null, renderList(I2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[4] || (s2[4] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createElementVNode("span", Ga, toDisplayString(e15.label.slice(0, 2)), 1)], 44, za))), 128)), v.value ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [b2.value || g2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true), createElementVNode("button", { type: "button", title: "添加笔记", "aria-label": "添加笔记", style: normalizeStyle(x("note", true)), onMouseenter: s2[5] || (s2[5] = (e15) => w2.value = "note"), onMouseleave: s2[6] || (s2[6] = (e15) => w2.value = null), onClick: s2[7] || (s2[7] = (e15) => a2("action", "note")) }, [createVNode(Oa, { kind: "note" })], 36)], 64)) : createCommentVNode("", true)], 4));
7557
+ } }), _a = { highlight: "highlight", underline: "underline", squiggly: "squiggly", strikeout: "strikeout" };
7387
7558
  const Wa = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props: { className: {}, features: {}, buildSelectionMenu: {}, annotationSelectionMenu: {}, redactionSelectionMenu: {}, showNoteMarkers: { type: Boolean, default: true }, showBookmarkMarkers: { type: Boolean, default: true }, showRedactionLayer: { type: Boolean, default: false }, renderPageOverlay: {}, onExtraSelectionAction: {} }, setup(e14) {
7388
7559
  const s2 = e14, n9 = computed(() => s2.features), { hostRef: r2 } = Kn(n9), a2 = computed(() => {
7389
7560
  var _a2, _b;
@@ -7436,7 +7607,7 @@ const Wa = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props
7436
7607
  const d2 = Ca(t2.item), c3 = ya(t2.item);
7437
7608
  return (u3) => {
7438
7609
  var _a3, _b2, _c2, _d;
7439
- return h$1("div", u3.menuWrapperProps, h$1(_a, { placementAbove: !!((_a3 = u3.placement) == null ? void 0 : _a3.suggestTop), anchorHeight: u3.rect.size.height, hiddenBuiltinActions: (_b2 = n10.value) == null ? void 0 : _b2.hiddenBuiltinActions, extraActions: (_c2 = n10.value) == null ? void 0 : _c2.extraActions, renderCopyIcon: (_d = n10.value) == null ? void 0 : _d.renderCopyIcon, onAction: (n11) => {
7610
+ return h$1("div", u3.menuWrapperProps, h$1(Ba, { placementAbove: !!((_a3 = u3.placement) == null ? void 0 : _a3.suggestTop), anchorHeight: u3.rect.size.height, hiddenBuiltinActions: (_b2 = n10.value) == null ? void 0 : _b2.hiddenBuiltinActions, extraActions: (_c2 = n10.value) == null ? void 0 : _c2.extraActions, renderCopyIcon: (_d = n10.value) == null ? void 0 : _d.renderCopyIcon, onAction: (n11) => {
7440
7611
  var _a4, _b3, _c3;
7441
7612
  if ("copy" !== n11) if ("highlight" !== n11 && "underline" !== n11 && "squiggly" !== n11 && "strikeout" !== n11) {
7442
7613
  if ("note" === n11) {
@@ -7450,7 +7621,7 @@ const Wa = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props
7450
7621
  (_a4 = s3 == null ? void 0 : s3.onExtraAction) == null ? void 0 : _a4.call(s3, n11, t2);
7451
7622
  } else {
7452
7623
  if (!o2.value || !l.value || !r3.value) return;
7453
- xe$1(d2, Ba[n11], l.value, o2.value, (_c3 = (_b3 = e15()) == null ? void 0 : _b3.markup) == null ? void 0 : _c3.styles);
7624
+ xe$1(d2, _a[n11], l.value, o2.value, (_c3 = (_b3 = e15()) == null ? void 0 : _b3.markup) == null ? void 0 : _c3.styles);
7454
7625
  }
7455
7626
  else {
7456
7627
  if (!o2.value || !i3.value) return;
@@ -7488,9 +7659,9 @@ const Wa = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props
7488
7659
  }), _: 1 }, 8, ["class", "style"]), renderSlot(t2.$slots, "default")], 6));
7489
7660
  } }), qa = { key: 0, style: { display: "flex", flexDirection: "column", height: "100%", minHeight: 0, width: "100%" } }, Ha = /* @__PURE__ */ defineComponent({ __name: "ChapterPdfViewer", props: { engine: {}, options: {}, editorOptions: {}, features: {}, className: {}, viewportClassName: {}, onExtraSelectionAction: { type: Function } }, setup(e14) {
7490
7661
  const s2 = e14, n9 = computed(() => (function(e15) {
7491
- if (e15.options) return Fn(e15.options);
7662
+ if (e15.options) return Tn(e15.options);
7492
7663
  if (!e15.editorOptions) throw new Error("ChapterPdfViewer: 请传入 `options`(推荐),或同时传入 `editorOptions` 与 `features`");
7493
- const t2 = ke$1(e15.features), { plugins: s3 } = En({ ...e15.editorOptions, features: t2 });
7664
+ const t2 = ke$1(e15.features), { plugins: s3 } = Un({ ...e15.editorOptions, features: t2 });
7494
7665
  return { plugins: s3, features: t2, editorOptions: { ...e15.editorOptions, features: t2 } };
7495
7666
  })({ engine: s2.engine, options: s2.options, editorOptions: s2.editorOptions, features: s2.features }));
7496
7667
  return (t2, s3) => (openBlock(), createElementBlock("div", { class: normalizeClass(e14.className), style: normalizeStyle(e14.className ? void 0 : unref(Pe$1)) }, [createVNode(unref(EmbedPDF), { engine: e14.engine, plugins: n9.value.plugins }, { default: withCtx(({ pluginsReady: s4 }) => [s4 ? (openBlock(), createElementBlock("div", qa, [renderSlot(t2.$slots, "prepend"), createVNode(Wa, { class: normalizeClass(e14.viewportClassName), style: { flex: 1, minHeight: 0 }, features: n9.value.features, "on-extra-selection-action": e14.onExtraSelectionAction }, null, 8, ["class", "features", "on-extra-selection-action"]), renderSlot(t2.$slots, "default")])) : createCommentVNode("", true)]), _: 3 }, 8, ["engine", "plugins"])], 6));
@@ -7557,12 +7728,12 @@ function L(e14) {
7557
7728
  function $$1(e14) {
7558
7729
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
7559
7730
  }
7560
- function D(e14) {
7731
+ function k(e14) {
7561
7732
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
7562
7733
  const t2 = e14.source;
7563
7734
  return $$1(t2) ? t2.segmentPageThreshold : null;
7564
7735
  }
7565
- function k(e14, t2) {
7736
+ function D(e14, t2) {
7566
7737
  return `${e14}#s${t2}`;
7567
7738
  }
7568
7739
  function U(e14) {
@@ -7571,22 +7742,22 @@ function U(e14) {
7571
7742
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
7572
7743
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
7573
7744
  }
7574
- function E(e14, t2) {
7745
+ function V(e14, t2) {
7575
7746
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
7576
7747
  }
7577
- function T(e14) {
7578
- const t2 = D(e14);
7748
+ function E(e14) {
7749
+ const t2 = k(e14);
7579
7750
  if (!t2) return null;
7580
- const s2 = L(e14), n9 = E(s2, t2), r2 = $$1(e14.source) ? e14.source.urls : void 0, a2 = [];
7581
- for (let o2 = 0; o2 < n9; o2++) {
7582
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
7751
+ const s2 = L(e14), n9 = V(s2, t2), r2 = $$1(e14.source) ? e14.source.urls : void 0, a2 = [];
7752
+ for (let i2 = 0; i2 < n9; i2++) {
7753
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
7583
7754
  if (n10 >= s2) break;
7584
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: k(e14.chapterId, o2) });
7755
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: D(e14.chapterId, i2) });
7585
7756
  }
7586
7757
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
7587
7758
  }
7588
- function F(e14, t2) {
7589
- const s2 = T(e14);
7759
+ function N(e14, t2) {
7760
+ const s2 = E(e14);
7590
7761
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
7591
7762
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
7592
7763
  if (!n9) {
@@ -7595,28 +7766,28 @@ function F(e14, t2) {
7595
7766
  }
7596
7767
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
7597
7768
  }
7598
- function N(e14, t2) {
7599
- const s2 = T(e14);
7769
+ function T(e14, t2) {
7770
+ const s2 = E(e14);
7600
7771
  if (!s2) return 0;
7601
7772
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
7602
7773
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
7603
7774
  }
7604
- function A(e14) {
7775
+ function F(e14) {
7605
7776
  var t2;
7606
- const s2 = D(e14);
7777
+ const s2 = k(e14);
7607
7778
  if (!s2) return [];
7608
7779
  const n9 = [], { chapterId: r2 } = e14, a2 = L(e14);
7609
7780
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
7610
- const o2 = e14.source;
7611
- if (!$$1(o2)) return n9;
7612
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
7613
- const i2 = E(a2, s2);
7614
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
7615
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
7781
+ const i2 = e14.source;
7782
+ if (!$$1(i2)) return n9;
7783
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
7784
+ const o2 = V(a2, s2);
7785
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
7786
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
7616
7787
  return n9;
7617
7788
  }
7618
- const O = { kind: "first-wins" };
7619
- function V(e14, t2, s2) {
7789
+ const A = { kind: "first-wins" };
7790
+ function O(e14, t2, s2) {
7620
7791
  if (1 === t2.length) return t2[0].chapterId;
7621
7792
  switch (s2.kind) {
7622
7793
  case "first-wins":
@@ -7634,7 +7805,7 @@ function V(e14, t2, s2) {
7634
7805
  return s2.resolve(e14, t2);
7635
7806
  }
7636
7807
  }
7637
- let G$1 = class G {
7808
+ let R$1 = class R {
7638
7809
  constructor(e14) {
7639
7810
  this._pages = e14, this._byChapter = /* @__PURE__ */ new Map(), this._byGlobalNumber = /* @__PURE__ */ new Map();
7640
7811
  for (const t2 of e14) {
@@ -7668,8 +7839,8 @@ let G$1 = class G {
7668
7839
  return s2 ? this._pages.indexOf(s2) : -1;
7669
7840
  }
7670
7841
  };
7671
- function R$1(e14, t2 = O) {
7672
- const s2 = (function(e15, t3 = O) {
7842
+ function G$1(e14, t2 = A) {
7843
+ const s2 = (function(e15, t3 = A) {
7673
7844
  const s3 = /* @__PURE__ */ new Map();
7674
7845
  for (const r3 of e15.chapters) {
7675
7846
  const [e16, t4] = r3.globalPageRange;
@@ -7679,26 +7850,26 @@ function R$1(e14, t2 = O) {
7679
7850
  }
7680
7851
  }
7681
7852
  const n10 = /* @__PURE__ */ new Map();
7682
- for (const [r3, a3] of s3) n10.set(r3, V(r3, a3, t3));
7853
+ for (const [r3, a3] of s3) n10.set(r3, O(r3, a3, t3));
7683
7854
  return n10;
7684
7855
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
7685
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
7856
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
7686
7857
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
7687
- let o2 = 0;
7688
- for (const i2 of r2) {
7689
- const e15 = s2.get(i2), t3 = n9.get(e15);
7858
+ let i2 = 0;
7859
+ for (const o2 of r2) {
7860
+ const e15 = s2.get(o2), t3 = n9.get(e15);
7690
7861
  if (!t3) continue;
7691
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
7692
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
7862
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
7863
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
7693
7864
  }
7694
- return new G$1(a2);
7865
+ return new R$1(a2);
7695
7866
  }
7696
- const z = class n extends BasePlugin {
7867
+ const B = class n extends BasePlugin {
7697
7868
  constructor(e14, n9) {
7698
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = O, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7869
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = A, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7699
7870
  }
7700
7871
  async initialize(e14) {
7701
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? O, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
7872
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? A, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
7702
7873
  const { chapterId: t2 } = U(e15.id);
7703
7874
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
7704
7875
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -7717,10 +7888,10 @@ const z = class n extends BasePlugin {
7717
7888
  t2 && (this.overlapStrategy = t2), this.setManifestInternal(e14);
7718
7889
  }, getManifest: () => this.manifest, getVirtualPageMap: () => this.virtualPageMap, setVisibleGlobalPages: (e14) => this.handleVisibleChange(e14), visibleRangeNeedsSegmentLoad: (e14) => this.visibleRangeNeedsSegmentLoad(e14), shouldShowChapterLoadError: (e14, t2) => this.shouldShowChapterLoadError(e14, t2), ensureChapterLoaded: (e14) => this.ensureChapterLoaded(e14), ensureSegmentLoaded: (e14, t2) => this.ensureSegmentLoaded(e14, t2), ensureAllSegmentsLoaded: (e14) => this.ensureAllSegmentsLoaded(e14), resolvePageDocument: (e14, t2) => {
7719
7890
  const s2 = this.findChapter(e14);
7720
- return s2 ? F(s2, t2) : null;
7891
+ return s2 ? N(s2, t2) : null;
7721
7892
  }, getSegmentPlan: (e14) => {
7722
7893
  const t2 = this.findChapter(e14);
7723
- return t2 ? T(t2) : null;
7894
+ return t2 ? E(t2) : null;
7724
7895
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
7725
7896
  }
7726
7897
  destroy() {
@@ -7750,9 +7921,9 @@ const z = class n extends BasePlugin {
7750
7921
  const t3 = [], s3 = /* @__PURE__ */ new Set();
7751
7922
  for (const n9 of e15.chapters) {
7752
7923
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
7753
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
7754
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
7755
- t3.push(...A(n9));
7924
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
7925
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
7926
+ t3.push(...F(n9));
7756
7927
  }
7757
7928
  return t3;
7758
7929
  })(t2);
@@ -7760,7 +7931,7 @@ const z = class n extends BasePlugin {
7760
7931
  const r2 = new Set(this.chapterStatus.keys()), a2 = new Set(t2.chapters.map((e15) => e15.chapterId));
7761
7932
  for (const n9 of r2) a2.has(n9) || (this.closeChapter(n9), this.chapterStatus.delete(n9), this.chapterErrorMessages.delete(n9), this.chapterUrlsCache.delete(n9), this.pendingChapterUrlsPromises.delete(n9));
7762
7933
  for (const n9 of t2.chapters) this.chapterStatus.has(n9.chapterId) || this.chapterStatus.set(n9.chapterId, "idle");
7763
- this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = R$1(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
7934
+ this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = G$1(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
7764
7935
  }
7765
7936
  eagerPrefetchFromManifest() {
7766
7937
  const e14 = this.config.prefetchChapters ?? 0;
@@ -7777,7 +7948,7 @@ const z = class n extends BasePlugin {
7777
7948
  }
7778
7949
  getSegmentPlanForChapter(e14) {
7779
7950
  const t2 = this.findChapter(e14);
7780
- return t2 ? T(t2) : null;
7951
+ return t2 ? E(t2) : null;
7781
7952
  }
7782
7953
  anySegmentOpen(e14) {
7783
7954
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -7821,7 +7992,7 @@ const z = class n extends BasePlugin {
7821
7992
  if (!e15) continue;
7822
7993
  const s2 = this.findChapter(e15.chapterId);
7823
7994
  if (!s2) continue;
7824
- const n9 = N(s2, e15.localPageIndex);
7995
+ const n9 = T(s2, e15.localPageIndex);
7825
7996
  if (!this.isSegmentDocumentOpen(e15.chapterId, n9)) return true;
7826
7997
  }
7827
7998
  return false;
@@ -7830,15 +8001,27 @@ const z = class n extends BasePlugin {
7830
8001
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
7831
8002
  }
7832
8003
  clearVisibleSegmentBacklog() {
7833
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
8004
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
8005
+ }
8006
+ chapterNeedsResolvedUrls(e14) {
8007
+ var t2;
8008
+ const s2 = e14.source;
8009
+ return !$$1(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
8010
+ }
8011
+ ensureVisibleChapterUrls(e14) {
8012
+ const { chapterId: t2 } = e14;
8013
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
8014
+ }).finally(() => {
8015
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
8016
+ })), false));
7834
8017
  }
7835
8018
  segmentStartGlobalIndex(e14, t2) {
7836
8019
  var s2;
7837
8020
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
7838
8021
  const n9 = this.findChapter(e14);
7839
8022
  if (!n9) return Number.MAX_SAFE_INTEGER;
7840
- const r2 = T(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
7841
- if (o2) return o2.globalPageIndex;
8023
+ const r2 = E(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
8024
+ if (i2) return i2.globalPageIndex;
7842
8025
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
7843
8026
  }
7844
8027
  scheduleVisibleSegmentLoads(e14) {
@@ -7851,11 +8034,18 @@ const z = class n extends BasePlugin {
7851
8034
  }
7852
8035
  pumpVisibleSegmentBacklog() {
7853
8036
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
8037
+ let t2 = 0;
7854
8038
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
7855
8039
  const e15 = this.visibleSegmentBacklog.shift();
7856
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
7857
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
7858
- })));
8040
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8041
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8042
+ const s2 = this.findChapter(e15.chapterId);
8043
+ if (s2) {
8044
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
8045
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
8046
+ });
8047
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8048
+ }
7859
8049
  }
7860
8050
  }
7861
8051
  prepareChapterForSegmentLoad(e14) {
@@ -7868,14 +8058,14 @@ const z = class n extends BasePlugin {
7868
8058
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
7869
8059
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
7870
8060
  this.lastVisibleIndicesKey = t2;
7871
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
8061
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
7872
8062
  if (t3 < 0) return;
7873
8063
  const s3 = this.findChapter(e15);
7874
8064
  if (!s3) return;
7875
- const n10 = T(s3);
8065
+ const n10 = E(s3);
7876
8066
  if (n10 && t3 >= n10.segments.length) return;
7877
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
7878
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
8067
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
8068
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
7879
8069
  };
7880
8070
  for (const d2 of e14) {
7881
8071
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -7883,8 +8073,8 @@ const z = class n extends BasePlugin {
7883
8073
  a2.add(e15.chapterId);
7884
8074
  const t3 = this.findChapter(e15.chapterId);
7885
8075
  if (!t3) continue;
7886
- const s3 = N(t3, e15.localPageIndex);
7887
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
8076
+ const s3 = T(t3, e15.localPageIndex);
8077
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
7888
8078
  });
7889
8079
  }
7890
8080
  if (0 === a2.size) return;
@@ -7892,19 +8082,19 @@ const z = class n extends BasePlugin {
7892
8082
  if (h2) {
7893
8083
  const e15 = this.findChapter(h2.chapterId);
7894
8084
  if (e15) {
7895
- const t3 = N(e15, h2.localPageIndex);
7896
- i2(h2.chapterId, t3 + 1);
8085
+ const t3 = T(e15, h2.localPageIndex);
8086
+ o2(h2.chapterId, t3 + 1);
7897
8087
  }
7898
8088
  }
7899
8089
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
7900
- const l = [...o2.entries()].map(([e15, t3]) => {
8090
+ const l = [...i2.entries()].map(([e15, t3]) => {
7901
8091
  const [s3, n10] = e15.split(":");
7902
8092
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
7903
8093
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
7904
8094
  if (this.scheduleVisibleSegmentLoads(l), h2) {
7905
8095
  const e15 = this.findChapter(h2.chapterId);
7906
8096
  if (e15) {
7907
- const t3 = N(e15, h2.localPageIndex);
8097
+ const t3 = T(e15, h2.localPageIndex);
7908
8098
  this.isSegmentDocumentOpen(h2.chapterId, t3) || (this.prepareChapterForSegmentLoad(h2.chapterId), this.loadSegmentNow(h2.chapterId, t3).finally(() => {
7909
8099
  this.pumpVisibleSegmentBacklog();
7910
8100
  }));
@@ -7913,7 +8103,7 @@ const z = class n extends BasePlugin {
7913
8103
  if (this.config.autoActivateOnLoad ?? 1) {
7914
8104
  const e15 = this.virtualPageMap.atIndex(r2);
7915
8105
  if (e15) {
7916
- const t3 = F(this.findChapter(e15.chapterId), e15.localPageIndex);
8106
+ const t3 = N(this.findChapter(e15.chapterId), e15.localPageIndex);
7917
8107
  if (this.documentManager.provides().isDocumentOpen(t3.documentId)) {
7918
8108
  this.documentManager.provides().getActiveDocumentId() !== t3.documentId && this.documentManager.provides().setActiveDocument(t3.documentId);
7919
8109
  }
@@ -7949,17 +8139,17 @@ const z = class n extends BasePlugin {
7949
8139
  this.prepareChapterForSegmentLoad(e14);
7950
8140
  const s2 = this.findChapter(e14);
7951
8141
  if (!s2) return Promise.resolve("error");
7952
- const n9 = T(s2);
8142
+ const n9 = E(s2);
7953
8143
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
7954
8144
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
7955
8145
  const r2 = n9.segments[t2], a2 = r2.documentId;
7956
8146
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
7957
- const o2 = this.pendingSegmentLoadPromises.get(a2);
7958
- if (o2) return o2;
7959
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
8147
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
8148
+ if (i2) return i2;
8149
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
7960
8150
  this.pendingSegmentLoadPromises.delete(a2);
7961
8151
  });
7962
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
8152
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
7963
8153
  }
7964
8154
  ensureSingleDocumentChapter(e14) {
7965
8155
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8005,14 +8195,14 @@ const z = class n extends BasePlugin {
8005
8195
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
8006
8196
  }
8007
8197
  async resolvePdfPayload(e14, t2) {
8008
- const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = D(e14);
8198
+ const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = k(e14);
8009
8199
  if (a2) {
8010
- const s3 = E(L(e14), a2);
8200
+ const s3 = V(L(e14), a2);
8011
8201
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
8012
8202
  const r3 = await this.resolveChapterUrls(e14);
8013
8203
  if (r3.length !== s3) {
8014
- const [t3, o2] = e14.globalPageRange;
8015
- this.logger.warn(n.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
8204
+ const [t3, i2] = e14.globalPageRange;
8205
+ this.logger.warn(n.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
8016
8206
  }
8017
8207
  return this.openPayloadFromUrl(e14, t2, r3);
8018
8208
  }
@@ -8035,20 +8225,20 @@ const z = class n extends BasePlugin {
8035
8225
  }
8036
8226
  async startLoadSegment(e14, t2) {
8037
8227
  var s2;
8038
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8228
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8039
8229
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
8040
8230
  try {
8041
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8231
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8042
8232
  l.wait((t4) => e15(t4), (e16) => t3(e16));
8043
8233
  });
8044
8234
  return await new Promise((e15, t3) => {
8045
8235
  d2.task.wait(() => e15(), (e16) => t3(e16));
8046
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8047
- } catch (i2) {
8236
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8237
+ } catch (o2) {
8048
8238
  const e15 = this.chapterStatus.get(r2);
8049
8239
  if ("loaded" !== e15 && "password-required" !== e15) {
8050
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
8051
- this.logger.error(n.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8240
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
8241
+ this.logger.error(n.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8052
8242
  }
8053
8243
  return this.chapterStatus.get(r2) ?? "error";
8054
8244
  }
@@ -8072,22 +8262,22 @@ const z = class n extends BasePlugin {
8072
8262
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
8073
8263
  const a2 = this.findChapter(t2);
8074
8264
  if (!a2) return;
8075
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
8076
- this.passwordAttempts.set(t2, o2);
8077
- let i2 = null;
8265
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
8266
+ this.passwordAttempts.set(t2, i2);
8267
+ let o2 = null;
8078
8268
  try {
8079
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
8269
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
8080
8270
  } catch (h2) {
8081
8271
  this.logger.error(n.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
8082
8272
  }
8083
- if (!i2) return void this.updateStatus(t2, "password-required");
8273
+ if (!o2) return void this.updateStatus(t2, "password-required");
8084
8274
  this.updateStatus(t2, "loading");
8085
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
8275
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
8086
8276
  }, () => {
8087
8277
  });
8088
8278
  }
8089
8279
  closeChapter(e14) {
8090
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? T(t2) : null, r2 = (e15) => {
8280
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? E(t2) : null, r2 = (e15) => {
8091
8281
  if (s2.isDocumentOpen(e15)) {
8092
8282
  s2.closeDocument(e15).wait(() => {
8093
8283
  }, () => {
@@ -8103,8 +8293,8 @@ const z = class n extends BasePlugin {
8103
8293
  "error" === t2 && s2 ? this.chapterErrorMessages.set(e14, s2) : "loading" !== t2 && "loaded" !== t2 || this.chapterErrorMessages.delete(e14), n9 !== t2 && (this.chapterStatus.set(e14, t2), this.statusChange$.emit({ chapterId: e14, status: t2, ..."error" === t2 && s2 ? { error: { message: s2 } } : {} }));
8104
8294
  }
8105
8295
  };
8106
- z.id = "chapter-manager";
8107
- let B = z;
8296
+ B.id = "chapter-manager";
8297
+ let z = B;
8108
8298
  function K(e14) {
8109
8299
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
8110
8300
  }
@@ -8130,10 +8320,10 @@ function X(e14) {
8130
8320
  })(e14), n9 = (function(e15, t3) {
8131
8321
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
8132
8322
  })(s2, t2), r2 = K(e14.source) ? e14.source.urls : void 0, a2 = [];
8133
- for (let o2 = 0; o2 < n9; o2++) {
8134
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
8323
+ for (let i2 = 0; i2 < n9; i2++) {
8324
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
8135
8325
  if (n10 >= s2) break;
8136
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: H(e14.chapterId, o2) });
8326
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: H(e14.chapterId, i2) });
8137
8327
  }
8138
8328
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
8139
8329
  }
@@ -8171,10 +8361,10 @@ function ne(e14) {
8171
8361
  const t2 = Z(e14);
8172
8362
  if (!t2) return null;
8173
8363
  const s2 = Q(e14), n9 = se(s2, t2), r2 = Y(e14.source) ? e14.source.urls : void 0, a2 = [];
8174
- for (let o2 = 0; o2 < n9; o2++) {
8175
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
8364
+ for (let i2 = 0; i2 < n9; i2++) {
8365
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
8176
8366
  if (n10 >= s2) break;
8177
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: ee(e14.chapterId, o2) });
8367
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: ee(e14.chapterId, i2) });
8178
8368
  }
8179
8369
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
8180
8370
  }
@@ -8194,21 +8384,21 @@ function ae(e14, t2) {
8194
8384
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
8195
8385
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
8196
8386
  }
8197
- function oe(e14) {
8387
+ function ie(e14) {
8198
8388
  var t2;
8199
8389
  const s2 = Z(e14);
8200
8390
  if (!s2) return [];
8201
8391
  const n9 = [], { chapterId: r2 } = e14, a2 = Q(e14);
8202
8392
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
8203
- const o2 = e14.source;
8204
- if (!Y(o2)) return n9;
8205
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
8206
- const i2 = se(a2, s2);
8207
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
8208
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
8393
+ const i2 = e14.source;
8394
+ if (!Y(i2)) return n9;
8395
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
8396
+ const o2 = se(a2, s2);
8397
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
8398
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
8209
8399
  return n9;
8210
8400
  }
8211
- const ie = { kind: "first-wins" };
8401
+ const oe = { kind: "first-wins" };
8212
8402
  function he(e14, t2, s2) {
8213
8403
  if (1 === t2.length) return t2[0].chapterId;
8214
8404
  switch (s2.kind) {
@@ -8261,8 +8451,8 @@ class le {
8261
8451
  return s2 ? this._pages.indexOf(s2) : -1;
8262
8452
  }
8263
8453
  }
8264
- function de(e14, t2 = ie) {
8265
- const s2 = (function(e15, t3 = ie) {
8454
+ function de(e14, t2 = oe) {
8455
+ const s2 = (function(e15, t3 = oe) {
8266
8456
  const s3 = /* @__PURE__ */ new Map();
8267
8457
  for (const r3 of e15.chapters) {
8268
8458
  const [e16, t4] = r3.globalPageRange;
@@ -8275,23 +8465,23 @@ function de(e14, t2 = ie) {
8275
8465
  for (const [r3, a3] of s3) n10.set(r3, he(r3, a3, t3));
8276
8466
  return n10;
8277
8467
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
8278
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
8468
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
8279
8469
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
8280
- let o2 = 0;
8281
- for (const i2 of r2) {
8282
- const e15 = s2.get(i2), t3 = n9.get(e15);
8470
+ let i2 = 0;
8471
+ for (const o2 of r2) {
8472
+ const e15 = s2.get(o2), t3 = n9.get(e15);
8283
8473
  if (!t3) continue;
8284
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
8285
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
8474
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
8475
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
8286
8476
  }
8287
8477
  return new le(a2);
8288
8478
  }
8289
8479
  const ce = class n2 extends BasePlugin {
8290
8480
  constructor(e14, n9) {
8291
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = ie, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8481
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = oe, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8292
8482
  }
8293
8483
  async initialize(e14) {
8294
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? ie, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
8484
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? oe, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
8295
8485
  const { chapterId: t2 } = te(e15.id);
8296
8486
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
8297
8487
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -8343,9 +8533,9 @@ const ce = class n2 extends BasePlugin {
8343
8533
  const t3 = [], s3 = /* @__PURE__ */ new Set();
8344
8534
  for (const n9 of e15.chapters) {
8345
8535
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
8346
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
8347
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
8348
- t3.push(...oe(n9));
8536
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
8537
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
8538
+ t3.push(...ie(n9));
8349
8539
  }
8350
8540
  return t3;
8351
8541
  })(t2);
@@ -8423,15 +8613,27 @@ const ce = class n2 extends BasePlugin {
8423
8613
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
8424
8614
  }
8425
8615
  clearVisibleSegmentBacklog() {
8426
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
8616
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
8617
+ }
8618
+ chapterNeedsResolvedUrls(e14) {
8619
+ var t2;
8620
+ const s2 = e14.source;
8621
+ return !Y(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
8622
+ }
8623
+ ensureVisibleChapterUrls(e14) {
8624
+ const { chapterId: t2 } = e14;
8625
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
8626
+ }).finally(() => {
8627
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
8628
+ })), false));
8427
8629
  }
8428
8630
  segmentStartGlobalIndex(e14, t2) {
8429
8631
  var s2;
8430
8632
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
8431
8633
  const n9 = this.findChapter(e14);
8432
8634
  if (!n9) return Number.MAX_SAFE_INTEGER;
8433
- const r2 = ne(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
8434
- if (o2) return o2.globalPageIndex;
8635
+ const r2 = ne(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
8636
+ if (i2) return i2.globalPageIndex;
8435
8637
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
8436
8638
  }
8437
8639
  scheduleVisibleSegmentLoads(e14) {
@@ -8444,11 +8646,18 @@ const ce = class n2 extends BasePlugin {
8444
8646
  }
8445
8647
  pumpVisibleSegmentBacklog() {
8446
8648
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
8649
+ let t2 = 0;
8447
8650
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
8448
8651
  const e15 = this.visibleSegmentBacklog.shift();
8449
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
8450
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
8451
- })));
8652
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8653
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8654
+ const s2 = this.findChapter(e15.chapterId);
8655
+ if (s2) {
8656
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
8657
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
8658
+ });
8659
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8660
+ }
8452
8661
  }
8453
8662
  }
8454
8663
  prepareChapterForSegmentLoad(e14) {
@@ -8461,14 +8670,14 @@ const ce = class n2 extends BasePlugin {
8461
8670
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
8462
8671
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
8463
8672
  this.lastVisibleIndicesKey = t2;
8464
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
8673
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
8465
8674
  if (t3 < 0) return;
8466
8675
  const s3 = this.findChapter(e15);
8467
8676
  if (!s3) return;
8468
8677
  const n10 = ne(s3);
8469
8678
  if (n10 && t3 >= n10.segments.length) return;
8470
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
8471
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
8679
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
8680
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
8472
8681
  };
8473
8682
  for (const d2 of e14) {
8474
8683
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -8477,7 +8686,7 @@ const ce = class n2 extends BasePlugin {
8477
8686
  const t3 = this.findChapter(e15.chapterId);
8478
8687
  if (!t3) continue;
8479
8688
  const s3 = ae(t3, e15.localPageIndex);
8480
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
8689
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
8481
8690
  });
8482
8691
  }
8483
8692
  if (0 === a2.size) return;
@@ -8486,11 +8695,11 @@ const ce = class n2 extends BasePlugin {
8486
8695
  const e15 = this.findChapter(h2.chapterId);
8487
8696
  if (e15) {
8488
8697
  const t3 = ae(e15, h2.localPageIndex);
8489
- i2(h2.chapterId, t3 + 1);
8698
+ o2(h2.chapterId, t3 + 1);
8490
8699
  }
8491
8700
  }
8492
8701
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
8493
- const l = [...o2.entries()].map(([e15, t3]) => {
8702
+ const l = [...i2.entries()].map(([e15, t3]) => {
8494
8703
  const [s3, n10] = e15.split(":");
8495
8704
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
8496
8705
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
@@ -8547,12 +8756,12 @@ const ce = class n2 extends BasePlugin {
8547
8756
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
8548
8757
  const r2 = n9.segments[t2], a2 = r2.documentId;
8549
8758
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8550
- const o2 = this.pendingSegmentLoadPromises.get(a2);
8551
- if (o2) return o2;
8552
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
8759
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
8760
+ if (i2) return i2;
8761
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
8553
8762
  this.pendingSegmentLoadPromises.delete(a2);
8554
8763
  });
8555
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
8764
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
8556
8765
  }
8557
8766
  ensureSingleDocumentChapter(e14) {
8558
8767
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8604,8 +8813,8 @@ const ce = class n2 extends BasePlugin {
8604
8813
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
8605
8814
  const r3 = await this.resolveChapterUrls(e14);
8606
8815
  if (r3.length !== s3) {
8607
- const [t3, o2] = e14.globalPageRange;
8608
- this.logger.warn(n2.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
8816
+ const [t3, i2] = e14.globalPageRange;
8817
+ this.logger.warn(n2.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
8609
8818
  }
8610
8819
  return this.openPayloadFromUrl(e14, t2, r3);
8611
8820
  }
@@ -8628,20 +8837,20 @@ const ce = class n2 extends BasePlugin {
8628
8837
  }
8629
8838
  async startLoadSegment(e14, t2) {
8630
8839
  var s2;
8631
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8840
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8632
8841
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
8633
8842
  try {
8634
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8843
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8635
8844
  l.wait((t4) => e15(t4), (e16) => t3(e16));
8636
8845
  });
8637
8846
  return await new Promise((e15, t3) => {
8638
8847
  d2.task.wait(() => e15(), (e16) => t3(e16));
8639
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8640
- } catch (i2) {
8848
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8849
+ } catch (o2) {
8641
8850
  const e15 = this.chapterStatus.get(r2);
8642
8851
  if ("loaded" !== e15 && "password-required" !== e15) {
8643
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
8644
- this.logger.error(n2.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8852
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
8853
+ this.logger.error(n2.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8645
8854
  }
8646
8855
  return this.chapterStatus.get(r2) ?? "error";
8647
8856
  }
@@ -8665,17 +8874,17 @@ const ce = class n2 extends BasePlugin {
8665
8874
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
8666
8875
  const a2 = this.findChapter(t2);
8667
8876
  if (!a2) return;
8668
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
8669
- this.passwordAttempts.set(t2, o2);
8670
- let i2 = null;
8877
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
8878
+ this.passwordAttempts.set(t2, i2);
8879
+ let o2 = null;
8671
8880
  try {
8672
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
8881
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
8673
8882
  } catch (h2) {
8674
8883
  this.logger.error(n2.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
8675
8884
  }
8676
- if (!i2) return void this.updateStatus(t2, "password-required");
8885
+ if (!o2) return void this.updateStatus(t2, "password-required");
8677
8886
  this.updateStatus(t2, "loading");
8678
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
8887
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
8679
8888
  }, () => {
8680
8889
  });
8681
8890
  }
@@ -8698,7 +8907,7 @@ const ce = class n2 extends BasePlugin {
8698
8907
  };
8699
8908
  ce.id = "chapter-manager";
8700
8909
  let ue = ce;
8701
- function ge(e14, t2) {
8910
+ function pe(e14, t2) {
8702
8911
  if (e14 === t2) return true;
8703
8912
  if (e14.totalHeight !== t2.totalHeight || e14.totalWidth !== t2.totalWidth || e14.pageGap !== t2.pageGap || e14.items.length !== t2.items.length) return false;
8704
8913
  for (let s2 = 0; s2 < e14.items.length; s2++) {
@@ -8707,9 +8916,9 @@ function ge(e14, t2) {
8707
8916
  }
8708
8917
  return true;
8709
8918
  }
8710
- const pe = class extends BasePlugin {
8919
+ const ge = class extends BasePlugin {
8711
8920
  constructor(e14, s2) {
8712
- super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, ge), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ue.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8921
+ super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, pe), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ue.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8713
8922
  }
8714
8923
  async initialize(e14) {
8715
8924
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
@@ -8804,16 +9013,16 @@ const pe = class extends BasePlugin {
8804
9013
  if (!this.virtualPageMap) return;
8805
9014
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14), r2 = this.virtualPageMap.pagesOfChapter(e14);
8806
9015
  if (!n9 || 0 === r2.length) return;
8807
- const a2 = X(n9), o2 = this.documentManager.provides(), i2 = [], h2 = (e15, t3, s3, n10) => {
9016
+ const a2 = X(n9), i2 = this.documentManager.provides(), o2 = [], h2 = (e15, t3, s3, n10) => {
8808
9017
  var r3;
8809
- const a3 = o2.getDocument(e15);
9018
+ const a3 = i2.getDocument(e15);
8810
9019
  if (!(null == (r3 = null == a3 ? void 0 : a3.pages) ? void 0 : r3.length)) return false;
8811
9020
  const h3 = this.getDocumentScale(e15);
8812
- for (let o3 = 0; o3 < n10; o3++) {
8813
- const e16 = a3.pages[s3 + o3];
8814
- (null == e16 ? void 0 : e16.size) && i2.push({ localPageIndex: t3 + o3, width: e16.size.width * h3, height: e16.size.height * h3 });
9021
+ for (let i3 = 0; i3 < n10; i3++) {
9022
+ const e16 = a3.pages[s3 + i3];
9023
+ (null == e16 ? void 0 : e16.size) && o2.push({ localPageIndex: t3 + i3, width: e16.size.width * h3, height: e16.size.height * h3 });
8815
9024
  }
8816
- return i2.length > 0;
9025
+ return o2.length > 0;
8817
9026
  };
8818
9027
  if (a2) for (const l of a2.segments) {
8819
9028
  if (!s2.isSegmentLoaded(e14, l.index)) continue;
@@ -8821,7 +9030,7 @@ const pe = class extends BasePlugin {
8821
9030
  h2(l.documentId, l.localPageStart, 0, t3);
8822
9031
  }
8823
9032
  else s2.isSegmentLoaded(e14, 0) && h2(e14, 0, 0, r2.length);
8824
- 0 !== i2.length ? this.applyChapterPageSizes(e14, i2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
9033
+ 0 !== o2.length ? this.applyChapterPageSizes(e14, o2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
8825
9034
  }
8826
9035
  onScaleChanged(e14) {
8827
9036
  const t2 = this.coreState.core.documents[e14];
@@ -8848,7 +9057,7 @@ const pe = class extends BasePlugin {
8848
9057
  expandIndicesWithBuffer(e14, t2) {
8849
9058
  if (0 === e14.length || t2 <= 0) return [];
8850
9059
  const s2 = this.config.bufferSize, n9 = Math.max(0, e14[0] - s2), r2 = Math.min(t2 - 1, e14[e14.length - 1] + s2), a2 = [];
8851
- for (let o2 = n9; o2 <= r2; o2++) a2.push(o2);
9060
+ for (let i2 = n9; i2 <= r2; i2++) a2.push(i2);
8852
9061
  return a2;
8853
9062
  }
8854
9063
  recomputeVisible() {
@@ -8865,13 +9074,13 @@ const pe = class extends BasePlugin {
8865
9074
  r2++;
8866
9075
  }
8867
9076
  const a2 = [];
8868
- let o2 = n9, i2 = -1;
9077
+ let i2 = n9, o2 = -1;
8869
9078
  for (let h2 = n9; h2 < r2; h2++) {
8870
9079
  a2.push(h2);
8871
9080
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
8872
- r3 > i2 && (i2 = r3, o2 = h2);
9081
+ r3 > o2 && (o2 = r3, i2 = h2);
8873
9082
  }
8874
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
9083
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
8875
9084
  }
8876
9085
  findFirstVisibleIndex(e14) {
8877
9086
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -8901,9 +9110,9 @@ const pe = class extends BasePlugin {
8901
9110
  if (!this.viewportEl || e14 < 0 || e14 >= this.offsets.length || !this.virtualPageMap) return;
8902
9111
  const r2 = (null == s2 ? void 0 : s2.marginTop) ?? 80, a2 = this.virtualPageMap.atIndex(e14);
8903
9112
  if (!a2) return void this.scrollToIndex(e14, s2);
8904
- const o2 = this.pageSizes.get(e14), i2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = i2 ? null == (n9 = this.documentManager.provides().getDocument(i2.documentId)) ? void 0 : n9.pages[i2.pageIndex] : void 0;
8905
- if (!o2 || !h2) return void this.scrollToIndex(e14, s2);
8906
- const l = o2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
9113
+ const i2 = this.pageSizes.get(e14), o2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = o2 ? null == (n9 = this.documentManager.provides().getDocument(o2.documentId)) ? void 0 : n9.pages[o2.pageIndex] : void 0;
9114
+ if (!i2 || !h2) return void this.scrollToIndex(e14, s2);
9115
+ const l = i2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
8907
9116
  this.viewportEl.scrollTo({ top: Math.max(0, d2), behavior: (null == s2 ? void 0 : s2.behavior) ?? "auto" });
8908
9117
  }
8909
9118
  scrollToChapter(e14, t2) {
@@ -8923,7 +9132,7 @@ const pe = class extends BasePlugin {
8923
9132
  s2 && this.scrollToIndex(s2.globalPageIndex, t2);
8924
9133
  }
8925
9134
  };
8926
- pe.id = "chapter-scroll";
9135
+ ge.id = "chapter-scroll";
8927
9136
  function Pe(e14) {
8928
9137
  const [t2, s2] = e14.localPageRange;
8929
9138
  return s2 - t2 + 1;
@@ -8952,10 +9161,10 @@ function Me(e14) {
8952
9161
  const t2 = Se(e14);
8953
9162
  if (!t2) return null;
8954
9163
  const s2 = Pe(e14), n9 = ye(s2, t2), r2 = be(e14.source) ? e14.source.urls : void 0, a2 = [];
8955
- for (let o2 = 0; o2 < n9; o2++) {
8956
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
9164
+ for (let i2 = 0; i2 < n9; i2++) {
9165
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
8957
9166
  if (n10 >= s2) break;
8958
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: Ce(e14.chapterId, o2) });
9167
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Ce(e14.chapterId, i2) });
8959
9168
  }
8960
9169
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
8961
9170
  }
@@ -8981,16 +9190,16 @@ function $e(e14) {
8981
9190
  if (!s2) return [];
8982
9191
  const n9 = [], { chapterId: r2 } = e14, a2 = Pe(e14);
8983
9192
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
8984
- const o2 = e14.source;
8985
- if (!be(o2)) return n9;
8986
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
8987
- const i2 = ye(a2, s2);
8988
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
8989
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
9193
+ const i2 = e14.source;
9194
+ if (!be(i2)) return n9;
9195
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
9196
+ const o2 = ye(a2, s2);
9197
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
9198
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
8990
9199
  return n9;
8991
9200
  }
8992
- const De = { kind: "first-wins" };
8993
- function ke(e14, t2, s2) {
9201
+ const ke = { kind: "first-wins" };
9202
+ function De(e14, t2, s2) {
8994
9203
  if (1 === t2.length) return t2[0].chapterId;
8995
9204
  switch (s2.kind) {
8996
9205
  case "first-wins":
@@ -9042,8 +9251,8 @@ let Ue = class {
9042
9251
  return s2 ? this._pages.indexOf(s2) : -1;
9043
9252
  }
9044
9253
  };
9045
- function Ee(e14, t2 = De) {
9046
- const s2 = (function(e15, t3 = De) {
9254
+ function Ve(e14, t2 = ke) {
9255
+ const s2 = (function(e15, t3 = ke) {
9047
9256
  const s3 = /* @__PURE__ */ new Map();
9048
9257
  for (const r3 of e15.chapters) {
9049
9258
  const [e16, t4] = r3.globalPageRange;
@@ -9053,26 +9262,26 @@ function Ee(e14, t2 = De) {
9053
9262
  }
9054
9263
  }
9055
9264
  const n10 = /* @__PURE__ */ new Map();
9056
- for (const [r3, a3] of s3) n10.set(r3, ke(r3, a3, t3));
9265
+ for (const [r3, a3] of s3) n10.set(r3, De(r3, a3, t3));
9057
9266
  return n10;
9058
9267
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
9059
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
9268
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
9060
9269
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
9061
- let o2 = 0;
9062
- for (const i2 of r2) {
9063
- const e15 = s2.get(i2), t3 = n9.get(e15);
9270
+ let i2 = 0;
9271
+ for (const o2 of r2) {
9272
+ const e15 = s2.get(o2), t3 = n9.get(e15);
9064
9273
  if (!t3) continue;
9065
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
9066
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
9274
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
9275
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
9067
9276
  }
9068
9277
  return new Ue(a2);
9069
9278
  }
9070
- const Te = class n3 extends BasePlugin {
9279
+ const Ee = class n3 extends BasePlugin {
9071
9280
  constructor(e14, n9) {
9072
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = De, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9281
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = ke, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9073
9282
  }
9074
9283
  async initialize(e14) {
9075
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? De, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
9284
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? ke, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
9076
9285
  const { chapterId: t2 } = we(e15.id);
9077
9286
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
9078
9287
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -9124,8 +9333,8 @@ const Te = class n3 extends BasePlugin {
9124
9333
  const t3 = [], s3 = /* @__PURE__ */ new Set();
9125
9334
  for (const n9 of e15.chapters) {
9126
9335
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
9127
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
9128
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
9336
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
9337
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
9129
9338
  t3.push(...$e(n9));
9130
9339
  }
9131
9340
  return t3;
@@ -9134,7 +9343,7 @@ const Te = class n3 extends BasePlugin {
9134
9343
  const r2 = new Set(this.chapterStatus.keys()), a2 = new Set(t2.chapters.map((e15) => e15.chapterId));
9135
9344
  for (const n9 of r2) a2.has(n9) || (this.closeChapter(n9), this.chapterStatus.delete(n9), this.chapterErrorMessages.delete(n9), this.chapterUrlsCache.delete(n9), this.pendingChapterUrlsPromises.delete(n9));
9136
9345
  for (const n9 of t2.chapters) this.chapterStatus.has(n9.chapterId) || this.chapterStatus.set(n9.chapterId, "idle");
9137
- this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = Ee(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
9346
+ this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = Ve(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
9138
9347
  }
9139
9348
  eagerPrefetchFromManifest() {
9140
9349
  const e14 = this.config.prefetchChapters ?? 0;
@@ -9204,15 +9413,27 @@ const Te = class n3 extends BasePlugin {
9204
9413
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
9205
9414
  }
9206
9415
  clearVisibleSegmentBacklog() {
9207
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
9416
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
9417
+ }
9418
+ chapterNeedsResolvedUrls(e14) {
9419
+ var t2;
9420
+ const s2 = e14.source;
9421
+ return !be(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
9422
+ }
9423
+ ensureVisibleChapterUrls(e14) {
9424
+ const { chapterId: t2 } = e14;
9425
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
9426
+ }).finally(() => {
9427
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
9428
+ })), false));
9208
9429
  }
9209
9430
  segmentStartGlobalIndex(e14, t2) {
9210
9431
  var s2;
9211
9432
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
9212
9433
  const n9 = this.findChapter(e14);
9213
9434
  if (!n9) return Number.MAX_SAFE_INTEGER;
9214
- const r2 = Me(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
9215
- if (o2) return o2.globalPageIndex;
9435
+ const r2 = Me(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
9436
+ if (i2) return i2.globalPageIndex;
9216
9437
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
9217
9438
  }
9218
9439
  scheduleVisibleSegmentLoads(e14) {
@@ -9225,11 +9446,18 @@ const Te = class n3 extends BasePlugin {
9225
9446
  }
9226
9447
  pumpVisibleSegmentBacklog() {
9227
9448
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
9449
+ let t2 = 0;
9228
9450
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
9229
9451
  const e15 = this.visibleSegmentBacklog.shift();
9230
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
9231
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
9232
- })));
9452
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
9453
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
9454
+ const s2 = this.findChapter(e15.chapterId);
9455
+ if (s2) {
9456
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
9457
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
9458
+ });
9459
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
9460
+ }
9233
9461
  }
9234
9462
  }
9235
9463
  prepareChapterForSegmentLoad(e14) {
@@ -9242,14 +9470,14 @@ const Te = class n3 extends BasePlugin {
9242
9470
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
9243
9471
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
9244
9472
  this.lastVisibleIndicesKey = t2;
9245
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
9473
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
9246
9474
  if (t3 < 0) return;
9247
9475
  const s3 = this.findChapter(e15);
9248
9476
  if (!s3) return;
9249
9477
  const n10 = Me(s3);
9250
9478
  if (n10 && t3 >= n10.segments.length) return;
9251
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
9252
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
9479
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
9480
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
9253
9481
  };
9254
9482
  for (const d2 of e14) {
9255
9483
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -9258,7 +9486,7 @@ const Te = class n3 extends BasePlugin {
9258
9486
  const t3 = this.findChapter(e15.chapterId);
9259
9487
  if (!t3) continue;
9260
9488
  const s3 = Le(t3, e15.localPageIndex);
9261
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
9489
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
9262
9490
  });
9263
9491
  }
9264
9492
  if (0 === a2.size) return;
@@ -9267,11 +9495,11 @@ const Te = class n3 extends BasePlugin {
9267
9495
  const e15 = this.findChapter(h2.chapterId);
9268
9496
  if (e15) {
9269
9497
  const t3 = Le(e15, h2.localPageIndex);
9270
- i2(h2.chapterId, t3 + 1);
9498
+ o2(h2.chapterId, t3 + 1);
9271
9499
  }
9272
9500
  }
9273
9501
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
9274
- const l = [...o2.entries()].map(([e15, t3]) => {
9502
+ const l = [...i2.entries()].map(([e15, t3]) => {
9275
9503
  const [s3, n10] = e15.split(":");
9276
9504
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
9277
9505
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
@@ -9328,12 +9556,12 @@ const Te = class n3 extends BasePlugin {
9328
9556
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
9329
9557
  const r2 = n9.segments[t2], a2 = r2.documentId;
9330
9558
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
9331
- const o2 = this.pendingSegmentLoadPromises.get(a2);
9332
- if (o2) return o2;
9333
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
9559
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
9560
+ if (i2) return i2;
9561
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
9334
9562
  this.pendingSegmentLoadPromises.delete(a2);
9335
9563
  });
9336
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
9564
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
9337
9565
  }
9338
9566
  ensureSingleDocumentChapter(e14) {
9339
9567
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -9385,8 +9613,8 @@ const Te = class n3 extends BasePlugin {
9385
9613
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
9386
9614
  const r3 = await this.resolveChapterUrls(e14);
9387
9615
  if (r3.length !== s3) {
9388
- const [t3, o2] = e14.globalPageRange;
9389
- this.logger.warn(n3.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
9616
+ const [t3, i2] = e14.globalPageRange;
9617
+ this.logger.warn(n3.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
9390
9618
  }
9391
9619
  return this.openPayloadFromUrl(e14, t2, r3);
9392
9620
  }
@@ -9409,20 +9637,20 @@ const Te = class n3 extends BasePlugin {
9409
9637
  }
9410
9638
  async startLoadSegment(e14, t2) {
9411
9639
  var s2;
9412
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
9640
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
9413
9641
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
9414
9642
  try {
9415
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
9643
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
9416
9644
  l.wait((t4) => e15(t4), (e16) => t3(e16));
9417
9645
  });
9418
9646
  return await new Promise((e15, t3) => {
9419
9647
  d2.task.wait(() => e15(), (e16) => t3(e16));
9420
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
9421
- } catch (i2) {
9648
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
9649
+ } catch (o2) {
9422
9650
  const e15 = this.chapterStatus.get(r2);
9423
9651
  if ("loaded" !== e15 && "password-required" !== e15) {
9424
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
9425
- this.logger.error(n3.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
9652
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
9653
+ this.logger.error(n3.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
9426
9654
  }
9427
9655
  return this.chapterStatus.get(r2) ?? "error";
9428
9656
  }
@@ -9446,17 +9674,17 @@ const Te = class n3 extends BasePlugin {
9446
9674
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
9447
9675
  const a2 = this.findChapter(t2);
9448
9676
  if (!a2) return;
9449
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
9450
- this.passwordAttempts.set(t2, o2);
9451
- let i2 = null;
9677
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
9678
+ this.passwordAttempts.set(t2, i2);
9679
+ let o2 = null;
9452
9680
  try {
9453
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
9681
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
9454
9682
  } catch (h2) {
9455
9683
  this.logger.error(n3.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
9456
9684
  }
9457
- if (!i2) return void this.updateStatus(t2, "password-required");
9685
+ if (!o2) return void this.updateStatus(t2, "password-required");
9458
9686
  this.updateStatus(t2, "loading");
9459
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
9687
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
9460
9688
  }, () => {
9461
9689
  });
9462
9690
  }
@@ -9477,25 +9705,25 @@ const Te = class n3 extends BasePlugin {
9477
9705
  "error" === t2 && s2 ? this.chapterErrorMessages.set(e14, s2) : "loading" !== t2 && "loaded" !== t2 || this.chapterErrorMessages.delete(e14), n9 !== t2 && (this.chapterStatus.set(e14, t2), this.statusChange$.emit({ chapterId: e14, status: t2, ..."error" === t2 && s2 ? { error: { message: s2 } } : {} }));
9478
9706
  }
9479
9707
  };
9480
- Te.id = "chapter-manager";
9481
- let Fe = Te;
9482
- function Ne2(e14) {
9708
+ Ee.id = "chapter-manager";
9709
+ let Ne2 = Ee;
9710
+ function Te(e14) {
9483
9711
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
9484
9712
  }
9485
- function Ae(e14, t2) {
9713
+ function Fe(e14, t2) {
9486
9714
  return `${e14}#s${t2}`;
9487
9715
  }
9488
- function Oe(e14) {
9716
+ function Ae(e14) {
9489
9717
  const t2 = e14.lastIndexOf("#s");
9490
9718
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
9491
9719
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
9492
9720
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
9493
9721
  }
9494
- function Ve(e14) {
9722
+ function Oe(e14) {
9495
9723
  const t2 = (function(e15) {
9496
9724
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
9497
9725
  const t3 = e15.source;
9498
- return Ne2(t3) ? t3.segmentPageThreshold : null;
9726
+ return Te(t3) ? t3.segmentPageThreshold : null;
9499
9727
  })(e14);
9500
9728
  if (!t2) return null;
9501
9729
  const s2 = (function(e15) {
@@ -9503,31 +9731,31 @@ function Ve(e14) {
9503
9731
  return s3 - t3 + 1;
9504
9732
  })(e14), n9 = (function(e15, t3) {
9505
9733
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
9506
- })(s2, t2), r2 = Ne2(e14.source) ? e14.source.urls : void 0, a2 = [];
9507
- for (let o2 = 0; o2 < n9; o2++) {
9508
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
9734
+ })(s2, t2), r2 = Te(e14.source) ? e14.source.urls : void 0, a2 = [];
9735
+ for (let i2 = 0; i2 < n9; i2++) {
9736
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
9509
9737
  if (n10 >= s2) break;
9510
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: Ae(e14.chapterId, o2) });
9738
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Fe(e14.chapterId, i2) });
9511
9739
  }
9512
9740
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
9513
9741
  }
9514
- function Ge(e14, t2) {
9515
- const s2 = Ve(e14);
9742
+ function Re(e14, t2) {
9743
+ const s2 = Oe(e14);
9516
9744
  if (!s2) return 0;
9517
9745
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
9518
9746
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
9519
9747
  }
9520
- function Re(e14) {
9748
+ function Ge(e14) {
9521
9749
  const [t2, s2] = e14.localPageRange;
9522
9750
  return s2 - t2 + 1;
9523
9751
  }
9524
- function ze(e14) {
9752
+ function Be(e14) {
9525
9753
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
9526
9754
  }
9527
- function Be(e14) {
9755
+ function ze(e14) {
9528
9756
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
9529
9757
  const t2 = e14.source;
9530
- return ze(t2) ? t2.segmentPageThreshold : null;
9758
+ return Be(t2) ? t2.segmentPageThreshold : null;
9531
9759
  }
9532
9760
  function _e(e14, t2) {
9533
9761
  return `${e14}#s${t2}`;
@@ -9542,13 +9770,13 @@ function We(e14, t2) {
9542
9770
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
9543
9771
  }
9544
9772
  function Ke(e14) {
9545
- const t2 = Be(e14);
9773
+ const t2 = ze(e14);
9546
9774
  if (!t2) return null;
9547
- const s2 = Re(e14), n9 = We(s2, t2), r2 = ze(e14.source) ? e14.source.urls : void 0, a2 = [];
9548
- for (let o2 = 0; o2 < n9; o2++) {
9549
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
9775
+ const s2 = Ge(e14), n9 = We(s2, t2), r2 = Be(e14.source) ? e14.source.urls : void 0, a2 = [];
9776
+ for (let i2 = 0; i2 < n9; i2++) {
9777
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
9550
9778
  if (n10 >= s2) break;
9551
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: _e(e14.chapterId, o2) });
9779
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: _e(e14.chapterId, i2) });
9552
9780
  }
9553
9781
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
9554
9782
  }
@@ -9570,16 +9798,16 @@ function je(e14, t2) {
9570
9798
  }
9571
9799
  function Xe(e14) {
9572
9800
  var t2;
9573
- const s2 = Be(e14);
9801
+ const s2 = ze(e14);
9574
9802
  if (!s2) return [];
9575
- const n9 = [], { chapterId: r2 } = e14, a2 = Re(e14);
9803
+ const n9 = [], { chapterId: r2 } = e14, a2 = Ge(e14);
9576
9804
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
9577
- const o2 = e14.source;
9578
- if (!ze(o2)) return n9;
9579
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
9580
- const i2 = We(a2, s2);
9581
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
9582
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
9805
+ const i2 = e14.source;
9806
+ if (!Be(i2)) return n9;
9807
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
9808
+ const o2 = We(a2, s2);
9809
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
9810
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
9583
9811
  return n9;
9584
9812
  }
9585
9813
  const Je = { kind: "first-wins" };
@@ -9649,20 +9877,20 @@ function Ze(e14, t2 = Je) {
9649
9877
  for (const [r3, a3] of s3) n10.set(r3, Qe(r3, a3, t3));
9650
9878
  return n10;
9651
9879
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
9652
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
9880
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
9653
9881
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
9654
- let o2 = 0;
9655
- for (const i2 of r2) {
9656
- const e15 = s2.get(i2), t3 = n9.get(e15);
9882
+ let i2 = 0;
9883
+ for (const o2 of r2) {
9884
+ const e15 = s2.get(o2), t3 = n9.get(e15);
9657
9885
  if (!t3) continue;
9658
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
9659
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
9886
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
9887
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
9660
9888
  }
9661
9889
  return new Ye(a2);
9662
9890
  }
9663
9891
  const et2 = class n4 extends BasePlugin {
9664
9892
  constructor(e14, n9) {
9665
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Je, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9893
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Je, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9666
9894
  }
9667
9895
  async initialize(e14) {
9668
9896
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Je, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -9717,8 +9945,8 @@ const et2 = class n4 extends BasePlugin {
9717
9945
  const t3 = [], s3 = /* @__PURE__ */ new Set();
9718
9946
  for (const n9 of e15.chapters) {
9719
9947
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
9720
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
9721
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
9948
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
9949
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
9722
9950
  t3.push(...Xe(n9));
9723
9951
  }
9724
9952
  return t3;
@@ -9797,15 +10025,27 @@ const et2 = class n4 extends BasePlugin {
9797
10025
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
9798
10026
  }
9799
10027
  clearVisibleSegmentBacklog() {
9800
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
10028
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
10029
+ }
10030
+ chapterNeedsResolvedUrls(e14) {
10031
+ var t2;
10032
+ const s2 = e14.source;
10033
+ return !Be(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
10034
+ }
10035
+ ensureVisibleChapterUrls(e14) {
10036
+ const { chapterId: t2 } = e14;
10037
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
10038
+ }).finally(() => {
10039
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
10040
+ })), false));
9801
10041
  }
9802
10042
  segmentStartGlobalIndex(e14, t2) {
9803
10043
  var s2;
9804
10044
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
9805
10045
  const n9 = this.findChapter(e14);
9806
10046
  if (!n9) return Number.MAX_SAFE_INTEGER;
9807
- const r2 = Ke(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
9808
- if (o2) return o2.globalPageIndex;
10047
+ const r2 = Ke(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
10048
+ if (i2) return i2.globalPageIndex;
9809
10049
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
9810
10050
  }
9811
10051
  scheduleVisibleSegmentLoads(e14) {
@@ -9818,11 +10058,18 @@ const et2 = class n4 extends BasePlugin {
9818
10058
  }
9819
10059
  pumpVisibleSegmentBacklog() {
9820
10060
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
10061
+ let t2 = 0;
9821
10062
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
9822
10063
  const e15 = this.visibleSegmentBacklog.shift();
9823
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
9824
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
9825
- })));
10064
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10065
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10066
+ const s2 = this.findChapter(e15.chapterId);
10067
+ if (s2) {
10068
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
10069
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
10070
+ });
10071
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10072
+ }
9826
10073
  }
9827
10074
  }
9828
10075
  prepareChapterForSegmentLoad(e14) {
@@ -9835,14 +10082,14 @@ const et2 = class n4 extends BasePlugin {
9835
10082
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
9836
10083
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
9837
10084
  this.lastVisibleIndicesKey = t2;
9838
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
10085
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
9839
10086
  if (t3 < 0) return;
9840
10087
  const s3 = this.findChapter(e15);
9841
10088
  if (!s3) return;
9842
10089
  const n10 = Ke(s3);
9843
10090
  if (n10 && t3 >= n10.segments.length) return;
9844
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
9845
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
10091
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
10092
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
9846
10093
  };
9847
10094
  for (const d2 of e14) {
9848
10095
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -9851,7 +10098,7 @@ const et2 = class n4 extends BasePlugin {
9851
10098
  const t3 = this.findChapter(e15.chapterId);
9852
10099
  if (!t3) continue;
9853
10100
  const s3 = je(t3, e15.localPageIndex);
9854
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
10101
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
9855
10102
  });
9856
10103
  }
9857
10104
  if (0 === a2.size) return;
@@ -9860,11 +10107,11 @@ const et2 = class n4 extends BasePlugin {
9860
10107
  const e15 = this.findChapter(h2.chapterId);
9861
10108
  if (e15) {
9862
10109
  const t3 = je(e15, h2.localPageIndex);
9863
- i2(h2.chapterId, t3 + 1);
10110
+ o2(h2.chapterId, t3 + 1);
9864
10111
  }
9865
10112
  }
9866
10113
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
9867
- const l = [...o2.entries()].map(([e15, t3]) => {
10114
+ const l = [...i2.entries()].map(([e15, t3]) => {
9868
10115
  const [s3, n10] = e15.split(":");
9869
10116
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
9870
10117
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
@@ -9921,12 +10168,12 @@ const et2 = class n4 extends BasePlugin {
9921
10168
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
9922
10169
  const r2 = n9.segments[t2], a2 = r2.documentId;
9923
10170
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
9924
- const o2 = this.pendingSegmentLoadPromises.get(a2);
9925
- if (o2) return o2;
9926
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
10171
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
10172
+ if (i2) return i2;
10173
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
9927
10174
  this.pendingSegmentLoadPromises.delete(a2);
9928
10175
  });
9929
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
10176
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
9930
10177
  }
9931
10178
  ensureSingleDocumentChapter(e14) {
9932
10179
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -9946,7 +10193,7 @@ const et2 = class n4 extends BasePlugin {
9946
10193
  if (n9) return n9;
9947
10194
  const r2 = (async () => {
9948
10195
  const s3 = e14.source;
9949
- if (ze(s3)) {
10196
+ if (Be(s3)) {
9950
10197
  const e15 = [...s3.urls];
9951
10198
  return this.chapterUrlsCache.set(t2, e15), e15;
9952
10199
  }
@@ -9972,20 +10219,20 @@ const et2 = class n4 extends BasePlugin {
9972
10219
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
9973
10220
  }
9974
10221
  async resolvePdfPayload(e14, t2) {
9975
- const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = Be(e14);
10222
+ const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = ze(e14);
9976
10223
  if (a2) {
9977
- const s3 = We(Re(e14), a2);
10224
+ const s3 = We(Ge(e14), a2);
9978
10225
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
9979
10226
  const r3 = await this.resolveChapterUrls(e14);
9980
10227
  if (r3.length !== s3) {
9981
- const [t3, o2] = e14.globalPageRange;
9982
- this.logger.warn(n4.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
10228
+ const [t3, i2] = e14.globalPageRange;
10229
+ this.logger.warn(n4.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
9983
10230
  }
9984
10231
  return this.openPayloadFromUrl(e14, t2, r3);
9985
10232
  }
9986
10233
  if (0 !== t2) throw new Error(`Chapter "${e14.chapterId}" is not segmented`);
9987
10234
  if (s2) {
9988
- if (ze(s2)) {
10235
+ if (Be(s2)) {
9989
10236
  const s3 = await this.resolveChapterUrls(e14);
9990
10237
  return this.openPayloadFromUrl(e14, t2, s3);
9991
10238
  }
@@ -10002,20 +10249,20 @@ const et2 = class n4 extends BasePlugin {
10002
10249
  }
10003
10250
  async startLoadSegment(e14, t2) {
10004
10251
  var s2;
10005
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
10252
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
10006
10253
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
10007
10254
  try {
10008
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
10255
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
10009
10256
  l.wait((t4) => e15(t4), (e16) => t3(e16));
10010
10257
  });
10011
10258
  return await new Promise((e15, t3) => {
10012
10259
  d2.task.wait(() => e15(), (e16) => t3(e16));
10013
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
10014
- } catch (i2) {
10260
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
10261
+ } catch (o2) {
10015
10262
  const e15 = this.chapterStatus.get(r2);
10016
10263
  if ("loaded" !== e15 && "password-required" !== e15) {
10017
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
10018
- this.logger.error(n4.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
10264
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
10265
+ this.logger.error(n4.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
10019
10266
  }
10020
10267
  return this.chapterStatus.get(r2) ?? "error";
10021
10268
  }
@@ -10039,17 +10286,17 @@ const et2 = class n4 extends BasePlugin {
10039
10286
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
10040
10287
  const a2 = this.findChapter(t2);
10041
10288
  if (!a2) return;
10042
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
10043
- this.passwordAttempts.set(t2, o2);
10044
- let i2 = null;
10289
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
10290
+ this.passwordAttempts.set(t2, i2);
10291
+ let o2 = null;
10045
10292
  try {
10046
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
10293
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
10047
10294
  } catch (h2) {
10048
10295
  this.logger.error(n4.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
10049
10296
  }
10050
- if (!i2) return void this.updateStatus(t2, "password-required");
10297
+ if (!o2) return void this.updateStatus(t2, "password-required");
10051
10298
  this.updateStatus(t2, "loading");
10052
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
10299
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
10053
10300
  }, () => {
10054
10301
  });
10055
10302
  }
@@ -10098,7 +10345,7 @@ const nt = class extends BasePlugin {
10098
10345
  isChapterPagePlaceholder(e14, t2) {
10099
10346
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14);
10100
10347
  if (!n9) return true;
10101
- const r2 = Ge(n9, t2);
10348
+ const r2 = Re(n9, t2);
10102
10349
  return !s2.isSegmentLoaded(e14, r2);
10103
10350
  }
10104
10351
  refreshLayout() {
@@ -10119,14 +10366,14 @@ const nt = class extends BasePlugin {
10119
10366
  }
10120
10367
  onDocumentLoaded(e14) {
10121
10368
  var t2;
10122
- const { chapterId: s2 } = Oe(e14);
10369
+ const { chapterId: s2 } = Ae(e14);
10123
10370
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
10124
10371
  const n9 = this.documentManager.provides();
10125
10372
  n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
10126
10373
  }
10127
10374
  onDocumentClosed(e14) {
10128
10375
  var t2;
10129
- const { chapterId: s2 } = Oe(e14);
10376
+ const { chapterId: s2 } = Ae(e14);
10130
10377
  0 !== ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0) && (this.dropChapterSizes(s2), this.refreshLayout());
10131
10378
  }
10132
10379
  buildCapability() {
@@ -10178,16 +10425,16 @@ const nt = class extends BasePlugin {
10178
10425
  if (!this.virtualPageMap) return;
10179
10426
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14), r2 = this.virtualPageMap.pagesOfChapter(e14);
10180
10427
  if (!n9 || 0 === r2.length) return;
10181
- const a2 = Ve(n9), o2 = this.documentManager.provides(), i2 = [], h2 = (e15, t3, s3, n10) => {
10428
+ const a2 = Oe(n9), i2 = this.documentManager.provides(), o2 = [], h2 = (e15, t3, s3, n10) => {
10182
10429
  var r3;
10183
- const a3 = o2.getDocument(e15);
10430
+ const a3 = i2.getDocument(e15);
10184
10431
  if (!(null == (r3 = null == a3 ? void 0 : a3.pages) ? void 0 : r3.length)) return false;
10185
10432
  const h3 = this.getDocumentScale(e15);
10186
- for (let o3 = 0; o3 < n10; o3++) {
10187
- const e16 = a3.pages[s3 + o3];
10188
- (null == e16 ? void 0 : e16.size) && i2.push({ localPageIndex: t3 + o3, width: e16.size.width * h3, height: e16.size.height * h3 });
10433
+ for (let i3 = 0; i3 < n10; i3++) {
10434
+ const e16 = a3.pages[s3 + i3];
10435
+ (null == e16 ? void 0 : e16.size) && o2.push({ localPageIndex: t3 + i3, width: e16.size.width * h3, height: e16.size.height * h3 });
10189
10436
  }
10190
- return i2.length > 0;
10437
+ return o2.length > 0;
10191
10438
  };
10192
10439
  if (a2) for (const l of a2.segments) {
10193
10440
  if (!s2.isSegmentLoaded(e14, l.index)) continue;
@@ -10195,12 +10442,12 @@ const nt = class extends BasePlugin {
10195
10442
  h2(l.documentId, l.localPageStart, 0, t3);
10196
10443
  }
10197
10444
  else s2.isSegmentLoaded(e14, 0) && h2(e14, 0, 0, r2.length);
10198
- 0 !== i2.length ? this.applyChapterPageSizes(e14, i2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
10445
+ 0 !== o2.length ? this.applyChapterPageSizes(e14, o2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
10199
10446
  }
10200
10447
  onScaleChanged(e14) {
10201
10448
  const t2 = this.coreState.core.documents[e14];
10202
10449
  if (!t2 || "loaded" !== t2.status) return;
10203
- const { chapterId: s2 } = Oe(e14);
10450
+ const { chapterId: s2 } = Ae(e14);
10204
10451
  this.chapterManager.provides().getChapter(s2) && this.harvestChapterSizes(s2);
10205
10452
  }
10206
10453
  dropChapterSizes(e14) {
@@ -10222,7 +10469,7 @@ const nt = class extends BasePlugin {
10222
10469
  expandIndicesWithBuffer(e14, t2) {
10223
10470
  if (0 === e14.length || t2 <= 0) return [];
10224
10471
  const s2 = this.config.bufferSize, n9 = Math.max(0, e14[0] - s2), r2 = Math.min(t2 - 1, e14[e14.length - 1] + s2), a2 = [];
10225
- for (let o2 = n9; o2 <= r2; o2++) a2.push(o2);
10472
+ for (let i2 = n9; i2 <= r2; i2++) a2.push(i2);
10226
10473
  return a2;
10227
10474
  }
10228
10475
  recomputeVisible() {
@@ -10239,13 +10486,13 @@ const nt = class extends BasePlugin {
10239
10486
  r2++;
10240
10487
  }
10241
10488
  const a2 = [];
10242
- let o2 = n9, i2 = -1;
10489
+ let i2 = n9, o2 = -1;
10243
10490
  for (let h2 = n9; h2 < r2; h2++) {
10244
10491
  a2.push(h2);
10245
10492
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
10246
- r3 > i2 && (i2 = r3, o2 = h2);
10493
+ r3 > o2 && (o2 = r3, i2 = h2);
10247
10494
  }
10248
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
10495
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
10249
10496
  }
10250
10497
  findFirstVisibleIndex(e14) {
10251
10498
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -10275,9 +10522,9 @@ const nt = class extends BasePlugin {
10275
10522
  if (!this.viewportEl || e14 < 0 || e14 >= this.offsets.length || !this.virtualPageMap) return;
10276
10523
  const r2 = (null == s2 ? void 0 : s2.marginTop) ?? 80, a2 = this.virtualPageMap.atIndex(e14);
10277
10524
  if (!a2) return void this.scrollToIndex(e14, s2);
10278
- const o2 = this.pageSizes.get(e14), i2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = i2 ? null == (n9 = this.documentManager.provides().getDocument(i2.documentId)) ? void 0 : n9.pages[i2.pageIndex] : void 0;
10279
- if (!o2 || !h2) return void this.scrollToIndex(e14, s2);
10280
- const l = o2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
10525
+ const i2 = this.pageSizes.get(e14), o2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = o2 ? null == (n9 = this.documentManager.provides().getDocument(o2.documentId)) ? void 0 : n9.pages[o2.pageIndex] : void 0;
10526
+ if (!i2 || !h2) return void this.scrollToIndex(e14, s2);
10527
+ const l = i2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
10281
10528
  this.viewportEl.scrollTo({ top: Math.max(0, d2), behavior: (null == s2 ? void 0 : s2.behavior) ?? "auto" });
10282
10529
  }
10283
10530
  scrollToChapter(e14, t2) {
@@ -10286,7 +10533,7 @@ const nt = class extends BasePlugin {
10286
10533
  if (s2 < 0) return;
10287
10534
  const n9 = this.chapterManager.provides(), r2 = n9.getChapter(e14), a2 = this.virtualPageMap.atIndex(s2);
10288
10535
  if (r2 && a2) {
10289
- const t3 = Ge(r2, a2.localPageIndex);
10536
+ const t3 = Re(r2, a2.localPageIndex);
10290
10537
  n9.ensureSegmentLoaded(e14, t3);
10291
10538
  } else n9.ensureChapterLoaded(e14);
10292
10539
  this.scrollToIndex(s2, t2);
@@ -10301,7 +10548,7 @@ nt.id = "chapter-scroll";
10301
10548
  let rt = nt;
10302
10549
  const at = class n5 extends BasePlugin {
10303
10550
  constructor(e14, n9) {
10304
- super(e14, n9), this.notesChange$ = createBehaviorEmitter([]), this.noteActivated$ = createEmitter(), this.notes = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(rt.id), this.chapterManager = this.registry.getPlugin(Fe.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10551
+ super(e14, n9), this.notesChange$ = createBehaviorEmitter([]), this.noteActivated$ = createEmitter(), this.notes = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(rt.id), this.chapterManager = this.registry.getPlugin(Ne2.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10305
10552
  }
10306
10553
  async initialize(e14) {
10307
10554
  if (this.config = e14, e14.callbacks.loadNotes) try {
@@ -10339,7 +10586,7 @@ const at = class n5 extends BasePlugin {
10339
10586
  this.notesChange$.emit(this.listAll());
10340
10587
  }
10341
10588
  buildDraft(e14) {
10342
- return { chapterId: e14.chapterId, localPageIndex: e14.localPageIndex, globalPageNumber: e14.globalPageNumber, globalPageIndex: e14.globalPageIndex, rectsPdfCoord: e14.rectsPdfCoord, endAnchor: it(e14.rectsPdfCoord), selectedText: e14.selectedText, content: e14.content };
10589
+ return { chapterId: e14.chapterId, localPageIndex: e14.localPageIndex, globalPageNumber: e14.globalPageNumber, globalPageIndex: e14.globalPageIndex, rectsPdfCoord: e14.rectsPdfCoord, endAnchor: ot(e14.rectsPdfCoord), selectedText: e14.selectedText, content: e14.content };
10343
10590
  }
10344
10591
  async requestCreateFromSelection(e14) {
10345
10592
  if (0 === e14.rectsPdfCoord.length) return null;
@@ -10356,8 +10603,8 @@ const at = class n5 extends BasePlugin {
10356
10603
  let r2 = null;
10357
10604
  try {
10358
10605
  r2 = await s2(t2);
10359
- } catch (o2) {
10360
- return this.logger.error(n5.id, "OnCreateNote", "Callback failed", o2), null;
10606
+ } catch (i2) {
10607
+ return this.logger.error(n5.id, "OnCreateNote", "Callback failed", i2), null;
10361
10608
  }
10362
10609
  if (!r2) return null;
10363
10610
  const a2 = { ...t2, noteId: r2.noteId };
@@ -10378,13 +10625,13 @@ const at = class n5 extends BasePlugin {
10378
10625
  var s2, r2;
10379
10626
  const a2 = this.notes.get(e14);
10380
10627
  if (!a2) return;
10381
- const o2 = { ...a2, content: t2 };
10628
+ const i2 = { ...a2, content: t2 };
10382
10629
  try {
10383
- await (null == (r2 = (s2 = this.config.callbacks).onUpdateNote) ? void 0 : r2.call(s2, o2));
10384
- } catch (i2) {
10385
- return void this.logger.error(n5.id, "OnUpdateNote", "Callback failed", i2);
10630
+ await (null == (r2 = (s2 = this.config.callbacks).onUpdateNote) ? void 0 : r2.call(s2, i2));
10631
+ } catch (o2) {
10632
+ return void this.logger.error(n5.id, "OnUpdateNote", "Callback failed", o2);
10386
10633
  }
10387
- this.notes.set(e14, o2), this.notesChange$.emit(this.listAll());
10634
+ this.notes.set(e14, i2), this.notesChange$.emit(this.listAll());
10388
10635
  }
10389
10636
  async scrollToNote(e14) {
10390
10637
  var t2;
@@ -10408,8 +10655,8 @@ const at = class n5 extends BasePlugin {
10408
10655
  }
10409
10656
  };
10410
10657
  at.id = "note";
10411
- let ot = at;
10412
- function it(e14) {
10658
+ let it = at;
10659
+ function ot(e14) {
10413
10660
  const t2 = e14[e14.length - 1];
10414
10661
  return { x: t2.origin.x + t2.size.width, y: t2.origin.y + t2.size.height };
10415
10662
  }
@@ -10420,12 +10667,12 @@ function ct(e14) {
10420
10667
  function ut(e14) {
10421
10668
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
10422
10669
  }
10423
- function gt(e14) {
10670
+ function pt(e14) {
10424
10671
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
10425
10672
  const t2 = e14.source;
10426
10673
  return ut(t2) ? t2.segmentPageThreshold : null;
10427
10674
  }
10428
- function pt(e14, t2) {
10675
+ function gt(e14, t2) {
10429
10676
  return `${e14}#s${t2}`;
10430
10677
  }
10431
10678
  function mt(e14) {
@@ -10438,13 +10685,13 @@ function ft(e14, t2) {
10438
10685
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
10439
10686
  }
10440
10687
  function vt(e14) {
10441
- const t2 = gt(e14);
10688
+ const t2 = pt(e14);
10442
10689
  if (!t2) return null;
10443
10690
  const s2 = ct(e14), n9 = ft(s2, t2), r2 = ut(e14.source) ? e14.source.urls : void 0, a2 = [];
10444
- for (let o2 = 0; o2 < n9; o2++) {
10445
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
10691
+ for (let i2 = 0; i2 < n9; i2++) {
10692
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
10446
10693
  if (n10 >= s2) break;
10447
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: pt(e14.chapterId, o2) });
10694
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: gt(e14.chapterId, i2) });
10448
10695
  }
10449
10696
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
10450
10697
  }
@@ -10466,16 +10713,16 @@ function Pt(e14, t2) {
10466
10713
  }
10467
10714
  function bt(e14) {
10468
10715
  var t2;
10469
- const s2 = gt(e14);
10716
+ const s2 = pt(e14);
10470
10717
  if (!s2) return [];
10471
10718
  const n9 = [], { chapterId: r2 } = e14, a2 = ct(e14);
10472
10719
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
10473
- const o2 = e14.source;
10474
- if (!ut(o2)) return n9;
10475
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
10476
- const i2 = ft(a2, s2);
10477
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
10478
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
10720
+ const i2 = e14.source;
10721
+ if (!ut(i2)) return n9;
10722
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
10723
+ const o2 = ft(a2, s2);
10724
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
10725
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
10479
10726
  return n9;
10480
10727
  }
10481
10728
  const St = { kind: "first-wins" };
@@ -10545,20 +10792,20 @@ function yt(e14, t2 = St) {
10545
10792
  for (const [r3, a3] of s3) n10.set(r3, Ct(r3, a3, t3));
10546
10793
  return n10;
10547
10794
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
10548
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
10795
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
10549
10796
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
10550
- let o2 = 0;
10551
- for (const i2 of r2) {
10552
- const e15 = s2.get(i2), t3 = n9.get(e15);
10797
+ let i2 = 0;
10798
+ for (const o2 of r2) {
10799
+ const e15 = s2.get(o2), t3 = n9.get(e15);
10553
10800
  if (!t3) continue;
10554
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
10555
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
10801
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
10802
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
10556
10803
  }
10557
10804
  return new wt2(a2);
10558
10805
  }
10559
10806
  const Mt = class n6 extends BasePlugin {
10560
10807
  constructor(e14, n9) {
10561
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = St, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10808
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = St, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10562
10809
  }
10563
10810
  async initialize(e14) {
10564
10811
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? St, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
@@ -10613,8 +10860,8 @@ const Mt = class n6 extends BasePlugin {
10613
10860
  const t3 = [], s3 = /* @__PURE__ */ new Set();
10614
10861
  for (const n9 of e15.chapters) {
10615
10862
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
10616
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
10617
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
10863
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
10864
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
10618
10865
  t3.push(...bt(n9));
10619
10866
  }
10620
10867
  return t3;
@@ -10693,15 +10940,27 @@ const Mt = class n6 extends BasePlugin {
10693
10940
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
10694
10941
  }
10695
10942
  clearVisibleSegmentBacklog() {
10696
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
10943
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
10944
+ }
10945
+ chapterNeedsResolvedUrls(e14) {
10946
+ var t2;
10947
+ const s2 = e14.source;
10948
+ return !ut(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
10949
+ }
10950
+ ensureVisibleChapterUrls(e14) {
10951
+ const { chapterId: t2 } = e14;
10952
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
10953
+ }).finally(() => {
10954
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
10955
+ })), false));
10697
10956
  }
10698
10957
  segmentStartGlobalIndex(e14, t2) {
10699
10958
  var s2;
10700
10959
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
10701
10960
  const n9 = this.findChapter(e14);
10702
10961
  if (!n9) return Number.MAX_SAFE_INTEGER;
10703
- const r2 = vt(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
10704
- if (o2) return o2.globalPageIndex;
10962
+ const r2 = vt(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
10963
+ if (i2) return i2.globalPageIndex;
10705
10964
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
10706
10965
  }
10707
10966
  scheduleVisibleSegmentLoads(e14) {
@@ -10714,11 +10973,18 @@ const Mt = class n6 extends BasePlugin {
10714
10973
  }
10715
10974
  pumpVisibleSegmentBacklog() {
10716
10975
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
10976
+ let t2 = 0;
10717
10977
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
10718
10978
  const e15 = this.visibleSegmentBacklog.shift();
10719
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
10720
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
10721
- })));
10979
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10980
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10981
+ const s2 = this.findChapter(e15.chapterId);
10982
+ if (s2) {
10983
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
10984
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
10985
+ });
10986
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10987
+ }
10722
10988
  }
10723
10989
  }
10724
10990
  prepareChapterForSegmentLoad(e14) {
@@ -10731,14 +10997,14 @@ const Mt = class n6 extends BasePlugin {
10731
10997
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
10732
10998
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
10733
10999
  this.lastVisibleIndicesKey = t2;
10734
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
11000
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
10735
11001
  if (t3 < 0) return;
10736
11002
  const s3 = this.findChapter(e15);
10737
11003
  if (!s3) return;
10738
11004
  const n10 = vt(s3);
10739
11005
  if (n10 && t3 >= n10.segments.length) return;
10740
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
10741
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
11006
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
11007
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
10742
11008
  };
10743
11009
  for (const d2 of e14) {
10744
11010
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -10747,7 +11013,7 @@ const Mt = class n6 extends BasePlugin {
10747
11013
  const t3 = this.findChapter(e15.chapterId);
10748
11014
  if (!t3) continue;
10749
11015
  const s3 = Pt(t3, e15.localPageIndex);
10750
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
11016
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
10751
11017
  });
10752
11018
  }
10753
11019
  if (0 === a2.size) return;
@@ -10756,11 +11022,11 @@ const Mt = class n6 extends BasePlugin {
10756
11022
  const e15 = this.findChapter(h2.chapterId);
10757
11023
  if (e15) {
10758
11024
  const t3 = Pt(e15, h2.localPageIndex);
10759
- i2(h2.chapterId, t3 + 1);
11025
+ o2(h2.chapterId, t3 + 1);
10760
11026
  }
10761
11027
  }
10762
11028
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
10763
- const l = [...o2.entries()].map(([e15, t3]) => {
11029
+ const l = [...i2.entries()].map(([e15, t3]) => {
10764
11030
  const [s3, n10] = e15.split(":");
10765
11031
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
10766
11032
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
@@ -10817,12 +11083,12 @@ const Mt = class n6 extends BasePlugin {
10817
11083
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
10818
11084
  const r2 = n9.segments[t2], a2 = r2.documentId;
10819
11085
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
10820
- const o2 = this.pendingSegmentLoadPromises.get(a2);
10821
- if (o2) return o2;
10822
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
11086
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
11087
+ if (i2) return i2;
11088
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
10823
11089
  this.pendingSegmentLoadPromises.delete(a2);
10824
11090
  });
10825
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
11091
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
10826
11092
  }
10827
11093
  ensureSingleDocumentChapter(e14) {
10828
11094
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -10868,14 +11134,14 @@ const Mt = class n6 extends BasePlugin {
10868
11134
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
10869
11135
  }
10870
11136
  async resolvePdfPayload(e14, t2) {
10871
- const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = gt(e14);
11137
+ const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = pt(e14);
10872
11138
  if (a2) {
10873
11139
  const s3 = ft(ct(e14), a2);
10874
11140
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
10875
11141
  const r3 = await this.resolveChapterUrls(e14);
10876
11142
  if (r3.length !== s3) {
10877
- const [t3, o2] = e14.globalPageRange;
10878
- this.logger.warn(n6.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
11143
+ const [t3, i2] = e14.globalPageRange;
11144
+ this.logger.warn(n6.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
10879
11145
  }
10880
11146
  return this.openPayloadFromUrl(e14, t2, r3);
10881
11147
  }
@@ -10898,20 +11164,20 @@ const Mt = class n6 extends BasePlugin {
10898
11164
  }
10899
11165
  async startLoadSegment(e14, t2) {
10900
11166
  var s2;
10901
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
11167
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
10902
11168
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
10903
11169
  try {
10904
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
11170
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
10905
11171
  l.wait((t4) => e15(t4), (e16) => t3(e16));
10906
11172
  });
10907
11173
  return await new Promise((e15, t3) => {
10908
11174
  d2.task.wait(() => e15(), (e16) => t3(e16));
10909
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
10910
- } catch (i2) {
11175
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
11176
+ } catch (o2) {
10911
11177
  const e15 = this.chapterStatus.get(r2);
10912
11178
  if ("loaded" !== e15 && "password-required" !== e15) {
10913
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
10914
- this.logger.error(n6.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
11179
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
11180
+ this.logger.error(n6.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
10915
11181
  }
10916
11182
  return this.chapterStatus.get(r2) ?? "error";
10917
11183
  }
@@ -10935,17 +11201,17 @@ const Mt = class n6 extends BasePlugin {
10935
11201
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
10936
11202
  const a2 = this.findChapter(t2);
10937
11203
  if (!a2) return;
10938
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
10939
- this.passwordAttempts.set(t2, o2);
10940
- let i2 = null;
11204
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
11205
+ this.passwordAttempts.set(t2, i2);
11206
+ let o2 = null;
10941
11207
  try {
10942
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
11208
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
10943
11209
  } catch (h2) {
10944
11210
  this.logger.error(n6.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
10945
11211
  }
10946
- if (!i2) return void this.updateStatus(t2, "password-required");
11212
+ if (!o2) return void this.updateStatus(t2, "password-required");
10947
11213
  this.updateStatus(t2, "loading");
10948
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
11214
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
10949
11215
  }, () => {
10950
11216
  });
10951
11217
  }
@@ -10974,13 +11240,13 @@ function Lt(e14) {
10974
11240
  function $t(e14, t2) {
10975
11241
  return `${e14}#s${t2}`;
10976
11242
  }
10977
- function Dt(e14) {
11243
+ function kt2(e14) {
10978
11244
  const t2 = e14.lastIndexOf("#s");
10979
11245
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
10980
11246
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
10981
11247
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
10982
11248
  }
10983
- function kt2(e14) {
11249
+ function Dt(e14) {
10984
11250
  const t2 = (function(e15) {
10985
11251
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
10986
11252
  const t3 = e15.source;
@@ -10993,56 +11259,56 @@ function kt2(e14) {
10993
11259
  })(e14), n9 = (function(e15, t3) {
10994
11260
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
10995
11261
  })(s2, t2), r2 = Lt(e14.source) ? e14.source.urls : void 0, a2 = [];
10996
- for (let o2 = 0; o2 < n9; o2++) {
10997
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
11262
+ for (let i2 = 0; i2 < n9; i2++) {
11263
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
10998
11264
  if (n10 >= s2) break;
10999
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: $t(e14.chapterId, o2) });
11265
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: $t(e14.chapterId, i2) });
11000
11266
  }
11001
11267
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
11002
11268
  }
11003
11269
  function Ut(e14, t2) {
11004
- const s2 = kt2(e14);
11270
+ const s2 = Dt(e14);
11005
11271
  if (!s2) return 0;
11006
11272
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
11007
11273
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
11008
11274
  }
11009
- function Et(e14) {
11275
+ function Vt(e14) {
11010
11276
  const [t2, s2] = e14.localPageRange;
11011
11277
  return s2 - t2 + 1;
11012
11278
  }
11013
- function Tt(e14) {
11279
+ function Et(e14) {
11014
11280
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
11015
11281
  }
11016
- function Ft(e14) {
11282
+ function Nt(e14) {
11017
11283
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
11018
11284
  const t2 = e14.source;
11019
- return Tt(t2) ? t2.segmentPageThreshold : null;
11285
+ return Et(t2) ? t2.segmentPageThreshold : null;
11020
11286
  }
11021
- function Nt(e14, t2) {
11287
+ function Tt(e14, t2) {
11022
11288
  return `${e14}#s${t2}`;
11023
11289
  }
11024
- function At(e14) {
11290
+ function Ft(e14) {
11025
11291
  const t2 = e14.lastIndexOf("#s");
11026
11292
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
11027
11293
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
11028
11294
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
11029
11295
  }
11030
- function Ot(e14, t2) {
11296
+ function At(e14, t2) {
11031
11297
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
11032
11298
  }
11033
- function Vt(e14) {
11034
- const t2 = Ft(e14);
11299
+ function Ot(e14) {
11300
+ const t2 = Nt(e14);
11035
11301
  if (!t2) return null;
11036
- const s2 = Et(e14), n9 = Ot(s2, t2), r2 = Tt(e14.source) ? e14.source.urls : void 0, a2 = [];
11037
- for (let o2 = 0; o2 < n9; o2++) {
11038
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
11302
+ const s2 = Vt(e14), n9 = At(s2, t2), r2 = Et(e14.source) ? e14.source.urls : void 0, a2 = [];
11303
+ for (let i2 = 0; i2 < n9; i2++) {
11304
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
11039
11305
  if (n10 >= s2) break;
11040
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: Nt(e14.chapterId, o2) });
11306
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Tt(e14.chapterId, i2) });
11041
11307
  }
11042
11308
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
11043
11309
  }
11044
- function Gt(e14, t2) {
11045
- const s2 = Vt(e14);
11310
+ function Rt(e14, t2) {
11311
+ const s2 = Ot(e14);
11046
11312
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
11047
11313
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
11048
11314
  if (!n9) {
@@ -11051,27 +11317,27 @@ function Gt(e14, t2) {
11051
11317
  }
11052
11318
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
11053
11319
  }
11054
- function Rt(e14, t2) {
11055
- const s2 = Vt(e14);
11320
+ function Gt(e14, t2) {
11321
+ const s2 = Ot(e14);
11056
11322
  if (!s2) return 0;
11057
11323
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
11058
11324
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
11059
11325
  }
11060
- function zt(e14) {
11326
+ function Bt(e14) {
11061
11327
  var t2;
11062
- const s2 = Ft(e14);
11328
+ const s2 = Nt(e14);
11063
11329
  if (!s2) return [];
11064
- const n9 = [], { chapterId: r2 } = e14, a2 = Et(e14);
11330
+ const n9 = [], { chapterId: r2 } = e14, a2 = Vt(e14);
11065
11331
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
11066
- const o2 = e14.source;
11067
- if (!Tt(o2)) return n9;
11068
- 0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
11069
- const i2 = Ot(a2, s2);
11070
- o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
11071
- for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
11332
+ const i2 = e14.source;
11333
+ if (!Et(i2)) return n9;
11334
+ 0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
11335
+ const o2 = At(a2, s2);
11336
+ i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
11337
+ for (let h2 = 0; h2 < i2.urls.length; h2++) (null == (t2 = i2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
11072
11338
  return n9;
11073
11339
  }
11074
- const Bt = { kind: "first-wins" };
11340
+ const zt = { kind: "first-wins" };
11075
11341
  function _t(e14, t2, s2) {
11076
11342
  if (1 === t2.length) return t2[0].chapterId;
11077
11343
  switch (s2.kind) {
@@ -11124,8 +11390,8 @@ class qt {
11124
11390
  return s2 ? this._pages.indexOf(s2) : -1;
11125
11391
  }
11126
11392
  }
11127
- function Wt(e14, t2 = Bt) {
11128
- const s2 = (function(e15, t3 = Bt) {
11393
+ function Wt(e14, t2 = zt) {
11394
+ const s2 = (function(e15, t3 = zt) {
11129
11395
  const s3 = /* @__PURE__ */ new Map();
11130
11396
  for (const r3 of e15.chapters) {
11131
11397
  const [e16, t4] = r3.globalPageRange;
@@ -11138,30 +11404,30 @@ function Wt(e14, t2 = Bt) {
11138
11404
  for (const [r3, a3] of s3) n10.set(r3, _t(r3, a3, t3));
11139
11405
  return n10;
11140
11406
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
11141
- for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
11407
+ for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
11142
11408
  const r2 = Array.from(s2.keys()).sort((e15, t3) => e15 - t3), a2 = [];
11143
- let o2 = 0;
11144
- for (const i2 of r2) {
11145
- const e15 = s2.get(i2), t3 = n9.get(e15);
11409
+ let i2 = 0;
11410
+ for (const o2 of r2) {
11411
+ const e15 = s2.get(o2), t3 = n9.get(e15);
11146
11412
  if (!t3) continue;
11147
- const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
11148
- a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
11413
+ const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (o2 - r3);
11414
+ a2.push({ globalPageIndex: i2++, globalPageNumber: o2, chapterId: e15, localPageIndex: l });
11149
11415
  }
11150
11416
  return new qt(a2);
11151
11417
  }
11152
11418
  const Kt = class n7 extends BasePlugin {
11153
11419
  constructor(e14, n9) {
11154
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Bt, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11420
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = zt, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11155
11421
  }
11156
11422
  async initialize(e14) {
11157
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Bt, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
11158
- const { chapterId: t2 } = At(e15.id);
11423
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? zt, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
11424
+ const { chapterId: t2 } = Ft(e15.id);
11159
11425
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
11160
11426
  }), this.documentManager.provides().onDocumentClosed((e15) => {
11161
- const { chapterId: t2 } = At(e15);
11427
+ const { chapterId: t2 } = Ft(e15);
11162
11428
  this.findChapter(t2) && this.syncChapterStatusFromDocuments(t2);
11163
11429
  }), this.documentManager.provides().onDocumentError((e15) => {
11164
- const { chapterId: t2 } = At(e15.documentId);
11430
+ const { chapterId: t2 } = Ft(e15.documentId);
11165
11431
  this.findChapter(t2) && this.handleDocumentError(e15.documentId, t2, e15.code, e15.message);
11166
11432
  })), this.setManifestInternal(e14.manifest), e14.unloadTimeoutMs && e14.unloadTimeoutMs > 0) {
11167
11433
  const t2 = Math.max(5e3, Math.floor(e14.unloadTimeoutMs / 4));
@@ -11173,10 +11439,10 @@ const Kt = class n7 extends BasePlugin {
11173
11439
  t2 && (this.overlapStrategy = t2), this.setManifestInternal(e14);
11174
11440
  }, getManifest: () => this.manifest, getVirtualPageMap: () => this.virtualPageMap, setVisibleGlobalPages: (e14) => this.handleVisibleChange(e14), visibleRangeNeedsSegmentLoad: (e14) => this.visibleRangeNeedsSegmentLoad(e14), shouldShowChapterLoadError: (e14, t2) => this.shouldShowChapterLoadError(e14, t2), ensureChapterLoaded: (e14) => this.ensureChapterLoaded(e14), ensureSegmentLoaded: (e14, t2) => this.ensureSegmentLoaded(e14, t2), ensureAllSegmentsLoaded: (e14) => this.ensureAllSegmentsLoaded(e14), resolvePageDocument: (e14, t2) => {
11175
11441
  const s2 = this.findChapter(e14);
11176
- return s2 ? Gt(s2, t2) : null;
11442
+ return s2 ? Rt(s2, t2) : null;
11177
11443
  }, getSegmentPlan: (e14) => {
11178
11444
  const t2 = this.findChapter(e14);
11179
- return t2 ? Vt(t2) : null;
11445
+ return t2 ? Ot(t2) : null;
11180
11446
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
11181
11447
  }
11182
11448
  destroy() {
@@ -11206,9 +11472,9 @@ const Kt = class n7 extends BasePlugin {
11206
11472
  const t3 = [], s3 = /* @__PURE__ */ new Set();
11207
11473
  for (const n9 of e15.chapters) {
11208
11474
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
11209
- const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
11210
- if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
11211
- t3.push(...zt(n9));
11475
+ const [e16, r3] = n9.globalPageRange, [a3, i2] = n9.localPageRange;
11476
+ if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > i2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${i2}]`), r3 - e16 !== i2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${i2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
11477
+ t3.push(...Bt(n9));
11212
11478
  }
11213
11479
  return t3;
11214
11480
  })(t2);
@@ -11233,7 +11499,7 @@ const Kt = class n7 extends BasePlugin {
11233
11499
  }
11234
11500
  getSegmentPlanForChapter(e14) {
11235
11501
  const t2 = this.findChapter(e14);
11236
- return t2 ? Vt(t2) : null;
11502
+ return t2 ? Ot(t2) : null;
11237
11503
  }
11238
11504
  anySegmentOpen(e14) {
11239
11505
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -11277,7 +11543,7 @@ const Kt = class n7 extends BasePlugin {
11277
11543
  if (!e15) continue;
11278
11544
  const s2 = this.findChapter(e15.chapterId);
11279
11545
  if (!s2) continue;
11280
- const n9 = Rt(s2, e15.localPageIndex);
11546
+ const n9 = Gt(s2, e15.localPageIndex);
11281
11547
  if (!this.isSegmentDocumentOpen(e15.chapterId, n9)) return true;
11282
11548
  }
11283
11549
  return false;
@@ -11286,15 +11552,27 @@ const Kt = class n7 extends BasePlugin {
11286
11552
  return 0 === t2 && ("error" === this.chapterStatus.get(e14) && !this.isAnySegmentWorkPending(e14));
11287
11553
  }
11288
11554
  clearVisibleSegmentBacklog() {
11289
- this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
11555
+ this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.pendingVisibleUrlRepumps.clear();
11556
+ }
11557
+ chapterNeedsResolvedUrls(e14) {
11558
+ var t2;
11559
+ const s2 = e14.source;
11560
+ return !Et(s2) && ((!s2 || !("url" in s2 || "buffer" in s2 || "load" in s2)) && !!(null == (t2 = this.config.chapterPdfLoader) ? void 0 : t2.loadChapterUrls));
11561
+ }
11562
+ ensureVisibleChapterUrls(e14) {
11563
+ const { chapterId: t2 } = e14;
11564
+ return !this.chapterNeedsResolvedUrls(e14) || (!!this.chapterUrlsCache.has(t2) || (this.pendingVisibleUrlRepumps.has(t2) || (this.pendingVisibleUrlRepumps.add(t2), this.resolveChapterUrls(e14).catch(() => {
11565
+ }).finally(() => {
11566
+ this.pendingVisibleUrlRepumps.delete(t2), this.pumpVisibleSegmentBacklog();
11567
+ })), false));
11290
11568
  }
11291
11569
  segmentStartGlobalIndex(e14, t2) {
11292
11570
  var s2;
11293
11571
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
11294
11572
  const n9 = this.findChapter(e14);
11295
11573
  if (!n9) return Number.MAX_SAFE_INTEGER;
11296
- const r2 = Vt(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
11297
- if (o2) return o2.globalPageIndex;
11574
+ const r2 = Ot(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
11575
+ if (i2) return i2.globalPageIndex;
11298
11576
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
11299
11577
  }
11300
11578
  scheduleVisibleSegmentLoads(e14) {
@@ -11307,11 +11585,18 @@ const Kt = class n7 extends BasePlugin {
11307
11585
  }
11308
11586
  pumpVisibleSegmentBacklog() {
11309
11587
  const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
11588
+ let t2 = 0;
11310
11589
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
11311
11590
  const e15 = this.visibleSegmentBacklog.shift();
11312
- this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
11313
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
11314
- })));
11591
+ if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
11592
+ if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
11593
+ const s2 = this.findChapter(e15.chapterId);
11594
+ if (s2) {
11595
+ if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
11596
+ this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
11597
+ });
11598
+ else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
11599
+ }
11315
11600
  }
11316
11601
  }
11317
11602
  prepareChapterForSegmentLoad(e14) {
@@ -11324,14 +11609,14 @@ const Kt = class n7 extends BasePlugin {
11324
11609
  const t2 = e14.join(","), s2 = this.visibleRangeNeedsSegmentLoad(e14);
11325
11610
  if (t2 === this.lastVisibleIndicesKey && !s2) return;
11326
11611
  this.lastVisibleIndicesKey = t2;
11327
- const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), o2 = /* @__PURE__ */ new Map(), i2 = (e15, t3) => {
11612
+ const n9 = Date.now(), r2 = e14[Math.floor(e14.length / 2)] ?? e14[0], a2 = /* @__PURE__ */ new Set(), i2 = /* @__PURE__ */ new Map(), o2 = (e15, t3) => {
11328
11613
  if (t3 < 0) return;
11329
11614
  const s3 = this.findChapter(e15);
11330
11615
  if (!s3) return;
11331
- const n10 = Vt(s3);
11616
+ const n10 = Ot(s3);
11332
11617
  if (n10 && t3 >= n10.segments.length) return;
11333
- const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
11334
- (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
11618
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(o3 - r2), l2 = i2.get(a3);
11619
+ (void 0 === l2 || h3 < l2) && i2.set(a3, h3);
11335
11620
  };
11336
11621
  for (const d2 of e14) {
11337
11622
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -11339,8 +11624,8 @@ const Kt = class n7 extends BasePlugin {
11339
11624
  a2.add(e15.chapterId);
11340
11625
  const t3 = this.findChapter(e15.chapterId);
11341
11626
  if (!t3) continue;
11342
- const s3 = Rt(t3, e15.localPageIndex);
11343
- i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
11627
+ const s3 = Gt(t3, e15.localPageIndex);
11628
+ o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
11344
11629
  });
11345
11630
  }
11346
11631
  if (0 === a2.size) return;
@@ -11348,19 +11633,19 @@ const Kt = class n7 extends BasePlugin {
11348
11633
  if (h2) {
11349
11634
  const e15 = this.findChapter(h2.chapterId);
11350
11635
  if (e15) {
11351
- const t3 = Rt(e15, h2.localPageIndex);
11352
- i2(h2.chapterId, t3 + 1);
11636
+ const t3 = Gt(e15, h2.localPageIndex);
11637
+ o2(h2.chapterId, t3 + 1);
11353
11638
  }
11354
11639
  }
11355
11640
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
11356
- const l = [...o2.entries()].map(([e15, t3]) => {
11641
+ const l = [...i2.entries()].map(([e15, t3]) => {
11357
11642
  const [s3, n10] = e15.split(":");
11358
11643
  return { chapterId: s3, segmentIndex: Number.parseInt(n10, 10), priority: t3 };
11359
11644
  }).filter((e15) => e15.chapterId && Number.isFinite(e15.segmentIndex));
11360
11645
  if (this.scheduleVisibleSegmentLoads(l), h2) {
11361
11646
  const e15 = this.findChapter(h2.chapterId);
11362
11647
  if (e15) {
11363
- const t3 = Rt(e15, h2.localPageIndex);
11648
+ const t3 = Gt(e15, h2.localPageIndex);
11364
11649
  this.isSegmentDocumentOpen(h2.chapterId, t3) || (this.prepareChapterForSegmentLoad(h2.chapterId), this.loadSegmentNow(h2.chapterId, t3).finally(() => {
11365
11650
  this.pumpVisibleSegmentBacklog();
11366
11651
  }));
@@ -11369,7 +11654,7 @@ const Kt = class n7 extends BasePlugin {
11369
11654
  if (this.config.autoActivateOnLoad ?? 1) {
11370
11655
  const e15 = this.virtualPageMap.atIndex(r2);
11371
11656
  if (e15) {
11372
- const t3 = Gt(this.findChapter(e15.chapterId), e15.localPageIndex);
11657
+ const t3 = Rt(this.findChapter(e15.chapterId), e15.localPageIndex);
11373
11658
  if (this.documentManager.provides().isDocumentOpen(t3.documentId)) {
11374
11659
  this.documentManager.provides().getActiveDocumentId() !== t3.documentId && this.documentManager.provides().setActiveDocument(t3.documentId);
11375
11660
  }
@@ -11405,17 +11690,17 @@ const Kt = class n7 extends BasePlugin {
11405
11690
  this.prepareChapterForSegmentLoad(e14);
11406
11691
  const s2 = this.findChapter(e14);
11407
11692
  if (!s2) return Promise.resolve("error");
11408
- const n9 = Vt(s2);
11693
+ const n9 = Ot(s2);
11409
11694
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
11410
11695
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
11411
11696
  const r2 = n9.segments[t2], a2 = r2.documentId;
11412
11697
  if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11413
- const o2 = this.pendingSegmentLoadPromises.get(a2);
11414
- if (o2) return o2;
11415
- const i2 = this.startLoadSegment(s2, r2).finally(() => {
11698
+ const i2 = this.pendingSegmentLoadPromises.get(a2);
11699
+ if (i2) return i2;
11700
+ const o2 = this.startLoadSegment(s2, r2).finally(() => {
11416
11701
  this.pendingSegmentLoadPromises.delete(a2);
11417
11702
  });
11418
- return this.pendingSegmentLoadPromises.set(a2, i2), i2;
11703
+ return this.pendingSegmentLoadPromises.set(a2, o2), o2;
11419
11704
  }
11420
11705
  ensureSingleDocumentChapter(e14) {
11421
11706
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -11435,7 +11720,7 @@ const Kt = class n7 extends BasePlugin {
11435
11720
  if (n9) return n9;
11436
11721
  const r2 = (async () => {
11437
11722
  const s3 = e14.source;
11438
- if (Tt(s3)) {
11723
+ if (Et(s3)) {
11439
11724
  const e15 = [...s3.urls];
11440
11725
  return this.chapterUrlsCache.set(t2, e15), e15;
11441
11726
  }
@@ -11461,20 +11746,20 @@ const Kt = class n7 extends BasePlugin {
11461
11746
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
11462
11747
  }
11463
11748
  async resolvePdfPayload(e14, t2) {
11464
- const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = Ft(e14);
11749
+ const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = Nt(e14);
11465
11750
  if (a2) {
11466
- const s3 = Ot(Et(e14), a2);
11751
+ const s3 = At(Vt(e14), a2);
11467
11752
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
11468
11753
  const r3 = await this.resolveChapterUrls(e14);
11469
11754
  if (r3.length !== s3) {
11470
- const [t3, o2] = e14.globalPageRange;
11471
- this.logger.warn(n7.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${o2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${o2}] and segmentPageThreshold.`);
11755
+ const [t3, i2] = e14.globalPageRange;
11756
+ this.logger.warn(n7.id, "SegmentUrlCount", `Chapter "${e14.chapterId}" loadChapterUrls returned ${r3.length} URL(s), expected ${s3} (= ceil((${i2}-${t3}+1) / ${a2})). Check globalPageRange [${t3},${i2}] and segmentPageThreshold.`);
11472
11757
  }
11473
11758
  return this.openPayloadFromUrl(e14, t2, r3);
11474
11759
  }
11475
11760
  if (0 !== t2) throw new Error(`Chapter "${e14.chapterId}" is not segmented`);
11476
11761
  if (s2) {
11477
- if (Tt(s2)) {
11762
+ if (Et(s2)) {
11478
11763
  const s3 = await this.resolveChapterUrls(e14);
11479
11764
  return this.openPayloadFromUrl(e14, t2, s3);
11480
11765
  }
@@ -11491,20 +11776,20 @@ const Kt = class n7 extends BasePlugin {
11491
11776
  }
11492
11777
  async startLoadSegment(e14, t2) {
11493
11778
  var s2;
11494
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
11779
+ const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
11495
11780
  this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
11496
11781
  try {
11497
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, i2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in i2 ? h2.openDocumentUrl({ documentId: o2, url: i2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: o2, buffer: i2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
11782
+ const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
11498
11783
  l.wait((t4) => e15(t4), (e16) => t3(e16));
11499
11784
  });
11500
11785
  return await new Promise((e15, t3) => {
11501
11786
  d2.task.wait(() => e15(), (e16) => t3(e16));
11502
- }), h2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
11503
- } catch (i2) {
11787
+ }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
11788
+ } catch (o2) {
11504
11789
  const e15 = this.chapterStatus.get(r2);
11505
11790
  if ("loaded" !== e15 && "password-required" !== e15) {
11506
- const e16 = i2 instanceof Error ? i2.message : `Failed to load chapter "${r2}"`;
11507
- this.logger.error(n7.id, "LoadChapter", e16, i2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
11791
+ const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
11792
+ this.logger.error(n7.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
11508
11793
  }
11509
11794
  return this.chapterStatus.get(r2) ?? "error";
11510
11795
  }
@@ -11522,28 +11807,28 @@ const Kt = class n7 extends BasePlugin {
11522
11807
  async handleDocumentError(e14, t2, s2, r2) {
11523
11808
  if (this.pendingSegmentLoadPromises.has(e14)) return;
11524
11809
  if (s2 !== PdfErrorCode.Password) {
11525
- const s3 = this.getSegmentPlanForChapter(t2), { segmentIndex: n9 } = At(e14);
11810
+ const s3 = this.getSegmentPlanForChapter(t2), { segmentIndex: n9 } = Ft(e14);
11526
11811
  return void (s3 && 0 !== n9 && null !== n9 || this.updateStatus(t2, "error"));
11527
11812
  }
11528
11813
  if (!this.passwordProvider) return void this.updateStatus(t2, "password-required");
11529
11814
  const a2 = this.findChapter(t2);
11530
11815
  if (!a2) return;
11531
- const o2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
11532
- this.passwordAttempts.set(t2, o2);
11533
- let i2 = null;
11816
+ const i2 = (this.passwordAttempts.get(t2) ?? 0) + 1;
11817
+ this.passwordAttempts.set(t2, i2);
11818
+ let o2 = null;
11534
11819
  try {
11535
- i2 = await this.passwordProvider.resolvePassword(a2, o2 - 1);
11820
+ o2 = await this.passwordProvider.resolvePassword(a2, i2 - 1);
11536
11821
  } catch (h2) {
11537
11822
  this.logger.error(n7.id, "PasswordProvider", `Provider rejected for ${t2}`, h2);
11538
11823
  }
11539
- if (!i2) return void this.updateStatus(t2, "password-required");
11824
+ if (!o2) return void this.updateStatus(t2, "password-required");
11540
11825
  this.updateStatus(t2, "loading");
11541
- this.documentManager.provides().retryDocument(e14, { password: i2 }).wait(() => {
11826
+ this.documentManager.provides().retryDocument(e14, { password: o2 }).wait(() => {
11542
11827
  }, () => {
11543
11828
  });
11544
11829
  }
11545
11830
  closeChapter(e14) {
11546
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Vt(t2) : null, r2 = (e15) => {
11831
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Ot(t2) : null, r2 = (e15) => {
11547
11832
  if (s2.isDocumentOpen(e15)) {
11548
11833
  s2.closeDocument(e15).wait(() => {
11549
11834
  }, () => {
@@ -11608,14 +11893,14 @@ const Xt = class extends BasePlugin {
11608
11893
  }
11609
11894
  onDocumentLoaded(e14) {
11610
11895
  var t2;
11611
- const { chapterId: s2 } = Dt(e14);
11896
+ const { chapterId: s2 } = kt2(e14);
11612
11897
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
11613
11898
  const n9 = this.documentManager.provides();
11614
11899
  n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
11615
11900
  }
11616
11901
  onDocumentClosed(e14) {
11617
11902
  var t2;
11618
- const { chapterId: s2 } = Dt(e14);
11903
+ const { chapterId: s2 } = kt2(e14);
11619
11904
  0 !== ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0) && (this.dropChapterSizes(s2), this.refreshLayout());
11620
11905
  }
11621
11906
  buildCapability() {
@@ -11667,16 +11952,16 @@ const Xt = class extends BasePlugin {
11667
11952
  if (!this.virtualPageMap) return;
11668
11953
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14), r2 = this.virtualPageMap.pagesOfChapter(e14);
11669
11954
  if (!n9 || 0 === r2.length) return;
11670
- const a2 = kt2(n9), o2 = this.documentManager.provides(), i2 = [], h2 = (e15, t3, s3, n10) => {
11955
+ const a2 = Dt(n9), i2 = this.documentManager.provides(), o2 = [], h2 = (e15, t3, s3, n10) => {
11671
11956
  var r3;
11672
- const a3 = o2.getDocument(e15);
11957
+ const a3 = i2.getDocument(e15);
11673
11958
  if (!(null == (r3 = null == a3 ? void 0 : a3.pages) ? void 0 : r3.length)) return false;
11674
11959
  const h3 = this.getDocumentScale(e15);
11675
- for (let o3 = 0; o3 < n10; o3++) {
11676
- const e16 = a3.pages[s3 + o3];
11677
- (null == e16 ? void 0 : e16.size) && i2.push({ localPageIndex: t3 + o3, width: e16.size.width * h3, height: e16.size.height * h3 });
11960
+ for (let i3 = 0; i3 < n10; i3++) {
11961
+ const e16 = a3.pages[s3 + i3];
11962
+ (null == e16 ? void 0 : e16.size) && o2.push({ localPageIndex: t3 + i3, width: e16.size.width * h3, height: e16.size.height * h3 });
11678
11963
  }
11679
- return i2.length > 0;
11964
+ return o2.length > 0;
11680
11965
  };
11681
11966
  if (a2) for (const l of a2.segments) {
11682
11967
  if (!s2.isSegmentLoaded(e14, l.index)) continue;
@@ -11684,12 +11969,12 @@ const Xt = class extends BasePlugin {
11684
11969
  h2(l.documentId, l.localPageStart, 0, t3);
11685
11970
  }
11686
11971
  else s2.isSegmentLoaded(e14, 0) && h2(e14, 0, 0, r2.length);
11687
- 0 !== i2.length ? this.applyChapterPageSizes(e14, i2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
11972
+ 0 !== o2.length ? this.applyChapterPageSizes(e14, o2) : t2 < 60 && requestAnimationFrame(() => this.harvestChapterSizes(e14, t2 + 1));
11688
11973
  }
11689
11974
  onScaleChanged(e14) {
11690
11975
  const t2 = this.coreState.core.documents[e14];
11691
11976
  if (!t2 || "loaded" !== t2.status) return;
11692
- const { chapterId: s2 } = Dt(e14);
11977
+ const { chapterId: s2 } = kt2(e14);
11693
11978
  this.chapterManager.provides().getChapter(s2) && this.harvestChapterSizes(s2);
11694
11979
  }
11695
11980
  dropChapterSizes(e14) {
@@ -11711,7 +11996,7 @@ const Xt = class extends BasePlugin {
11711
11996
  expandIndicesWithBuffer(e14, t2) {
11712
11997
  if (0 === e14.length || t2 <= 0) return [];
11713
11998
  const s2 = this.config.bufferSize, n9 = Math.max(0, e14[0] - s2), r2 = Math.min(t2 - 1, e14[e14.length - 1] + s2), a2 = [];
11714
- for (let o2 = n9; o2 <= r2; o2++) a2.push(o2);
11999
+ for (let i2 = n9; i2 <= r2; i2++) a2.push(i2);
11715
12000
  return a2;
11716
12001
  }
11717
12002
  recomputeVisible() {
@@ -11728,13 +12013,13 @@ const Xt = class extends BasePlugin {
11728
12013
  r2++;
11729
12014
  }
11730
12015
  const a2 = [];
11731
- let o2 = n9, i2 = -1;
12016
+ let i2 = n9, o2 = -1;
11732
12017
  for (let h2 = n9; h2 < r2; h2++) {
11733
12018
  a2.push(h2);
11734
12019
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
11735
- r3 > i2 && (i2 = r3, o2 = h2);
12020
+ r3 > o2 && (o2 = r3, i2 = h2);
11736
12021
  }
11737
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
12022
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
11738
12023
  }
11739
12024
  findFirstVisibleIndex(e14) {
11740
12025
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -11764,9 +12049,9 @@ const Xt = class extends BasePlugin {
11764
12049
  if (!this.viewportEl || e14 < 0 || e14 >= this.offsets.length || !this.virtualPageMap) return;
11765
12050
  const r2 = (null == s2 ? void 0 : s2.marginTop) ?? 80, a2 = this.virtualPageMap.atIndex(e14);
11766
12051
  if (!a2) return void this.scrollToIndex(e14, s2);
11767
- const o2 = this.pageSizes.get(e14), i2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = i2 ? null == (n9 = this.documentManager.provides().getDocument(i2.documentId)) ? void 0 : n9.pages[i2.pageIndex] : void 0;
11768
- if (!o2 || !h2) return void this.scrollToIndex(e14, s2);
11769
- const l = o2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
12052
+ const i2 = this.pageSizes.get(e14), o2 = this.chapterManager.provides().resolvePageDocument(a2.chapterId, a2.localPageIndex), h2 = o2 ? null == (n9 = this.documentManager.provides().getDocument(o2.documentId)) ? void 0 : n9.pages[o2.pageIndex] : void 0;
12053
+ if (!i2 || !h2) return void this.scrollToIndex(e14, s2);
12054
+ const l = i2.height / h2.size.height, d2 = (this.offsets[e14] ?? 0) + t2 * l - r2;
11770
12055
  this.viewportEl.scrollTo({ top: Math.max(0, d2), behavior: (null == s2 ? void 0 : s2.behavior) ?? "auto" });
11771
12056
  }
11772
12057
  scrollToChapter(e14, t2) {
@@ -11793,15 +12078,15 @@ function Qt(e14) {
11793
12078
  return { x: t2.origin.x + t2.size.width, y: t2.origin.y + t2.size.height };
11794
12079
  }
11795
12080
  function Yt(e14, t2) {
11796
- const s2 = e14.origin.y, n9 = e14.origin.y + e14.size.height, r2 = t2.origin.y, a2 = t2.origin.y + t2.size.height, o2 = Math.max(n9, a2) - Math.min(s2, r2);
11797
- if (o2 <= 0) return 0;
11798
- return Math.max(0, Math.min(n9, a2) - Math.max(s2, r2)) / o2;
12081
+ const s2 = e14.origin.y, n9 = e14.origin.y + e14.size.height, r2 = t2.origin.y, a2 = t2.origin.y + t2.size.height, i2 = Math.max(n9, a2) - Math.min(s2, r2);
12082
+ if (i2 <= 0) return 0;
12083
+ return Math.max(0, Math.min(n9, a2) - Math.max(s2, r2)) / i2;
11799
12084
  }
11800
12085
  function Zt(e14, t2) {
11801
12086
  var s2, n9;
11802
12087
  if (e14.chapterId !== t2.chapterId || e14.localPageIndex !== t2.localPageIndex) return false;
11803
- const r2 = (null == (s2 = e14.rectsPdfCoord) ? void 0 : s2.length) ? e14.rectsPdfCoord : [e14.rectPdfCoord], a2 = (null == (n9 = t2.rectsPdfCoord) ? void 0 : n9.length) ? t2.rectsPdfCoord : [t2.rectPdfCoord], o2 = e14.markerAnchor ?? Qt(r2), i2 = t2.markerAnchor ?? Qt(a2);
11804
- if (Math.hypot(o2.x - i2.x, o2.y - i2.y) <= 6) return true;
12088
+ const r2 = (null == (s2 = e14.rectsPdfCoord) ? void 0 : s2.length) ? e14.rectsPdfCoord : [e14.rectPdfCoord], a2 = (null == (n9 = t2.rectsPdfCoord) ? void 0 : n9.length) ? t2.rectsPdfCoord : [t2.rectPdfCoord], i2 = e14.markerAnchor ?? Qt(r2), o2 = t2.markerAnchor ?? Qt(a2);
12089
+ if (Math.hypot(i2.x - o2.x, i2.y - o2.y) <= 6) return true;
11805
12090
  for (const h2 of r2) for (const e15 of a2) {
11806
12091
  if (Yt(h2, e15) < 0.5) continue;
11807
12092
  const t3 = Math.max(h2.origin.x, e15.origin.x);
@@ -11855,8 +12140,8 @@ const ts = class s extends BasePlugin {
11855
12140
  const a2 = null == (n9 = null == (t2 = this.config) ? void 0 : t2.callbacks) ? void 0 : n9.onRequestRemove;
11856
12141
  if (a2) try {
11857
12142
  if (!await a2(r2)) return false;
11858
- } catch (o2) {
11859
- return this.logger.error(s.id, "RequestRemove", "callback failed", o2), false;
12143
+ } catch (i2) {
12144
+ return this.logger.error(s.id, "RequestRemove", "callback failed", i2), false;
11860
12145
  }
11861
12146
  return this.removeBookmark(e14), true;
11862
12147
  }
@@ -11907,8 +12192,8 @@ function cs(e14, t2) {
11907
12192
  }
11908
12193
  function us(e14) {
11909
12194
  var t2;
11910
- const s2 = cs(void 0, { enabled: true }), n9 = cs(void 0, { enabled: true }), r2 = cs(void 0, { enabled: true }), a2 = cs(void 0, { enabled: true, min: 0.5, max: 3, initial: 1 }), o2 = cs(false !== s2.enabled || false !== r2.enabled, { enabled: true });
11911
- return { markup: s2, bookmarks: n9, notes: r2, zoom: a2, scrollViewport: { background: (null == (t2 = void 0) ? void 0 : t2.background) ?? "#f1f5f9" }, selectionToolbar: o2 };
12195
+ const s2 = cs(void 0, { enabled: true }), n9 = cs(void 0, { enabled: true }), r2 = cs(void 0, { enabled: true }), a2 = cs(void 0, { enabled: true, min: 0.5, max: 3, initial: 1 }), i2 = cs(false !== s2.enabled || false !== r2.enabled, { enabled: true });
12196
+ return { markup: s2, bookmarks: n9, notes: r2, zoom: a2, scrollViewport: { background: (null == (t2 = void 0) ? void 0 : t2.background) ?? "#f1f5f9" }, selectionToolbar: i2 };
11912
12197
  }
11913
12198
  PdfAnnotationSubtype.HIGHLIGHT, PdfAnnotationSubtype.UNDERLINE, PdfAnnotationSubtype.SQUIGGLY, PdfAnnotationSubtype.STRIKEOUT, PdfAnnotationSubtype.HIGHLIGHT, PdfAnnotationSubtype.UNDERLINE, PdfAnnotationSubtype.SQUIGGLY, PdfAnnotationSubtype.STRIKEOUT;
11914
12199
  us();
@@ -11931,10 +12216,10 @@ function Is(e14) {
11931
12216
  })(e14), n9 = (function(e15, t3) {
11932
12217
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
11933
12218
  })(s2, t2), r2 = fs(e14.source) ? e14.source.urls : void 0, a2 = [];
11934
- for (let o2 = 0; o2 < n9; o2++) {
11935
- const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
12219
+ for (let i2 = 0; i2 < n9; i2++) {
12220
+ const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
11936
12221
  if (n10 >= s2) break;
11937
- a2.push({ index: o2, url: (r2 == null ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: vs(e14.chapterId, o2) });
12222
+ a2.push({ index: i2, url: (r2 == null ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: vs(e14.chapterId, i2) });
11938
12223
  }
11939
12224
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
11940
12225
  }
@@ -11962,21 +12247,21 @@ async function $s(e14) {
11962
12247
  return false;
11963
12248
  }
11964
12249
  }
11965
- const ks = 1;
12250
+ const Ds = 1;
11966
12251
  function Us(e14) {
11967
12252
  const t2 = new Uint8Array(e14);
11968
12253
  let s2 = "";
11969
12254
  for (let n9 = 0; n9 < t2.length; n9++) s2 += String.fromCharCode(t2[n9]);
11970
12255
  return btoa(s2);
11971
12256
  }
11972
- function Es(e14) {
12257
+ function Vs(e14) {
11973
12258
  return e14.map((e15) => {
11974
12259
  if (!(e15.ctx && "data" in e15.ctx && e15.ctx.data instanceof ArrayBuffer)) return { annotation: e15.annotation };
11975
12260
  const t2 = e15.ctx;
11976
12261
  return { annotation: e15.annotation, ctx: { dataBase64: Us(t2.data), mimeType: t2.mimeType } };
11977
12262
  });
11978
12263
  }
11979
- function Ts(e14) {
12264
+ function Es(e14) {
11980
12265
  return e14.map((e15) => {
11981
12266
  var _a2;
11982
12267
  if (!((_a2 = e15.ctx) == null ? void 0 : _a2.dataBase64)) return { annotation: e15.annotation };
@@ -11988,29 +12273,29 @@ function Ts(e14) {
11988
12273
  return { annotation: e15.annotation, ctx: { data: t2, ...e15.ctx.mimeType ? { mimeType: e15.ctx.mimeType } : {} } };
11989
12274
  });
11990
12275
  }
11991
- function Fs(e14) {
12276
+ function Ns(e14) {
11992
12277
  return { bookmarks: false !== (e14 == null ? void 0 : e14.bookmarks), notes: false !== (e14 == null ? void 0 : e14.notes), markup: false !== (e14 == null ? void 0 : e14.markup) };
11993
12278
  }
11994
- function Ns(e14) {
12279
+ function Ts(e14) {
11995
12280
  return new Promise((t2, s2) => {
11996
12281
  e14.wait((e15) => t2(e15), (e15) => s2(e15));
11997
12282
  });
11998
12283
  }
11999
- function As(e14) {
12284
+ function Fs(e14) {
12000
12285
  var _a2, _b, _c, _d;
12001
- const t2 = (_a2 = e14.getPlugin(AnnotationPlugin.id)) == null ? void 0 : _a2.provides(), s2 = (_b = e14.getPlugin(ot.id)) == null ? void 0 : _b.provides(), n9 = (_c = e14.getPlugin(ss.id)) == null ? void 0 : _c.provides(), r2 = (_d = e14.getPlugin(B.id)) == null ? void 0 : _d.provides();
12286
+ const t2 = (_a2 = e14.getPlugin(AnnotationPlugin.id)) == null ? void 0 : _a2.provides(), s2 = (_b = e14.getPlugin(it.id)) == null ? void 0 : _b.provides(), n9 = (_c = e14.getPlugin(ss.id)) == null ? void 0 : _c.provides(), r2 = (_d = e14.getPlugin(z.id)) == null ? void 0 : _d.provides();
12002
12287
  return { annotation: t2, note: s2, bookmark: n9, chapterManager: r2 };
12003
12288
  }
12004
- async function Os2(e14, t2, s2) {
12289
+ async function As(e14, t2, s2) {
12005
12290
  if (!s2 || !e14) return;
12006
12291
  const n9 = e14.getSegmentPlan(t2) ? await e14.ensureAllSegmentsLoaded(t2) : await e14.ensureChapterLoaded(t2);
12007
12292
  if ("loaded" !== n9) throw new Error(`章节 ${t2} 未加载成功(${n9}),无法导入/导出划线`);
12008
12293
  }
12009
- async function Vs(e14, t2, s2) {
12010
- const n9 = Fs(s2), { annotation: r2, note: a2, bookmark: o2, chapterManager: i2 } = As(e14), h2 = { chapterId: t2 };
12294
+ async function Os2(e14, t2, s2) {
12295
+ const n9 = Ns(s2), { annotation: r2, note: a2, bookmark: i2, chapterManager: o2 } = Fs(e14), h2 = { chapterId: t2 };
12011
12296
  if (n9.bookmarks) {
12012
- if (!o2) throw new Error("paragraph-bookmark 插件未注册");
12013
- h2.bookmarks = o2.listBookmarksForChapter(t2);
12297
+ if (!i2) throw new Error("paragraph-bookmark 插件未注册");
12298
+ h2.bookmarks = i2.listBookmarksForChapter(t2);
12014
12299
  }
12015
12300
  if (n9.notes) {
12016
12301
  if (!a2) throw new Error("note 插件未注册");
@@ -12018,76 +12303,76 @@ async function Vs(e14, t2, s2) {
12018
12303
  }
12019
12304
  if (n9.markup) {
12020
12305
  if (!r2) throw new Error("annotation 插件未注册");
12021
- await Os2(i2, t2, false !== (s2 == null ? void 0 : s2.ensureChapterLoaded));
12306
+ await As(o2, t2, false !== (s2 == null ? void 0 : s2.ensureChapterLoaded));
12022
12307
  const e15 = await (async function(e16, t3, s3) {
12023
12308
  const n10 = t3.getSegmentPlan(s3);
12024
- if (!n10) return Ns(e16.forDocument(s3).exportAnnotations());
12309
+ if (!n10) return Ts(e16.forDocument(s3).exportAnnotations());
12025
12310
  const r3 = [];
12026
12311
  for (const a3 of n10.segments) {
12027
12312
  if (!t3.isSegmentLoaded(s3, a3.index)) continue;
12028
- const n11 = await Ns(e16.forDocument(a3.documentId).exportAnnotations());
12313
+ const n11 = await Ts(e16.forDocument(a3.documentId).exportAnnotations());
12029
12314
  for (const e17 of n11) {
12030
12315
  const t4 = e17.annotation.pageIndex;
12031
12316
  r3.push({ ...e17, annotation: { ...e17.annotation, pageIndex: t4 + a3.localPageStart } });
12032
12317
  }
12033
12318
  }
12034
12319
  return r3;
12035
- })(r2, i2, t2);
12036
- h2.markup = Es(e15);
12320
+ })(r2, o2, t2);
12321
+ h2.markup = Vs(e15);
12037
12322
  }
12038
12323
  return h2;
12039
12324
  }
12040
- async function Gs(e14, t2, s2) {
12325
+ async function Rs(e14, t2, s2) {
12041
12326
  const n9 = {};
12042
12327
  for (const r2 of t2.chapters) {
12043
- const t3 = await Vs(e14, r2.chapterId, s2), { chapterId: a2, ...o2 } = t3;
12044
- n9[r2.chapterId] = o2;
12328
+ const t3 = await Os2(e14, r2.chapterId, s2), { chapterId: a2, ...i2 } = t3;
12329
+ n9[r2.chapterId] = i2;
12045
12330
  }
12046
12331
  return { version: 1, exportedAt: /* @__PURE__ */ (/* @__PURE__ */ new Date()).toISOString(), chapters: n9 };
12047
12332
  }
12048
- async function Rs(e14, t2, s2) {
12049
- const n9 = Fs(s2), r2 = (s2 == null ? void 0 : s2.mode) ?? "replace", { annotation: a2, note: o2, bookmark: i2, chapterManager: h2 } = As(e14), l = t2.chapterId;
12333
+ async function Gs(e14, t2, s2) {
12334
+ const n9 = Ns(s2), r2 = (s2 == null ? void 0 : s2.mode) ?? "replace", { annotation: a2, note: i2, bookmark: o2, chapterManager: h2 } = Fs(e14), l = t2.chapterId;
12050
12335
  if (n9.bookmarks && t2.bookmarks) {
12051
- if (!i2) throw new Error("paragraph-bookmark 插件未注册");
12052
- if ("replace" === r2) i2.replaceBookmarksForChapter(l, t2.bookmarks);
12053
- else for (const e15 of t2.bookmarks) i2.addBookmark(e15);
12336
+ if (!o2) throw new Error("paragraph-bookmark 插件未注册");
12337
+ if ("replace" === r2) o2.replaceBookmarksForChapter(l, t2.bookmarks);
12338
+ else for (const e15 of t2.bookmarks) o2.addBookmark(e15);
12054
12339
  }
12055
12340
  if (n9.notes && t2.notes) {
12056
- if (!o2) throw new Error("note 插件未注册");
12057
- if ("replace" === r2) o2.replaceNotesForChapter(l, t2.notes);
12058
- else for (const e15 of t2.notes) o2.registerNote(e15);
12341
+ if (!i2) throw new Error("note 插件未注册");
12342
+ if ("replace" === r2) i2.replaceNotesForChapter(l, t2.notes);
12343
+ else for (const e15 of t2.notes) i2.registerNote(e15);
12059
12344
  }
12060
12345
  if (n9.markup && t2.markup) {
12061
12346
  if (!a2) throw new Error("annotation 插件未注册");
12062
- await Os2(h2, l, false !== (s2 == null ? void 0 : s2.ensureChapterLoaded));
12347
+ await As(h2, l, false !== (s2 == null ? void 0 : s2.ensureChapterLoaded));
12063
12348
  !(function(e15, t3, s3, n10, r3) {
12064
12349
  const a3 = t3.getSegmentPlan(s3);
12065
12350
  if (!a3) {
12066
12351
  const t4 = e15.forDocument(s3);
12067
12352
  return "replace" === r3 && t4.deleteAllAnnotations(), void t4.importAnnotations(n10);
12068
12353
  }
12069
- const o3 = t3.getChapter(s3);
12070
- if (!o3) return;
12071
- const i3 = /* @__PURE__ */ new Map();
12354
+ const i3 = t3.getChapter(s3);
12355
+ if (!i3) return;
12356
+ const o3 = /* @__PURE__ */ new Map();
12072
12357
  for (const h3 of n10) {
12073
- const e16 = h3.annotation.pageIndex, t4 = Ps2(o3, e16), s4 = a3.segments[t4];
12358
+ const e16 = h3.annotation.pageIndex, t4 = Ps2(i3, e16), s4 = a3.segments[t4];
12074
12359
  if (!s4) continue;
12075
- const n11 = i3.get(t4) ?? [];
12076
- n11.push({ ...h3, annotation: { ...h3.annotation, pageIndex: e16 - s4.localPageStart } }), i3.set(t4, n11);
12360
+ const n11 = o3.get(t4) ?? [];
12361
+ n11.push({ ...h3, annotation: { ...h3.annotation, pageIndex: e16 - s4.localPageStart } }), o3.set(t4, n11);
12077
12362
  }
12078
12363
  for (const h3 of a3.segments) {
12079
- const t4 = e15.forDocument(h3.documentId), s4 = i3.get(h3.index) ?? [];
12364
+ const t4 = e15.forDocument(h3.documentId), s4 = o3.get(h3.index) ?? [];
12080
12365
  "replace" === r3 && t4.deleteAllAnnotations(), s4.length > 0 && t4.importAnnotations(s4);
12081
12366
  }
12082
- })(a2, h2, l, Ts(t2.markup), r2);
12367
+ })(a2, h2, l, Es(t2.markup), r2);
12083
12368
  }
12084
- (s2 == null ? void 0 : s2.persistNotes) && o2 && await s2.persistNotes(o2.listAllNotes()), (s2 == null ? void 0 : s2.persistBookmarks) && i2 && await s2.persistBookmarks(i2.listBookmarks());
12369
+ (s2 == null ? void 0 : s2.persistNotes) && i2 && await s2.persistNotes(i2.listAllNotes()), (s2 == null ? void 0 : s2.persistBookmarks) && o2 && await s2.persistBookmarks(o2.listBookmarks());
12085
12370
  }
12086
- async function zs(e14, t2, s2) {
12371
+ async function Bs(e14, t2, s2) {
12087
12372
  if (1 !== t2.version) throw new Error(`不支持的归档版本 ${t2.version},当前为 1`);
12088
- for (const [n9, r2] of Object.entries(t2.chapters)) await Rs(e14, { chapterId: n9, ...r2 }, s2);
12373
+ for (const [n9, r2] of Object.entries(t2.chapters)) await Gs(e14, { chapterId: n9, ...r2 }, s2);
12089
12374
  }
12090
- function Bs(e14) {
12375
+ function zs(e14) {
12091
12376
  return JSON.stringify(e14, null, 2);
12092
12377
  }
12093
12378
  function _s(e14) {
@@ -12108,7 +12393,7 @@ function Ks(e14, t2) {
12108
12393
  r2.href = n9, r2.download = e14, r2.click(), URL.revokeObjectURL(n9);
12109
12394
  }
12110
12395
  function Hs(e14, t2 = "chapter-annotations.json") {
12111
- Ks(t2, Bs(e14));
12396
+ Ks(t2, zs(e14));
12112
12397
  }
12113
12398
  function js(e14, t2) {
12114
12399
  Ks(t2 ?? `chapter-annotations-${e14.chapterId}.json`, qs(e14));
@@ -12313,6 +12598,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12313
12598
  this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map();
12314
12599
  this.chapterUrlsCache = /* @__PURE__ */ new Map();
12315
12600
  this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map();
12601
+ this.pendingVisibleUrlRepumps = /* @__PURE__ */ new Set();
12316
12602
  this.unloadTimer = null;
12317
12603
  this.documentManagerUnsubs = [];
12318
12604
  this.visibleSegmentBacklog = [];
@@ -12546,6 +12832,28 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12546
12832
  clearVisibleSegmentBacklog() {
12547
12833
  this.visibleSegmentBacklog = [];
12548
12834
  this.activeVisibleSegmentLoads = 0;
12835
+ this.pendingVisibleUrlRepumps.clear();
12836
+ }
12837
+ chapterNeedsResolvedUrls(chapter) {
12838
+ var _a2;
12839
+ const src = chapter.source;
12840
+ if (n8(src)) return false;
12841
+ if (src && ("url" in src || "buffer" in src || "load" in src)) return false;
12842
+ return !!((_a2 = this.config.chapterPdfLoader) == null ? void 0 : _a2.loadChapterUrls);
12843
+ }
12844
+ ensureVisibleChapterUrls(chapter) {
12845
+ const { chapterId } = chapter;
12846
+ if (!this.chapterNeedsResolvedUrls(chapter)) return true;
12847
+ if (this.chapterUrlsCache.has(chapterId)) return true;
12848
+ if (!this.pendingVisibleUrlRepumps.has(chapterId)) {
12849
+ this.pendingVisibleUrlRepumps.add(chapterId);
12850
+ void this.resolveChapterUrls(chapter).catch(() => {
12851
+ }).finally(() => {
12852
+ this.pendingVisibleUrlRepumps.delete(chapterId);
12853
+ this.pumpVisibleSegmentBacklog();
12854
+ });
12855
+ }
12856
+ return false;
12549
12857
  }
12550
12858
  /** 分段起始页在整本中的 globalPageIndex(用于算与视口距离) */
12551
12859
  segmentStartGlobalIndex(chapterId, segmentIndex) {
@@ -12577,10 +12885,20 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12577
12885
  }
12578
12886
  pumpVisibleSegmentBacklog() {
12579
12887
  const max = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
12888
+ let stalled = 0;
12580
12889
  while (this.activeVisibleSegmentLoads < max && this.visibleSegmentBacklog.length > 0) {
12581
12890
  const t2 = this.visibleSegmentBacklog.shift();
12582
12891
  if (this.isSegmentDocumentOpen(t2.chapterId, t2.segmentIndex)) continue;
12583
12892
  if (this.isSegmentLoadPending(t2.chapterId, t2.segmentIndex)) continue;
12893
+ const chapter = this.findChapter(t2.chapterId);
12894
+ if (!chapter) continue;
12895
+ if (!this.ensureVisibleChapterUrls(chapter)) {
12896
+ this.visibleSegmentBacklog.push(t2);
12897
+ stalled++;
12898
+ if (stalled >= this.visibleSegmentBacklog.length) break;
12899
+ continue;
12900
+ }
12901
+ stalled = 0;
12584
12902
  this.prepareChapterForSegmentLoad(t2.chapterId);
12585
12903
  this.activeVisibleSegmentLoads++;
12586
12904
  void this.loadSegmentNow(t2.chapterId, t2.segmentIndex).finally(() => {
@@ -13021,28 +13339,28 @@ const m = { kind: "first-wins" };
13021
13339
  function _(e14) {
13022
13340
  return "last" === e14 ? { kind: "last-wins" } : { kind: "first-wins" };
13023
13341
  }
13024
- function R(e14) {
13342
+ function R2(e14) {
13025
13343
  const t2 = e14.endPage - e14.startPage + 1;
13026
13344
  return { chapterId: e14.chapterId, title: e14.title, globalPageRange: [e14.startPage, e14.endPage], localPageRange: [0, t2 - 1], ...e14.source ? { source: e14.source } : {}, ...null != e14.segmentPageThreshold ? { segmentPageThreshold: e14.segmentPageThreshold } : {}, ...e14.encrypted ? { encrypted: e14.encrypted } : {} };
13027
13345
  }
13028
13346
  function C(e14) {
13029
13347
  const t2 = [], n9 = (e15) => {
13030
13348
  var _a2;
13031
- for (const r2 of e15) t2.push(R(r2)), ((_a2 = r2.children) == null ? void 0 : _a2.length) && n9(r2.children);
13349
+ for (const r2 of e15) t2.push(R2(r2)), ((_a2 = r2.children) == null ? void 0 : _a2.length) && n9(r2.children);
13032
13350
  };
13033
13351
  return n9(e14), t2;
13034
13352
  }
13035
- function G2(e14) {
13353
+ function G(e14) {
13036
13354
  return e14.map((e15) => {
13037
13355
  var _a2;
13038
- return { id: e15.chapterId, title: e15.title, startPage: e15.startPage, endPage: e15.endPage, children: ((_a2 = e15.children) == null ? void 0 : _a2.length) ? G2(e15.children) : void 0 };
13356
+ return { id: e15.chapterId, title: e15.title, startPage: e15.startPage, endPage: e15.endPage, children: ((_a2 = e15.children) == null ? void 0 : _a2.length) ? G(e15.children) : void 0 };
13039
13357
  });
13040
13358
  }
13041
13359
  function M(e14) {
13042
- return { tree: G2(e14), manifest: { chapters: C(e14) } };
13360
+ return { tree: G(e14), manifest: { chapters: C(e14) } };
13043
13361
  }
13044
13362
  export {
13045
- ks as CHAPTER_ANNOTATIONS_ARCHIVE_VERSION,
13363
+ Ds as CHAPTER_ANNOTATIONS_ARCHIVE_VERSION,
13046
13364
  Ne$1 as CallbackPasswordProvider,
13047
13365
  ChapterManagerPlugin,
13048
13366
  _sfc_main as ChapterPdfViewer,
@@ -13055,27 +13373,27 @@ export {
13055
13373
  xe$1 as applySelectionMarkup,
13056
13374
  M as buildChapterViewerCatalog,
13057
13375
  vn as buildParagraphBookmarkAnchor,
13058
- Bs as chapterAnnotationsArchiveToJson,
13376
+ zs as chapterAnnotationsArchiveToJson,
13059
13377
  qs as chapterAnnotationsSnapshotToJson,
13060
- R as chapterDescriptorFromTreeNode,
13378
+ R2 as chapterDescriptorFromTreeNode,
13061
13379
  $s as copyTextToClipboard,
13062
- Fn as createChapterViewerBundle,
13063
- En as createChapterViewerEditor,
13064
- Vn as createChapterViewerEditorOptions,
13380
+ Tn as createChapterViewerBundle,
13381
+ Un as createChapterViewerEditor,
13382
+ Fn as createChapterViewerEditorOptions,
13065
13383
  Ln as createPdfChapterEditor,
13066
13384
  m as defaultOverlapStrategy,
13067
13385
  Hs as downloadChapterAnnotationsArchive,
13068
13386
  js as downloadChapterAnnotationsSnapshot,
13069
13387
  Ks as downloadJsonFile,
13070
- Gs as exportAllChapterAnnotations,
13071
- Vs as exportChapterAnnotations,
13388
+ Rs as exportAllChapterAnnotations,
13389
+ Os2 as exportChapterAnnotations,
13072
13390
  C as flattenChapterTree,
13073
- Rs as importChapterAnnotations,
13074
- zs as importChapterAnnotationsArchive,
13391
+ Gs as importChapterAnnotations,
13392
+ Bs as importChapterAnnotationsArchive,
13075
13393
  _ as overlapStrategyForSamePageOwner,
13076
13394
  _s as parseChapterAnnotationsArchiveJson,
13077
13395
  Ws as parseChapterAnnotationsSnapshotJson,
13078
- G2 as toChapterTreeNodes,
13396
+ G as toChapterTreeNodes,
13079
13397
  useCapability2 as useCapability,
13080
13398
  usePdfiumEngine,
13081
13399
  useRegistry2 as useRegistry