@scarlett-player/embed 0.1.2 → 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,4611 @@
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 = {
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, ...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 styles = `
2863
+ /* ============================================
2864
+ Container & Base
2865
+ ============================================ */
2866
+ .sp-container {
2867
+ position: relative;
2868
+ width: 100%;
2869
+ height: 100%;
2870
+ background: #000;
2871
+ overflow: hidden;
2872
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
2873
+ }
2874
+
2875
+ .sp-container video {
2876
+ width: 100%;
2877
+ height: 100%;
2878
+ display: block;
2879
+ object-fit: contain;
2880
+ }
2881
+
2882
+ .sp-container:focus {
2883
+ outline: none;
2884
+ }
2885
+
2886
+ /* ============================================
2887
+ Gradient Overlay
2888
+ ============================================ */
2889
+ .sp-gradient {
2890
+ position: absolute;
2891
+ bottom: 0;
2892
+ left: 0;
2893
+ right: 0;
2894
+ height: 160px;
2895
+ background: linear-gradient(
2896
+ to top,
2897
+ rgba(0, 0, 0, 0.8) 0%,
2898
+ rgba(0, 0, 0, 0.4) 50%,
2899
+ transparent 100%
2900
+ );
2901
+ pointer-events: none;
2902
+ opacity: 0;
2903
+ transition: opacity 0.25s ease;
2904
+ z-index: 5;
2905
+ }
2906
+
2907
+ .sp-gradient--visible {
2908
+ opacity: 1;
2909
+ }
2910
+
2911
+ /* ============================================
2912
+ Controls Container
2913
+ ============================================ */
2914
+ .sp-controls {
2915
+ position: absolute;
2916
+ bottom: 0;
2917
+ left: 0;
2918
+ right: 0;
2919
+ display: flex;
2920
+ align-items: center;
2921
+ padding: 0 12px 12px;
2922
+ gap: 4px;
2923
+ opacity: 0;
2924
+ transform: translateY(4px);
2925
+ transition: opacity 0.25s ease, transform 0.25s ease;
2926
+ z-index: 10;
2927
+ }
2928
+
2929
+ .sp-controls--visible {
2930
+ opacity: 1;
2931
+ transform: translateY(0);
2932
+ }
2933
+
2934
+ .sp-controls--hidden {
2935
+ opacity: 0;
2936
+ transform: translateY(4px);
2937
+ pointer-events: none;
2938
+ }
2939
+
2940
+ /* ============================================
2941
+ Progress Bar (Above Controls)
2942
+ ============================================ */
2943
+ .sp-progress-wrapper {
2944
+ position: absolute;
2945
+ bottom: 48px;
2946
+ left: 12px;
2947
+ right: 12px;
2948
+ height: 20px;
2949
+ display: flex;
2950
+ align-items: center;
2951
+ cursor: pointer;
2952
+ z-index: 10;
2953
+ opacity: 0;
2954
+ transition: opacity 0.25s ease;
2955
+ }
2956
+
2957
+ .sp-progress-wrapper--visible {
2958
+ opacity: 1;
2959
+ }
2960
+
2961
+ .sp-progress {
2962
+ position: relative;
2963
+ width: 100%;
2964
+ height: 3px;
2965
+ background: rgba(255, 255, 255, 0.3);
2966
+ border-radius: 1.5px;
2967
+ transition: height 0.15s ease;
2968
+ }
2969
+
2970
+ .sp-progress-wrapper:hover .sp-progress,
2971
+ .sp-progress--dragging {
2972
+ height: 5px;
2973
+ }
2974
+
2975
+ .sp-progress__track {
2976
+ position: absolute;
2977
+ top: 0;
2978
+ left: 0;
2979
+ right: 0;
2980
+ bottom: 0;
2981
+ border-radius: inherit;
2982
+ overflow: hidden;
2983
+ }
2984
+
2985
+ .sp-progress__buffered {
2986
+ position: absolute;
2987
+ top: 0;
2988
+ left: 0;
2989
+ height: 100%;
2990
+ background: rgba(255, 255, 255, 0.4);
2991
+ border-radius: inherit;
2992
+ transition: width 0.1s linear;
2993
+ }
2994
+
2995
+ .sp-progress__filled {
2996
+ position: absolute;
2997
+ top: 0;
2998
+ left: 0;
2999
+ height: 100%;
3000
+ background: var(--sp-accent, #e50914);
3001
+ border-radius: inherit;
3002
+ }
3003
+
3004
+ .sp-progress__handle {
3005
+ position: absolute;
3006
+ top: 50%;
3007
+ width: 14px;
3008
+ height: 14px;
3009
+ background: var(--sp-accent, #e50914);
3010
+ border-radius: 50%;
3011
+ transform: translate(-50%, -50%) scale(0);
3012
+ transition: transform 0.15s ease;
3013
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
3014
+ }
3015
+
3016
+ .sp-progress-wrapper:hover .sp-progress__handle,
3017
+ .sp-progress--dragging .sp-progress__handle {
3018
+ transform: translate(-50%, -50%) scale(1);
3019
+ }
3020
+
3021
+ /* Progress Tooltip */
3022
+ .sp-progress__tooltip {
3023
+ position: absolute;
3024
+ bottom: calc(100% + 8px);
3025
+ padding: 6px 10px;
3026
+ background: rgba(20, 20, 20, 0.95);
3027
+ color: #fff;
3028
+ font-size: 12px;
3029
+ font-weight: 500;
3030
+ font-variant-numeric: tabular-nums;
3031
+ border-radius: 4px;
3032
+ white-space: nowrap;
3033
+ transform: translateX(-50%);
3034
+ pointer-events: none;
3035
+ opacity: 0;
3036
+ transition: opacity 0.15s ease;
3037
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
3038
+ }
3039
+
3040
+ .sp-progress-wrapper:hover .sp-progress__tooltip {
3041
+ opacity: 1;
3042
+ }
3043
+
3044
+ /* ============================================
3045
+ Control Buttons
3046
+ ============================================ */
3047
+ .sp-control {
3048
+ background: none;
3049
+ border: none;
3050
+ color: rgba(255, 255, 255, 0.9);
3051
+ cursor: pointer;
3052
+ padding: 8px;
3053
+ display: flex;
3054
+ align-items: center;
3055
+ justify-content: center;
3056
+ border-radius: 4px;
3057
+ transition: color 0.15s ease, transform 0.15s ease, background 0.15s ease;
3058
+ flex-shrink: 0;
3059
+ }
3060
+
3061
+ .sp-control:hover {
3062
+ color: #fff;
3063
+ background: rgba(255, 255, 255, 0.1);
3064
+ }
3065
+
3066
+ .sp-control:active {
3067
+ transform: scale(0.92);
3068
+ }
3069
+
3070
+ .sp-control:focus-visible {
3071
+ outline: 2px solid var(--sp-accent, #e50914);
3072
+ outline-offset: 2px;
3073
+ }
3074
+
3075
+ .sp-control:disabled {
3076
+ opacity: 0.4;
3077
+ cursor: not-allowed;
3078
+ transform: none;
3079
+ }
3080
+
3081
+ .sp-control:disabled:hover {
3082
+ background: none;
3083
+ }
3084
+
3085
+ .sp-control svg {
3086
+ width: 24px;
3087
+ height: 24px;
3088
+ fill: currentColor;
3089
+ display: block;
3090
+ }
3091
+
3092
+ .sp-control--small svg {
3093
+ width: 20px;
3094
+ height: 20px;
3095
+ }
3096
+
3097
+ /* ============================================
3098
+ Spacer
3099
+ ============================================ */
3100
+ .sp-spacer {
3101
+ flex: 1;
3102
+ min-width: 0;
3103
+ }
3104
+
3105
+ /* ============================================
3106
+ Time Display
3107
+ ============================================ */
3108
+ .sp-time {
3109
+ font-size: 13px;
3110
+ font-variant-numeric: tabular-nums;
3111
+ color: rgba(255, 255, 255, 0.9);
3112
+ white-space: nowrap;
3113
+ padding: 0 4px;
3114
+ letter-spacing: 0.02em;
3115
+ }
3116
+
3117
+ /* ============================================
3118
+ Volume Control
3119
+ ============================================ */
3120
+ .sp-volume {
3121
+ display: flex;
3122
+ align-items: center;
3123
+ position: relative;
3124
+ }
3125
+
3126
+ .sp-volume__slider-wrap {
3127
+ width: 0;
3128
+ overflow: hidden;
3129
+ transition: width 0.2s ease;
3130
+ }
3131
+
3132
+ .sp-volume:hover .sp-volume__slider-wrap,
3133
+ .sp-volume:focus-within .sp-volume__slider-wrap {
3134
+ width: 64px;
3135
+ }
3136
+
3137
+ .sp-volume__slider {
3138
+ width: 64px;
3139
+ height: 3px;
3140
+ background: rgba(255, 255, 255, 0.3);
3141
+ border-radius: 1.5px;
3142
+ cursor: pointer;
3143
+ position: relative;
3144
+ margin: 0 8px 0 4px;
3145
+ }
3146
+
3147
+ .sp-volume__level {
3148
+ position: absolute;
3149
+ top: 0;
3150
+ left: 0;
3151
+ height: 100%;
3152
+ background: #fff;
3153
+ border-radius: inherit;
3154
+ transition: width 0.1s ease;
3155
+ }
3156
+
3157
+ /* ============================================
3158
+ Live Indicator
3159
+ ============================================ */
3160
+ .sp-live {
3161
+ display: flex;
3162
+ align-items: center;
3163
+ gap: 6px;
3164
+ font-size: 11px;
3165
+ font-weight: 600;
3166
+ text-transform: uppercase;
3167
+ letter-spacing: 0.05em;
3168
+ color: var(--sp-accent, #e50914);
3169
+ cursor: pointer;
3170
+ padding: 6px 10px;
3171
+ border-radius: 4px;
3172
+ transition: background 0.15s ease, opacity 0.15s ease;
3173
+ }
3174
+
3175
+ .sp-live:hover {
3176
+ background: rgba(255, 255, 255, 0.1);
3177
+ }
3178
+
3179
+ .sp-live__dot {
3180
+ width: 8px;
3181
+ height: 8px;
3182
+ background: currentColor;
3183
+ border-radius: 50%;
3184
+ animation: sp-pulse 2s ease-in-out infinite;
3185
+ }
3186
+
3187
+ .sp-live--behind {
3188
+ opacity: 0.6;
3189
+ }
3190
+
3191
+ .sp-live--behind .sp-live__dot {
3192
+ animation: none;
3193
+ }
3194
+
3195
+ @keyframes sp-pulse {
3196
+ 0%, 100% { opacity: 1; }
3197
+ 50% { opacity: 0.4; }
3198
+ }
3199
+
3200
+ /* ============================================
3201
+ Quality / Settings Menu
3202
+ ============================================ */
3203
+ .sp-quality {
3204
+ position: relative;
3205
+ }
3206
+
3207
+ .sp-quality__btn {
3208
+ display: flex;
3209
+ align-items: center;
3210
+ gap: 4px;
3211
+ }
3212
+
3213
+ .sp-quality__label {
3214
+ font-size: 12px;
3215
+ font-weight: 500;
3216
+ opacity: 0.9;
3217
+ }
3218
+
3219
+ .sp-quality-menu {
3220
+ position: absolute;
3221
+ bottom: calc(100% + 8px);
3222
+ right: 0;
3223
+ background: rgba(20, 20, 20, 0.95);
3224
+ backdrop-filter: blur(8px);
3225
+ -webkit-backdrop-filter: blur(8px);
3226
+ border-radius: 8px;
3227
+ padding: 8px 0;
3228
+ min-width: 150px;
3229
+ box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
3230
+ opacity: 0;
3231
+ visibility: hidden;
3232
+ transform: translateY(8px);
3233
+ transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
3234
+ z-index: 20;
3235
+ }
3236
+
3237
+ .sp-quality-menu--open {
3238
+ opacity: 1;
3239
+ visibility: visible;
3240
+ transform: translateY(0);
3241
+ }
3242
+
3243
+ .sp-quality-menu__item {
3244
+ display: flex;
3245
+ align-items: center;
3246
+ justify-content: space-between;
3247
+ padding: 10px 16px;
3248
+ font-size: 13px;
3249
+ color: rgba(255, 255, 255, 0.8);
3250
+ cursor: pointer;
3251
+ transition: background 0.1s ease, color 0.1s ease;
3252
+ }
3253
+
3254
+ .sp-quality-menu__item:hover {
3255
+ background: rgba(255, 255, 255, 0.1);
3256
+ color: #fff;
3257
+ }
3258
+
3259
+ .sp-quality-menu__item--active {
3260
+ color: var(--sp-accent, #e50914);
3261
+ }
3262
+
3263
+ .sp-quality-menu__check {
3264
+ width: 16px;
3265
+ height: 16px;
3266
+ fill: currentColor;
3267
+ margin-left: 8px;
3268
+ opacity: 0;
3269
+ }
3270
+
3271
+ .sp-quality-menu__item--active .sp-quality-menu__check {
3272
+ opacity: 1;
3273
+ }
3274
+
3275
+ /* ============================================
3276
+ Cast Button States
3277
+ ============================================ */
3278
+ .sp-cast--active {
3279
+ color: var(--sp-accent, #e50914);
3280
+ }
3281
+
3282
+ .sp-cast--unavailable {
3283
+ opacity: 0.4;
3284
+ }
3285
+
3286
+ /* ============================================
3287
+ Buffering Indicator
3288
+ ============================================ */
3289
+ .sp-buffering {
3290
+ position: absolute;
3291
+ top: 50%;
3292
+ left: 50%;
3293
+ transform: translate(-50%, -50%);
3294
+ z-index: 15;
3295
+ pointer-events: none;
3296
+ opacity: 0;
3297
+ transition: opacity 0.2s ease;
3298
+ }
3299
+
3300
+ .sp-buffering--visible {
3301
+ opacity: 1;
3302
+ }
3303
+
3304
+ .sp-buffering svg {
3305
+ width: 48px;
3306
+ height: 48px;
3307
+ fill: rgba(255, 255, 255, 0.9);
3308
+ filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
3309
+ }
3310
+
3311
+ @keyframes sp-spin {
3312
+ from { transform: rotate(0deg); }
3313
+ to { transform: rotate(360deg); }
3314
+ }
3315
+
3316
+ .sp-spin {
3317
+ animation: sp-spin 0.8s linear infinite;
3318
+ }
3319
+
3320
+ /* ============================================
3321
+ Reduced Motion
3322
+ ============================================ */
3323
+ @media (prefers-reduced-motion: reduce) {
3324
+ .sp-gradient,
3325
+ .sp-controls,
3326
+ .sp-progress-wrapper,
3327
+ .sp-progress,
3328
+ .sp-progress__handle,
3329
+ .sp-progress__tooltip,
3330
+ .sp-control,
3331
+ .sp-volume__slider-wrap,
3332
+ .sp-quality-menu,
3333
+ .sp-buffering {
3334
+ transition: none;
3335
+ }
3336
+
3337
+ .sp-live__dot,
3338
+ .sp-spin {
3339
+ animation: none;
3340
+ }
3341
+ }
3342
+
3343
+ /* ============================================
3344
+ CSS Custom Properties (Theming)
3345
+ ============================================ */
3346
+ :root {
3347
+ --sp-accent: #e50914;
3348
+ --sp-color: #fff;
3349
+ --sp-bg: rgba(0, 0, 0, 0.8);
3350
+ --sp-control-height: 48px;
3351
+ --sp-icon-size: 24px;
3352
+ }
3353
+ `;
3354
+ var icons = {
3355
+ play: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>`,
3356
+ pause: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z"/></svg>`,
3357
+ replay: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"/></svg>`,
3358
+ volumeHigh: `<svg viewBox="0 0 24 24" fill="currentColor"><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>`,
3359
+ volumeLow: `<svg viewBox="0 0 24 24" fill="currentColor"><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.02z"/></svg>`,
3360
+ volumeMute: `<svg viewBox="0 0 24 24" fill="currentColor"><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>`,
3361
+ fullscreen: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/></svg>`,
3362
+ exitFullscreen: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/></svg>`,
3363
+ pip: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M19 7h-8v6h8V7zm2-4H3c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H3V5h18v14z"/></svg>`,
3364
+ settings: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M19.14 12.94c.04-.31.06-.63.06-.94 0-.31-.02-.63-.06-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.04.31-.06.63-.06.94s.02.63.06.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z"/></svg>`,
3365
+ chromecast: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M1 18v3h3c0-1.66-1.34-3-3-3zm0-4v2c2.76 0 5 2.24 5 5h2c0-3.87-3.13-7-7-7zm0-4v2c4.97 0 9 4.03 9 9h2c0-6.08-4.93-11-11-11zm20-7H3c-1.1 0-2 .9-2 2v3h2V5h18v14h-7v2h7c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/></svg>`,
3366
+ chromecastConnected: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M1 18v3h3c0-1.66-1.34-3-3-3zm0-4v2c2.76 0 5 2.24 5 5h2c0-3.87-3.13-7-7-7zm18-7H5v1.63c3.96 1.28 7.09 4.41 8.37 8.37H19V7zM1 10v2c4.97 0 9 4.03 9 9h2c0-6.08-4.93-11-11-11zm20-7H3c-1.1 0-2 .9-2 2v3h2V5h18v14h-7v2h7c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/></svg>`,
3367
+ airplay: `<svg viewBox="0 0 24 24" fill="currentColor"><path d="M6 22h12l-6-6-6 6zM21 3H3c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h4v-2H3V5h18v12h-4v2h4c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/></svg>`,
3368
+ spinner: `<svg viewBox="0 0 24 24" fill="currentColor" class="sp-spin"><path d="M12 4V2A10 10 0 0 0 2 12h2a8 8 0 0 1 8-8z"/></svg>`
3369
+ };
3370
+ function createElement(tag, attrs, children) {
3371
+ const el = document.createElement(tag);
3372
+ if (attrs) {
3373
+ for (const [key, value] of Object.entries(attrs)) {
3374
+ if (key === "className") {
3375
+ el.className = value;
3376
+ } else {
3377
+ el.setAttribute(key, value);
3378
+ }
3379
+ }
3380
+ }
3381
+ return el;
3382
+ }
3383
+ function createButton(className, label, icon) {
3384
+ const btn = createElement("button", {
3385
+ className: `sp-control ${className}`,
3386
+ "aria-label": label,
3387
+ type: "button"
3388
+ });
3389
+ btn.innerHTML = icon;
3390
+ return btn;
3391
+ }
3392
+ function getVideo(container) {
3393
+ return container.querySelector("video");
3394
+ }
3395
+ function formatTime(seconds) {
3396
+ if (!isFinite(seconds) || isNaN(seconds)) {
3397
+ return "0:00";
3398
+ }
3399
+ const absSeconds = Math.abs(seconds);
3400
+ const h = Math.floor(absSeconds / 3600);
3401
+ const m = Math.floor(absSeconds % 3600 / 60);
3402
+ const s = Math.floor(absSeconds % 60);
3403
+ const sign = seconds < 0 ? "-" : "";
3404
+ if (h > 0) {
3405
+ return `${sign}${h}:${pad(m)}:${pad(s)}`;
3406
+ }
3407
+ return `${sign}${m}:${pad(s)}`;
3408
+ }
3409
+ function pad(n) {
3410
+ return n < 10 ? `0${n}` : `${n}`;
3411
+ }
3412
+ function formatLiveTime(behindLive) {
3413
+ if (behindLive <= 0) {
3414
+ return "LIVE";
3415
+ }
3416
+ return `-${formatTime(behindLive)}`;
3417
+ }
3418
+ var PlayButton = class {
3419
+ constructor(api) {
3420
+ this.clickHandler = () => {
3421
+ this.toggle();
3422
+ };
3423
+ this.api = api;
3424
+ this.el = createButton("sp-play", "Play", icons.play);
3425
+ this.el.addEventListener("click", this.clickHandler);
3426
+ }
3427
+ render() {
3428
+ return this.el;
3429
+ }
3430
+ update() {
3431
+ const playing = this.api.getState("playing");
3432
+ const ended = this.api.getState("ended");
3433
+ let icon;
3434
+ let label;
3435
+ if (ended) {
3436
+ icon = icons.replay;
3437
+ label = "Replay";
3438
+ } else if (playing) {
3439
+ icon = icons.pause;
3440
+ label = "Pause";
3441
+ } else {
3442
+ icon = icons.play;
3443
+ label = "Play";
3444
+ }
3445
+ this.el.innerHTML = icon;
3446
+ this.el.setAttribute("aria-label", label);
3447
+ }
3448
+ toggle() {
3449
+ const video = getVideo(this.api.container);
3450
+ if (!video) return;
3451
+ const ended = this.api.getState("ended");
3452
+ const playing = this.api.getState("playing");
3453
+ if (ended) {
3454
+ video.currentTime = 0;
3455
+ video.play().catch(() => {
3456
+ });
3457
+ } else if (playing) {
3458
+ video.pause();
3459
+ } else {
3460
+ video.play().catch(() => {
3461
+ });
3462
+ }
3463
+ }
3464
+ destroy() {
3465
+ this.el.removeEventListener("click", this.clickHandler);
3466
+ this.el.remove();
3467
+ }
3468
+ };
3469
+ var ProgressBar = class {
3470
+ constructor(api) {
3471
+ this.isDragging = false;
3472
+ this.lastSeekTime = 0;
3473
+ this.seekThrottleMs = 100;
3474
+ this.wasPlayingBeforeDrag = false;
3475
+ this.onMouseDown = (e) => {
3476
+ e.preventDefault();
3477
+ const video = getVideo(this.api.container);
3478
+ this.wasPlayingBeforeDrag = video ? !video.paused : false;
3479
+ this.isDragging = true;
3480
+ this.el.classList.add("sp-progress--dragging");
3481
+ this.lastSeekTime = 0;
3482
+ this.seek(e.clientX, true);
3483
+ };
3484
+ this.onDocMouseMove = (e) => {
3485
+ if (this.isDragging) {
3486
+ this.seek(e.clientX);
3487
+ this.updateVisualPosition(e.clientX);
3488
+ }
3489
+ };
3490
+ this.onMouseUp = (e) => {
3491
+ if (this.isDragging) {
3492
+ this.seek(e.clientX, true);
3493
+ this.isDragging = false;
3494
+ this.el.classList.remove("sp-progress--dragging");
3495
+ if (this.wasPlayingBeforeDrag) {
3496
+ const video = getVideo(this.api.container);
3497
+ if (video && video.paused) {
3498
+ const resumePlayback = () => {
3499
+ video.removeEventListener("seeked", resumePlayback);
3500
+ video.play().catch(() => {
3501
+ });
3502
+ };
3503
+ video.addEventListener("seeked", resumePlayback);
3504
+ }
3505
+ }
3506
+ }
3507
+ };
3508
+ this.onMouseMove = (e) => {
3509
+ this.updateTooltip(e.clientX);
3510
+ };
3511
+ this.onMouseLeave = () => {
3512
+ if (!this.isDragging) {
3513
+ this.tooltip.style.opacity = "0";
3514
+ }
3515
+ };
3516
+ this.onKeyDown = (e) => {
3517
+ const video = getVideo(this.api.container);
3518
+ if (!video) return;
3519
+ const step = 5;
3520
+ const duration = this.api.getState("duration") || 0;
3521
+ switch (e.key) {
3522
+ case "ArrowLeft":
3523
+ e.preventDefault();
3524
+ video.currentTime = Math.max(0, video.currentTime - step);
3525
+ break;
3526
+ case "ArrowRight":
3527
+ e.preventDefault();
3528
+ video.currentTime = Math.min(duration, video.currentTime + step);
3529
+ break;
3530
+ case "Home":
3531
+ e.preventDefault();
3532
+ video.currentTime = 0;
3533
+ break;
3534
+ case "End":
3535
+ e.preventDefault();
3536
+ video.currentTime = duration;
3537
+ break;
3538
+ }
3539
+ };
3540
+ this.api = api;
3541
+ this.wrapper = createElement("div", { className: "sp-progress-wrapper" });
3542
+ this.el = createElement("div", { className: "sp-progress" });
3543
+ const track = createElement("div", { className: "sp-progress__track" });
3544
+ this.buffered = createElement("div", { className: "sp-progress__buffered" });
3545
+ this.filled = createElement("div", { className: "sp-progress__filled" });
3546
+ this.handle = createElement("div", { className: "sp-progress__handle" });
3547
+ this.tooltip = createElement("div", { className: "sp-progress__tooltip" });
3548
+ this.tooltip.textContent = "0:00";
3549
+ track.appendChild(this.buffered);
3550
+ track.appendChild(this.filled);
3551
+ track.appendChild(this.handle);
3552
+ this.el.appendChild(track);
3553
+ this.el.appendChild(this.tooltip);
3554
+ this.wrapper.appendChild(this.el);
3555
+ this.el.setAttribute("role", "slider");
3556
+ this.el.setAttribute("aria-label", "Seek");
3557
+ this.el.setAttribute("aria-valuemin", "0");
3558
+ this.el.setAttribute("tabindex", "0");
3559
+ this.wrapper.addEventListener("mousedown", this.onMouseDown);
3560
+ this.wrapper.addEventListener("mousemove", this.onMouseMove);
3561
+ this.wrapper.addEventListener("mouseleave", this.onMouseLeave);
3562
+ this.el.addEventListener("keydown", this.onKeyDown);
3563
+ document.addEventListener("mousemove", this.onDocMouseMove);
3564
+ document.addEventListener("mouseup", this.onMouseUp);
3565
+ }
3566
+ render() {
3567
+ return this.wrapper;
3568
+ }
3569
+ /** Show the progress bar */
3570
+ show() {
3571
+ this.wrapper.classList.add("sp-progress-wrapper--visible");
3572
+ }
3573
+ /** Hide the progress bar */
3574
+ hide() {
3575
+ this.wrapper.classList.remove("sp-progress-wrapper--visible");
3576
+ }
3577
+ update() {
3578
+ const currentTime = this.api.getState("currentTime") || 0;
3579
+ const duration = this.api.getState("duration") || 0;
3580
+ const bufferedRanges = this.api.getState("buffered");
3581
+ if (duration > 0) {
3582
+ const progress = currentTime / duration * 100;
3583
+ this.filled.style.width = `${progress}%`;
3584
+ this.handle.style.left = `${progress}%`;
3585
+ if (bufferedRanges && bufferedRanges.length > 0) {
3586
+ const bufferedEnd = bufferedRanges.end(bufferedRanges.length - 1);
3587
+ const bufferedPercent = bufferedEnd / duration * 100;
3588
+ this.buffered.style.width = `${bufferedPercent}%`;
3589
+ }
3590
+ this.el.setAttribute("aria-valuemax", String(Math.floor(duration)));
3591
+ this.el.setAttribute("aria-valuenow", String(Math.floor(currentTime)));
3592
+ this.el.setAttribute("aria-valuetext", formatTime(currentTime));
3593
+ }
3594
+ }
3595
+ getTimeFromPosition(clientX) {
3596
+ const rect = this.el.getBoundingClientRect();
3597
+ const percent = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
3598
+ const duration = this.api.getState("duration") || 0;
3599
+ return percent * duration;
3600
+ }
3601
+ updateTooltip(clientX) {
3602
+ const rect = this.el.getBoundingClientRect();
3603
+ const percent = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
3604
+ const time = this.getTimeFromPosition(clientX);
3605
+ this.tooltip.textContent = formatTime(time);
3606
+ this.tooltip.style.left = `${percent * 100}%`;
3607
+ }
3608
+ updateVisualPosition(clientX) {
3609
+ const rect = this.el.getBoundingClientRect();
3610
+ const percent = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
3611
+ this.filled.style.width = `${percent * 100}%`;
3612
+ this.handle.style.left = `${percent * 100}%`;
3613
+ }
3614
+ seek(clientX, force = false) {
3615
+ const video = getVideo(this.api.container);
3616
+ if (!video) return;
3617
+ const now = Date.now();
3618
+ if (!force && this.isDragging && now - this.lastSeekTime < this.seekThrottleMs) {
3619
+ return;
3620
+ }
3621
+ this.lastSeekTime = now;
3622
+ const time = this.getTimeFromPosition(clientX);
3623
+ video.currentTime = time;
3624
+ }
3625
+ destroy() {
3626
+ this.wrapper.removeEventListener("mousedown", this.onMouseDown);
3627
+ this.wrapper.removeEventListener("mousemove", this.onMouseMove);
3628
+ this.wrapper.removeEventListener("mouseleave", this.onMouseLeave);
3629
+ document.removeEventListener("mousemove", this.onDocMouseMove);
3630
+ document.removeEventListener("mouseup", this.onMouseUp);
3631
+ this.wrapper.remove();
3632
+ }
3633
+ };
3634
+ var TimeDisplay = class {
3635
+ constructor(api) {
3636
+ this.api = api;
3637
+ this.el = createElement("div", { className: "sp-time" });
3638
+ this.el.setAttribute("aria-live", "off");
3639
+ }
3640
+ render() {
3641
+ return this.el;
3642
+ }
3643
+ update() {
3644
+ const live = this.api.getState("live");
3645
+ const currentTime = this.api.getState("currentTime") || 0;
3646
+ const duration = this.api.getState("duration") || 0;
3647
+ if (live) {
3648
+ const seekableRange = this.api.getState("seekableRange");
3649
+ if (seekableRange) {
3650
+ const behindLive = seekableRange.end - currentTime;
3651
+ this.el.textContent = formatLiveTime(behindLive);
3652
+ } else {
3653
+ this.el.textContent = formatLiveTime(0);
3654
+ }
3655
+ } else {
3656
+ this.el.textContent = `${formatTime(currentTime)} / ${formatTime(duration)}`;
3657
+ }
3658
+ }
3659
+ destroy() {
3660
+ this.el.remove();
3661
+ }
3662
+ };
3663
+ var VolumeControl = class {
3664
+ constructor(api) {
3665
+ this.isDragging = false;
3666
+ this.onMouseDown = (e) => {
3667
+ e.preventDefault();
3668
+ this.isDragging = true;
3669
+ this.setVolume(this.getVolumeFromPosition(e.clientX));
3670
+ };
3671
+ this.onDocMouseMove = (e) => {
3672
+ if (this.isDragging) {
3673
+ this.setVolume(this.getVolumeFromPosition(e.clientX));
3674
+ }
3675
+ };
3676
+ this.onMouseUp = () => {
3677
+ this.isDragging = false;
3678
+ };
3679
+ this.onKeyDown = (e) => {
3680
+ const video = getVideo(this.api.container);
3681
+ if (!video) return;
3682
+ const step = 0.1;
3683
+ switch (e.key) {
3684
+ case "ArrowUp":
3685
+ case "ArrowRight":
3686
+ e.preventDefault();
3687
+ this.setVolume(video.volume + step);
3688
+ break;
3689
+ case "ArrowDown":
3690
+ case "ArrowLeft":
3691
+ e.preventDefault();
3692
+ this.setVolume(video.volume - step);
3693
+ break;
3694
+ }
3695
+ };
3696
+ this.api = api;
3697
+ this.el = createElement("div", { className: "sp-volume" });
3698
+ this.btn = createElement("button", {
3699
+ className: "sp-control sp-volume__btn",
3700
+ "aria-label": "Mute",
3701
+ type: "button"
3702
+ });
3703
+ this.btn.innerHTML = icons.volumeHigh;
3704
+ this.btn.onclick = () => this.toggleMute();
3705
+ const sliderWrap = createElement("div", { className: "sp-volume__slider-wrap" });
3706
+ this.slider = createElement("div", { className: "sp-volume__slider" });
3707
+ this.slider.setAttribute("role", "slider");
3708
+ this.slider.setAttribute("aria-label", "Volume");
3709
+ this.slider.setAttribute("aria-valuemin", "0");
3710
+ this.slider.setAttribute("aria-valuemax", "100");
3711
+ this.slider.setAttribute("tabindex", "0");
3712
+ this.level = createElement("div", { className: "sp-volume__level" });
3713
+ this.slider.appendChild(this.level);
3714
+ sliderWrap.appendChild(this.slider);
3715
+ this.el.appendChild(this.btn);
3716
+ this.el.appendChild(sliderWrap);
3717
+ this.slider.addEventListener("mousedown", this.onMouseDown);
3718
+ this.slider.addEventListener("keydown", this.onKeyDown);
3719
+ document.addEventListener("mousemove", this.onDocMouseMove);
3720
+ document.addEventListener("mouseup", this.onMouseUp);
3721
+ }
3722
+ render() {
3723
+ return this.el;
3724
+ }
3725
+ update() {
3726
+ const volume = this.api.getState("volume") ?? 1;
3727
+ const muted = this.api.getState("muted") ?? false;
3728
+ let icon;
3729
+ let label;
3730
+ if (muted || volume === 0) {
3731
+ icon = icons.volumeMute;
3732
+ label = "Unmute";
3733
+ } else if (volume < 0.5) {
3734
+ icon = icons.volumeLow;
3735
+ label = "Mute";
3736
+ } else {
3737
+ icon = icons.volumeHigh;
3738
+ label = "Mute";
3739
+ }
3740
+ this.btn.innerHTML = icon;
3741
+ this.btn.setAttribute("aria-label", label);
3742
+ const displayVolume = muted ? 0 : volume;
3743
+ this.level.style.width = `${displayVolume * 100}%`;
3744
+ this.slider.setAttribute("aria-valuenow", String(Math.round(displayVolume * 100)));
3745
+ }
3746
+ toggleMute() {
3747
+ const video = getVideo(this.api.container);
3748
+ if (!video) return;
3749
+ video.muted = !video.muted;
3750
+ }
3751
+ setVolume(percent) {
3752
+ const video = getVideo(this.api.container);
3753
+ if (!video) return;
3754
+ const vol = Math.max(0, Math.min(1, percent));
3755
+ video.volume = vol;
3756
+ if (vol > 0 && video.muted) {
3757
+ video.muted = false;
3758
+ }
3759
+ }
3760
+ getVolumeFromPosition(clientX) {
3761
+ const rect = this.slider.getBoundingClientRect();
3762
+ return Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
3763
+ }
3764
+ destroy() {
3765
+ document.removeEventListener("mousemove", this.onDocMouseMove);
3766
+ document.removeEventListener("mouseup", this.onMouseUp);
3767
+ this.el.remove();
3768
+ }
3769
+ };
3770
+ var LiveIndicator = class {
3771
+ constructor(api) {
3772
+ this.api = api;
3773
+ this.el = createElement("div", { className: "sp-live" });
3774
+ this.el.innerHTML = '<div class="sp-live__dot"></div><span>LIVE</span>';
3775
+ this.el.setAttribute("role", "button");
3776
+ this.el.setAttribute("aria-label", "Seek to live");
3777
+ this.el.setAttribute("tabindex", "0");
3778
+ this.el.onclick = () => this.seekToLive();
3779
+ this.el.onkeydown = (e) => {
3780
+ if (e.key === "Enter" || e.key === " ") {
3781
+ e.preventDefault();
3782
+ this.seekToLive();
3783
+ }
3784
+ };
3785
+ }
3786
+ render() {
3787
+ return this.el;
3788
+ }
3789
+ update() {
3790
+ const live = this.api.getState("live");
3791
+ const liveEdge = this.api.getState("liveEdge");
3792
+ this.el.style.display = live ? "" : "none";
3793
+ if (liveEdge) {
3794
+ this.el.classList.remove("sp-live--behind");
3795
+ } else {
3796
+ this.el.classList.add("sp-live--behind");
3797
+ }
3798
+ }
3799
+ seekToLive() {
3800
+ const video = getVideo(this.api.container);
3801
+ if (!video) return;
3802
+ const seekableRange = this.api.getState("seekableRange");
3803
+ if (seekableRange) {
3804
+ video.currentTime = seekableRange.end;
3805
+ }
3806
+ }
3807
+ destroy() {
3808
+ this.el.remove();
3809
+ }
3810
+ };
3811
+ var QualityMenu = class {
3812
+ constructor(api) {
3813
+ this.isOpen = false;
3814
+ this.lastQualitiesJson = "";
3815
+ this.api = api;
3816
+ this.el = createElement("div", { className: "sp-quality" });
3817
+ this.btn = createButton("sp-quality__btn", "Quality", icons.settings);
3818
+ this.btnLabel = createElement("span", { className: "sp-quality__label" });
3819
+ this.btnLabel.textContent = "Auto";
3820
+ this.btn.appendChild(this.btnLabel);
3821
+ this.btn.addEventListener("click", (e) => {
3822
+ e.stopPropagation();
3823
+ this.toggle();
3824
+ });
3825
+ this.menu = createElement("div", { className: "sp-quality-menu" });
3826
+ this.menu.setAttribute("role", "menu");
3827
+ this.menu.addEventListener("click", (e) => {
3828
+ e.stopPropagation();
3829
+ });
3830
+ this.el.appendChild(this.btn);
3831
+ this.el.appendChild(this.menu);
3832
+ this.closeHandler = (e) => {
3833
+ if (!this.el.contains(e.target)) {
3834
+ this.close();
3835
+ }
3836
+ };
3837
+ document.addEventListener("click", this.closeHandler);
3838
+ }
3839
+ render() {
3840
+ return this.el;
3841
+ }
3842
+ update() {
3843
+ const qualities = this.api.getState("qualities") || [];
3844
+ const currentQuality = this.api.getState("currentQuality");
3845
+ this.el.style.display = qualities.length > 0 ? "" : "none";
3846
+ this.btnLabel.textContent = currentQuality?.label || "Auto";
3847
+ const qualitiesJson = JSON.stringify(qualities.map((q) => q.id));
3848
+ const currentId = currentQuality?.id || "auto";
3849
+ if (qualitiesJson !== this.lastQualitiesJson) {
3850
+ this.lastQualitiesJson = qualitiesJson;
3851
+ this.rebuildMenu(qualities);
3852
+ }
3853
+ this.updateActiveStates(currentId);
3854
+ }
3855
+ rebuildMenu(qualities) {
3856
+ this.menu.innerHTML = "";
3857
+ const autoItem = this.createMenuItem("Auto", "auto");
3858
+ this.menu.appendChild(autoItem);
3859
+ const sorted = [...qualities].sort((a, b) => b.height - a.height);
3860
+ for (const q of sorted) {
3861
+ if (q.id === "auto") continue;
3862
+ const item = this.createMenuItem(q.label, q.id);
3863
+ this.menu.appendChild(item);
3864
+ }
3865
+ }
3866
+ updateActiveStates(activeId) {
3867
+ const items = this.menu.querySelectorAll(".sp-quality-menu__item");
3868
+ items.forEach((item) => {
3869
+ const id = item.getAttribute("data-quality-id");
3870
+ const isActive = id === activeId;
3871
+ item.classList.toggle("sp-quality-menu__item--active", isActive);
3872
+ });
3873
+ }
3874
+ createMenuItem(label, qualityId) {
3875
+ const item = createElement("div", {
3876
+ className: "sp-quality-menu__item"
3877
+ });
3878
+ item.setAttribute("role", "menuitem");
3879
+ item.setAttribute("data-quality-id", qualityId);
3880
+ const labelSpan = createElement("span", { className: "sp-quality-menu__label" });
3881
+ labelSpan.textContent = label;
3882
+ item.appendChild(labelSpan);
3883
+ item.addEventListener("click", (e) => {
3884
+ e.preventDefault();
3885
+ e.stopPropagation();
3886
+ this.selectQuality(qualityId);
3887
+ });
3888
+ return item;
3889
+ }
3890
+ selectQuality(qualityId) {
3891
+ this.api.emit("quality:select", {
3892
+ quality: qualityId,
3893
+ auto: qualityId === "auto"
3894
+ });
3895
+ this.close();
3896
+ }
3897
+ toggle() {
3898
+ this.isOpen ? this.close() : this.open();
3899
+ }
3900
+ open() {
3901
+ this.isOpen = true;
3902
+ this.menu.classList.add("sp-quality-menu--open");
3903
+ this.btn.setAttribute("aria-expanded", "true");
3904
+ }
3905
+ close() {
3906
+ this.isOpen = false;
3907
+ this.menu.classList.remove("sp-quality-menu--open");
3908
+ this.btn.setAttribute("aria-expanded", "false");
3909
+ }
3910
+ destroy() {
3911
+ document.removeEventListener("click", this.closeHandler);
3912
+ this.el.remove();
3913
+ }
3914
+ };
3915
+ function isChromecastSupported() {
3916
+ if (typeof navigator === "undefined") return false;
3917
+ const ua = navigator.userAgent;
3918
+ return /Chrome/.test(ua) && !/Edge|Edg/.test(ua);
3919
+ }
3920
+ function isAirPlaySupported() {
3921
+ if (typeof HTMLVideoElement === "undefined") return false;
3922
+ return typeof HTMLVideoElement.prototype.webkitShowPlaybackTargetPicker === "function";
3923
+ }
3924
+ var CastButton = class {
3925
+ constructor(api, type) {
3926
+ this.api = api;
3927
+ this.type = type;
3928
+ this.supported = type === "chromecast" ? isChromecastSupported() : isAirPlaySupported();
3929
+ const icon = type === "chromecast" ? icons.chromecast : icons.airplay;
3930
+ const label = type === "chromecast" ? "Cast" : "AirPlay";
3931
+ this.el = createButton(`sp-cast sp-cast--${type}`, label, icon);
3932
+ this.el.addEventListener("click", () => this.handleClick());
3933
+ if (!this.supported) {
3934
+ this.el.style.display = "none";
3935
+ }
3936
+ }
3937
+ render() {
3938
+ return this.el;
3939
+ }
3940
+ update() {
3941
+ if (!this.supported) {
3942
+ this.el.style.display = "none";
3943
+ return;
3944
+ }
3945
+ if (this.type === "chromecast") {
3946
+ const available = this.api.getState("chromecastAvailable");
3947
+ const active = this.api.getState("chromecastActive");
3948
+ this.el.style.display = "";
3949
+ this.el.disabled = !available && !active;
3950
+ this.el.classList.toggle("sp-cast--active", !!active);
3951
+ this.el.classList.toggle("sp-cast--unavailable", !available && !active);
3952
+ if (active) {
3953
+ this.el.innerHTML = icons.chromecastConnected;
3954
+ this.el.setAttribute("aria-label", "Stop casting");
3955
+ } else {
3956
+ this.el.innerHTML = icons.chromecast;
3957
+ this.el.setAttribute("aria-label", available ? "Cast" : "No Cast devices found");
3958
+ }
3959
+ } else {
3960
+ const active = this.api.getState("airplayActive");
3961
+ this.el.style.display = "";
3962
+ this.el.disabled = false;
3963
+ this.el.classList.toggle("sp-cast--active", !!active);
3964
+ this.el.classList.remove("sp-cast--unavailable");
3965
+ this.el.setAttribute("aria-label", active ? "Stop AirPlay" : "AirPlay");
3966
+ }
3967
+ }
3968
+ handleClick() {
3969
+ if (this.type === "chromecast") {
3970
+ this.handleChromecast();
3971
+ } else {
3972
+ this.handleAirPlay();
3973
+ }
3974
+ }
3975
+ handleChromecast() {
3976
+ const chromecast = this.api.getPlugin("chromecast");
3977
+ if (!chromecast) return;
3978
+ if (chromecast.isConnected()) {
3979
+ chromecast.endSession();
3980
+ } else {
3981
+ chromecast.requestSession().catch(() => {
3982
+ });
3983
+ }
3984
+ }
3985
+ async handleAirPlay() {
3986
+ const airplayPlugin = this.api.getPlugin("airplay");
3987
+ if (airplayPlugin) {
3988
+ await airplayPlugin.showPicker();
3989
+ } else {
3990
+ const video = getVideo(this.api.container);
3991
+ video?.webkitShowPlaybackTargetPicker?.();
3992
+ }
3993
+ }
3994
+ destroy() {
3995
+ this.el.remove();
3996
+ }
3997
+ };
3998
+ var PipButton = class {
3999
+ constructor(api) {
4000
+ this.clickHandler = () => {
4001
+ this.toggle();
4002
+ };
4003
+ this.api = api;
4004
+ const video = document.createElement("video");
4005
+ this.supported = "pictureInPictureEnabled" in document || "webkitSetPresentationMode" in video;
4006
+ this.el = createButton("sp-pip", "Picture-in-Picture", icons.pip);
4007
+ this.el.addEventListener("click", this.clickHandler);
4008
+ if (!this.supported) {
4009
+ this.el.style.display = "none";
4010
+ }
4011
+ }
4012
+ render() {
4013
+ return this.el;
4014
+ }
4015
+ update() {
4016
+ if (!this.supported) return;
4017
+ const pip = this.api.getState("pip");
4018
+ this.el.setAttribute("aria-label", pip ? "Exit Picture-in-Picture" : "Picture-in-Picture");
4019
+ this.el.classList.toggle("sp-pip--active", !!pip);
4020
+ }
4021
+ async toggle() {
4022
+ const video = getVideo(this.api.container);
4023
+ if (!video) {
4024
+ this.api.logger.warn("PiP: video element not found");
4025
+ return;
4026
+ }
4027
+ try {
4028
+ const isInPip = document.pictureInPictureElement === video || video.webkitPresentationMode === "picture-in-picture";
4029
+ if (isInPip) {
4030
+ if (document.pictureInPictureElement) {
4031
+ await document.exitPictureInPicture();
4032
+ } else if (video.webkitSetPresentationMode) {
4033
+ video.webkitSetPresentationMode("inline");
4034
+ }
4035
+ this.api.logger.debug("PiP: exited");
4036
+ } else {
4037
+ if (video.requestPictureInPicture) {
4038
+ await video.requestPictureInPicture();
4039
+ } else if (video.webkitSetPresentationMode) {
4040
+ video.webkitSetPresentationMode("picture-in-picture");
4041
+ }
4042
+ this.api.logger.debug("PiP: entered");
4043
+ }
4044
+ } catch (e) {
4045
+ this.api.logger.warn("PiP: failed", { error: e.message });
4046
+ }
4047
+ }
4048
+ destroy() {
4049
+ this.el.removeEventListener("click", this.clickHandler);
4050
+ this.el.remove();
4051
+ }
4052
+ };
4053
+ var FullscreenButton = class {
4054
+ constructor(api) {
4055
+ this.clickHandler = () => {
4056
+ this.toggle();
4057
+ };
4058
+ this.api = api;
4059
+ this.el = createButton("sp-fullscreen", "Fullscreen", icons.fullscreen);
4060
+ this.el.addEventListener("click", this.clickHandler);
4061
+ }
4062
+ render() {
4063
+ return this.el;
4064
+ }
4065
+ update() {
4066
+ const fullscreen = this.api.getState("fullscreen");
4067
+ if (fullscreen) {
4068
+ this.el.innerHTML = icons.exitFullscreen;
4069
+ this.el.setAttribute("aria-label", "Exit fullscreen");
4070
+ } else {
4071
+ this.el.innerHTML = icons.fullscreen;
4072
+ this.el.setAttribute("aria-label", "Fullscreen");
4073
+ }
4074
+ }
4075
+ async toggle() {
4076
+ const container = this.api.container;
4077
+ const video = getVideo(container);
4078
+ try {
4079
+ if (document.fullscreenElement) {
4080
+ await document.exitFullscreen();
4081
+ } else if (container.requestFullscreen) {
4082
+ await container.requestFullscreen();
4083
+ } else if (video?.webkitEnterFullscreen) {
4084
+ video.webkitEnterFullscreen();
4085
+ }
4086
+ } catch {
4087
+ }
4088
+ }
4089
+ destroy() {
4090
+ this.el.removeEventListener("click", this.clickHandler);
4091
+ this.el.remove();
4092
+ }
4093
+ };
4094
+ var Spacer = class {
4095
+ constructor() {
4096
+ this.el = createElement("div", { className: "sp-spacer" });
4097
+ }
4098
+ render() {
4099
+ return this.el;
4100
+ }
4101
+ update() {
4102
+ }
4103
+ destroy() {
4104
+ this.el.remove();
4105
+ }
4106
+ };
4107
+ var DEFAULT_LAYOUT = [
4108
+ "play",
4109
+ "volume",
4110
+ "time",
4111
+ "live-indicator",
4112
+ "spacer",
4113
+ "quality",
4114
+ "chromecast",
4115
+ "airplay",
4116
+ "pip",
4117
+ "fullscreen"
4118
+ ];
4119
+ var DEFAULT_HIDE_DELAY = 3e3;
4120
+ function uiPlugin(config = {}) {
4121
+ let api;
4122
+ let controlBar = null;
4123
+ let gradient = null;
4124
+ let progressBar = null;
4125
+ let bufferingIndicator = null;
4126
+ let styleEl = null;
4127
+ let controls = [];
4128
+ let hideTimeout = null;
4129
+ let stateUnsubscribe = null;
4130
+ let controlsVisible = true;
4131
+ const layout = config.controls || DEFAULT_LAYOUT;
4132
+ const hideDelay = config.hideDelay ?? DEFAULT_HIDE_DELAY;
4133
+ const createControl = (slot) => {
4134
+ switch (slot) {
4135
+ case "play":
4136
+ return new PlayButton(api);
4137
+ case "volume":
4138
+ return new VolumeControl(api);
4139
+ case "progress":
4140
+ return null;
4141
+ case "time":
4142
+ return new TimeDisplay(api);
4143
+ case "live-indicator":
4144
+ return new LiveIndicator(api);
4145
+ case "quality":
4146
+ return new QualityMenu(api);
4147
+ case "chromecast":
4148
+ return new CastButton(api, "chromecast");
4149
+ case "airplay":
4150
+ return new CastButton(api, "airplay");
4151
+ case "pip":
4152
+ return new PipButton(api);
4153
+ case "fullscreen":
4154
+ return new FullscreenButton(api);
4155
+ case "spacer":
4156
+ return new Spacer();
4157
+ default:
4158
+ return null;
4159
+ }
4160
+ };
4161
+ const updateControls = () => {
4162
+ controls.forEach((c) => c.update());
4163
+ progressBar?.update();
4164
+ const waiting = api?.getState("waiting");
4165
+ const seeking = api?.getState("seeking");
4166
+ const playbackState = api?.getState("playbackState");
4167
+ const isLoading = playbackState === "loading";
4168
+ const showSpinner = waiting || seeking && !api?.getState("paused") || isLoading;
4169
+ bufferingIndicator?.classList.toggle("sp-buffering--visible", !!showSpinner);
4170
+ };
4171
+ const showControls = () => {
4172
+ if (controlsVisible) {
4173
+ resetHideTimer();
4174
+ return;
4175
+ }
4176
+ controlsVisible = true;
4177
+ controlBar?.classList.add("sp-controls--visible");
4178
+ controlBar?.classList.remove("sp-controls--hidden");
4179
+ gradient?.classList.add("sp-gradient--visible");
4180
+ progressBar?.show();
4181
+ api?.setState("controlsVisible", true);
4182
+ resetHideTimer();
4183
+ };
4184
+ const hideControls = () => {
4185
+ const paused = api?.getState("paused");
4186
+ if (paused) return;
4187
+ controlsVisible = false;
4188
+ controlBar?.classList.remove("sp-controls--visible");
4189
+ controlBar?.classList.add("sp-controls--hidden");
4190
+ gradient?.classList.remove("sp-gradient--visible");
4191
+ progressBar?.hide();
4192
+ api?.setState("controlsVisible", false);
4193
+ };
4194
+ const resetHideTimer = () => {
4195
+ if (hideTimeout) {
4196
+ clearTimeout(hideTimeout);
4197
+ }
4198
+ hideTimeout = setTimeout(hideControls, hideDelay);
4199
+ };
4200
+ const handleInteraction = () => {
4201
+ showControls();
4202
+ };
4203
+ const handleMouseLeave = () => {
4204
+ hideControls();
4205
+ };
4206
+ const handleKeyDown = (e) => {
4207
+ if (!api.container.contains(document.activeElement)) return;
4208
+ const video = api.container.querySelector("video");
4209
+ if (!video) return;
4210
+ switch (e.key) {
4211
+ case " ":
4212
+ case "k":
4213
+ e.preventDefault();
4214
+ video.paused ? video.play() : video.pause();
4215
+ break;
4216
+ case "m":
4217
+ e.preventDefault();
4218
+ video.muted = !video.muted;
4219
+ break;
4220
+ case "f":
4221
+ e.preventDefault();
4222
+ if (document.fullscreenElement) {
4223
+ document.exitFullscreen();
4224
+ } else {
4225
+ api.container.requestFullscreen?.();
4226
+ }
4227
+ break;
4228
+ case "ArrowLeft":
4229
+ e.preventDefault();
4230
+ video.currentTime = Math.max(0, video.currentTime - 5);
4231
+ showControls();
4232
+ break;
4233
+ case "ArrowRight":
4234
+ e.preventDefault();
4235
+ video.currentTime = Math.min(video.duration || 0, video.currentTime + 5);
4236
+ showControls();
4237
+ break;
4238
+ case "ArrowUp":
4239
+ e.preventDefault();
4240
+ video.volume = Math.min(1, video.volume + 0.1);
4241
+ showControls();
4242
+ break;
4243
+ case "ArrowDown":
4244
+ e.preventDefault();
4245
+ video.volume = Math.max(0, video.volume - 0.1);
4246
+ showControls();
4247
+ break;
4248
+ }
4249
+ };
4250
+ return {
4251
+ id: "ui-controls",
4252
+ name: "UI Controls",
4253
+ type: "ui",
4254
+ version: "1.0.0",
4255
+ async init(pluginApi) {
4256
+ api = pluginApi;
4257
+ styleEl = document.createElement("style");
4258
+ styleEl.textContent = styles;
4259
+ document.head.appendChild(styleEl);
4260
+ if (config.theme) {
4261
+ this.setTheme(config.theme);
4262
+ }
4263
+ const container = api.container;
4264
+ if (!container) {
4265
+ api.logger.error("UI plugin: container not found");
4266
+ return;
4267
+ }
4268
+ const containerStyle = getComputedStyle(container);
4269
+ if (containerStyle.position === "static") {
4270
+ container.style.position = "relative";
4271
+ }
4272
+ gradient = document.createElement("div");
4273
+ gradient.className = "sp-gradient sp-gradient--visible";
4274
+ container.appendChild(gradient);
4275
+ bufferingIndicator = document.createElement("div");
4276
+ bufferingIndicator.className = "sp-buffering";
4277
+ bufferingIndicator.innerHTML = icons.spinner;
4278
+ bufferingIndicator.setAttribute("aria-hidden", "true");
4279
+ container.appendChild(bufferingIndicator);
4280
+ progressBar = new ProgressBar(api);
4281
+ container.appendChild(progressBar.render());
4282
+ progressBar.show();
4283
+ controlBar = document.createElement("div");
4284
+ controlBar.className = "sp-controls sp-controls--visible";
4285
+ controlBar.setAttribute("role", "toolbar");
4286
+ controlBar.setAttribute("aria-label", "Video controls");
4287
+ for (const slot of layout) {
4288
+ const control = createControl(slot);
4289
+ if (control) {
4290
+ controls.push(control);
4291
+ controlBar.appendChild(control.render());
4292
+ }
4293
+ }
4294
+ container.appendChild(controlBar);
4295
+ container.addEventListener("mousemove", handleInteraction);
4296
+ container.addEventListener("mouseenter", handleInteraction);
4297
+ container.addEventListener("mouseleave", handleMouseLeave);
4298
+ container.addEventListener("touchstart", handleInteraction, { passive: true });
4299
+ container.addEventListener("click", handleInteraction);
4300
+ document.addEventListener("keydown", handleKeyDown);
4301
+ stateUnsubscribe = api.subscribeToState(updateControls);
4302
+ document.addEventListener("fullscreenchange", updateControls);
4303
+ updateControls();
4304
+ if (!container.hasAttribute("tabindex")) {
4305
+ container.setAttribute("tabindex", "0");
4306
+ }
4307
+ api.logger.debug("UI controls plugin initialized");
4308
+ },
4309
+ async destroy() {
4310
+ if (hideTimeout) {
4311
+ clearTimeout(hideTimeout);
4312
+ hideTimeout = null;
4313
+ }
4314
+ stateUnsubscribe?.();
4315
+ stateUnsubscribe = null;
4316
+ if (api?.container) {
4317
+ api.container.removeEventListener("mousemove", handleInteraction);
4318
+ api.container.removeEventListener("mouseenter", handleInteraction);
4319
+ api.container.removeEventListener("mouseleave", handleMouseLeave);
4320
+ api.container.removeEventListener("touchstart", handleInteraction);
4321
+ api.container.removeEventListener("click", handleInteraction);
4322
+ }
4323
+ document.removeEventListener("keydown", handleKeyDown);
4324
+ document.removeEventListener("fullscreenchange", updateControls);
4325
+ controls.forEach((c) => c.destroy());
4326
+ controls = [];
4327
+ progressBar?.destroy();
4328
+ progressBar = null;
4329
+ controlBar?.remove();
4330
+ controlBar = null;
4331
+ gradient?.remove();
4332
+ gradient = null;
4333
+ bufferingIndicator?.remove();
4334
+ bufferingIndicator = null;
4335
+ styleEl?.remove();
4336
+ styleEl = null;
4337
+ api?.logger.debug("UI controls plugin destroyed");
4338
+ },
4339
+ // Public API
4340
+ show() {
4341
+ showControls();
4342
+ },
4343
+ hide() {
4344
+ controlsVisible = false;
4345
+ controlBar?.classList.remove("sp-controls--visible");
4346
+ controlBar?.classList.add("sp-controls--hidden");
4347
+ gradient?.classList.remove("sp-gradient--visible");
4348
+ progressBar?.hide();
4349
+ api?.setState("controlsVisible", false);
4350
+ },
4351
+ setTheme(theme) {
4352
+ const root = api?.container || document.documentElement;
4353
+ if (theme.primaryColor) {
4354
+ root.style.setProperty("--sp-color", theme.primaryColor);
4355
+ }
4356
+ if (theme.accentColor) {
4357
+ root.style.setProperty("--sp-accent", theme.accentColor);
4358
+ }
4359
+ if (theme.backgroundColor) {
4360
+ root.style.setProperty("--sp-bg", theme.backgroundColor);
4361
+ }
4362
+ if (theme.controlBarHeight) {
4363
+ root.style.setProperty("--sp-control-height", `${theme.controlBarHeight}px`);
4364
+ }
4365
+ if (theme.iconSize) {
4366
+ root.style.setProperty("--sp-icon-size", `${theme.iconSize}px`);
4367
+ }
4368
+ },
4369
+ getControlBar() {
4370
+ return controlBar;
4371
+ }
4372
+ };
4373
+ }
4374
+ function getAttr(element, ...names) {
4375
+ for (const name of names) {
4376
+ const value = element.getAttribute(name);
4377
+ if (value !== null) return value;
4378
+ }
4379
+ return null;
4380
+ }
4381
+ function parseDataAttributes(element) {
4382
+ const config = {};
4383
+ const src = getAttr(element, "data-src", "src", "href");
4384
+ if (src) {
4385
+ config.src = src;
4386
+ }
4387
+ const autoplay = getAttr(element, "data-autoplay", "autoplay");
4388
+ if (autoplay !== null) {
4389
+ config.autoplay = autoplay !== "false";
4390
+ }
4391
+ const muted = getAttr(element, "data-muted", "muted");
4392
+ if (muted !== null) {
4393
+ config.muted = muted !== "false";
4394
+ }
4395
+ const controls = getAttr(element, "data-controls", "controls");
4396
+ if (controls !== null) {
4397
+ config.controls = controls !== "false";
4398
+ }
4399
+ const keyboard = getAttr(element, "data-keyboard", "keyboard");
4400
+ if (keyboard !== null) {
4401
+ config.keyboard = keyboard !== "false";
4402
+ }
4403
+ const loop = getAttr(element, "data-loop", "loop");
4404
+ if (loop !== null) {
4405
+ config.loop = loop !== "false";
4406
+ }
4407
+ const poster = getAttr(element, "data-poster", "poster");
4408
+ if (poster) {
4409
+ config.poster = poster;
4410
+ }
4411
+ const brandColor = getAttr(element, "data-brand-color", "data-color", "color");
4412
+ if (brandColor) {
4413
+ config.brandColor = brandColor;
4414
+ }
4415
+ const primaryColor = element.getAttribute("data-primary-color");
4416
+ if (primaryColor) {
4417
+ config.primaryColor = primaryColor;
4418
+ }
4419
+ const backgroundColor = element.getAttribute("data-background-color");
4420
+ if (backgroundColor) {
4421
+ config.backgroundColor = backgroundColor;
4422
+ }
4423
+ const width = element.getAttribute("data-width");
4424
+ if (width) {
4425
+ config.width = width;
4426
+ }
4427
+ const height = element.getAttribute("data-height");
4428
+ if (height) {
4429
+ config.height = height;
4430
+ }
4431
+ const aspectRatio = element.getAttribute("data-aspect-ratio");
4432
+ if (aspectRatio) {
4433
+ config.aspectRatio = aspectRatio;
4434
+ }
4435
+ const className = element.getAttribute("data-class");
4436
+ if (className) {
4437
+ config.className = className;
4438
+ }
4439
+ const hideDelay = element.getAttribute("data-hide-delay");
4440
+ if (hideDelay) {
4441
+ const parsed = parseInt(hideDelay, 10);
4442
+ if (!isNaN(parsed)) {
4443
+ config.hideDelay = parsed;
4444
+ }
4445
+ }
4446
+ const playbackRate = element.getAttribute("data-playback-rate");
4447
+ if (playbackRate) {
4448
+ const parsed = parseFloat(playbackRate);
4449
+ if (!isNaN(parsed)) {
4450
+ config.playbackRate = parsed;
4451
+ }
4452
+ }
4453
+ const startTime = element.getAttribute("data-start-time");
4454
+ if (startTime) {
4455
+ const parsed = parseFloat(startTime);
4456
+ if (!isNaN(parsed)) {
4457
+ config.startTime = parsed;
4458
+ }
4459
+ }
4460
+ return config;
4461
+ }
4462
+ function aspectRatioToPercent(ratio) {
4463
+ const parts = ratio.split(":").map(Number);
4464
+ const width = parts[0];
4465
+ const height = parts[1];
4466
+ if (parts.length === 2 && width !== void 0 && height !== void 0 && !isNaN(width) && !isNaN(height) && width > 0) {
4467
+ return height / width * 100;
4468
+ }
4469
+ return 56.25;
4470
+ }
4471
+ function applyContainerStyles(container, config) {
4472
+ if (config.className) {
4473
+ container.classList.add(...config.className.split(" "));
4474
+ }
4475
+ if (config.width) {
4476
+ container.style.width = config.width;
4477
+ }
4478
+ if (config.height) {
4479
+ container.style.height = config.height;
4480
+ } else if (config.aspectRatio) {
4481
+ container.style.position = "relative";
4482
+ container.style.paddingBottom = `${aspectRatioToPercent(config.aspectRatio)}%`;
4483
+ container.style.height = "0";
4484
+ }
4485
+ }
4486
+ async function createEmbedPlayer(container, config) {
4487
+ if (!config.src) {
4488
+ console.error("[Scarlett Player] No source URL provided");
4489
+ return null;
4490
+ }
4491
+ try {
4492
+ applyContainerStyles(container, config);
4493
+ const theme = {};
4494
+ if (config.brandColor) {
4495
+ theme.accentColor = config.brandColor;
4496
+ }
4497
+ if (config.primaryColor) {
4498
+ theme.primaryColor = config.primaryColor;
4499
+ }
4500
+ if (config.backgroundColor) {
4501
+ theme.backgroundColor = config.backgroundColor;
4502
+ }
4503
+ const uiConfig = {};
4504
+ if (Object.keys(theme).length > 0) {
4505
+ uiConfig.theme = theme;
4506
+ }
4507
+ if (config.hideDelay !== void 0) {
4508
+ uiConfig.hideDelay = config.hideDelay;
4509
+ }
4510
+ const plugins = [createHLSPlugin()];
4511
+ if (config.controls !== false) {
4512
+ plugins.push(uiPlugin(uiConfig));
4513
+ }
4514
+ const player = await createPlayer({
4515
+ container,
4516
+ src: config.src,
4517
+ autoplay: config.autoplay || false,
4518
+ muted: config.muted || false,
4519
+ poster: config.poster,
4520
+ loop: config.loop || false,
4521
+ plugins
4522
+ });
4523
+ const video = container.querySelector("video");
4524
+ if (video) {
4525
+ if (config.playbackRate) {
4526
+ video.playbackRate = config.playbackRate;
4527
+ }
4528
+ if (config.startTime) {
4529
+ video.currentTime = config.startTime;
4530
+ }
4531
+ }
4532
+ return player;
4533
+ } catch (error) {
4534
+ console.error("[Scarlett Player] Failed to create player:", error);
4535
+ return null;
4536
+ }
4537
+ }
4538
+ async function initElement(element) {
4539
+ if (element.hasAttribute("data-scarlett-initialized")) {
4540
+ return null;
4541
+ }
4542
+ const config = parseDataAttributes(element);
4543
+ element.setAttribute("data-scarlett-initialized", "true");
4544
+ const player = await createEmbedPlayer(element, config);
4545
+ if (!player) {
4546
+ element.removeAttribute("data-scarlett-initialized");
4547
+ }
4548
+ return player;
4549
+ }
4550
+ const PLAYER_SELECTORS = [
4551
+ "[data-scarlett-player]",
4552
+ // Full name: <div data-scarlett-player>
4553
+ "[data-sp]",
4554
+ // Short: <div data-sp>
4555
+ "[data-video-player]",
4556
+ // Generic: <div data-video-player>
4557
+ ".scarlett-player"
4558
+ // Class-based: <div class="scarlett-player">
4559
+ ];
4560
+ async function initAll() {
4561
+ const selector = PLAYER_SELECTORS.join(", ");
4562
+ const elements = document.querySelectorAll(selector);
4563
+ const promises = Array.from(elements).map((element) => initElement(element));
4564
+ await Promise.all(promises);
4565
+ if (elements.length > 0) {
4566
+ console.log(`[Scarlett Player] Initialized ${elements.length} player(s) (light build)`);
4567
+ }
4568
+ }
4569
+ async function create(options) {
4570
+ let container = null;
4571
+ if (typeof options.container === "string") {
4572
+ container = document.querySelector(options.container);
4573
+ if (!container) {
4574
+ console.error(`[Scarlett Player] Container not found: ${options.container}`);
4575
+ return null;
4576
+ }
4577
+ } else {
4578
+ container = options.container;
4579
+ }
4580
+ const config = { ...options };
4581
+ return createEmbedPlayer(container, config);
4582
+ }
4583
+ const VERSION = "0.1.0-light";
4584
+ const ScarlettPlayerAPI = {
4585
+ create,
4586
+ initAll,
4587
+ version: VERSION
4588
+ };
4589
+ if (typeof window !== "undefined") {
4590
+ window.ScarlettPlayer = ScarlettPlayerAPI;
4591
+ }
4592
+ if (typeof document !== "undefined") {
4593
+ if (document.readyState === "loading") {
4594
+ document.addEventListener("DOMContentLoaded", () => {
4595
+ initAll();
4596
+ });
4597
+ } else {
4598
+ initAll();
4599
+ }
4600
+ }
4601
+ export {
4602
+ applyContainerStyles,
4603
+ aspectRatioToPercent,
4604
+ create,
4605
+ createEmbedPlayer,
4606
+ ScarlettPlayerAPI as default,
4607
+ initAll,
4608
+ initElement,
4609
+ parseDataAttributes
4610
+ };
4611
+ //# sourceMappingURL=embed.light.js.map