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