@scarlett-player/embed 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4338 @@
1
+ class Signal {
2
+ constructor(initialValue) {
3
+ this.subscribers = /* @__PURE__ */ new Set();
4
+ this.value = initialValue;
5
+ }
6
+ /**
7
+ * Get the current value and track dependency if called within an effect.
8
+ *
9
+ * @returns Current value
10
+ */
11
+ get() {
12
+ return this.value;
13
+ }
14
+ /**
15
+ * Set a new value and notify subscribers if changed.
16
+ *
17
+ * @param newValue - New value to set
18
+ */
19
+ set(newValue) {
20
+ if (Object.is(this.value, newValue)) {
21
+ return;
22
+ }
23
+ this.value = newValue;
24
+ this.notify();
25
+ }
26
+ /**
27
+ * Update the value using a function.
28
+ *
29
+ * @param updater - Function that receives current value and returns new value
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const count = new Signal(0);
34
+ * count.update(n => n + 1); // Increments by 1
35
+ * ```
36
+ */
37
+ update(updater) {
38
+ this.set(updater(this.value));
39
+ }
40
+ /**
41
+ * Subscribe to changes without automatic dependency tracking.
42
+ *
43
+ * @param callback - Function to call when value changes
44
+ * @returns Unsubscribe function
45
+ */
46
+ subscribe(callback) {
47
+ this.subscribers.add(callback);
48
+ return () => this.subscribers.delete(callback);
49
+ }
50
+ /**
51
+ * Notify all subscribers of a change.
52
+ * @internal
53
+ */
54
+ notify() {
55
+ this.subscribers.forEach((subscriber) => {
56
+ try {
57
+ subscriber();
58
+ } catch (error) {
59
+ console.error("[Scarlett Player] Error in signal subscriber:", error);
60
+ }
61
+ });
62
+ }
63
+ /**
64
+ * Clean up all subscriptions.
65
+ * Call this when destroying the signal.
66
+ */
67
+ destroy() {
68
+ this.subscribers.clear();
69
+ }
70
+ /**
71
+ * Get the current number of subscribers (for debugging).
72
+ * @internal
73
+ */
74
+ getSubscriberCount() {
75
+ return this.subscribers.size;
76
+ }
77
+ }
78
+ function signal(initialValue) {
79
+ return new Signal(initialValue);
80
+ }
81
+ const DEFAULT_STATE = {
82
+ // Core Playback State
83
+ playbackState: "idle",
84
+ playing: false,
85
+ paused: true,
86
+ ended: false,
87
+ buffering: false,
88
+ waiting: false,
89
+ seeking: false,
90
+ // Time & Duration
91
+ currentTime: 0,
92
+ duration: NaN,
93
+ buffered: null,
94
+ bufferedAmount: 0,
95
+ // Media Info
96
+ mediaType: "unknown",
97
+ source: null,
98
+ title: "",
99
+ poster: "",
100
+ // Volume & Audio
101
+ volume: 1,
102
+ muted: false,
103
+ // Playback Controls
104
+ playbackRate: 1,
105
+ fullscreen: false,
106
+ pip: false,
107
+ controlsVisible: true,
108
+ // Quality & Tracks
109
+ qualities: [],
110
+ currentQuality: null,
111
+ audioTracks: [],
112
+ currentAudioTrack: null,
113
+ textTracks: [],
114
+ currentTextTrack: null,
115
+ // Live/DVR State (TSP features)
116
+ live: false,
117
+ liveEdge: true,
118
+ seekableRange: null,
119
+ liveLatency: 0,
120
+ lowLatencyMode: false,
121
+ // Chapters (TSP features)
122
+ chapters: [],
123
+ currentChapter: null,
124
+ // Error State
125
+ error: null,
126
+ // Network & Performance
127
+ bandwidth: 0,
128
+ autoplay: false,
129
+ loop: false,
130
+ // Casting State
131
+ airplayAvailable: false,
132
+ airplayActive: false,
133
+ chromecastAvailable: false,
134
+ chromecastActive: false,
135
+ // UI State
136
+ interacting: false,
137
+ hovering: false,
138
+ focused: false
139
+ };
140
+ class StateManager {
141
+ /**
142
+ * Create a new StateManager with default initial state.
143
+ *
144
+ * @param initialState - Optional partial initial state (merged with defaults)
145
+ */
146
+ constructor(initialState) {
147
+ this.signals = /* @__PURE__ */ new Map();
148
+ this.changeSubscribers = /* @__PURE__ */ new Set();
149
+ this.initializeSignals(initialState);
150
+ }
151
+ /**
152
+ * Initialize all state signals with default or provided values.
153
+ * @private
154
+ */
155
+ initializeSignals(overrides) {
156
+ const initialState = { ...DEFAULT_STATE, ...overrides };
157
+ for (const [key, value] of Object.entries(initialState)) {
158
+ const stateKey = key;
159
+ const stateSignal = signal(value);
160
+ stateSignal.subscribe(() => {
161
+ this.notifyChangeSubscribers(stateKey);
162
+ });
163
+ this.signals.set(stateKey, stateSignal);
164
+ }
165
+ }
166
+ /**
167
+ * Get the signal for a state property.
168
+ *
169
+ * @param key - State property key
170
+ * @returns Signal for the property
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const playingSignal = state.get('playing');
175
+ * playingSignal.get(); // false
176
+ * playingSignal.set(true);
177
+ * ```
178
+ */
179
+ get(key) {
180
+ const stateSignal = this.signals.get(key);
181
+ if (!stateSignal) {
182
+ throw new Error(`[StateManager] Unknown state key: ${key}`);
183
+ }
184
+ return stateSignal;
185
+ }
186
+ /**
187
+ * Get the current value of a state property (convenience method).
188
+ *
189
+ * @param key - State property key
190
+ * @returns Current value
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * state.getValue('playing'); // false
195
+ * ```
196
+ */
197
+ getValue(key) {
198
+ return this.get(key).get();
199
+ }
200
+ /**
201
+ * Set the value of a state property.
202
+ *
203
+ * @param key - State property key
204
+ * @param value - New value
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * state.set('playing', true);
209
+ * state.set('currentTime', 10.5);
210
+ * ```
211
+ */
212
+ set(key, value) {
213
+ this.get(key).set(value);
214
+ }
215
+ /**
216
+ * Update multiple state properties at once (batch update).
217
+ *
218
+ * More efficient than calling set() multiple times.
219
+ *
220
+ * @param updates - Partial state object with updates
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * state.update({
225
+ * playing: true,
226
+ * currentTime: 0,
227
+ * volume: 1.0,
228
+ * });
229
+ * ```
230
+ */
231
+ update(updates) {
232
+ for (const [key, value] of Object.entries(updates)) {
233
+ const stateKey = key;
234
+ if (this.signals.has(stateKey)) {
235
+ this.set(stateKey, value);
236
+ }
237
+ }
238
+ }
239
+ /**
240
+ * Subscribe to changes on a specific state property.
241
+ *
242
+ * @param key - State property key
243
+ * @param callback - Callback function receiving new value
244
+ * @returns Unsubscribe function
245
+ *
246
+ * @example
247
+ * ```ts
248
+ * const unsub = state.subscribe('playing', (value) => {
249
+ * console.log('Playing:', value);
250
+ * });
251
+ * ```
252
+ */
253
+ subscribeToKey(key, callback) {
254
+ const stateSignal = this.get(key);
255
+ return stateSignal.subscribe(() => {
256
+ callback(stateSignal.get());
257
+ });
258
+ }
259
+ /**
260
+ * Subscribe to all state changes.
261
+ *
262
+ * Receives a StateChangeEvent for every state property change.
263
+ *
264
+ * @param callback - Callback function receiving change events
265
+ * @returns Unsubscribe function
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * const unsub = state.subscribe((event) => {
270
+ * console.log(`${event.key} changed:`, event.value);
271
+ * });
272
+ * ```
273
+ */
274
+ subscribe(callback) {
275
+ this.changeSubscribers.add(callback);
276
+ return () => this.changeSubscribers.delete(callback);
277
+ }
278
+ /**
279
+ * Notify all global change subscribers.
280
+ * @private
281
+ */
282
+ notifyChangeSubscribers(key) {
283
+ const stateSignal = this.get(key);
284
+ const value = stateSignal.get();
285
+ const event = {
286
+ key,
287
+ value,
288
+ previousValue: value
289
+ // Note: We don't track previous values in this simple impl
290
+ };
291
+ this.changeSubscribers.forEach((subscriber) => {
292
+ try {
293
+ subscriber(event);
294
+ } catch (error) {
295
+ console.error("[StateManager] Error in change subscriber:", error);
296
+ }
297
+ });
298
+ }
299
+ /**
300
+ * Reset all state to default values.
301
+ *
302
+ * @example
303
+ * ```ts
304
+ * state.reset();
305
+ * ```
306
+ */
307
+ reset() {
308
+ this.update(DEFAULT_STATE);
309
+ }
310
+ /**
311
+ * Reset a specific state property to its default value.
312
+ *
313
+ * @param key - State property key
314
+ *
315
+ * @example
316
+ * ```ts
317
+ * state.resetKey('playing');
318
+ * ```
319
+ */
320
+ resetKey(key) {
321
+ const defaultValue = DEFAULT_STATE[key];
322
+ this.set(key, defaultValue);
323
+ }
324
+ /**
325
+ * Get a snapshot of all current state values.
326
+ *
327
+ * @returns Frozen snapshot of current state
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * const snapshot = state.snapshot();
332
+ * console.log(snapshot.playing, snapshot.currentTime);
333
+ * ```
334
+ */
335
+ snapshot() {
336
+ const snapshot = {};
337
+ for (const [key, stateSignal] of this.signals) {
338
+ snapshot[key] = stateSignal.get();
339
+ }
340
+ return Object.freeze(snapshot);
341
+ }
342
+ /**
343
+ * Get the number of subscribers for a state property (for debugging).
344
+ *
345
+ * @param key - State property key
346
+ * @returns Number of subscribers
347
+ * @internal
348
+ */
349
+ getSubscriberCount(key) {
350
+ return this.signals.get(key)?.getSubscriberCount() ?? 0;
351
+ }
352
+ /**
353
+ * Destroy the state manager and cleanup all signals.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * state.destroy();
358
+ * ```
359
+ */
360
+ destroy() {
361
+ this.signals.forEach((stateSignal) => stateSignal.destroy());
362
+ this.signals.clear();
363
+ this.changeSubscribers.clear();
364
+ }
365
+ }
366
+ const DEFAULT_OPTIONS = {
367
+ maxListeners: 100,
368
+ async: false,
369
+ interceptors: true
370
+ };
371
+ class EventBus {
372
+ /**
373
+ * Create a new EventBus.
374
+ *
375
+ * @param options - Optional configuration
376
+ */
377
+ constructor(options) {
378
+ this.listeners = /* @__PURE__ */ new Map();
379
+ this.onceListeners = /* @__PURE__ */ new Map();
380
+ this.interceptors = /* @__PURE__ */ new Map();
381
+ this.options = { ...DEFAULT_OPTIONS, ...options };
382
+ }
383
+ /**
384
+ * Subscribe to an event.
385
+ *
386
+ * @param event - Event name
387
+ * @param handler - Event handler function
388
+ * @returns Unsubscribe function
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * const unsub = events.on('playback:play', () => {
393
+ * console.log('Playing!');
394
+ * });
395
+ *
396
+ * // Later: unsubscribe
397
+ * unsub();
398
+ * ```
399
+ */
400
+ on(event, handler) {
401
+ if (!this.listeners.has(event)) {
402
+ this.listeners.set(event, /* @__PURE__ */ new Set());
403
+ }
404
+ const handlers = this.listeners.get(event);
405
+ handlers.add(handler);
406
+ this.checkMaxListeners(event);
407
+ return () => this.off(event, handler);
408
+ }
409
+ /**
410
+ * Subscribe to an event once (auto-unsubscribe after first call).
411
+ *
412
+ * @param event - Event name
413
+ * @param handler - Event handler function
414
+ * @returns Unsubscribe function
415
+ *
416
+ * @example
417
+ * ```ts
418
+ * events.once('player:ready', () => {
419
+ * console.log('Player ready!');
420
+ * });
421
+ * ```
422
+ */
423
+ once(event, handler) {
424
+ if (!this.onceListeners.has(event)) {
425
+ this.onceListeners.set(event, /* @__PURE__ */ new Set());
426
+ }
427
+ const handlers = this.onceListeners.get(event);
428
+ handlers.add(handler);
429
+ if (!this.listeners.has(event)) {
430
+ this.listeners.set(event, /* @__PURE__ */ new Set());
431
+ }
432
+ return () => {
433
+ handlers.delete(handler);
434
+ };
435
+ }
436
+ /**
437
+ * Unsubscribe from an event.
438
+ *
439
+ * @param event - Event name
440
+ * @param handler - Event handler function to remove
441
+ *
442
+ * @example
443
+ * ```ts
444
+ * const handler = () => console.log('Playing!');
445
+ * events.on('playback:play', handler);
446
+ * events.off('playback:play', handler);
447
+ * ```
448
+ */
449
+ off(event, handler) {
450
+ const handlers = this.listeners.get(event);
451
+ if (handlers) {
452
+ handlers.delete(handler);
453
+ if (handlers.size === 0) {
454
+ this.listeners.delete(event);
455
+ }
456
+ }
457
+ const onceHandlers = this.onceListeners.get(event);
458
+ if (onceHandlers) {
459
+ onceHandlers.delete(handler);
460
+ if (onceHandlers.size === 0) {
461
+ this.onceListeners.delete(event);
462
+ }
463
+ }
464
+ }
465
+ /**
466
+ * Emit an event synchronously.
467
+ *
468
+ * Runs interceptors first, then calls all handlers.
469
+ *
470
+ * @param event - Event name
471
+ * @param payload - Event payload
472
+ *
473
+ * @example
474
+ * ```ts
475
+ * events.emit('playback:play', undefined);
476
+ * events.emit('playback:timeupdate', { currentTime: 10.5 });
477
+ * ```
478
+ */
479
+ emit(event, payload) {
480
+ const interceptedPayload = this.runInterceptors(event, payload);
481
+ if (interceptedPayload === null) {
482
+ return;
483
+ }
484
+ const handlers = this.listeners.get(event);
485
+ if (handlers) {
486
+ const handlersArray = Array.from(handlers);
487
+ handlersArray.forEach((handler) => {
488
+ this.safeCallHandler(handler, interceptedPayload);
489
+ });
490
+ }
491
+ const onceHandlers = this.onceListeners.get(event);
492
+ if (onceHandlers) {
493
+ const handlersArray = Array.from(onceHandlers);
494
+ handlersArray.forEach((handler) => {
495
+ this.safeCallHandler(handler, interceptedPayload);
496
+ });
497
+ this.onceListeners.delete(event);
498
+ }
499
+ }
500
+ /**
501
+ * Emit an event asynchronously (next tick).
502
+ *
503
+ * @param event - Event name
504
+ * @param payload - Event payload
505
+ * @returns Promise that resolves when all handlers complete
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * await events.emitAsync('media:loaded', { src: 'video.mp4', type: 'video/mp4' });
510
+ * ```
511
+ */
512
+ async emitAsync(event, payload) {
513
+ const interceptedPayload = await this.runInterceptorsAsync(event, payload);
514
+ if (interceptedPayload === null) {
515
+ return;
516
+ }
517
+ const handlers = this.listeners.get(event);
518
+ if (handlers) {
519
+ const promises = Array.from(handlers).map(
520
+ (handler) => this.safeCallHandlerAsync(handler, interceptedPayload)
521
+ );
522
+ await Promise.all(promises);
523
+ }
524
+ const onceHandlers = this.onceListeners.get(event);
525
+ if (onceHandlers) {
526
+ const handlersArray = Array.from(onceHandlers);
527
+ const promises = handlersArray.map(
528
+ (handler) => this.safeCallHandlerAsync(handler, interceptedPayload)
529
+ );
530
+ await Promise.all(promises);
531
+ this.onceListeners.delete(event);
532
+ }
533
+ }
534
+ /**
535
+ * Add an event interceptor.
536
+ *
537
+ * Interceptors run before handlers and can modify or cancel events.
538
+ *
539
+ * @param event - Event name
540
+ * @param interceptor - Interceptor function
541
+ * @returns Remove interceptor function
542
+ *
543
+ * @example
544
+ * ```ts
545
+ * events.intercept('playback:timeupdate', (payload) => {
546
+ * // Round time to 2 decimals
547
+ * return { currentTime: Math.round(payload.currentTime * 100) / 100 };
548
+ * });
549
+ *
550
+ * // Cancel events
551
+ * events.intercept('playback:play', (payload) => {
552
+ * if (notReady) return null; // Cancel event
553
+ * return payload;
554
+ * });
555
+ * ```
556
+ */
557
+ intercept(event, interceptor) {
558
+ if (!this.options.interceptors) {
559
+ return () => {
560
+ };
561
+ }
562
+ if (!this.interceptors.has(event)) {
563
+ this.interceptors.set(event, /* @__PURE__ */ new Set());
564
+ }
565
+ const interceptorsSet = this.interceptors.get(event);
566
+ interceptorsSet.add(interceptor);
567
+ return () => {
568
+ interceptorsSet.delete(interceptor);
569
+ if (interceptorsSet.size === 0) {
570
+ this.interceptors.delete(event);
571
+ }
572
+ };
573
+ }
574
+ /**
575
+ * Remove all listeners for an event (or all events if no event specified).
576
+ *
577
+ * @param event - Optional event name
578
+ *
579
+ * @example
580
+ * ```ts
581
+ * events.removeAllListeners('playback:play'); // Remove all playback:play listeners
582
+ * events.removeAllListeners(); // Remove ALL listeners
583
+ * ```
584
+ */
585
+ removeAllListeners(event) {
586
+ if (event) {
587
+ this.listeners.delete(event);
588
+ this.onceListeners.delete(event);
589
+ } else {
590
+ this.listeners.clear();
591
+ this.onceListeners.clear();
592
+ }
593
+ }
594
+ /**
595
+ * Get the number of listeners for an event.
596
+ *
597
+ * @param event - Event name
598
+ * @returns Number of listeners
599
+ *
600
+ * @example
601
+ * ```ts
602
+ * events.listenerCount('playback:play'); // 3
603
+ * ```
604
+ */
605
+ listenerCount(event) {
606
+ const regularCount = this.listeners.get(event)?.size ?? 0;
607
+ const onceCount = this.onceListeners.get(event)?.size ?? 0;
608
+ return regularCount + onceCount;
609
+ }
610
+ /**
611
+ * Destroy event bus and cleanup all listeners/interceptors.
612
+ *
613
+ * @example
614
+ * ```ts
615
+ * events.destroy();
616
+ * ```
617
+ */
618
+ destroy() {
619
+ this.listeners.clear();
620
+ this.onceListeners.clear();
621
+ this.interceptors.clear();
622
+ }
623
+ /**
624
+ * Run interceptors synchronously.
625
+ * @private
626
+ */
627
+ runInterceptors(event, payload) {
628
+ if (!this.options.interceptors) {
629
+ return payload;
630
+ }
631
+ const interceptorsSet = this.interceptors.get(event);
632
+ if (!interceptorsSet || interceptorsSet.size === 0) {
633
+ return payload;
634
+ }
635
+ let currentPayload = payload;
636
+ for (const interceptor of interceptorsSet) {
637
+ try {
638
+ currentPayload = interceptor(currentPayload);
639
+ if (currentPayload === null) {
640
+ return null;
641
+ }
642
+ } catch (error) {
643
+ console.error("[EventBus] Error in interceptor:", error);
644
+ }
645
+ }
646
+ return currentPayload;
647
+ }
648
+ /**
649
+ * Run interceptors asynchronously.
650
+ * @private
651
+ */
652
+ async runInterceptorsAsync(event, payload) {
653
+ if (!this.options.interceptors) {
654
+ return payload;
655
+ }
656
+ const interceptorsSet = this.interceptors.get(event);
657
+ if (!interceptorsSet || interceptorsSet.size === 0) {
658
+ return payload;
659
+ }
660
+ let currentPayload = payload;
661
+ for (const interceptor of interceptorsSet) {
662
+ try {
663
+ const result = interceptor(currentPayload);
664
+ currentPayload = result instanceof Promise ? await result : result;
665
+ if (currentPayload === null) {
666
+ return null;
667
+ }
668
+ } catch (error) {
669
+ console.error("[EventBus] Error in interceptor:", error);
670
+ }
671
+ }
672
+ return currentPayload;
673
+ }
674
+ /**
675
+ * Safely call a handler with error handling.
676
+ * @private
677
+ */
678
+ safeCallHandler(handler, payload) {
679
+ try {
680
+ handler(payload);
681
+ } catch (error) {
682
+ console.error("[EventBus] Error in event handler:", error);
683
+ }
684
+ }
685
+ /**
686
+ * Safely call a handler asynchronously with error handling.
687
+ * @private
688
+ */
689
+ async safeCallHandlerAsync(handler, payload) {
690
+ try {
691
+ const result = handler(payload);
692
+ if (result instanceof Promise) {
693
+ await result;
694
+ }
695
+ } catch (error) {
696
+ console.error("[EventBus] Error in event handler:", error);
697
+ }
698
+ }
699
+ /**
700
+ * Check if max listeners exceeded and warn.
701
+ * @private
702
+ */
703
+ checkMaxListeners(event) {
704
+ const count = this.listenerCount(event);
705
+ if (count > this.options.maxListeners) {
706
+ console.warn(
707
+ `[EventBus] Max listeners (${this.options.maxListeners}) exceeded for event: ${event}. Current count: ${count}. This may indicate a memory leak.`
708
+ );
709
+ }
710
+ }
711
+ }
712
+ const LOG_LEVELS = ["debug", "info", "warn", "error"];
713
+ const defaultConsoleHandler = (entry) => {
714
+ const prefix = entry.scope ? `[${entry.scope}]` : "[ScarlettPlayer]";
715
+ const message = `${prefix} ${entry.message}`;
716
+ const metadata = entry.metadata ?? "";
717
+ switch (entry.level) {
718
+ case "debug":
719
+ console.debug(message, metadata);
720
+ break;
721
+ case "info":
722
+ console.info(message, metadata);
723
+ break;
724
+ case "warn":
725
+ console.warn(message, metadata);
726
+ break;
727
+ case "error":
728
+ console.error(message, metadata);
729
+ break;
730
+ }
731
+ };
732
+ class Logger {
733
+ /**
734
+ * Create a new Logger.
735
+ *
736
+ * @param options - Logger configuration
737
+ */
738
+ constructor(options) {
739
+ this.level = options?.level ?? "warn";
740
+ this.scope = options?.scope;
741
+ this.enabled = options?.enabled ?? true;
742
+ this.handlers = options?.handlers ?? [defaultConsoleHandler];
743
+ }
744
+ /**
745
+ * Create a child logger with a scope.
746
+ *
747
+ * Child loggers inherit parent settings and chain scopes.
748
+ *
749
+ * @param scope - Child logger scope
750
+ * @returns New child logger
751
+ *
752
+ * @example
753
+ * ```ts
754
+ * const logger = new Logger();
755
+ * const hlsLogger = logger.child('hls-plugin');
756
+ * hlsLogger.info('Loading manifest');
757
+ * // Output: [ScarlettPlayer:hls-plugin] Loading manifest
758
+ * ```
759
+ */
760
+ child(scope) {
761
+ return new Logger({
762
+ level: this.level,
763
+ scope: this.scope ? `${this.scope}:${scope}` : scope,
764
+ enabled: this.enabled,
765
+ handlers: this.handlers
766
+ });
767
+ }
768
+ /**
769
+ * Log a debug message.
770
+ *
771
+ * @param message - Log message
772
+ * @param metadata - Optional structured metadata
773
+ *
774
+ * @example
775
+ * ```ts
776
+ * logger.debug('Request sent', { url: '/api/video' });
777
+ * ```
778
+ */
779
+ debug(message, metadata) {
780
+ this.log("debug", message, metadata);
781
+ }
782
+ /**
783
+ * Log an info message.
784
+ *
785
+ * @param message - Log message
786
+ * @param metadata - Optional structured metadata
787
+ *
788
+ * @example
789
+ * ```ts
790
+ * logger.info('Player ready');
791
+ * ```
792
+ */
793
+ info(message, metadata) {
794
+ this.log("info", message, metadata);
795
+ }
796
+ /**
797
+ * Log a warning message.
798
+ *
799
+ * @param message - Log message
800
+ * @param metadata - Optional structured metadata
801
+ *
802
+ * @example
803
+ * ```ts
804
+ * logger.warn('Low buffer', { buffered: 2.5 });
805
+ * ```
806
+ */
807
+ warn(message, metadata) {
808
+ this.log("warn", message, metadata);
809
+ }
810
+ /**
811
+ * Log an error message.
812
+ *
813
+ * @param message - Log message
814
+ * @param metadata - Optional structured metadata
815
+ *
816
+ * @example
817
+ * ```ts
818
+ * logger.error('Playback failed', { code: 'MEDIA_ERR_DECODE' });
819
+ * ```
820
+ */
821
+ error(message, metadata) {
822
+ this.log("error", message, metadata);
823
+ }
824
+ /**
825
+ * Set the minimum log level threshold.
826
+ *
827
+ * @param level - New log level
828
+ *
829
+ * @example
830
+ * ```ts
831
+ * logger.setLevel('debug'); // Show all logs
832
+ * logger.setLevel('error'); // Show only errors
833
+ * ```
834
+ */
835
+ setLevel(level) {
836
+ this.level = level;
837
+ }
838
+ /**
839
+ * Enable or disable logging.
840
+ *
841
+ * @param enabled - Enable flag
842
+ *
843
+ * @example
844
+ * ```ts
845
+ * logger.setEnabled(false); // Disable all logging
846
+ * ```
847
+ */
848
+ setEnabled(enabled) {
849
+ this.enabled = enabled;
850
+ }
851
+ /**
852
+ * Add a custom log handler.
853
+ *
854
+ * @param handler - Log handler function
855
+ *
856
+ * @example
857
+ * ```ts
858
+ * logger.addHandler((entry) => {
859
+ * if (entry.level === 'error') {
860
+ * sendToAnalytics(entry);
861
+ * }
862
+ * });
863
+ * ```
864
+ */
865
+ addHandler(handler) {
866
+ this.handlers.push(handler);
867
+ }
868
+ /**
869
+ * Remove a custom log handler.
870
+ *
871
+ * @param handler - Log handler function to remove
872
+ *
873
+ * @example
874
+ * ```ts
875
+ * logger.removeHandler(myHandler);
876
+ * ```
877
+ */
878
+ removeHandler(handler) {
879
+ const index = this.handlers.indexOf(handler);
880
+ if (index !== -1) {
881
+ this.handlers.splice(index, 1);
882
+ }
883
+ }
884
+ /**
885
+ * Core logging implementation.
886
+ * @private
887
+ */
888
+ log(level, message, metadata) {
889
+ if (!this.enabled || !this.shouldLog(level)) {
890
+ return;
891
+ }
892
+ const entry = {
893
+ level,
894
+ message,
895
+ timestamp: Date.now(),
896
+ scope: this.scope,
897
+ metadata
898
+ };
899
+ for (const handler of this.handlers) {
900
+ try {
901
+ handler(entry);
902
+ } catch (error) {
903
+ console.error("[Logger] Handler error:", error);
904
+ }
905
+ }
906
+ }
907
+ /**
908
+ * Check if a log level should be output.
909
+ * @private
910
+ */
911
+ shouldLog(level) {
912
+ return LOG_LEVELS.indexOf(level) >= LOG_LEVELS.indexOf(this.level);
913
+ }
914
+ }
915
+ var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
916
+ ErrorCode2["SOURCE_NOT_SUPPORTED"] = "SOURCE_NOT_SUPPORTED";
917
+ ErrorCode2["SOURCE_LOAD_FAILED"] = "SOURCE_LOAD_FAILED";
918
+ ErrorCode2["PROVIDER_NOT_FOUND"] = "PROVIDER_NOT_FOUND";
919
+ ErrorCode2["PROVIDER_SETUP_FAILED"] = "PROVIDER_SETUP_FAILED";
920
+ ErrorCode2["PLUGIN_SETUP_FAILED"] = "PLUGIN_SETUP_FAILED";
921
+ ErrorCode2["PLUGIN_NOT_FOUND"] = "PLUGIN_NOT_FOUND";
922
+ ErrorCode2["PLAYBACK_FAILED"] = "PLAYBACK_FAILED";
923
+ ErrorCode2["MEDIA_DECODE_ERROR"] = "MEDIA_DECODE_ERROR";
924
+ ErrorCode2["MEDIA_NETWORK_ERROR"] = "MEDIA_NETWORK_ERROR";
925
+ ErrorCode2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
926
+ return ErrorCode2;
927
+ })(ErrorCode || {});
928
+ class ErrorHandler {
929
+ /**
930
+ * Create a new ErrorHandler.
931
+ *
932
+ * @param eventBus - Event bus for error emission
933
+ * @param logger - Logger for error logging
934
+ * @param options - Optional configuration
935
+ */
936
+ constructor(eventBus, logger, options) {
937
+ this.errors = [];
938
+ this.eventBus = eventBus;
939
+ this.logger = logger;
940
+ this.maxHistory = options?.maxHistory ?? 10;
941
+ }
942
+ /**
943
+ * Handle an error.
944
+ *
945
+ * Normalizes, logs, emits, and tracks the error.
946
+ *
947
+ * @param error - Error to handle (native or PlayerError)
948
+ * @param context - Optional context (what was happening)
949
+ * @returns Normalized PlayerError
950
+ *
951
+ * @example
952
+ * ```ts
953
+ * try {
954
+ * loadVideo();
955
+ * } catch (error) {
956
+ * errorHandler.handle(error as Error, { src: 'video.mp4' });
957
+ * }
958
+ * ```
959
+ */
960
+ handle(error, context) {
961
+ const playerError = this.normalizeError(error, context);
962
+ this.addToHistory(playerError);
963
+ this.logError(playerError);
964
+ this.eventBus.emit("error", playerError);
965
+ return playerError;
966
+ }
967
+ /**
968
+ * Create and handle an error from code.
969
+ *
970
+ * @param code - Error code
971
+ * @param message - Error message
972
+ * @param options - Optional error options
973
+ * @returns Created PlayerError
974
+ *
975
+ * @example
976
+ * ```ts
977
+ * errorHandler.throw(
978
+ * ErrorCode.SOURCE_NOT_SUPPORTED,
979
+ * 'MP4 not supported',
980
+ * { fatal: true, context: { type: 'video/mp4' } }
981
+ * );
982
+ * ```
983
+ */
984
+ throw(code, message, options) {
985
+ const error = {
986
+ code,
987
+ message,
988
+ fatal: options?.fatal ?? this.isFatalCode(code),
989
+ timestamp: Date.now(),
990
+ context: options?.context,
991
+ originalError: options?.originalError
992
+ };
993
+ return this.handle(error, options?.context);
994
+ }
995
+ /**
996
+ * Get error history.
997
+ *
998
+ * @returns Readonly copy of error history
999
+ *
1000
+ * @example
1001
+ * ```ts
1002
+ * const history = errorHandler.getHistory();
1003
+ * console.log(`${history.length} errors occurred`);
1004
+ * ```
1005
+ */
1006
+ getHistory() {
1007
+ return [...this.errors];
1008
+ }
1009
+ /**
1010
+ * Get last error that occurred.
1011
+ *
1012
+ * @returns Last error or null if none
1013
+ *
1014
+ * @example
1015
+ * ```ts
1016
+ * const lastError = errorHandler.getLastError();
1017
+ * if (lastError?.fatal) {
1018
+ * showErrorMessage(lastError.message);
1019
+ * }
1020
+ * ```
1021
+ */
1022
+ getLastError() {
1023
+ return this.errors[this.errors.length - 1] ?? null;
1024
+ }
1025
+ /**
1026
+ * Clear error history.
1027
+ *
1028
+ * @example
1029
+ * ```ts
1030
+ * errorHandler.clearHistory();
1031
+ * ```
1032
+ */
1033
+ clearHistory() {
1034
+ this.errors = [];
1035
+ }
1036
+ /**
1037
+ * Check if any fatal errors occurred.
1038
+ *
1039
+ * @returns True if any fatal error in history
1040
+ *
1041
+ * @example
1042
+ * ```ts
1043
+ * if (errorHandler.hasFatalError()) {
1044
+ * player.reset();
1045
+ * }
1046
+ * ```
1047
+ */
1048
+ hasFatalError() {
1049
+ return this.errors.some((e) => e.fatal);
1050
+ }
1051
+ /**
1052
+ * Normalize error to PlayerError.
1053
+ * @private
1054
+ */
1055
+ normalizeError(error, context) {
1056
+ if (this.isPlayerError(error)) {
1057
+ return {
1058
+ ...error,
1059
+ context: { ...error.context, ...context }
1060
+ };
1061
+ }
1062
+ return {
1063
+ code: this.getErrorCode(error),
1064
+ message: error.message,
1065
+ fatal: this.isFatal(error),
1066
+ timestamp: Date.now(),
1067
+ context,
1068
+ originalError: error
1069
+ };
1070
+ }
1071
+ /**
1072
+ * Determine error code from native Error.
1073
+ * @private
1074
+ */
1075
+ getErrorCode(error) {
1076
+ const message = error.message.toLowerCase();
1077
+ if (message.includes("network")) {
1078
+ return "MEDIA_NETWORK_ERROR";
1079
+ }
1080
+ if (message.includes("decode")) {
1081
+ return "MEDIA_DECODE_ERROR";
1082
+ }
1083
+ if (message.includes("source")) {
1084
+ return "SOURCE_LOAD_FAILED";
1085
+ }
1086
+ if (message.includes("plugin")) {
1087
+ return "PLUGIN_SETUP_FAILED";
1088
+ }
1089
+ if (message.includes("provider")) {
1090
+ return "PROVIDER_SETUP_FAILED";
1091
+ }
1092
+ return "UNKNOWN_ERROR";
1093
+ }
1094
+ /**
1095
+ * Determine if error is fatal.
1096
+ * @private
1097
+ */
1098
+ isFatal(error) {
1099
+ return this.isFatalCode(this.getErrorCode(error));
1100
+ }
1101
+ /**
1102
+ * Determine if error code is fatal.
1103
+ * @private
1104
+ */
1105
+ isFatalCode(code) {
1106
+ const fatalCodes = [
1107
+ "SOURCE_NOT_SUPPORTED",
1108
+ "PROVIDER_NOT_FOUND",
1109
+ "MEDIA_DECODE_ERROR"
1110
+ /* MEDIA_DECODE_ERROR */
1111
+ ];
1112
+ return fatalCodes.includes(code);
1113
+ }
1114
+ /**
1115
+ * Type guard for PlayerError.
1116
+ * @private
1117
+ */
1118
+ isPlayerError(error) {
1119
+ return typeof error === "object" && error !== null && "code" in error && "message" in error && "fatal" in error && "timestamp" in error;
1120
+ }
1121
+ /**
1122
+ * Add error to history.
1123
+ * @private
1124
+ */
1125
+ addToHistory(error) {
1126
+ this.errors.push(error);
1127
+ if (this.errors.length > this.maxHistory) {
1128
+ this.errors.shift();
1129
+ }
1130
+ }
1131
+ /**
1132
+ * Log error with appropriate level.
1133
+ * @private
1134
+ */
1135
+ logError(error) {
1136
+ const logMessage = `[${error.code}] ${error.message}`;
1137
+ if (error.fatal) {
1138
+ this.logger.error(logMessage, {
1139
+ code: error.code,
1140
+ context: error.context
1141
+ });
1142
+ } else {
1143
+ this.logger.warn(logMessage, {
1144
+ code: error.code,
1145
+ context: error.context
1146
+ });
1147
+ }
1148
+ }
1149
+ }
1150
+ class PluginAPI {
1151
+ /**
1152
+ * Create a new PluginAPI.
1153
+ *
1154
+ * @param pluginId - ID of the plugin this API belongs to
1155
+ * @param deps - Dependencies (stateManager, eventBus, logger, container, getPlugin)
1156
+ */
1157
+ constructor(pluginId, deps) {
1158
+ this.cleanupFns = [];
1159
+ this.pluginId = pluginId;
1160
+ this.stateManager = deps.stateManager;
1161
+ this.eventBus = deps.eventBus;
1162
+ this.container = deps.container;
1163
+ this.getPluginFn = deps.getPlugin;
1164
+ this.logger = {
1165
+ debug: (msg, metadata) => deps.logger.debug(`[${pluginId}] ${msg}`, metadata),
1166
+ info: (msg, metadata) => deps.logger.info(`[${pluginId}] ${msg}`, metadata),
1167
+ warn: (msg, metadata) => deps.logger.warn(`[${pluginId}] ${msg}`, metadata),
1168
+ error: (msg, metadata) => deps.logger.error(`[${pluginId}] ${msg}`, metadata)
1169
+ };
1170
+ }
1171
+ /**
1172
+ * Get a state value.
1173
+ *
1174
+ * @param key - State property key
1175
+ * @returns Current state value
1176
+ */
1177
+ getState(key) {
1178
+ return this.stateManager.getValue(key);
1179
+ }
1180
+ /**
1181
+ * Set a state value.
1182
+ *
1183
+ * @param key - State property key
1184
+ * @param value - New state value
1185
+ */
1186
+ setState(key, value) {
1187
+ this.stateManager.set(key, value);
1188
+ }
1189
+ /**
1190
+ * Subscribe to an event.
1191
+ *
1192
+ * @param event - Event name
1193
+ * @param handler - Event handler
1194
+ * @returns Unsubscribe function
1195
+ */
1196
+ on(event, handler) {
1197
+ return this.eventBus.on(event, handler);
1198
+ }
1199
+ /**
1200
+ * Unsubscribe from an event.
1201
+ *
1202
+ * @param event - Event name
1203
+ * @param handler - Event handler to remove
1204
+ */
1205
+ off(event, handler) {
1206
+ this.eventBus.off(event, handler);
1207
+ }
1208
+ /**
1209
+ * Emit an event.
1210
+ *
1211
+ * @param event - Event name
1212
+ * @param payload - Event payload
1213
+ */
1214
+ emit(event, payload) {
1215
+ this.eventBus.emit(event, payload);
1216
+ }
1217
+ /**
1218
+ * Get another plugin by ID (if ready).
1219
+ *
1220
+ * @param id - Plugin ID
1221
+ * @returns Plugin instance or null if not found/ready
1222
+ */
1223
+ getPlugin(id) {
1224
+ return this.getPluginFn(id);
1225
+ }
1226
+ /**
1227
+ * Register a cleanup function to run when plugin is destroyed.
1228
+ *
1229
+ * @param cleanup - Cleanup function
1230
+ */
1231
+ onDestroy(cleanup) {
1232
+ this.cleanupFns.push(cleanup);
1233
+ }
1234
+ /**
1235
+ * Subscribe to state changes.
1236
+ *
1237
+ * @param callback - Callback function called on any state change
1238
+ * @returns Unsubscribe function
1239
+ */
1240
+ subscribeToState(callback) {
1241
+ return this.stateManager.subscribe(callback);
1242
+ }
1243
+ /**
1244
+ * Run all registered cleanup functions.
1245
+ * Called by PluginManager when destroying the plugin.
1246
+ *
1247
+ * @internal
1248
+ */
1249
+ runCleanups() {
1250
+ for (const cleanup of this.cleanupFns) {
1251
+ try {
1252
+ cleanup();
1253
+ } catch (error) {
1254
+ this.logger.error("Cleanup function failed", { error });
1255
+ }
1256
+ }
1257
+ this.cleanupFns = [];
1258
+ }
1259
+ /**
1260
+ * Get all registered cleanup functions.
1261
+ *
1262
+ * @returns Array of cleanup functions
1263
+ * @internal
1264
+ */
1265
+ getCleanupFns() {
1266
+ return this.cleanupFns;
1267
+ }
1268
+ }
1269
+ class PluginManager {
1270
+ constructor(eventBus, stateManager, logger, options) {
1271
+ this.plugins = /* @__PURE__ */ new Map();
1272
+ this.eventBus = eventBus;
1273
+ this.stateManager = stateManager;
1274
+ this.logger = logger;
1275
+ this.container = options.container;
1276
+ }
1277
+ /** Register a plugin with optional configuration. */
1278
+ register(plugin, config) {
1279
+ if (this.plugins.has(plugin.id)) {
1280
+ throw new Error(`Plugin "${plugin.id}" is already registered`);
1281
+ }
1282
+ this.validatePlugin(plugin);
1283
+ const api = new PluginAPI(plugin.id, {
1284
+ stateManager: this.stateManager,
1285
+ eventBus: this.eventBus,
1286
+ logger: this.logger,
1287
+ container: this.container,
1288
+ getPlugin: (id) => this.getReadyPlugin(id)
1289
+ });
1290
+ this.plugins.set(plugin.id, {
1291
+ plugin,
1292
+ state: "registered",
1293
+ config,
1294
+ cleanupFns: [],
1295
+ api
1296
+ });
1297
+ this.logger.info(`Plugin registered: ${plugin.id}`);
1298
+ this.eventBus.emit("plugin:registered", { name: plugin.id, type: plugin.type });
1299
+ }
1300
+ /** Unregister a plugin. Destroys it first if active. */
1301
+ async unregister(id) {
1302
+ const record = this.plugins.get(id);
1303
+ if (!record) return;
1304
+ if (record.state === "ready") {
1305
+ await this.destroyPlugin(id);
1306
+ }
1307
+ this.plugins.delete(id);
1308
+ this.logger.info(`Plugin unregistered: ${id}`);
1309
+ }
1310
+ /** Initialize all registered plugins in dependency order. */
1311
+ async initAll() {
1312
+ const order = this.resolveDependencyOrder();
1313
+ for (const id of order) {
1314
+ await this.initPlugin(id);
1315
+ }
1316
+ }
1317
+ /** Initialize a specific plugin. */
1318
+ async initPlugin(id) {
1319
+ const record = this.plugins.get(id);
1320
+ if (!record) {
1321
+ throw new Error(`Plugin "${id}" not found`);
1322
+ }
1323
+ if (record.state === "ready") return;
1324
+ if (record.state === "initializing") {
1325
+ throw new Error(`Plugin "${id}" is already initializing (possible circular dependency)`);
1326
+ }
1327
+ for (const depId of record.plugin.dependencies || []) {
1328
+ const dep = this.plugins.get(depId);
1329
+ if (!dep) {
1330
+ throw new Error(`Plugin "${id}" depends on missing plugin "${depId}"`);
1331
+ }
1332
+ if (dep.state !== "ready") {
1333
+ await this.initPlugin(depId);
1334
+ }
1335
+ }
1336
+ try {
1337
+ record.state = "initializing";
1338
+ if (record.plugin.onStateChange) {
1339
+ const unsub = this.stateManager.subscribe(record.plugin.onStateChange.bind(record.plugin));
1340
+ record.api.onDestroy(unsub);
1341
+ }
1342
+ if (record.plugin.onError) {
1343
+ const unsub = this.eventBus.on("error", (err) => {
1344
+ record.plugin.onError?.(err.originalError || new Error(err.message));
1345
+ });
1346
+ record.api.onDestroy(unsub);
1347
+ }
1348
+ await record.plugin.init(record.api, record.config);
1349
+ record.state = "ready";
1350
+ this.logger.info(`Plugin ready: ${id}`);
1351
+ this.eventBus.emit("plugin:active", { name: id });
1352
+ } catch (error) {
1353
+ record.state = "error";
1354
+ record.error = error;
1355
+ this.logger.error(`Plugin init failed: ${id}`, { error });
1356
+ this.eventBus.emit("plugin:error", { name: id, error });
1357
+ throw error;
1358
+ }
1359
+ }
1360
+ /** Destroy all plugins in reverse dependency order. */
1361
+ async destroyAll() {
1362
+ const order = this.resolveDependencyOrder().reverse();
1363
+ for (const id of order) {
1364
+ await this.destroyPlugin(id);
1365
+ }
1366
+ }
1367
+ /** Destroy a specific plugin. */
1368
+ async destroyPlugin(id) {
1369
+ const record = this.plugins.get(id);
1370
+ if (!record || record.state !== "ready") return;
1371
+ try {
1372
+ await record.plugin.destroy();
1373
+ record.api.runCleanups();
1374
+ record.state = "registered";
1375
+ this.logger.info(`Plugin destroyed: ${id}`);
1376
+ this.eventBus.emit("plugin:destroyed", { name: id });
1377
+ } catch (error) {
1378
+ this.logger.error(`Plugin destroy failed: ${id}`, { error });
1379
+ record.state = "registered";
1380
+ }
1381
+ }
1382
+ /** Get a plugin by ID (returns any registered plugin). */
1383
+ getPlugin(id) {
1384
+ const record = this.plugins.get(id);
1385
+ return record ? record.plugin : null;
1386
+ }
1387
+ /** Get a plugin by ID only if ready (used by PluginAPI). */
1388
+ getReadyPlugin(id) {
1389
+ const record = this.plugins.get(id);
1390
+ return record?.state === "ready" ? record.plugin : null;
1391
+ }
1392
+ /** Check if a plugin is registered. */
1393
+ hasPlugin(id) {
1394
+ return this.plugins.has(id);
1395
+ }
1396
+ /** Get plugin state. */
1397
+ getPluginState(id) {
1398
+ return this.plugins.get(id)?.state ?? null;
1399
+ }
1400
+ /** Get all registered plugin IDs. */
1401
+ getPluginIds() {
1402
+ return Array.from(this.plugins.keys());
1403
+ }
1404
+ /** Get all ready plugins. */
1405
+ getReadyPlugins() {
1406
+ return Array.from(this.plugins.values()).filter((r) => r.state === "ready").map((r) => r.plugin);
1407
+ }
1408
+ /** Get plugins by type. */
1409
+ getPluginsByType(type) {
1410
+ return Array.from(this.plugins.values()).filter((r) => r.plugin.type === type).map((r) => r.plugin);
1411
+ }
1412
+ /** Select a provider plugin that can play a source. */
1413
+ selectProvider(source) {
1414
+ const providers = this.getPluginsByType("provider");
1415
+ for (const provider of providers) {
1416
+ const canPlay = provider.canPlay;
1417
+ if (typeof canPlay === "function" && canPlay(source)) {
1418
+ return provider;
1419
+ }
1420
+ }
1421
+ return null;
1422
+ }
1423
+ /** Resolve plugin initialization order using topological sort. */
1424
+ resolveDependencyOrder() {
1425
+ const visited = /* @__PURE__ */ new Set();
1426
+ const visiting = /* @__PURE__ */ new Set();
1427
+ const sorted = [];
1428
+ const visit = (id, path = []) => {
1429
+ if (visited.has(id)) return;
1430
+ if (visiting.has(id)) {
1431
+ const cycle = [...path, id].join(" -> ");
1432
+ throw new Error(`Circular dependency detected: ${cycle}`);
1433
+ }
1434
+ const record = this.plugins.get(id);
1435
+ if (!record) return;
1436
+ visiting.add(id);
1437
+ for (const depId of record.plugin.dependencies || []) {
1438
+ if (this.plugins.has(depId)) {
1439
+ visit(depId, [...path, id]);
1440
+ }
1441
+ }
1442
+ visiting.delete(id);
1443
+ visited.add(id);
1444
+ sorted.push(id);
1445
+ };
1446
+ for (const id of this.plugins.keys()) {
1447
+ visit(id);
1448
+ }
1449
+ return sorted;
1450
+ }
1451
+ /** Validate plugin has required properties. */
1452
+ validatePlugin(plugin) {
1453
+ if (!plugin.id || typeof plugin.id !== "string") {
1454
+ throw new Error("Plugin must have a valid id");
1455
+ }
1456
+ if (!plugin.name || typeof plugin.name !== "string") {
1457
+ throw new Error(`Plugin "${plugin.id}" must have a valid name`);
1458
+ }
1459
+ if (!plugin.version || typeof plugin.version !== "string") {
1460
+ throw new Error(`Plugin "${plugin.id}" must have a valid version`);
1461
+ }
1462
+ if (!plugin.type || typeof plugin.type !== "string") {
1463
+ throw new Error(`Plugin "${plugin.id}" must have a valid type`);
1464
+ }
1465
+ if (typeof plugin.init !== "function") {
1466
+ throw new Error(`Plugin "${plugin.id}" must have an init() method`);
1467
+ }
1468
+ if (typeof plugin.destroy !== "function") {
1469
+ throw new Error(`Plugin "${plugin.id}" must have a destroy() method`);
1470
+ }
1471
+ }
1472
+ }
1473
+ class ScarlettPlayer {
1474
+ /**
1475
+ * Create a new ScarlettPlayer.
1476
+ *
1477
+ * @param options - Player configuration
1478
+ */
1479
+ constructor(options) {
1480
+ this._currentProvider = null;
1481
+ this.destroyed = false;
1482
+ this.seekingWhilePlaying = false;
1483
+ this.seekResumeTimeout = null;
1484
+ if (typeof options.container === "string") {
1485
+ const el = document.querySelector(options.container);
1486
+ if (!el || !(el instanceof HTMLElement)) {
1487
+ throw new Error(`ScarlettPlayer: container not found: ${options.container}`);
1488
+ }
1489
+ this.container = el;
1490
+ } else if (options.container instanceof HTMLElement) {
1491
+ this.container = options.container;
1492
+ } else {
1493
+ throw new Error("ScarlettPlayer requires a valid HTMLElement container or CSS selector");
1494
+ }
1495
+ this.initialSrc = options.src;
1496
+ this.eventBus = new EventBus();
1497
+ this.stateManager = new StateManager({
1498
+ autoplay: options.autoplay ?? false,
1499
+ loop: options.loop ?? false,
1500
+ volume: options.volume ?? 1,
1501
+ muted: options.muted ?? false,
1502
+ poster: options.poster ?? ""
1503
+ });
1504
+ this.logger = new Logger({
1505
+ level: options.logLevel ?? "warn",
1506
+ scope: "ScarlettPlayer"
1507
+ });
1508
+ this.errorHandler = new ErrorHandler(this.eventBus, this.logger);
1509
+ this.pluginManager = new PluginManager(
1510
+ this.eventBus,
1511
+ this.stateManager,
1512
+ this.logger,
1513
+ { container: this.container }
1514
+ );
1515
+ if (options.plugins) {
1516
+ for (const plugin of options.plugins) {
1517
+ this.pluginManager.register(plugin);
1518
+ }
1519
+ }
1520
+ this.logger.info("ScarlettPlayer initialized", {
1521
+ autoplay: options.autoplay,
1522
+ plugins: options.plugins?.length ?? 0
1523
+ });
1524
+ this.eventBus.emit("player:ready", void 0);
1525
+ }
1526
+ /**
1527
+ * Initialize the player asynchronously.
1528
+ * Initializes non-provider plugins and loads initial source if provided.
1529
+ */
1530
+ async init() {
1531
+ this.checkDestroyed();
1532
+ for (const [id, record] of this.pluginManager.plugins) {
1533
+ if (record.plugin.type !== "provider" && record.state === "registered") {
1534
+ await this.pluginManager.initPlugin(id);
1535
+ }
1536
+ }
1537
+ if (this.initialSrc) {
1538
+ await this.load(this.initialSrc);
1539
+ }
1540
+ return Promise.resolve();
1541
+ }
1542
+ /**
1543
+ * Load a media source.
1544
+ *
1545
+ * Selects appropriate provider plugin and loads the source.
1546
+ *
1547
+ * @param source - Media source URL
1548
+ * @returns Promise that resolves when source is loaded
1549
+ *
1550
+ * @example
1551
+ * ```ts
1552
+ * await player.load('video.m3u8');
1553
+ * ```
1554
+ */
1555
+ async load(source) {
1556
+ this.checkDestroyed();
1557
+ try {
1558
+ this.logger.info("Loading source", { source });
1559
+ this.stateManager.update({
1560
+ playing: false,
1561
+ paused: true,
1562
+ ended: false,
1563
+ buffering: true,
1564
+ currentTime: 0,
1565
+ duration: 0,
1566
+ bufferedAmount: 0,
1567
+ playbackState: "loading"
1568
+ });
1569
+ if (this._currentProvider) {
1570
+ const previousProviderId = this._currentProvider.id;
1571
+ this.logger.info("Destroying previous provider", { provider: previousProviderId });
1572
+ await this.pluginManager.destroyPlugin(previousProviderId);
1573
+ this._currentProvider = null;
1574
+ }
1575
+ const provider = this.pluginManager.selectProvider(source);
1576
+ if (!provider) {
1577
+ this.errorHandler.throw(
1578
+ ErrorCode.PROVIDER_NOT_FOUND,
1579
+ `No provider found for source: ${source}`,
1580
+ {
1581
+ fatal: true,
1582
+ context: { source }
1583
+ }
1584
+ );
1585
+ return;
1586
+ }
1587
+ this._currentProvider = provider;
1588
+ this.logger.info("Provider selected", { provider: provider.id });
1589
+ await this.pluginManager.initPlugin(provider.id);
1590
+ this.stateManager.set("source", { src: source, type: this.detectMimeType(source) });
1591
+ if (typeof provider.loadSource === "function") {
1592
+ await provider.loadSource(source);
1593
+ }
1594
+ if (this.stateManager.getValue("autoplay")) {
1595
+ await this.play();
1596
+ }
1597
+ } catch (error) {
1598
+ this.errorHandler.handle(error, {
1599
+ operation: "load",
1600
+ source
1601
+ });
1602
+ }
1603
+ }
1604
+ /**
1605
+ * Start playback.
1606
+ *
1607
+ * @returns Promise that resolves when playback starts
1608
+ *
1609
+ * @example
1610
+ * ```ts
1611
+ * await player.play();
1612
+ * ```
1613
+ */
1614
+ async play() {
1615
+ this.checkDestroyed();
1616
+ try {
1617
+ this.logger.debug("Play requested");
1618
+ this.eventBus.emit("playback:play", void 0);
1619
+ } catch (error) {
1620
+ this.errorHandler.handle(error, { operation: "play" });
1621
+ }
1622
+ }
1623
+ /**
1624
+ * Pause playback.
1625
+ *
1626
+ * @example
1627
+ * ```ts
1628
+ * player.pause();
1629
+ * ```
1630
+ */
1631
+ pause() {
1632
+ this.checkDestroyed();
1633
+ try {
1634
+ this.logger.debug("Pause requested");
1635
+ this.seekingWhilePlaying = false;
1636
+ if (this.seekResumeTimeout !== null) {
1637
+ clearTimeout(this.seekResumeTimeout);
1638
+ this.seekResumeTimeout = null;
1639
+ }
1640
+ this.eventBus.emit("playback:pause", void 0);
1641
+ } catch (error) {
1642
+ this.errorHandler.handle(error, { operation: "pause" });
1643
+ }
1644
+ }
1645
+ /**
1646
+ * Seek to a specific time.
1647
+ *
1648
+ * @param time - Time in seconds
1649
+ *
1650
+ * @example
1651
+ * ```ts
1652
+ * player.seek(30); // Seek to 30 seconds
1653
+ * ```
1654
+ */
1655
+ seek(time) {
1656
+ this.checkDestroyed();
1657
+ try {
1658
+ this.logger.debug("Seek requested", { time });
1659
+ const wasPlaying = this.stateManager.getValue("playing");
1660
+ if (wasPlaying) {
1661
+ this.seekingWhilePlaying = true;
1662
+ }
1663
+ if (this.seekResumeTimeout !== null) {
1664
+ clearTimeout(this.seekResumeTimeout);
1665
+ this.seekResumeTimeout = null;
1666
+ }
1667
+ this.eventBus.emit("playback:seeking", { time });
1668
+ this.stateManager.set("currentTime", time);
1669
+ if (this.seekingWhilePlaying) {
1670
+ this.seekResumeTimeout = setTimeout(() => {
1671
+ if (this.seekingWhilePlaying && this.stateManager.getValue("playing")) {
1672
+ this.logger.debug("Resuming playback after seek");
1673
+ this.seekingWhilePlaying = false;
1674
+ this.eventBus.emit("playback:play", void 0);
1675
+ }
1676
+ this.seekResumeTimeout = null;
1677
+ }, 300);
1678
+ }
1679
+ } catch (error) {
1680
+ this.errorHandler.handle(error, { operation: "seek", time });
1681
+ }
1682
+ }
1683
+ /**
1684
+ * Set volume.
1685
+ *
1686
+ * @param volume - Volume 0-1
1687
+ *
1688
+ * @example
1689
+ * ```ts
1690
+ * player.setVolume(0.5); // 50% volume
1691
+ * ```
1692
+ */
1693
+ setVolume(volume) {
1694
+ this.checkDestroyed();
1695
+ const clampedVolume = Math.max(0, Math.min(1, volume));
1696
+ this.stateManager.set("volume", clampedVolume);
1697
+ this.eventBus.emit("volume:change", {
1698
+ volume: clampedVolume,
1699
+ muted: this.stateManager.getValue("muted")
1700
+ });
1701
+ }
1702
+ /**
1703
+ * Set muted state.
1704
+ *
1705
+ * @param muted - Mute flag
1706
+ *
1707
+ * @example
1708
+ * ```ts
1709
+ * player.setMuted(true);
1710
+ * ```
1711
+ */
1712
+ setMuted(muted) {
1713
+ this.checkDestroyed();
1714
+ this.stateManager.set("muted", muted);
1715
+ this.eventBus.emit("volume:mute", { muted });
1716
+ }
1717
+ /**
1718
+ * Set playback rate.
1719
+ *
1720
+ * @param rate - Playback rate (e.g., 1.0 = normal, 2.0 = 2x speed)
1721
+ *
1722
+ * @example
1723
+ * ```ts
1724
+ * player.setPlaybackRate(1.5); // 1.5x speed
1725
+ * ```
1726
+ */
1727
+ setPlaybackRate(rate) {
1728
+ this.checkDestroyed();
1729
+ this.stateManager.set("playbackRate", rate);
1730
+ this.eventBus.emit("playback:ratechange", { rate });
1731
+ }
1732
+ /**
1733
+ * Set autoplay state.
1734
+ *
1735
+ * When enabled, videos will automatically play after loading.
1736
+ *
1737
+ * @param autoplay - Autoplay flag
1738
+ *
1739
+ * @example
1740
+ * ```ts
1741
+ * player.setAutoplay(true);
1742
+ * await player.load('video.mp4'); // Will auto-play
1743
+ * ```
1744
+ */
1745
+ setAutoplay(autoplay) {
1746
+ this.checkDestroyed();
1747
+ this.stateManager.set("autoplay", autoplay);
1748
+ this.logger.debug("Autoplay set", { autoplay });
1749
+ }
1750
+ /**
1751
+ * Subscribe to an event.
1752
+ *
1753
+ * @param event - Event name
1754
+ * @param handler - Event handler
1755
+ * @returns Unsubscribe function
1756
+ *
1757
+ * @example
1758
+ * ```ts
1759
+ * const unsub = player.on('playback:play', () => {
1760
+ * console.log('Playing!');
1761
+ * });
1762
+ *
1763
+ * // Later: unsubscribe
1764
+ * unsub();
1765
+ * ```
1766
+ */
1767
+ on(event, handler) {
1768
+ this.checkDestroyed();
1769
+ return this.eventBus.on(event, handler);
1770
+ }
1771
+ /**
1772
+ * Subscribe to an event once.
1773
+ *
1774
+ * @param event - Event name
1775
+ * @param handler - Event handler
1776
+ * @returns Unsubscribe function
1777
+ *
1778
+ * @example
1779
+ * ```ts
1780
+ * player.once('player:ready', () => {
1781
+ * console.log('Player ready!');
1782
+ * });
1783
+ * ```
1784
+ */
1785
+ once(event, handler) {
1786
+ this.checkDestroyed();
1787
+ return this.eventBus.once(event, handler);
1788
+ }
1789
+ /**
1790
+ * Get a plugin by name.
1791
+ *
1792
+ * @param name - Plugin name
1793
+ * @returns Plugin instance or null
1794
+ *
1795
+ * @example
1796
+ * ```ts
1797
+ * const hls = player.getPlugin('hls-plugin');
1798
+ * ```
1799
+ */
1800
+ getPlugin(name) {
1801
+ this.checkDestroyed();
1802
+ return this.pluginManager.getPlugin(name);
1803
+ }
1804
+ /**
1805
+ * Register a plugin.
1806
+ *
1807
+ * @param plugin - Plugin to register
1808
+ *
1809
+ * @example
1810
+ * ```ts
1811
+ * player.registerPlugin(myPlugin);
1812
+ * ```
1813
+ */
1814
+ registerPlugin(plugin) {
1815
+ this.checkDestroyed();
1816
+ this.pluginManager.register(plugin);
1817
+ }
1818
+ /**
1819
+ * Get current state snapshot.
1820
+ *
1821
+ * @returns Readonly state snapshot
1822
+ *
1823
+ * @example
1824
+ * ```ts
1825
+ * const state = player.getState();
1826
+ * console.log(state.playing, state.currentTime);
1827
+ * ```
1828
+ */
1829
+ getState() {
1830
+ this.checkDestroyed();
1831
+ return this.stateManager.snapshot();
1832
+ }
1833
+ // ===== Quality Methods (proxied to provider) =====
1834
+ /**
1835
+ * Get available quality levels from the current provider.
1836
+ * @returns Array of quality levels or empty array if not available
1837
+ */
1838
+ getQualities() {
1839
+ this.checkDestroyed();
1840
+ if (!this._currentProvider) return [];
1841
+ const provider = this._currentProvider;
1842
+ if (typeof provider.getLevels === "function") {
1843
+ return provider.getLevels();
1844
+ }
1845
+ return [];
1846
+ }
1847
+ /**
1848
+ * Set quality level (-1 for auto).
1849
+ * @param index - Quality level index
1850
+ */
1851
+ setQuality(index) {
1852
+ this.checkDestroyed();
1853
+ if (!this._currentProvider) {
1854
+ this.logger.warn("No provider available for quality change");
1855
+ return;
1856
+ }
1857
+ const provider = this._currentProvider;
1858
+ if (typeof provider.setLevel === "function") {
1859
+ provider.setLevel(index);
1860
+ this.eventBus.emit("quality:change", {
1861
+ quality: index === -1 ? "auto" : `level-${index}`,
1862
+ auto: index === -1
1863
+ });
1864
+ }
1865
+ }
1866
+ /**
1867
+ * Get current quality level index (-1 = auto).
1868
+ */
1869
+ getCurrentQuality() {
1870
+ this.checkDestroyed();
1871
+ if (!this._currentProvider) return -1;
1872
+ const provider = this._currentProvider;
1873
+ if (typeof provider.getCurrentLevel === "function") {
1874
+ return provider.getCurrentLevel();
1875
+ }
1876
+ return -1;
1877
+ }
1878
+ // ===== Fullscreen Methods =====
1879
+ /**
1880
+ * Request fullscreen mode.
1881
+ */
1882
+ async requestFullscreen() {
1883
+ this.checkDestroyed();
1884
+ try {
1885
+ if (this.container.requestFullscreen) {
1886
+ await this.container.requestFullscreen();
1887
+ } else if (this.container.webkitRequestFullscreen) {
1888
+ await this.container.webkitRequestFullscreen();
1889
+ }
1890
+ this.stateManager.set("fullscreen", true);
1891
+ this.eventBus.emit("fullscreen:change", { fullscreen: true });
1892
+ } catch (error) {
1893
+ this.logger.error("Fullscreen request failed", { error });
1894
+ }
1895
+ }
1896
+ /**
1897
+ * Exit fullscreen mode.
1898
+ */
1899
+ async exitFullscreen() {
1900
+ this.checkDestroyed();
1901
+ try {
1902
+ if (document.exitFullscreen) {
1903
+ await document.exitFullscreen();
1904
+ } else if (document.webkitExitFullscreen) {
1905
+ await document.webkitExitFullscreen();
1906
+ }
1907
+ this.stateManager.set("fullscreen", false);
1908
+ this.eventBus.emit("fullscreen:change", { fullscreen: false });
1909
+ } catch (error) {
1910
+ this.logger.error("Exit fullscreen failed", { error });
1911
+ }
1912
+ }
1913
+ /**
1914
+ * Toggle fullscreen mode.
1915
+ */
1916
+ async toggleFullscreen() {
1917
+ if (this.fullscreen) {
1918
+ await this.exitFullscreen();
1919
+ } else {
1920
+ await this.requestFullscreen();
1921
+ }
1922
+ }
1923
+ // ===== Casting Methods (proxied to plugins) =====
1924
+ /**
1925
+ * Request AirPlay (proxied to airplay plugin).
1926
+ */
1927
+ requestAirPlay() {
1928
+ this.checkDestroyed();
1929
+ const airplay = this.pluginManager.getPlugin("airplay");
1930
+ if (airplay && typeof airplay.showPicker === "function") {
1931
+ airplay.showPicker();
1932
+ } else {
1933
+ this.logger.warn("AirPlay plugin not available");
1934
+ }
1935
+ }
1936
+ /**
1937
+ * Request Chromecast session (proxied to chromecast plugin).
1938
+ */
1939
+ async requestChromecast() {
1940
+ this.checkDestroyed();
1941
+ const chromecast = this.pluginManager.getPlugin("chromecast");
1942
+ if (chromecast && typeof chromecast.requestSession === "function") {
1943
+ await chromecast.requestSession();
1944
+ } else {
1945
+ this.logger.warn("Chromecast plugin not available");
1946
+ }
1947
+ }
1948
+ /**
1949
+ * Stop casting (AirPlay or Chromecast).
1950
+ */
1951
+ stopCasting() {
1952
+ this.checkDestroyed();
1953
+ const airplay = this.pluginManager.getPlugin("airplay");
1954
+ if (airplay && typeof airplay.stop === "function") {
1955
+ airplay.stop();
1956
+ }
1957
+ const chromecast = this.pluginManager.getPlugin("chromecast");
1958
+ if (chromecast && typeof chromecast.stopSession === "function") {
1959
+ chromecast.stopSession();
1960
+ }
1961
+ }
1962
+ // ===== Live Stream Methods =====
1963
+ /**
1964
+ * Seek to live edge (for live streams).
1965
+ */
1966
+ seekToLive() {
1967
+ this.checkDestroyed();
1968
+ const isLive = this.stateManager.getValue("live");
1969
+ if (!isLive) {
1970
+ this.logger.warn("Not a live stream");
1971
+ return;
1972
+ }
1973
+ if (this._currentProvider) {
1974
+ const provider = this._currentProvider;
1975
+ if (typeof provider.getLiveInfo === "function") {
1976
+ const liveInfo = provider.getLiveInfo();
1977
+ if (liveInfo?.liveSyncPosition !== void 0) {
1978
+ this.seek(liveInfo.liveSyncPosition);
1979
+ return;
1980
+ }
1981
+ }
1982
+ }
1983
+ const duration = this.stateManager.getValue("duration");
1984
+ if (duration > 0) {
1985
+ this.seek(duration);
1986
+ }
1987
+ }
1988
+ /**
1989
+ * Destroy the player and cleanup all resources.
1990
+ *
1991
+ * @example
1992
+ * ```ts
1993
+ * player.destroy();
1994
+ * ```
1995
+ */
1996
+ destroy() {
1997
+ if (this.destroyed) {
1998
+ return;
1999
+ }
2000
+ this.logger.info("Destroying player");
2001
+ if (this.seekResumeTimeout !== null) {
2002
+ clearTimeout(this.seekResumeTimeout);
2003
+ this.seekResumeTimeout = null;
2004
+ }
2005
+ this.eventBus.emit("player:destroy", void 0);
2006
+ this.pluginManager.destroyAll();
2007
+ this.eventBus.destroy();
2008
+ this.stateManager.destroy();
2009
+ this.destroyed = true;
2010
+ this.logger.info("Player destroyed");
2011
+ }
2012
+ // ===== State Getters =====
2013
+ /**
2014
+ * Get playing state.
2015
+ */
2016
+ get playing() {
2017
+ return this.stateManager.getValue("playing");
2018
+ }
2019
+ /**
2020
+ * Get paused state.
2021
+ */
2022
+ get paused() {
2023
+ return this.stateManager.getValue("paused");
2024
+ }
2025
+ /**
2026
+ * Get current time in seconds.
2027
+ */
2028
+ get currentTime() {
2029
+ return this.stateManager.getValue("currentTime");
2030
+ }
2031
+ /**
2032
+ * Get duration in seconds.
2033
+ */
2034
+ get duration() {
2035
+ return this.stateManager.getValue("duration");
2036
+ }
2037
+ /**
2038
+ * Get volume (0-1).
2039
+ */
2040
+ get volume() {
2041
+ return this.stateManager.getValue("volume");
2042
+ }
2043
+ /**
2044
+ * Get muted state.
2045
+ */
2046
+ get muted() {
2047
+ return this.stateManager.getValue("muted");
2048
+ }
2049
+ /**
2050
+ * Get playback rate.
2051
+ */
2052
+ get playbackRate() {
2053
+ return this.stateManager.getValue("playbackRate");
2054
+ }
2055
+ /**
2056
+ * Get buffered amount (0-1).
2057
+ */
2058
+ get bufferedAmount() {
2059
+ return this.stateManager.getValue("bufferedAmount");
2060
+ }
2061
+ /**
2062
+ * Get current provider plugin.
2063
+ */
2064
+ get currentProvider() {
2065
+ return this._currentProvider;
2066
+ }
2067
+ /**
2068
+ * Get fullscreen state.
2069
+ */
2070
+ get fullscreen() {
2071
+ return this.stateManager.getValue("fullscreen");
2072
+ }
2073
+ /**
2074
+ * Get live stream state.
2075
+ */
2076
+ get live() {
2077
+ return this.stateManager.getValue("live");
2078
+ }
2079
+ /**
2080
+ * Get autoplay state.
2081
+ */
2082
+ get autoplay() {
2083
+ return this.stateManager.getValue("autoplay");
2084
+ }
2085
+ /**
2086
+ * Check if player is destroyed.
2087
+ * @private
2088
+ */
2089
+ checkDestroyed() {
2090
+ if (this.destroyed) {
2091
+ throw new Error("Cannot call methods on destroyed player");
2092
+ }
2093
+ }
2094
+ /**
2095
+ * Detect MIME type from source URL.
2096
+ * @private
2097
+ */
2098
+ detectMimeType(source) {
2099
+ const ext = source.split(".").pop()?.toLowerCase();
2100
+ switch (ext) {
2101
+ case "m3u8":
2102
+ return "application/x-mpegURL";
2103
+ case "mpd":
2104
+ return "application/dash+xml";
2105
+ case "mp4":
2106
+ return "video/mp4";
2107
+ case "webm":
2108
+ return "video/webm";
2109
+ case "ogg":
2110
+ return "video/ogg";
2111
+ default:
2112
+ return "video/mp4";
2113
+ }
2114
+ }
2115
+ }
2116
+ async function createPlayer(options) {
2117
+ const player = new ScarlettPlayer(options);
2118
+ await player.init();
2119
+ return player;
2120
+ }
2121
+ function formatLevel(level) {
2122
+ if (level.name) {
2123
+ return level.name;
2124
+ }
2125
+ if (level.height) {
2126
+ const standardLabels = {
2127
+ 2160: "4K",
2128
+ 1440: "1440p",
2129
+ 1080: "1080p",
2130
+ 720: "720p",
2131
+ 480: "480p",
2132
+ 360: "360p",
2133
+ 240: "240p",
2134
+ 144: "144p"
2135
+ };
2136
+ const closest = Object.keys(standardLabels).map(Number).sort((a, b) => Math.abs(a - level.height) - Math.abs(b - level.height))[0];
2137
+ if (Math.abs(closest - level.height) <= 20) {
2138
+ return standardLabels[closest];
2139
+ }
2140
+ return `${level.height}p`;
2141
+ }
2142
+ if (level.bitrate) {
2143
+ return formatBitrate(level.bitrate);
2144
+ }
2145
+ return "Unknown";
2146
+ }
2147
+ function formatBitrate(bitrate) {
2148
+ if (bitrate >= 1e6) {
2149
+ return `${(bitrate / 1e6).toFixed(1)} Mbps`;
2150
+ }
2151
+ if (bitrate >= 1e3) {
2152
+ return `${Math.round(bitrate / 1e3)} Kbps`;
2153
+ }
2154
+ return `${bitrate} bps`;
2155
+ }
2156
+ function mapLevels(levels, currentLevel) {
2157
+ return levels.map((level, index) => ({
2158
+ index,
2159
+ width: level.width || 0,
2160
+ height: level.height || 0,
2161
+ bitrate: level.bitrate || 0,
2162
+ label: formatLevel(level),
2163
+ codec: level.codecSet
2164
+ }));
2165
+ }
2166
+ var HLS_ERROR_TYPES = {
2167
+ NETWORK_ERROR: "networkError",
2168
+ MEDIA_ERROR: "mediaError",
2169
+ MUX_ERROR: "muxError"
2170
+ };
2171
+ function mapErrorType(hlsType) {
2172
+ switch (hlsType) {
2173
+ case HLS_ERROR_TYPES.NETWORK_ERROR:
2174
+ return "network";
2175
+ case HLS_ERROR_TYPES.MEDIA_ERROR:
2176
+ return "media";
2177
+ case HLS_ERROR_TYPES.MUX_ERROR:
2178
+ return "mux";
2179
+ default:
2180
+ return "other";
2181
+ }
2182
+ }
2183
+ function parseHlsError(data) {
2184
+ return {
2185
+ type: mapErrorType(data.type),
2186
+ details: data.details || "Unknown error",
2187
+ fatal: data.fatal || false,
2188
+ url: data.url,
2189
+ reason: data.reason,
2190
+ response: data.response
2191
+ };
2192
+ }
2193
+ function setupHlsEventHandlers(hls, api, callbacks) {
2194
+ const handlers = [];
2195
+ const addHandler = (event, handler) => {
2196
+ hls.on(event, handler);
2197
+ handlers.push({ event, handler });
2198
+ };
2199
+ addHandler("hlsManifestParsed", (_event, data) => {
2200
+ api.logger.debug("HLS manifest parsed", { levels: data.levels.length });
2201
+ const levels = data.levels.map((level, index) => ({
2202
+ id: `level-${index}`,
2203
+ label: formatLevel(level),
2204
+ width: level.width,
2205
+ height: level.height,
2206
+ bitrate: level.bitrate,
2207
+ active: index === hls.currentLevel
2208
+ }));
2209
+ api.setState("qualities", levels);
2210
+ api.emit("quality:levels", {
2211
+ levels: levels.map((l) => ({ id: l.id, label: l.label }))
2212
+ });
2213
+ callbacks.onManifestParsed?.(data.levels);
2214
+ });
2215
+ addHandler("hlsLevelSwitched", (_event, data) => {
2216
+ const level = hls.levels[data.level];
2217
+ const isAuto = callbacks.getIsAutoQuality?.() ?? hls.autoLevelEnabled;
2218
+ api.logger.debug("HLS level switched", { level: data.level, height: level?.height, auto: isAuto });
2219
+ if (level) {
2220
+ const label = isAuto ? `Auto (${formatLevel(level)})` : formatLevel(level);
2221
+ api.setState("currentQuality", {
2222
+ id: isAuto ? "auto" : `level-${data.level}`,
2223
+ label,
2224
+ width: level.width,
2225
+ height: level.height,
2226
+ bitrate: level.bitrate,
2227
+ active: true
2228
+ });
2229
+ }
2230
+ api.emit("quality:change", {
2231
+ quality: level ? formatLevel(level) : "auto",
2232
+ auto: isAuto
2233
+ });
2234
+ callbacks.onLevelSwitched?.(data.level);
2235
+ });
2236
+ addHandler("hlsFragBuffered", () => {
2237
+ api.setState("buffering", false);
2238
+ callbacks.onBufferUpdate?.();
2239
+ });
2240
+ addHandler("hlsFragLoading", () => {
2241
+ api.setState("buffering", true);
2242
+ });
2243
+ addHandler("hlsLevelLoaded", (_event, data) => {
2244
+ if (data.details?.live !== void 0) {
2245
+ api.setState("live", data.details.live);
2246
+ callbacks.onLiveUpdate?.();
2247
+ }
2248
+ });
2249
+ addHandler("hlsError", (_event, data) => {
2250
+ const error = parseHlsError(data);
2251
+ api.logger.warn("HLS error", { error });
2252
+ callbacks.onError?.(error);
2253
+ });
2254
+ return () => {
2255
+ for (const { event, handler } of handlers) {
2256
+ hls.off(event, handler);
2257
+ }
2258
+ handlers.length = 0;
2259
+ };
2260
+ }
2261
+ function setupVideoEventHandlers(video, api) {
2262
+ const handlers = [];
2263
+ const addHandler = (event, handler) => {
2264
+ video.addEventListener(event, handler);
2265
+ handlers.push({ event, handler });
2266
+ };
2267
+ addHandler("playing", () => {
2268
+ api.setState("playing", true);
2269
+ api.setState("paused", false);
2270
+ api.setState("playbackState", "playing");
2271
+ });
2272
+ addHandler("pause", () => {
2273
+ api.setState("playing", false);
2274
+ api.setState("paused", true);
2275
+ api.setState("playbackState", "paused");
2276
+ });
2277
+ addHandler("ended", () => {
2278
+ api.setState("playing", false);
2279
+ api.setState("ended", true);
2280
+ api.setState("playbackState", "ended");
2281
+ api.emit("playback:ended", void 0);
2282
+ });
2283
+ addHandler("timeupdate", () => {
2284
+ api.setState("currentTime", video.currentTime);
2285
+ api.emit("playback:timeupdate", { currentTime: video.currentTime });
2286
+ });
2287
+ addHandler("durationchange", () => {
2288
+ api.setState("duration", video.duration || 0);
2289
+ api.emit("media:loadedmetadata", { duration: video.duration || 0 });
2290
+ });
2291
+ addHandler("waiting", () => {
2292
+ api.setState("waiting", true);
2293
+ api.setState("buffering", true);
2294
+ api.emit("media:waiting", void 0);
2295
+ });
2296
+ addHandler("canplay", () => {
2297
+ api.setState("waiting", false);
2298
+ api.setState("playbackState", "ready");
2299
+ api.emit("media:canplay", void 0);
2300
+ });
2301
+ addHandler("canplaythrough", () => {
2302
+ api.setState("buffering", false);
2303
+ api.emit("media:canplaythrough", void 0);
2304
+ });
2305
+ addHandler("progress", () => {
2306
+ if (video.buffered.length > 0) {
2307
+ const bufferedEnd = video.buffered.end(video.buffered.length - 1);
2308
+ const bufferedAmount = video.duration > 0 ? bufferedEnd / video.duration : 0;
2309
+ api.setState("bufferedAmount", bufferedAmount);
2310
+ api.setState("buffered", video.buffered);
2311
+ api.emit("media:progress", { buffered: bufferedAmount });
2312
+ }
2313
+ });
2314
+ addHandler("seeking", () => {
2315
+ api.setState("seeking", true);
2316
+ });
2317
+ addHandler("seeked", () => {
2318
+ api.setState("seeking", false);
2319
+ api.emit("playback:seeked", { time: video.currentTime });
2320
+ });
2321
+ addHandler("volumechange", () => {
2322
+ api.setState("volume", video.volume);
2323
+ api.setState("muted", video.muted);
2324
+ api.emit("volume:change", { volume: video.volume, muted: video.muted });
2325
+ });
2326
+ addHandler("ratechange", () => {
2327
+ api.setState("playbackRate", video.playbackRate);
2328
+ api.emit("playback:ratechange", { rate: video.playbackRate });
2329
+ });
2330
+ addHandler("loadedmetadata", () => {
2331
+ api.setState("duration", video.duration);
2332
+ api.setState("mediaType", video.videoWidth > 0 ? "video" : "audio");
2333
+ });
2334
+ addHandler("error", () => {
2335
+ const error = video.error;
2336
+ if (error) {
2337
+ api.logger.error("Video element error", { code: error.code, message: error.message });
2338
+ api.emit("media:error", { error: new Error(error.message || "Video playback error") });
2339
+ }
2340
+ });
2341
+ addHandler("enterpictureinpicture", () => {
2342
+ api.setState("pip", true);
2343
+ api.logger.debug("PiP: entered (standard)");
2344
+ });
2345
+ addHandler("leavepictureinpicture", () => {
2346
+ api.setState("pip", false);
2347
+ api.logger.debug("PiP: exited (standard)");
2348
+ if (!video.paused || api.getState("playing")) {
2349
+ video.play().catch(() => {
2350
+ });
2351
+ }
2352
+ });
2353
+ const webkitVideo = video;
2354
+ if ("webkitPresentationMode" in video) {
2355
+ addHandler("webkitpresentationmodechanged", () => {
2356
+ const mode = webkitVideo.webkitPresentationMode;
2357
+ const isInPip = mode === "picture-in-picture";
2358
+ api.setState("pip", isInPip);
2359
+ api.logger.debug(`PiP: mode changed to ${mode} (webkit)`);
2360
+ if (mode === "inline" && video.paused) {
2361
+ video.play().catch(() => {
2362
+ });
2363
+ }
2364
+ });
2365
+ }
2366
+ return () => {
2367
+ for (const { event, handler } of handlers) {
2368
+ video.removeEventListener(event, handler);
2369
+ }
2370
+ handlers.length = 0;
2371
+ };
2372
+ }
2373
+ var hlsConstructor = null;
2374
+ var loadingPromise = null;
2375
+ function supportsNativeHLS() {
2376
+ if (typeof document === "undefined") return false;
2377
+ const video = document.createElement("video");
2378
+ return video.canPlayType("application/vnd.apple.mpegurl") !== "";
2379
+ }
2380
+ function isHlsJsSupported() {
2381
+ if (hlsConstructor) {
2382
+ return hlsConstructor.isSupported();
2383
+ }
2384
+ if (typeof window === "undefined") return false;
2385
+ return !!(window.MediaSource || window.WebKitMediaSource);
2386
+ }
2387
+ function isHLSSupported() {
2388
+ return supportsNativeHLS() || isHlsJsSupported();
2389
+ }
2390
+ async function loadHlsJs() {
2391
+ if (hlsConstructor) {
2392
+ return hlsConstructor;
2393
+ }
2394
+ if (loadingPromise) {
2395
+ return loadingPromise;
2396
+ }
2397
+ loadingPromise = (async () => {
2398
+ try {
2399
+ const hlsModule = await import("./hls.light-YJMge-Mx.js");
2400
+ hlsConstructor = hlsModule.default;
2401
+ if (!hlsConstructor.isSupported()) {
2402
+ throw new Error("hls.js is not supported in this browser");
2403
+ }
2404
+ return hlsConstructor;
2405
+ } catch (error) {
2406
+ loadingPromise = null;
2407
+ throw new Error(
2408
+ `Failed to load hls.js: ${error instanceof Error ? error.message : "Unknown error"}`
2409
+ );
2410
+ }
2411
+ })();
2412
+ return loadingPromise;
2413
+ }
2414
+ function createHlsInstance(config) {
2415
+ if (!hlsConstructor) {
2416
+ throw new Error("hls.js is not loaded. Call loadHlsJs() first.");
2417
+ }
2418
+ return new hlsConstructor(config);
2419
+ }
2420
+ function getHlsConstructor() {
2421
+ return hlsConstructor;
2422
+ }
2423
+ var DEFAULT_CONFIG$3 = {
2424
+ debug: false,
2425
+ autoStartLoad: true,
2426
+ startPosition: -1,
2427
+ lowLatencyMode: false,
2428
+ maxBufferLength: 30,
2429
+ maxMaxBufferLength: 600,
2430
+ backBufferLength: 30,
2431
+ enableWorker: true,
2432
+ maxNetworkRetries: 3,
2433
+ maxMediaRetries: 2,
2434
+ retryDelayMs: 1e3,
2435
+ retryBackoffFactor: 2
2436
+ };
2437
+ function createHLSPlugin(config) {
2438
+ const mergedConfig = { ...DEFAULT_CONFIG$3, ...config };
2439
+ let api = null;
2440
+ let hls = null;
2441
+ let video = null;
2442
+ let isNative = false;
2443
+ let currentSrc = null;
2444
+ let cleanupHlsEvents = null;
2445
+ let cleanupVideoEvents = null;
2446
+ let isAutoQuality = true;
2447
+ let networkRetryCount = 0;
2448
+ let mediaRetryCount = 0;
2449
+ let retryTimeout = null;
2450
+ let errorCount = 0;
2451
+ let errorWindowStart = 0;
2452
+ const MAX_ERRORS_IN_WINDOW = 10;
2453
+ const ERROR_WINDOW_MS = 5e3;
2454
+ const getOrCreateVideo = () => {
2455
+ if (video) return video;
2456
+ const existing = api?.container.querySelector("video");
2457
+ if (existing) {
2458
+ video = existing;
2459
+ return video;
2460
+ }
2461
+ video = document.createElement("video");
2462
+ video.style.cssText = "width:100%;height:100%;display:block;object-fit:contain;background:#000";
2463
+ video.preload = "metadata";
2464
+ video.controls = false;
2465
+ video.playsInline = true;
2466
+ api?.container.appendChild(video);
2467
+ return video;
2468
+ };
2469
+ const cleanup = () => {
2470
+ cleanupHlsEvents?.();
2471
+ cleanupHlsEvents = null;
2472
+ cleanupVideoEvents?.();
2473
+ cleanupVideoEvents = null;
2474
+ if (retryTimeout) {
2475
+ clearTimeout(retryTimeout);
2476
+ retryTimeout = null;
2477
+ }
2478
+ if (hls) {
2479
+ hls.destroy();
2480
+ hls = null;
2481
+ }
2482
+ currentSrc = null;
2483
+ isNative = false;
2484
+ isAutoQuality = true;
2485
+ networkRetryCount = 0;
2486
+ mediaRetryCount = 0;
2487
+ errorCount = 0;
2488
+ errorWindowStart = 0;
2489
+ };
2490
+ const buildHlsConfig = () => ({
2491
+ debug: mergedConfig.debug,
2492
+ autoStartLoad: mergedConfig.autoStartLoad,
2493
+ startPosition: mergedConfig.startPosition,
2494
+ startLevel: -1,
2495
+ lowLatencyMode: mergedConfig.lowLatencyMode,
2496
+ maxBufferLength: mergedConfig.maxBufferLength,
2497
+ maxMaxBufferLength: mergedConfig.maxMaxBufferLength,
2498
+ backBufferLength: mergedConfig.backBufferLength,
2499
+ enableWorker: mergedConfig.enableWorker,
2500
+ fragLoadingMaxRetry: 1,
2501
+ manifestLoadingMaxRetry: 1,
2502
+ levelLoadingMaxRetry: 1,
2503
+ fragLoadingRetryDelay: 500,
2504
+ manifestLoadingRetryDelay: 500,
2505
+ levelLoadingRetryDelay: 500
2506
+ });
2507
+ const getRetryDelay = (retryCount) => {
2508
+ const baseDelay = mergedConfig.retryDelayMs ?? 1e3;
2509
+ const backoffFactor = mergedConfig.retryBackoffFactor ?? 2;
2510
+ return baseDelay * Math.pow(backoffFactor, retryCount);
2511
+ };
2512
+ const emitFatalError = (error, retriesExhausted) => {
2513
+ const message = retriesExhausted ? `HLS error: ${error.details} (max retries exceeded)` : `HLS error: ${error.details}`;
2514
+ api?.logger.error(message, { type: error.type, details: error.details });
2515
+ api?.setState("playbackState", "error");
2516
+ api?.setState("buffering", false);
2517
+ api?.emit("error", {
2518
+ code: "MEDIA_ERROR",
2519
+ message,
2520
+ fatal: true,
2521
+ timestamp: Date.now()
2522
+ });
2523
+ };
2524
+ const handleHlsError = (error) => {
2525
+ const Hls = getHlsConstructor();
2526
+ if (!Hls || !hls) return;
2527
+ const now = Date.now();
2528
+ if (now - errorWindowStart > ERROR_WINDOW_MS) {
2529
+ errorCount = 1;
2530
+ errorWindowStart = now;
2531
+ } else {
2532
+ errorCount++;
2533
+ }
2534
+ if (errorCount >= MAX_ERRORS_IN_WINDOW) {
2535
+ api?.logger.error(`Too many errors (${errorCount} in ${ERROR_WINDOW_MS}ms), giving up`);
2536
+ emitFatalError(error, true);
2537
+ cleanupHlsEvents?.();
2538
+ cleanupHlsEvents = null;
2539
+ hls.destroy();
2540
+ hls = null;
2541
+ return;
2542
+ }
2543
+ if (error.fatal) {
2544
+ api?.logger.error("Fatal HLS error", { type: error.type, details: error.details });
2545
+ switch (error.type) {
2546
+ case "network": {
2547
+ const maxRetries = mergedConfig.maxNetworkRetries ?? 3;
2548
+ if (networkRetryCount >= maxRetries) {
2549
+ api?.logger.error(`Network error recovery failed after ${networkRetryCount} attempts`);
2550
+ emitFatalError(error, true);
2551
+ return;
2552
+ }
2553
+ networkRetryCount++;
2554
+ const delay = getRetryDelay(networkRetryCount - 1);
2555
+ api?.logger.info(`Attempting network error recovery (attempt ${networkRetryCount}/${maxRetries}) in ${delay}ms`);
2556
+ api?.emit("error:network", { error: new Error(error.details) });
2557
+ if (retryTimeout) {
2558
+ clearTimeout(retryTimeout);
2559
+ }
2560
+ retryTimeout = setTimeout(() => {
2561
+ if (hls) {
2562
+ hls.startLoad();
2563
+ }
2564
+ }, delay);
2565
+ break;
2566
+ }
2567
+ case "media": {
2568
+ const maxRetries = mergedConfig.maxMediaRetries ?? 2;
2569
+ if (mediaRetryCount >= maxRetries) {
2570
+ api?.logger.error(`Media error recovery failed after ${mediaRetryCount} attempts`);
2571
+ emitFatalError(error, true);
2572
+ return;
2573
+ }
2574
+ mediaRetryCount++;
2575
+ const delay = getRetryDelay(mediaRetryCount - 1);
2576
+ api?.logger.info(`Attempting media error recovery (attempt ${mediaRetryCount}/${maxRetries}) in ${delay}ms`);
2577
+ api?.emit("error:media", { error: new Error(error.details) });
2578
+ if (retryTimeout) {
2579
+ clearTimeout(retryTimeout);
2580
+ }
2581
+ retryTimeout = setTimeout(() => {
2582
+ if (hls) {
2583
+ hls.recoverMediaError();
2584
+ }
2585
+ }, delay);
2586
+ break;
2587
+ }
2588
+ default:
2589
+ emitFatalError(error, false);
2590
+ break;
2591
+ }
2592
+ }
2593
+ };
2594
+ const loadNative = async (src) => {
2595
+ const videoEl = getOrCreateVideo();
2596
+ isNative = true;
2597
+ if (api) {
2598
+ cleanupVideoEvents = setupVideoEventHandlers(videoEl, api);
2599
+ }
2600
+ return new Promise((resolve, reject) => {
2601
+ const onLoaded = () => {
2602
+ videoEl.removeEventListener("loadedmetadata", onLoaded);
2603
+ videoEl.removeEventListener("error", onError);
2604
+ api?.setState("source", { src, type: "application/x-mpegURL" });
2605
+ api?.emit("media:loaded", { src, type: "application/x-mpegURL" });
2606
+ resolve();
2607
+ };
2608
+ const onError = () => {
2609
+ videoEl.removeEventListener("loadedmetadata", onLoaded);
2610
+ videoEl.removeEventListener("error", onError);
2611
+ const error = videoEl.error;
2612
+ reject(new Error(error?.message || "Failed to load HLS source"));
2613
+ };
2614
+ videoEl.addEventListener("loadedmetadata", onLoaded);
2615
+ videoEl.addEventListener("error", onError);
2616
+ videoEl.src = src;
2617
+ videoEl.load();
2618
+ });
2619
+ };
2620
+ const loadWithHlsJs = async (src) => {
2621
+ await loadHlsJs();
2622
+ const videoEl = getOrCreateVideo();
2623
+ isNative = false;
2624
+ hls = createHlsInstance(buildHlsConfig());
2625
+ if (api) {
2626
+ cleanupVideoEvents = setupVideoEventHandlers(videoEl, api);
2627
+ }
2628
+ return new Promise((resolve, reject) => {
2629
+ if (!hls || !api) {
2630
+ reject(new Error("HLS not initialized"));
2631
+ return;
2632
+ }
2633
+ let resolved = false;
2634
+ cleanupHlsEvents = setupHlsEventHandlers(hls, api, {
2635
+ onManifestParsed: () => {
2636
+ if (!resolved) {
2637
+ resolved = true;
2638
+ api?.setState("source", { src, type: "application/x-mpegURL" });
2639
+ api?.emit("media:loaded", { src, type: "application/x-mpegURL" });
2640
+ resolve();
2641
+ }
2642
+ },
2643
+ onLevelSwitched: () => {
2644
+ },
2645
+ onError: (error) => {
2646
+ handleHlsError(error);
2647
+ if (error.fatal && !resolved && error.type !== "network" && error.type !== "media") {
2648
+ resolved = true;
2649
+ reject(new Error(error.details));
2650
+ }
2651
+ },
2652
+ getIsAutoQuality: () => isAutoQuality
2653
+ });
2654
+ hls.attachMedia(videoEl);
2655
+ hls.loadSource(src);
2656
+ });
2657
+ };
2658
+ const plugin = {
2659
+ id: "hls-provider",
2660
+ name: "HLS Provider (Light)",
2661
+ version: "1.0.0",
2662
+ type: "provider",
2663
+ description: "HLS playback provider using hls.js/light (smaller bundle)",
2664
+ canPlay(src) {
2665
+ if (!isHLSSupported()) return false;
2666
+ const url = src.toLowerCase();
2667
+ const urlWithoutQuery = url.split("?")[0].split("#")[0];
2668
+ if (urlWithoutQuery.endsWith(".m3u8")) return true;
2669
+ if (url.includes("application/x-mpegurl")) return true;
2670
+ if (url.includes("application/vnd.apple.mpegurl")) return true;
2671
+ return false;
2672
+ },
2673
+ async init(pluginApi) {
2674
+ api = pluginApi;
2675
+ api.logger.info("HLS plugin (light) initialized");
2676
+ const unsubPlay = api.on("playback:play", async () => {
2677
+ if (!video) return;
2678
+ try {
2679
+ await video.play();
2680
+ } catch (e) {
2681
+ api?.logger.error("Play failed", e);
2682
+ }
2683
+ });
2684
+ const unsubPause = api.on("playback:pause", () => {
2685
+ video?.pause();
2686
+ });
2687
+ const unsubSeek = api.on("playback:seeking", ({ time }) => {
2688
+ if (!video) return;
2689
+ const clampedTime = Math.max(0, Math.min(time, video.duration || 0));
2690
+ video.currentTime = clampedTime;
2691
+ });
2692
+ const unsubVolume = api.on("volume:change", ({ volume }) => {
2693
+ if (video) video.volume = volume;
2694
+ });
2695
+ const unsubMute = api.on("volume:mute", ({ muted }) => {
2696
+ if (video) video.muted = muted;
2697
+ });
2698
+ const unsubRate = api.on("playback:ratechange", ({ rate }) => {
2699
+ if (video) video.playbackRate = rate;
2700
+ });
2701
+ const unsubQuality = api.on("quality:select", ({ quality, auto }) => {
2702
+ if (!hls || isNative) {
2703
+ api?.logger.warn("Quality selection not available");
2704
+ return;
2705
+ }
2706
+ if (auto || quality === "auto") {
2707
+ isAutoQuality = true;
2708
+ hls.currentLevel = -1;
2709
+ api?.logger.debug("Quality: auto selection enabled");
2710
+ api?.setState("currentQuality", {
2711
+ id: "auto",
2712
+ label: "Auto",
2713
+ width: 0,
2714
+ height: 0,
2715
+ bitrate: 0,
2716
+ active: true
2717
+ });
2718
+ } else {
2719
+ isAutoQuality = false;
2720
+ const levelIndex = parseInt(quality.replace("level-", ""), 10);
2721
+ if (!isNaN(levelIndex) && levelIndex >= 0 && levelIndex < hls.levels.length) {
2722
+ hls.nextLevel = levelIndex;
2723
+ api?.logger.debug(`Quality: queued switch to level ${levelIndex}`);
2724
+ }
2725
+ }
2726
+ });
2727
+ api.onDestroy(() => {
2728
+ unsubPlay();
2729
+ unsubPause();
2730
+ unsubSeek();
2731
+ unsubVolume();
2732
+ unsubMute();
2733
+ unsubRate();
2734
+ unsubQuality();
2735
+ });
2736
+ },
2737
+ async destroy() {
2738
+ api?.logger.info("HLS plugin (light) destroying");
2739
+ cleanup();
2740
+ if (video?.parentNode) {
2741
+ video.parentNode.removeChild(video);
2742
+ }
2743
+ video = null;
2744
+ api = null;
2745
+ },
2746
+ async loadSource(src) {
2747
+ if (!api) throw new Error("Plugin not initialized");
2748
+ api.logger.info("Loading HLS source (light)", { src });
2749
+ cleanup();
2750
+ currentSrc = src;
2751
+ api.setState("playbackState", "loading");
2752
+ api.setState("buffering", true);
2753
+ if (isHlsJsSupported()) {
2754
+ api.logger.info("Using hls.js/light for HLS playback");
2755
+ await loadWithHlsJs(src);
2756
+ } else if (supportsNativeHLS()) {
2757
+ api.logger.info("Using native HLS playback (hls.js not supported)");
2758
+ await loadNative(src);
2759
+ } else {
2760
+ throw new Error("HLS playback not supported in this browser");
2761
+ }
2762
+ api.setState("playbackState", "ready");
2763
+ api.setState("buffering", false);
2764
+ },
2765
+ getCurrentLevel() {
2766
+ if (isNative || !hls) return -1;
2767
+ return hls.currentLevel;
2768
+ },
2769
+ setLevel(index) {
2770
+ if (isNative || !hls) {
2771
+ api?.logger.warn("Quality selection not available in native HLS mode");
2772
+ return;
2773
+ }
2774
+ hls.currentLevel = index;
2775
+ },
2776
+ getLevels() {
2777
+ if (isNative || !hls) return [];
2778
+ return mapLevels(hls.levels, hls.currentLevel);
2779
+ },
2780
+ getHlsInstance() {
2781
+ return hls;
2782
+ },
2783
+ isNativeHLS() {
2784
+ return isNative;
2785
+ },
2786
+ getLiveInfo() {
2787
+ if (isNative || !hls) return null;
2788
+ const live = api?.getState("live") || false;
2789
+ if (!live) return null;
2790
+ return {
2791
+ isLive: true,
2792
+ latency: hls.latency || 0,
2793
+ targetLatency: hls.targetLatency || 3,
2794
+ drift: hls.drift || 0
2795
+ };
2796
+ },
2797
+ async switchToNative() {
2798
+ if (isNative) {
2799
+ api?.logger.debug("Already using native HLS");
2800
+ return;
2801
+ }
2802
+ if (!supportsNativeHLS()) {
2803
+ api?.logger.warn("Native HLS not supported in this browser");
2804
+ return;
2805
+ }
2806
+ if (!currentSrc) {
2807
+ api?.logger.warn("No source loaded");
2808
+ return;
2809
+ }
2810
+ api?.logger.info("Switching to native HLS for AirPlay");
2811
+ const wasPlaying = api?.getState("playing") || false;
2812
+ const currentTime = video?.currentTime || 0;
2813
+ const savedSrc = currentSrc;
2814
+ cleanup();
2815
+ await loadNative(savedSrc);
2816
+ if (video && currentTime > 0) {
2817
+ video.currentTime = currentTime;
2818
+ }
2819
+ if (wasPlaying && video) {
2820
+ try {
2821
+ await video.play();
2822
+ } catch (e) {
2823
+ api?.logger.debug("Could not auto-resume after switch");
2824
+ }
2825
+ }
2826
+ api?.logger.info("Switched to native HLS");
2827
+ },
2828
+ async switchToHlsJs() {
2829
+ if (!isNative) {
2830
+ api?.logger.debug("Already using hls.js");
2831
+ return;
2832
+ }
2833
+ if (!isHlsJsSupported()) {
2834
+ api?.logger.warn("hls.js not supported in this browser");
2835
+ return;
2836
+ }
2837
+ if (!currentSrc) {
2838
+ api?.logger.warn("No source loaded");
2839
+ return;
2840
+ }
2841
+ api?.logger.info("Switching back to hls.js");
2842
+ const wasPlaying = api?.getState("playing") || false;
2843
+ const currentTime = video?.currentTime || 0;
2844
+ const savedSrc = currentSrc;
2845
+ cleanup();
2846
+ await loadWithHlsJs(savedSrc);
2847
+ if (video && currentTime > 0) {
2848
+ video.currentTime = currentTime;
2849
+ }
2850
+ if (wasPlaying && video) {
2851
+ try {
2852
+ await video.play();
2853
+ } catch (e) {
2854
+ api?.logger.debug("Could not auto-resume after switch");
2855
+ }
2856
+ }
2857
+ api?.logger.info("Switched to hls.js");
2858
+ }
2859
+ };
2860
+ return plugin;
2861
+ }
2862
+ var DEFAULT_THEME = {
2863
+ primary: "#6366f1",
2864
+ background: "#18181b",
2865
+ text: "#fafafa",
2866
+ textSecondary: "#a1a1aa",
2867
+ progressBackground: "#3f3f46",
2868
+ progressFill: "#6366f1",
2869
+ borderRadius: "12px",
2870
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
2871
+ };
2872
+ var DEFAULT_CONFIG$2 = {
2873
+ layout: "full",
2874
+ showArtwork: true,
2875
+ showTitle: true,
2876
+ showArtist: true,
2877
+ showTime: true,
2878
+ showVolume: true,
2879
+ showShuffle: true,
2880
+ showRepeat: true,
2881
+ showNavigation: true,
2882
+ classPrefix: "scarlett-audio",
2883
+ autoHide: 0,
2884
+ theme: DEFAULT_THEME
2885
+ };
2886
+ function formatTime(seconds) {
2887
+ if (!isFinite(seconds) || seconds < 0) return "0:00";
2888
+ const h = Math.floor(seconds / 3600);
2889
+ const m = Math.floor(seconds % 3600 / 60);
2890
+ const s = Math.floor(seconds % 60);
2891
+ if (h > 0) {
2892
+ return `${h}:${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`;
2893
+ }
2894
+ return `${m}:${s.toString().padStart(2, "0")}`;
2895
+ }
2896
+ function createStyles(prefix, theme) {
2897
+ return `
2898
+ .${prefix} {
2899
+ font-family: ${theme.fontFamily};
2900
+ background: ${theme.background};
2901
+ color: ${theme.text};
2902
+ border-radius: ${theme.borderRadius};
2903
+ overflow: hidden;
2904
+ user-select: none;
2905
+ }
2906
+
2907
+ .${prefix}--full {
2908
+ display: flex;
2909
+ flex-direction: column;
2910
+ padding: 20px;
2911
+ gap: 16px;
2912
+ max-width: 400px;
2913
+ }
2914
+
2915
+ .${prefix}--compact {
2916
+ display: flex;
2917
+ align-items: center;
2918
+ padding: 12px 16px;
2919
+ gap: 12px;
2920
+ }
2921
+
2922
+ .${prefix}--mini {
2923
+ display: flex;
2924
+ align-items: center;
2925
+ padding: 8px 12px;
2926
+ gap: 8px;
2927
+ }
2928
+
2929
+ .${prefix}__artwork {
2930
+ flex-shrink: 0;
2931
+ background: ${theme.progressBackground};
2932
+ border-radius: 8px;
2933
+ overflow: hidden;
2934
+ display: flex;
2935
+ align-items: center;
2936
+ justify-content: center;
2937
+ }
2938
+
2939
+ .${prefix}--full .${prefix}__artwork {
2940
+ width: 100%;
2941
+ aspect-ratio: 1;
2942
+ border-radius: ${theme.borderRadius};
2943
+ }
2944
+
2945
+ .${prefix}--compact .${prefix}__artwork {
2946
+ width: 56px;
2947
+ height: 56px;
2948
+ }
2949
+
2950
+ .${prefix}--mini .${prefix}__artwork {
2951
+ width: 40px;
2952
+ height: 40px;
2953
+ border-radius: 6px;
2954
+ }
2955
+
2956
+ .${prefix}__artwork img {
2957
+ width: 100%;
2958
+ height: 100%;
2959
+ object-fit: cover;
2960
+ }
2961
+
2962
+ .${prefix}__artwork-placeholder {
2963
+ width: 50%;
2964
+ height: 50%;
2965
+ opacity: 0.3;
2966
+ }
2967
+
2968
+ .${prefix}__info {
2969
+ flex: 1;
2970
+ min-width: 0;
2971
+ display: flex;
2972
+ flex-direction: column;
2973
+ gap: 4px;
2974
+ }
2975
+
2976
+ .${prefix}__title {
2977
+ font-size: 16px;
2978
+ font-weight: 600;
2979
+ white-space: nowrap;
2980
+ overflow: hidden;
2981
+ text-overflow: ellipsis;
2982
+ }
2983
+
2984
+ .${prefix}--mini .${prefix}__title {
2985
+ font-size: 14px;
2986
+ display: inline-block;
2987
+ animation: none;
2988
+ }
2989
+
2990
+ .${prefix}__title-wrapper {
2991
+ overflow: hidden;
2992
+ width: 100%;
2993
+ }
2994
+
2995
+ .${prefix}--mini .${prefix}__title-wrapper .${prefix}__title.scrolling {
2996
+ animation: marquee 8s linear infinite;
2997
+ }
2998
+
2999
+ @keyframes marquee {
3000
+ 0% { transform: translateX(0); }
3001
+ 100% { transform: translateX(-50%); }
3002
+ }
3003
+
3004
+ .${prefix}--mini .${prefix}__progress {
3005
+ margin-top: 4px;
3006
+ }
3007
+
3008
+ .${prefix}--mini .${prefix}__progress-bar {
3009
+ height: 4px;
3010
+ }
3011
+
3012
+ .${prefix}__artist {
3013
+ font-size: 14px;
3014
+ color: ${theme.textSecondary};
3015
+ white-space: nowrap;
3016
+ overflow: hidden;
3017
+ text-overflow: ellipsis;
3018
+ }
3019
+
3020
+ .${prefix}--mini .${prefix}__artist {
3021
+ font-size: 12px;
3022
+ }
3023
+
3024
+ .${prefix}__progress {
3025
+ display: flex;
3026
+ align-items: center;
3027
+ gap: 12px;
3028
+ }
3029
+
3030
+ .${prefix}__progress-bar {
3031
+ flex: 1;
3032
+ height: 6px;
3033
+ background: ${theme.progressBackground};
3034
+ border-radius: 3px;
3035
+ cursor: pointer;
3036
+ position: relative;
3037
+ overflow: hidden;
3038
+ }
3039
+
3040
+ .${prefix}__progress-bar:hover {
3041
+ height: 8px;
3042
+ }
3043
+
3044
+ .${prefix}__progress-fill {
3045
+ height: 100%;
3046
+ background: ${theme.progressFill};
3047
+ border-radius: 3px;
3048
+ width: 100%;
3049
+ transform-origin: left center;
3050
+ will-change: transform;
3051
+ }
3052
+
3053
+ .${prefix}__progress-buffered {
3054
+ position: absolute;
3055
+ top: 0;
3056
+ left: 0;
3057
+ height: 100%;
3058
+ background: ${theme.progressBackground};
3059
+ opacity: 0.5;
3060
+ border-radius: 3px;
3061
+ }
3062
+
3063
+ .${prefix}__time {
3064
+ font-size: 12px;
3065
+ color: ${theme.textSecondary};
3066
+ min-width: 40px;
3067
+ text-align: center;
3068
+ }
3069
+
3070
+ .${prefix}__controls {
3071
+ display: flex;
3072
+ align-items: center;
3073
+ justify-content: center;
3074
+ gap: 8px;
3075
+ }
3076
+
3077
+ .${prefix}__btn {
3078
+ background: transparent;
3079
+ border: none;
3080
+ color: ${theme.text};
3081
+ cursor: pointer;
3082
+ padding: 8px;
3083
+ border-radius: 50%;
3084
+ display: flex;
3085
+ align-items: center;
3086
+ justify-content: center;
3087
+ transition: background 0.2s, transform 0.1s;
3088
+ }
3089
+
3090
+ .${prefix}__btn:hover {
3091
+ background: rgba(255, 255, 255, 0.1);
3092
+ }
3093
+
3094
+ .${prefix}__btn:active {
3095
+ transform: scale(0.95);
3096
+ }
3097
+
3098
+ .${prefix}__btn--primary {
3099
+ background: ${theme.primary};
3100
+ width: 48px;
3101
+ height: 48px;
3102
+ }
3103
+
3104
+ .${prefix}__btn--primary:hover {
3105
+ background: ${theme.primary};
3106
+ opacity: 0.9;
3107
+ }
3108
+
3109
+ .${prefix}--mini .${prefix}__btn--primary {
3110
+ width: 36px;
3111
+ height: 36px;
3112
+ }
3113
+
3114
+ .${prefix}__btn--active {
3115
+ color: ${theme.primary};
3116
+ }
3117
+
3118
+ .${prefix}__btn svg {
3119
+ width: 20px;
3120
+ height: 20px;
3121
+ fill: currentColor;
3122
+ }
3123
+
3124
+ .${prefix}__btn--primary svg {
3125
+ width: 24px;
3126
+ height: 24px;
3127
+ }
3128
+
3129
+ .${prefix}__volume {
3130
+ display: flex;
3131
+ align-items: center;
3132
+ gap: 8px;
3133
+ }
3134
+
3135
+ .${prefix}__volume-slider {
3136
+ width: 80px;
3137
+ height: 4px;
3138
+ background: ${theme.progressBackground};
3139
+ border-radius: 2px;
3140
+ cursor: pointer;
3141
+ position: relative;
3142
+ }
3143
+
3144
+ .${prefix}__volume-fill {
3145
+ height: 100%;
3146
+ background: ${theme.text};
3147
+ border-radius: 2px;
3148
+ }
3149
+
3150
+ .${prefix}__secondary-controls {
3151
+ display: flex;
3152
+ align-items: center;
3153
+ justify-content: space-between;
3154
+ }
3155
+
3156
+ .${prefix}--hidden {
3157
+ display: none;
3158
+ }
3159
+ `;
3160
+ }
3161
+ var ICONS = {
3162
+ play: `<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>`,
3163
+ pause: `<svg viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>`,
3164
+ previous: `<svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>`,
3165
+ next: `<svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>`,
3166
+ shuffle: `<svg viewBox="0 0 24 24"><path d="M10.59 9.17L5.41 4 4 5.41l5.17 5.17 1.42-1.41zM14.5 4l2.04 2.04L4 18.59 5.41 20 17.96 7.46 20 9.5V4h-5.5zm.33 9.41l-1.41 1.41 3.13 3.13L14.5 20H20v-5.5l-2.04 2.04-3.13-3.13z"/></svg>`,
3167
+ repeatOff: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>`,
3168
+ repeatAll: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>`,
3169
+ repeatOne: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4zm-4-2V9h-1l-2 1v1h1.5v4H13z"/></svg>`,
3170
+ volumeHigh: `<svg viewBox="0 0 24 24"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>`,
3171
+ volumeMuted: `<svg viewBox="0 0 24 24"><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"/></svg>`,
3172
+ music: `<svg viewBox="0 0 24 24"><path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z"/></svg>`
3173
+ };
3174
+ function createAudioUIPlugin(config) {
3175
+ const mergedConfig = { ...DEFAULT_CONFIG$2, ...config };
3176
+ const theme = { ...DEFAULT_THEME, ...mergedConfig.theme };
3177
+ const prefix = mergedConfig.classPrefix;
3178
+ let api = null;
3179
+ let container = null;
3180
+ let styleElement = null;
3181
+ let layout = mergedConfig.layout;
3182
+ let isVisible = true;
3183
+ let animationFrameId = null;
3184
+ let lastKnownTime = 0;
3185
+ let lastUpdateTimestamp = 0;
3186
+ let isPlaying = false;
3187
+ let artworkImg = null;
3188
+ let titleEl = null;
3189
+ let artistEl = null;
3190
+ let progressFill = null;
3191
+ let currentTimeEl = null;
3192
+ let durationEl = null;
3193
+ let playPauseBtn = null;
3194
+ let shuffleBtn = null;
3195
+ let repeatBtn = null;
3196
+ let volumeBtn = null;
3197
+ let volumeFill = null;
3198
+ const startProgressAnimation = () => {
3199
+ if (animationFrameId !== null) return;
3200
+ const animate = (timestamp) => {
3201
+ if (!api || !isPlaying) {
3202
+ animationFrameId = null;
3203
+ return;
3204
+ }
3205
+ const duration = api.getState("duration") || 0;
3206
+ if (duration <= 0) {
3207
+ animationFrameId = requestAnimationFrame(animate);
3208
+ return;
3209
+ }
3210
+ const elapsed = (timestamp - lastUpdateTimestamp) / 1e3;
3211
+ const interpolatedTime = Math.min(lastKnownTime + elapsed, duration);
3212
+ const scale = interpolatedTime / duration;
3213
+ if (progressFill) {
3214
+ progressFill.style.transform = `scaleX(${scale})`;
3215
+ }
3216
+ if (currentTimeEl) {
3217
+ currentTimeEl.textContent = formatTime(interpolatedTime);
3218
+ }
3219
+ animationFrameId = requestAnimationFrame(animate);
3220
+ };
3221
+ lastUpdateTimestamp = performance.now();
3222
+ animationFrameId = requestAnimationFrame(animate);
3223
+ };
3224
+ const stopProgressAnimation = () => {
3225
+ if (animationFrameId !== null) {
3226
+ cancelAnimationFrame(animationFrameId);
3227
+ animationFrameId = null;
3228
+ }
3229
+ };
3230
+ const createUI = () => {
3231
+ if (!api) return;
3232
+ styleElement = document.createElement("style");
3233
+ styleElement.textContent = createStyles(prefix, theme);
3234
+ document.head.appendChild(styleElement);
3235
+ container = document.createElement("div");
3236
+ container.className = `${prefix} ${prefix}--${layout}`;
3237
+ if (layout === "full") {
3238
+ container.innerHTML = buildFullLayout();
3239
+ } else if (layout === "compact") {
3240
+ container.innerHTML = buildCompactLayout();
3241
+ } else {
3242
+ container.innerHTML = buildMiniLayout();
3243
+ }
3244
+ artworkImg = container.querySelector(`.${prefix}__artwork img`);
3245
+ titleEl = container.querySelector(`.${prefix}__title`);
3246
+ artistEl = container.querySelector(`.${prefix}__artist`);
3247
+ progressFill = container.querySelector(`.${prefix}__progress-fill`);
3248
+ currentTimeEl = container.querySelector(`.${prefix}__time--current`);
3249
+ durationEl = container.querySelector(`.${prefix}__time--duration`);
3250
+ playPauseBtn = container.querySelector(`.${prefix}__btn--play`);
3251
+ shuffleBtn = container.querySelector(`.${prefix}__btn--shuffle`);
3252
+ repeatBtn = container.querySelector(`.${prefix}__btn--repeat`);
3253
+ volumeBtn = container.querySelector(`.${prefix}__btn--volume`);
3254
+ volumeFill = container.querySelector(`.${prefix}__volume-fill`);
3255
+ attachEventListeners();
3256
+ api.container.appendChild(container);
3257
+ };
3258
+ const buildFullLayout = () => {
3259
+ return `
3260
+ ${mergedConfig.showArtwork ? `
3261
+ <div class="${prefix}__artwork">
3262
+ <img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
3263
+ ${!mergedConfig.defaultArtwork ? `<div class="${prefix}__artwork-placeholder">${ICONS.music}</div>` : ""}
3264
+ </div>
3265
+ ` : ""}
3266
+ <div class="${prefix}__info">
3267
+ ${mergedConfig.showTitle ? `<div class="${prefix}__title">-</div>` : ""}
3268
+ ${mergedConfig.showArtist ? `<div class="${prefix}__artist">-</div>` : ""}
3269
+ </div>
3270
+ <div class="${prefix}__progress">
3271
+ ${mergedConfig.showTime ? `<span class="${prefix}__time ${prefix}__time--current">0:00</span>` : ""}
3272
+ <div class="${prefix}__progress-bar">
3273
+ <div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
3274
+ </div>
3275
+ ${mergedConfig.showTime ? `<span class="${prefix}__time ${prefix}__time--duration">0:00</span>` : ""}
3276
+ </div>
3277
+ <div class="${prefix}__controls">
3278
+ ${mergedConfig.showShuffle ? `<button class="${prefix}__btn ${prefix}__btn--shuffle" title="Shuffle">${ICONS.shuffle}</button>` : ""}
3279
+ ${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--prev" title="Previous">${ICONS.previous}</button>` : ""}
3280
+ <button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
3281
+ ${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--next" title="Next">${ICONS.next}</button>` : ""}
3282
+ ${mergedConfig.showRepeat ? `<button class="${prefix}__btn ${prefix}__btn--repeat" title="Repeat">${ICONS.repeatOff}</button>` : ""}
3283
+ </div>
3284
+ ${mergedConfig.showVolume ? `
3285
+ <div class="${prefix}__secondary-controls">
3286
+ <div class="${prefix}__volume">
3287
+ <button class="${prefix}__btn ${prefix}__btn--volume" title="Volume">${ICONS.volumeHigh}</button>
3288
+ <div class="${prefix}__volume-slider">
3289
+ <div class="${prefix}__volume-fill" style="width: 100%"></div>
3290
+ </div>
3291
+ </div>
3292
+ </div>
3293
+ ` : ""}
3294
+ `;
3295
+ };
3296
+ const buildCompactLayout = () => {
3297
+ return `
3298
+ ${mergedConfig.showArtwork ? `
3299
+ <div class="${prefix}__artwork">
3300
+ <img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
3301
+ </div>
3302
+ ` : ""}
3303
+ <div class="${prefix}__info">
3304
+ ${mergedConfig.showTitle ? `<div class="${prefix}__title">-</div>` : ""}
3305
+ ${mergedConfig.showArtist ? `<div class="${prefix}__artist">-</div>` : ""}
3306
+ <div class="${prefix}__progress">
3307
+ <div class="${prefix}__progress-bar">
3308
+ <div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
3309
+ </div>
3310
+ </div>
3311
+ </div>
3312
+ <div class="${prefix}__controls">
3313
+ ${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--prev" title="Previous">${ICONS.previous}</button>` : ""}
3314
+ <button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
3315
+ ${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--next" title="Next">${ICONS.next}</button>` : ""}
3316
+ </div>
3317
+ `;
3318
+ };
3319
+ const buildMiniLayout = () => {
3320
+ return `
3321
+ <button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
3322
+ ${mergedConfig.showArtwork ? `
3323
+ <div class="${prefix}__artwork">
3324
+ <img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
3325
+ </div>
3326
+ ` : ""}
3327
+ <div class="${prefix}__info">
3328
+ ${mergedConfig.showTitle ? `<div class="${prefix}__title-wrapper"><div class="${prefix}__title">-</div></div>` : ""}
3329
+ <div class="${prefix}__progress">
3330
+ <div class="${prefix}__progress-bar">
3331
+ <div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
3332
+ </div>
3333
+ </div>
3334
+ </div>
3335
+ `;
3336
+ };
3337
+ const attachEventListeners = () => {
3338
+ if (!container || !api) return;
3339
+ playPauseBtn?.addEventListener("click", () => {
3340
+ const playing = api?.getState("playing");
3341
+ if (playing) {
3342
+ api?.emit("playback:pause", void 0);
3343
+ } else {
3344
+ api?.emit("playback:play", void 0);
3345
+ }
3346
+ });
3347
+ container.querySelector(`.${prefix}__btn--prev`)?.addEventListener("click", () => {
3348
+ const playlist = api?.getPlugin("playlist");
3349
+ if (playlist) {
3350
+ playlist.previous();
3351
+ } else {
3352
+ api?.emit("playback:seeking", { time: 0 });
3353
+ }
3354
+ });
3355
+ container.querySelector(`.${prefix}__btn--next`)?.addEventListener("click", () => {
3356
+ const playlist = api?.getPlugin("playlist");
3357
+ playlist?.next();
3358
+ });
3359
+ shuffleBtn?.addEventListener("click", () => {
3360
+ const playlist = api?.getPlugin("playlist");
3361
+ playlist?.toggleShuffle();
3362
+ });
3363
+ repeatBtn?.addEventListener("click", () => {
3364
+ const playlist = api?.getPlugin("playlist");
3365
+ playlist?.cycleRepeat();
3366
+ });
3367
+ volumeBtn?.addEventListener("click", () => {
3368
+ const muted = api?.getState("muted");
3369
+ api?.emit("volume:mute", { muted: !muted });
3370
+ });
3371
+ const progressBar = container.querySelector(`.${prefix}__progress-bar`);
3372
+ progressBar?.addEventListener("click", (e) => {
3373
+ const mouseEvent = e;
3374
+ const rect = mouseEvent.currentTarget.getBoundingClientRect();
3375
+ const percent = (mouseEvent.clientX - rect.left) / rect.width;
3376
+ const duration = api?.getState("duration") || 0;
3377
+ const time = percent * duration;
3378
+ api?.emit("playback:seeking", { time });
3379
+ });
3380
+ const volumeSlider = container.querySelector(`.${prefix}__volume-slider`);
3381
+ volumeSlider?.addEventListener("click", (e) => {
3382
+ const mouseEvent = e;
3383
+ const rect = mouseEvent.currentTarget.getBoundingClientRect();
3384
+ const percent = Math.max(0, Math.min(1, (mouseEvent.clientX - rect.left) / rect.width));
3385
+ api?.emit("volume:change", { volume: percent, muted: false });
3386
+ });
3387
+ };
3388
+ const updateUI = () => {
3389
+ if (!api || !container) return;
3390
+ const playing = api.getState("playing");
3391
+ const wasPlaying = isPlaying;
3392
+ isPlaying = playing;
3393
+ if (playPauseBtn) {
3394
+ playPauseBtn.innerHTML = playing ? ICONS.pause : ICONS.play;
3395
+ playPauseBtn.title = playing ? "Pause" : "Play";
3396
+ }
3397
+ const currentTime = api.getState("currentTime") || 0;
3398
+ const duration = api.getState("duration") || 0;
3399
+ lastKnownTime = currentTime;
3400
+ lastUpdateTimestamp = performance.now();
3401
+ if (playing && !wasPlaying) {
3402
+ startProgressAnimation();
3403
+ } else if (!playing && wasPlaying) {
3404
+ stopProgressAnimation();
3405
+ }
3406
+ if (!playing) {
3407
+ const scale = duration > 0 ? currentTime / duration : 0;
3408
+ if (progressFill) {
3409
+ progressFill.style.transform = `scaleX(${scale})`;
3410
+ }
3411
+ if (currentTimeEl) {
3412
+ currentTimeEl.textContent = formatTime(currentTime);
3413
+ }
3414
+ }
3415
+ if (durationEl) {
3416
+ durationEl.textContent = formatTime(duration);
3417
+ }
3418
+ const title = api.getState("title");
3419
+ const poster = api.getState("poster");
3420
+ if (titleEl && title) {
3421
+ titleEl.textContent = title;
3422
+ if (layout === "mini") {
3423
+ const wrapper = titleEl.parentElement;
3424
+ if (wrapper && titleEl.scrollWidth > wrapper.clientWidth) {
3425
+ titleEl.textContent = `${title} • ${title} • `;
3426
+ titleEl.classList.add("scrolling");
3427
+ } else {
3428
+ titleEl.classList.remove("scrolling");
3429
+ }
3430
+ }
3431
+ }
3432
+ if (artworkImg && poster) {
3433
+ artworkImg.src = poster;
3434
+ }
3435
+ const volume = api.getState("volume") || 1;
3436
+ const muted = api.getState("muted");
3437
+ if (volumeFill) {
3438
+ volumeFill.style.width = `${(muted ? 0 : volume) * 100}%`;
3439
+ }
3440
+ if (volumeBtn) {
3441
+ volumeBtn.innerHTML = muted || volume === 0 ? ICONS.volumeMuted : ICONS.volumeHigh;
3442
+ }
3443
+ const playlist = api.getPlugin("playlist");
3444
+ if (playlist) {
3445
+ const state = playlist.getState();
3446
+ if (shuffleBtn) {
3447
+ shuffleBtn.classList.toggle(`${prefix}__btn--active`, state.shuffle);
3448
+ }
3449
+ if (repeatBtn) {
3450
+ repeatBtn.classList.toggle(`${prefix}__btn--active`, state.repeat !== "none");
3451
+ if (state.repeat === "one") {
3452
+ repeatBtn.innerHTML = ICONS.repeatOne;
3453
+ } else if (state.repeat === "all") {
3454
+ repeatBtn.innerHTML = ICONS.repeatAll;
3455
+ } else {
3456
+ repeatBtn.innerHTML = ICONS.repeatOff;
3457
+ }
3458
+ }
3459
+ }
3460
+ };
3461
+ const plugin = {
3462
+ id: "audio-ui",
3463
+ name: "Audio UI",
3464
+ version: "1.0.0",
3465
+ type: "ui",
3466
+ description: "Compact audio player interface",
3467
+ async init(pluginApi) {
3468
+ api = pluginApi;
3469
+ api.logger.info("Audio UI plugin initialized");
3470
+ createUI();
3471
+ const unsubState = api.subscribeToState(() => {
3472
+ updateUI();
3473
+ });
3474
+ const unsubTime = api.on("playback:timeupdate", () => {
3475
+ updateUI();
3476
+ });
3477
+ const unsubPlaylist = api.on("playlist:change", (payload) => {
3478
+ if (payload?.track) {
3479
+ if (titleEl) titleEl.textContent = payload.track.title || "-";
3480
+ if (artistEl) artistEl.textContent = payload.track.artist || "-";
3481
+ if (artworkImg && payload.track.artwork) {
3482
+ artworkImg.src = payload.track.artwork;
3483
+ }
3484
+ }
3485
+ });
3486
+ const unsubShuffle = api.on("playlist:shuffle", () => {
3487
+ updateUI();
3488
+ });
3489
+ const unsubRepeat = api.on("playlist:repeat", () => {
3490
+ updateUI();
3491
+ });
3492
+ api.onDestroy(() => {
3493
+ unsubState();
3494
+ unsubTime();
3495
+ unsubPlaylist();
3496
+ unsubShuffle();
3497
+ unsubRepeat();
3498
+ });
3499
+ updateUI();
3500
+ },
3501
+ async destroy() {
3502
+ api?.logger.info("Audio UI plugin destroying");
3503
+ stopProgressAnimation();
3504
+ if (container?.parentNode) {
3505
+ container.parentNode.removeChild(container);
3506
+ }
3507
+ if (styleElement?.parentNode) {
3508
+ styleElement.parentNode.removeChild(styleElement);
3509
+ }
3510
+ container = null;
3511
+ styleElement = null;
3512
+ api = null;
3513
+ },
3514
+ getElement() {
3515
+ return container;
3516
+ },
3517
+ setLayout(newLayout) {
3518
+ if (!container) return;
3519
+ stopProgressAnimation();
3520
+ layout = newLayout;
3521
+ container.className = `${prefix} ${prefix}--${layout}`;
3522
+ if (layout === "full") {
3523
+ container.innerHTML = buildFullLayout();
3524
+ } else if (layout === "compact") {
3525
+ container.innerHTML = buildCompactLayout();
3526
+ } else {
3527
+ container.innerHTML = buildMiniLayout();
3528
+ }
3529
+ artworkImg = container.querySelector(`.${prefix}__artwork img`);
3530
+ titleEl = container.querySelector(`.${prefix}__title`);
3531
+ artistEl = container.querySelector(`.${prefix}__artist`);
3532
+ progressFill = container.querySelector(`.${prefix}__progress-fill`);
3533
+ currentTimeEl = container.querySelector(`.${prefix}__time--current`);
3534
+ durationEl = container.querySelector(`.${prefix}__time--duration`);
3535
+ playPauseBtn = container.querySelector(`.${prefix}__btn--play`);
3536
+ shuffleBtn = container.querySelector(`.${prefix}__btn--shuffle`);
3537
+ repeatBtn = container.querySelector(`.${prefix}__btn--repeat`);
3538
+ volumeBtn = container.querySelector(`.${prefix}__btn--volume`);
3539
+ volumeFill = container.querySelector(`.${prefix}__volume-fill`);
3540
+ attachEventListeners();
3541
+ updateUI();
3542
+ if (isPlaying) {
3543
+ startProgressAnimation();
3544
+ }
3545
+ },
3546
+ setTheme(newTheme) {
3547
+ Object.assign(theme, newTheme);
3548
+ if (styleElement) {
3549
+ styleElement.textContent = createStyles(prefix, theme);
3550
+ }
3551
+ },
3552
+ show() {
3553
+ isVisible = true;
3554
+ container?.classList.remove(`${prefix}--hidden`);
3555
+ },
3556
+ hide() {
3557
+ isVisible = false;
3558
+ container?.classList.add(`${prefix}--hidden`);
3559
+ },
3560
+ toggle() {
3561
+ if (isVisible) {
3562
+ this.hide();
3563
+ } else {
3564
+ this.show();
3565
+ }
3566
+ }
3567
+ };
3568
+ return plugin;
3569
+ }
3570
+ var DEFAULT_CONFIG$1 = {
3571
+ enablePlayPause: true,
3572
+ enableSeek: true,
3573
+ enableTrackNavigation: true,
3574
+ seekOffset: 10,
3575
+ updatePositionState: true
3576
+ };
3577
+ function isMediaSessionSupported() {
3578
+ return typeof navigator !== "undefined" && "mediaSession" in navigator;
3579
+ }
3580
+ function createMediaSessionPlugin(config) {
3581
+ const mergedConfig = { ...DEFAULT_CONFIG$1, ...config };
3582
+ let api = null;
3583
+ let currentMetadata = {};
3584
+ const updateMetadata = () => {
3585
+ if (!isMediaSessionSupported()) return;
3586
+ const artwork = [];
3587
+ const artworkSources = currentMetadata.artwork || mergedConfig.defaultArtwork || [];
3588
+ for (const art of artworkSources) {
3589
+ artwork.push({
3590
+ src: art.src,
3591
+ sizes: art.sizes || "512x512",
3592
+ type: art.type || "image/png"
3593
+ });
3594
+ }
3595
+ try {
3596
+ navigator.mediaSession.metadata = new MediaMetadata({
3597
+ title: currentMetadata.title || "Unknown",
3598
+ artist: currentMetadata.artist || "",
3599
+ album: currentMetadata.album || "",
3600
+ artwork
3601
+ });
3602
+ } catch (e) {
3603
+ api?.logger.warn("Failed to set media session metadata", e);
3604
+ }
3605
+ };
3606
+ const updatePositionState = () => {
3607
+ if (!isMediaSessionSupported() || !mergedConfig.updatePositionState) return;
3608
+ const duration = api?.getState("duration") || 0;
3609
+ const position = api?.getState("currentTime") || 0;
3610
+ const playbackRate = api?.getState("playbackRate") || 1;
3611
+ if (duration > 0 && isFinite(duration)) {
3612
+ try {
3613
+ navigator.mediaSession.setPositionState({
3614
+ duration,
3615
+ position: Math.min(position, duration),
3616
+ playbackRate
3617
+ });
3618
+ } catch (e) {
3619
+ api?.logger.debug("Failed to set position state", e);
3620
+ }
3621
+ }
3622
+ };
3623
+ const setupActionHandlers = () => {
3624
+ if (!isMediaSessionSupported()) return;
3625
+ const seekOffset = mergedConfig.seekOffset || 10;
3626
+ if (mergedConfig.enablePlayPause) {
3627
+ try {
3628
+ navigator.mediaSession.setActionHandler("play", () => {
3629
+ api?.logger.debug("Media session: play");
3630
+ api?.emit("playback:play", void 0);
3631
+ });
3632
+ navigator.mediaSession.setActionHandler("pause", () => {
3633
+ api?.logger.debug("Media session: pause");
3634
+ api?.emit("playback:pause", void 0);
3635
+ });
3636
+ navigator.mediaSession.setActionHandler("stop", () => {
3637
+ api?.logger.debug("Media session: stop");
3638
+ api?.emit("playback:pause", void 0);
3639
+ api?.emit("playback:seeking", { time: 0 });
3640
+ });
3641
+ } catch (e) {
3642
+ api?.logger.debug("Some play/pause actions not supported", e);
3643
+ }
3644
+ }
3645
+ if (mergedConfig.enableSeek) {
3646
+ try {
3647
+ navigator.mediaSession.setActionHandler("seekbackward", (details) => {
3648
+ const offset = details.seekOffset || seekOffset;
3649
+ const currentTime = api?.getState("currentTime") || 0;
3650
+ const newTime = Math.max(0, currentTime - offset);
3651
+ api?.logger.debug("Media session: seekbackward", { offset, newTime });
3652
+ api?.emit("playback:seeking", { time: newTime });
3653
+ });
3654
+ navigator.mediaSession.setActionHandler("seekforward", (details) => {
3655
+ const offset = details.seekOffset || seekOffset;
3656
+ const currentTime = api?.getState("currentTime") || 0;
3657
+ const duration = api?.getState("duration") || 0;
3658
+ const newTime = Math.min(duration, currentTime + offset);
3659
+ api?.logger.debug("Media session: seekforward", { offset, newTime });
3660
+ api?.emit("playback:seeking", { time: newTime });
3661
+ });
3662
+ navigator.mediaSession.setActionHandler("seekto", (details) => {
3663
+ if (details.seekTime !== void 0) {
3664
+ api?.logger.debug("Media session: seekto", { time: details.seekTime });
3665
+ api?.emit("playback:seeking", { time: details.seekTime });
3666
+ }
3667
+ });
3668
+ } catch (e) {
3669
+ api?.logger.debug("Some seek actions not supported", e);
3670
+ }
3671
+ }
3672
+ if (mergedConfig.enableTrackNavigation) {
3673
+ try {
3674
+ navigator.mediaSession.setActionHandler("previoustrack", () => {
3675
+ api?.logger.debug("Media session: previoustrack");
3676
+ const playlist = api?.getPlugin("playlist");
3677
+ if (playlist) {
3678
+ playlist.previous();
3679
+ } else {
3680
+ api?.emit("playback:seeking", { time: 0 });
3681
+ }
3682
+ });
3683
+ navigator.mediaSession.setActionHandler("nexttrack", () => {
3684
+ api?.logger.debug("Media session: nexttrack");
3685
+ const playlist = api?.getPlugin("playlist");
3686
+ if (playlist) {
3687
+ playlist.next();
3688
+ }
3689
+ });
3690
+ } catch (e) {
3691
+ api?.logger.debug("Track navigation not supported", e);
3692
+ }
3693
+ }
3694
+ };
3695
+ const clearActionHandlers = () => {
3696
+ if (!isMediaSessionSupported()) return;
3697
+ const actions = [
3698
+ "play",
3699
+ "pause",
3700
+ "stop",
3701
+ "seekbackward",
3702
+ "seekforward",
3703
+ "seekto",
3704
+ "previoustrack",
3705
+ "nexttrack"
3706
+ ];
3707
+ for (const action of actions) {
3708
+ try {
3709
+ navigator.mediaSession.setActionHandler(action, null);
3710
+ } catch (e) {
3711
+ }
3712
+ }
3713
+ };
3714
+ const plugin = {
3715
+ id: "media-session",
3716
+ name: "Media Session",
3717
+ version: "1.0.0",
3718
+ type: "feature",
3719
+ description: "Media Session API integration for system-level media controls",
3720
+ async init(pluginApi) {
3721
+ api = pluginApi;
3722
+ if (!isMediaSessionSupported()) {
3723
+ api.logger.info("Media Session API not supported in this browser");
3724
+ return;
3725
+ }
3726
+ api.logger.info("Media Session plugin initialized");
3727
+ setupActionHandlers();
3728
+ const unsubPlay = api.on("playback:play", () => {
3729
+ if (isMediaSessionSupported()) {
3730
+ navigator.mediaSession.playbackState = "playing";
3731
+ }
3732
+ });
3733
+ const unsubPause = api.on("playback:pause", () => {
3734
+ if (isMediaSessionSupported()) {
3735
+ navigator.mediaSession.playbackState = "paused";
3736
+ }
3737
+ });
3738
+ const unsubEnded = api.on("playback:ended", () => {
3739
+ if (isMediaSessionSupported()) {
3740
+ navigator.mediaSession.playbackState = "none";
3741
+ }
3742
+ });
3743
+ let lastPositionUpdate = 0;
3744
+ const unsubTimeUpdate = api.on("playback:timeupdate", () => {
3745
+ const now = Date.now();
3746
+ if (now - lastPositionUpdate >= 1e3) {
3747
+ lastPositionUpdate = now;
3748
+ updatePositionState();
3749
+ }
3750
+ });
3751
+ const unsubMetadata = api.on("media:loadedmetadata", () => {
3752
+ updatePositionState();
3753
+ });
3754
+ const unsubState = api.subscribeToState((event) => {
3755
+ if (event.key === "title" && typeof event.value === "string") {
3756
+ currentMetadata.title = event.value;
3757
+ updateMetadata();
3758
+ } else if (event.key === "poster" && typeof event.value === "string") {
3759
+ currentMetadata.artwork = [{ src: event.value, sizes: "512x512" }];
3760
+ updateMetadata();
3761
+ }
3762
+ });
3763
+ const unsubPlaylist = api.on("playlist:change", (payload) => {
3764
+ if (payload?.track) {
3765
+ const track = payload.track;
3766
+ currentMetadata = {
3767
+ title: track.title,
3768
+ artist: track.artist,
3769
+ album: track.album,
3770
+ artwork: track.artwork ? [{ src: track.artwork, sizes: "512x512" }] : void 0
3771
+ };
3772
+ updateMetadata();
3773
+ }
3774
+ });
3775
+ api.onDestroy(() => {
3776
+ unsubPlay();
3777
+ unsubPause();
3778
+ unsubEnded();
3779
+ unsubTimeUpdate();
3780
+ unsubMetadata();
3781
+ unsubState();
3782
+ unsubPlaylist();
3783
+ clearActionHandlers();
3784
+ });
3785
+ },
3786
+ async destroy() {
3787
+ api?.logger.info("Media Session plugin destroying");
3788
+ clearActionHandlers();
3789
+ if (isMediaSessionSupported()) {
3790
+ navigator.mediaSession.metadata = null;
3791
+ navigator.mediaSession.playbackState = "none";
3792
+ }
3793
+ api = null;
3794
+ },
3795
+ isSupported() {
3796
+ return isMediaSessionSupported();
3797
+ },
3798
+ setMetadata(metadata) {
3799
+ currentMetadata = { ...currentMetadata, ...metadata };
3800
+ updateMetadata();
3801
+ },
3802
+ setPlaybackState(state) {
3803
+ if (isMediaSessionSupported()) {
3804
+ navigator.mediaSession.playbackState = state;
3805
+ }
3806
+ },
3807
+ setPositionState(state) {
3808
+ if (isMediaSessionSupported()) {
3809
+ try {
3810
+ navigator.mediaSession.setPositionState(state);
3811
+ } catch (e) {
3812
+ api?.logger.debug("Failed to set position state", e);
3813
+ }
3814
+ }
3815
+ },
3816
+ setActionHandler(action, handler) {
3817
+ if (isMediaSessionSupported()) {
3818
+ try {
3819
+ navigator.mediaSession.setActionHandler(action, handler);
3820
+ } catch (e) {
3821
+ api?.logger.debug(`Action ${action} not supported`, e);
3822
+ }
3823
+ }
3824
+ }
3825
+ };
3826
+ return plugin;
3827
+ }
3828
+ var DEFAULT_CONFIG = {
3829
+ autoAdvance: true,
3830
+ preloadNext: true,
3831
+ persist: false,
3832
+ persistKey: "scarlett-playlist",
3833
+ shuffle: false,
3834
+ repeat: "none"
3835
+ };
3836
+ function generateId() {
3837
+ return `track-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
3838
+ }
3839
+ function shuffleArray(array) {
3840
+ const result = [...array];
3841
+ for (let i = result.length - 1; i > 0; i--) {
3842
+ const j = Math.floor(Math.random() * (i + 1));
3843
+ [result[i], result[j]] = [result[j], result[i]];
3844
+ }
3845
+ return result;
3846
+ }
3847
+ function createPlaylistPlugin(config) {
3848
+ const mergedConfig = { ...DEFAULT_CONFIG, ...config };
3849
+ let api = null;
3850
+ let tracks = mergedConfig.tracks || [];
3851
+ let currentIndex = -1;
3852
+ let shuffle = mergedConfig.shuffle || false;
3853
+ let repeat = mergedConfig.repeat || "none";
3854
+ let shuffleOrder = [];
3855
+ tracks = tracks.map((t) => ({ ...t, id: t.id || generateId() }));
3856
+ const generateShuffleOrder = () => {
3857
+ const indices = tracks.map((_, i) => i);
3858
+ shuffleOrder = shuffleArray(indices);
3859
+ if (currentIndex >= 0) {
3860
+ const currentPos = shuffleOrder.indexOf(currentIndex);
3861
+ if (currentPos > 0) {
3862
+ shuffleOrder.splice(currentPos, 1);
3863
+ shuffleOrder.unshift(currentIndex);
3864
+ }
3865
+ }
3866
+ };
3867
+ const getActualIndex = (logicalIndex) => {
3868
+ if (!shuffle || shuffleOrder.length === 0) {
3869
+ return logicalIndex;
3870
+ }
3871
+ return shuffleOrder[logicalIndex] ?? logicalIndex;
3872
+ };
3873
+ const getLogicalIndex = (actualIndex) => {
3874
+ if (!shuffle || shuffleOrder.length === 0) {
3875
+ return actualIndex;
3876
+ }
3877
+ return shuffleOrder.indexOf(actualIndex);
3878
+ };
3879
+ const hasNextTrack = () => {
3880
+ if (tracks.length === 0) return false;
3881
+ if (repeat === "one" || repeat === "all") return true;
3882
+ const logicalIndex = getLogicalIndex(currentIndex);
3883
+ return logicalIndex < tracks.length - 1;
3884
+ };
3885
+ const hasPreviousTrack = () => {
3886
+ if (tracks.length === 0) return false;
3887
+ if (repeat === "one" || repeat === "all") return true;
3888
+ const logicalIndex = getLogicalIndex(currentIndex);
3889
+ return logicalIndex > 0;
3890
+ };
3891
+ const getNextIndex = () => {
3892
+ if (tracks.length === 0) return -1;
3893
+ if (repeat === "one") {
3894
+ return currentIndex;
3895
+ }
3896
+ const logicalIndex = getLogicalIndex(currentIndex);
3897
+ let nextLogical = logicalIndex + 1;
3898
+ if (nextLogical >= tracks.length) {
3899
+ if (repeat === "all") {
3900
+ if (shuffle) {
3901
+ generateShuffleOrder();
3902
+ }
3903
+ nextLogical = 0;
3904
+ } else {
3905
+ return -1;
3906
+ }
3907
+ }
3908
+ return getActualIndex(nextLogical);
3909
+ };
3910
+ const getPreviousIndex = () => {
3911
+ if (tracks.length === 0) return -1;
3912
+ if (repeat === "one") {
3913
+ return currentIndex;
3914
+ }
3915
+ const logicalIndex = getLogicalIndex(currentIndex);
3916
+ let prevLogical = logicalIndex - 1;
3917
+ if (prevLogical < 0) {
3918
+ if (repeat === "all") {
3919
+ prevLogical = tracks.length - 1;
3920
+ } else {
3921
+ return -1;
3922
+ }
3923
+ }
3924
+ return getActualIndex(prevLogical);
3925
+ };
3926
+ const persistPlaylist = () => {
3927
+ if (!mergedConfig.persist) return;
3928
+ try {
3929
+ const data = {
3930
+ tracks,
3931
+ currentIndex,
3932
+ shuffle,
3933
+ repeat,
3934
+ shuffleOrder
3935
+ };
3936
+ localStorage.setItem(mergedConfig.persistKey, JSON.stringify(data));
3937
+ } catch (e) {
3938
+ api?.logger.warn("Failed to persist playlist", e);
3939
+ }
3940
+ };
3941
+ const loadPersistedPlaylist = () => {
3942
+ if (!mergedConfig.persist) return;
3943
+ try {
3944
+ const data = localStorage.getItem(mergedConfig.persistKey);
3945
+ if (data) {
3946
+ const parsed = JSON.parse(data);
3947
+ tracks = parsed.tracks || [];
3948
+ currentIndex = parsed.currentIndex ?? -1;
3949
+ shuffle = parsed.shuffle ?? false;
3950
+ repeat = parsed.repeat ?? "none";
3951
+ shuffleOrder = parsed.shuffleOrder || [];
3952
+ }
3953
+ } catch (e) {
3954
+ api?.logger.warn("Failed to load persisted playlist", e);
3955
+ }
3956
+ };
3957
+ const emitChange = () => {
3958
+ const track = currentIndex >= 0 ? tracks[currentIndex] : null;
3959
+ api?.emit("playlist:change", { track, index: currentIndex });
3960
+ persistPlaylist();
3961
+ };
3962
+ const setCurrentTrack = (index) => {
3963
+ if (index < 0 || index >= tracks.length) {
3964
+ api?.logger.warn("Invalid track index", { index });
3965
+ return;
3966
+ }
3967
+ const track = tracks[index];
3968
+ currentIndex = index;
3969
+ api?.logger.info("Track changed", { index, title: track.title, src: track.src });
3970
+ if (track.title) {
3971
+ api?.setState("title", track.title);
3972
+ }
3973
+ if (track.artwork) {
3974
+ api?.setState("poster", track.artwork);
3975
+ }
3976
+ api?.setState("mediaType", track.type || "audio");
3977
+ emitChange();
3978
+ };
3979
+ const plugin = {
3980
+ id: "playlist",
3981
+ name: "Playlist",
3982
+ version: "1.0.0",
3983
+ type: "feature",
3984
+ description: "Playlist management with shuffle, repeat, and gapless playback",
3985
+ async init(pluginApi) {
3986
+ api = pluginApi;
3987
+ api.logger.info("Playlist plugin initialized");
3988
+ loadPersistedPlaylist();
3989
+ if (shuffle && tracks.length > 0) {
3990
+ generateShuffleOrder();
3991
+ }
3992
+ const unsubEnded = api.on("playback:ended", () => {
3993
+ if (!mergedConfig.autoAdvance) return;
3994
+ const nextIdx = getNextIndex();
3995
+ if (nextIdx >= 0) {
3996
+ api?.logger.debug("Auto-advancing to next track", { nextIdx });
3997
+ setCurrentTrack(nextIdx);
3998
+ } else {
3999
+ api?.logger.info("Playlist ended");
4000
+ api?.emit("playlist:ended", void 0);
4001
+ }
4002
+ });
4003
+ api.onDestroy(() => {
4004
+ unsubEnded();
4005
+ persistPlaylist();
4006
+ });
4007
+ },
4008
+ async destroy() {
4009
+ api?.logger.info("Playlist plugin destroying");
4010
+ persistPlaylist();
4011
+ api = null;
4012
+ },
4013
+ add(trackOrTracks) {
4014
+ const newTracks = Array.isArray(trackOrTracks) ? trackOrTracks : [trackOrTracks];
4015
+ newTracks.forEach((track) => {
4016
+ const normalizedTrack = { ...track, id: track.id || generateId() };
4017
+ const index = tracks.length;
4018
+ tracks.push(normalizedTrack);
4019
+ api?.emit("playlist:add", { track: normalizedTrack, index });
4020
+ api?.logger.debug("Track added", { title: normalizedTrack.title, index });
4021
+ });
4022
+ if (shuffle) {
4023
+ const startIndex = tracks.length - newTracks.length;
4024
+ for (let i = startIndex; i < tracks.length; i++) {
4025
+ const insertPos = Math.floor(Math.random() * (shuffleOrder.length - getLogicalIndex(currentIndex))) + getLogicalIndex(currentIndex) + 1;
4026
+ shuffleOrder.splice(Math.min(insertPos, shuffleOrder.length), 0, i);
4027
+ }
4028
+ }
4029
+ persistPlaylist();
4030
+ },
4031
+ insert(index, track) {
4032
+ const normalizedTrack = { ...track, id: track.id || generateId() };
4033
+ const clampedIndex = Math.max(0, Math.min(index, tracks.length));
4034
+ tracks.splice(clampedIndex, 0, normalizedTrack);
4035
+ if (currentIndex >= clampedIndex) {
4036
+ currentIndex++;
4037
+ }
4038
+ if (shuffle) {
4039
+ shuffleOrder = shuffleOrder.map((i) => i >= clampedIndex ? i + 1 : i);
4040
+ const insertPos = Math.floor(Math.random() * shuffleOrder.length);
4041
+ shuffleOrder.splice(insertPos, 0, clampedIndex);
4042
+ }
4043
+ api?.emit("playlist:add", { track: normalizedTrack, index: clampedIndex });
4044
+ persistPlaylist();
4045
+ },
4046
+ remove(idOrIndex) {
4047
+ let index;
4048
+ if (typeof idOrIndex === "string") {
4049
+ index = tracks.findIndex((t) => t.id === idOrIndex);
4050
+ if (index === -1) {
4051
+ api?.logger.warn("Track not found", { id: idOrIndex });
4052
+ return;
4053
+ }
4054
+ } else {
4055
+ index = idOrIndex;
4056
+ }
4057
+ if (index < 0 || index >= tracks.length) {
4058
+ api?.logger.warn("Invalid track index", { index });
4059
+ return;
4060
+ }
4061
+ const [removedTrack] = tracks.splice(index, 1);
4062
+ if (index < currentIndex) {
4063
+ currentIndex--;
4064
+ } else if (index === currentIndex) {
4065
+ if (currentIndex >= tracks.length) {
4066
+ currentIndex = tracks.length - 1;
4067
+ }
4068
+ emitChange();
4069
+ }
4070
+ if (shuffle) {
4071
+ shuffleOrder = shuffleOrder.filter((i) => i !== index).map((i) => i > index ? i - 1 : i);
4072
+ }
4073
+ api?.emit("playlist:remove", { track: removedTrack, index });
4074
+ persistPlaylist();
4075
+ },
4076
+ clear() {
4077
+ tracks = [];
4078
+ currentIndex = -1;
4079
+ shuffleOrder = [];
4080
+ api?.emit("playlist:clear", void 0);
4081
+ emitChange();
4082
+ },
4083
+ play(idOrIndex) {
4084
+ let index;
4085
+ if (idOrIndex === void 0) {
4086
+ index = currentIndex >= 0 ? currentIndex : shuffle ? getActualIndex(0) : 0;
4087
+ } else if (typeof idOrIndex === "string") {
4088
+ index = tracks.findIndex((t) => t.id === idOrIndex);
4089
+ if (index === -1) {
4090
+ api?.logger.warn("Track not found", { id: idOrIndex });
4091
+ return;
4092
+ }
4093
+ } else {
4094
+ index = idOrIndex;
4095
+ }
4096
+ if (tracks.length === 0) {
4097
+ api?.logger.warn("Playlist is empty");
4098
+ return;
4099
+ }
4100
+ setCurrentTrack(index);
4101
+ },
4102
+ next() {
4103
+ const nextIdx = getNextIndex();
4104
+ if (nextIdx >= 0) {
4105
+ setCurrentTrack(nextIdx);
4106
+ } else {
4107
+ api?.logger.info("No next track");
4108
+ }
4109
+ },
4110
+ previous() {
4111
+ const currentTime = api?.getState("currentTime") || 0;
4112
+ if (currentTime > 3) {
4113
+ api?.emit("playback:seeking", { time: 0 });
4114
+ return;
4115
+ }
4116
+ const prevIdx = getPreviousIndex();
4117
+ if (prevIdx >= 0) {
4118
+ setCurrentTrack(prevIdx);
4119
+ } else {
4120
+ api?.logger.info("No previous track");
4121
+ }
4122
+ },
4123
+ toggleShuffle() {
4124
+ this.setShuffle(!shuffle);
4125
+ },
4126
+ setShuffle(enabled) {
4127
+ shuffle = enabled;
4128
+ if (enabled) {
4129
+ generateShuffleOrder();
4130
+ } else {
4131
+ shuffleOrder = [];
4132
+ }
4133
+ api?.emit("playlist:shuffle", { enabled });
4134
+ api?.logger.info("Shuffle mode", { enabled });
4135
+ persistPlaylist();
4136
+ },
4137
+ cycleRepeat() {
4138
+ const modes = ["none", "all", "one"];
4139
+ const currentIdx = modes.indexOf(repeat);
4140
+ const nextIdx = (currentIdx + 1) % modes.length;
4141
+ this.setRepeat(modes[nextIdx]);
4142
+ },
4143
+ setRepeat(mode) {
4144
+ repeat = mode;
4145
+ api?.emit("playlist:repeat", { mode });
4146
+ api?.logger.info("Repeat mode", { mode });
4147
+ persistPlaylist();
4148
+ },
4149
+ move(fromIndex, toIndex) {
4150
+ if (fromIndex < 0 || fromIndex >= tracks.length) return;
4151
+ if (toIndex < 0 || toIndex >= tracks.length) return;
4152
+ if (fromIndex === toIndex) return;
4153
+ const [track] = tracks.splice(fromIndex, 1);
4154
+ tracks.splice(toIndex, 0, track);
4155
+ if (currentIndex === fromIndex) {
4156
+ currentIndex = toIndex;
4157
+ } else if (fromIndex < currentIndex && toIndex >= currentIndex) {
4158
+ currentIndex--;
4159
+ } else if (fromIndex > currentIndex && toIndex <= currentIndex) {
4160
+ currentIndex++;
4161
+ }
4162
+ if (shuffle) {
4163
+ generateShuffleOrder();
4164
+ }
4165
+ api?.emit("playlist:reorder", { tracks: [...tracks] });
4166
+ persistPlaylist();
4167
+ },
4168
+ getState() {
4169
+ return {
4170
+ tracks: [...tracks],
4171
+ currentIndex,
4172
+ currentTrack: currentIndex >= 0 ? tracks[currentIndex] : null,
4173
+ shuffle,
4174
+ repeat,
4175
+ shuffleOrder: [...shuffleOrder],
4176
+ hasNext: hasNextTrack(),
4177
+ hasPrevious: hasPreviousTrack()
4178
+ };
4179
+ },
4180
+ getTracks() {
4181
+ return [...tracks];
4182
+ },
4183
+ getCurrentTrack() {
4184
+ return currentIndex >= 0 ? tracks[currentIndex] : null;
4185
+ },
4186
+ getTrack(id) {
4187
+ return tracks.find((t) => t.id === id) || null;
4188
+ }
4189
+ };
4190
+ return plugin;
4191
+ }
4192
+ function parseAudioDataAttributes(element) {
4193
+ const dataset = element.dataset;
4194
+ const config = {
4195
+ src: dataset.src,
4196
+ autoplay: dataset.autoplay !== void 0 && dataset.autoplay !== "false",
4197
+ muted: dataset.muted !== void 0 && dataset.muted !== "false",
4198
+ loop: dataset.loop !== void 0 && dataset.loop !== "false",
4199
+ compact: dataset.compact !== void 0 && dataset.compact !== "false",
4200
+ brandColor: dataset.brandColor,
4201
+ primaryColor: dataset.primaryColor,
4202
+ backgroundColor: dataset.backgroundColor,
4203
+ title: dataset.title,
4204
+ artist: dataset.artist,
4205
+ album: dataset.album,
4206
+ artwork: dataset.artwork || dataset.poster
4207
+ };
4208
+ if (dataset.playlist) {
4209
+ try {
4210
+ config.playlist = JSON.parse(dataset.playlist);
4211
+ } catch (e) {
4212
+ console.warn("[Scarlett Audio] Invalid playlist JSON");
4213
+ }
4214
+ }
4215
+ return config;
4216
+ }
4217
+ async function createAudioPlayer(container, config) {
4218
+ if (!config.src && !config.playlist?.length) {
4219
+ console.error("[Scarlett Audio] No source URL or playlist provided");
4220
+ return null;
4221
+ }
4222
+ try {
4223
+ container.style.position = container.style.position || "relative";
4224
+ if (config.compact) {
4225
+ container.style.height = container.style.height || "64px";
4226
+ } else {
4227
+ container.style.height = container.style.height || "120px";
4228
+ }
4229
+ container.style.width = container.style.width || "100%";
4230
+ const plugins = [
4231
+ createHLSPlugin(),
4232
+ createMediaSessionPlugin({
4233
+ title: config.title || config.playlist?.[0]?.title,
4234
+ artist: config.artist || config.playlist?.[0]?.artist,
4235
+ album: config.album,
4236
+ artwork: config.artwork || config.playlist?.[0]?.artwork
4237
+ })
4238
+ ];
4239
+ if (config.playlist?.length) {
4240
+ plugins.push(createPlaylistPlugin({
4241
+ items: config.playlist.map((item, index) => ({
4242
+ id: `track-${index}`,
4243
+ src: item.src,
4244
+ title: item.title,
4245
+ artist: item.artist,
4246
+ poster: item.artwork,
4247
+ duration: item.duration
4248
+ }))
4249
+ }));
4250
+ }
4251
+ plugins.push(createAudioUIPlugin({
4252
+ layout: config.compact ? "compact" : "full",
4253
+ theme: {
4254
+ primary: config.brandColor,
4255
+ text: config.primaryColor,
4256
+ background: config.backgroundColor
4257
+ }
4258
+ }));
4259
+ const player = await createPlayer({
4260
+ container,
4261
+ src: config.src || config.playlist?.[0]?.src || "",
4262
+ autoplay: config.autoplay || false,
4263
+ muted: config.muted || false,
4264
+ loop: config.loop || false,
4265
+ plugins
4266
+ });
4267
+ return player;
4268
+ } catch (error) {
4269
+ console.error("[Scarlett Audio] Failed to create player:", error);
4270
+ return null;
4271
+ }
4272
+ }
4273
+ async function initElement(element) {
4274
+ if (element.hasAttribute("data-scarlett-initialized")) {
4275
+ return null;
4276
+ }
4277
+ const config = parseAudioDataAttributes(element);
4278
+ element.setAttribute("data-scarlett-initialized", "true");
4279
+ const player = await createAudioPlayer(element, config);
4280
+ if (!player) {
4281
+ element.removeAttribute("data-scarlett-initialized");
4282
+ }
4283
+ return player;
4284
+ }
4285
+ const AUDIO_SELECTORS = [
4286
+ "[data-scarlett-audio]",
4287
+ "[data-audio-player]",
4288
+ ".scarlett-audio"
4289
+ ];
4290
+ async function initAll() {
4291
+ const selector = AUDIO_SELECTORS.join(", ");
4292
+ const elements = document.querySelectorAll(selector);
4293
+ const promises = Array.from(elements).map((element) => initElement(element));
4294
+ await Promise.all(promises);
4295
+ if (elements.length > 0) {
4296
+ console.log(`[Scarlett Audio] Initialized ${elements.length} player(s) (light build)`);
4297
+ }
4298
+ }
4299
+ async function create(options) {
4300
+ let container = null;
4301
+ if (typeof options.container === "string") {
4302
+ container = document.querySelector(options.container);
4303
+ if (!container) {
4304
+ console.error(`[Scarlett Audio] Container not found: ${options.container}`);
4305
+ return null;
4306
+ }
4307
+ } else {
4308
+ container = options.container;
4309
+ }
4310
+ return createAudioPlayer(container, options);
4311
+ }
4312
+ const VERSION = "0.1.2-audio-light";
4313
+ const ScarlettAudioAPI = {
4314
+ create,
4315
+ initAll,
4316
+ version: VERSION
4317
+ };
4318
+ if (typeof window !== "undefined") {
4319
+ window.ScarlettAudio = ScarlettAudioAPI;
4320
+ }
4321
+ if (typeof document !== "undefined") {
4322
+ if (document.readyState === "loading") {
4323
+ document.addEventListener("DOMContentLoaded", () => {
4324
+ initAll();
4325
+ });
4326
+ } else {
4327
+ initAll();
4328
+ }
4329
+ }
4330
+ export {
4331
+ create,
4332
+ createAudioPlayer,
4333
+ ScarlettAudioAPI as default,
4334
+ initAll,
4335
+ initElement,
4336
+ parseAudioDataAttributes
4337
+ };
4338
+ //# sourceMappingURL=embed.audio.light.js.map