@mattebox/player-ads 0.0.0 → 1.0.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.
Files changed (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -2
  3. package/dist/ad-info.d.ts +25 -0
  4. package/dist/ad-info.d.ts.map +1 -0
  5. package/dist/ad-info.js +80 -0
  6. package/dist/ad-info.js.map +1 -0
  7. package/dist/ad-link.d.ts +19 -0
  8. package/dist/ad-link.d.ts.map +1 -0
  9. package/dist/ad-link.js +51 -0
  10. package/dist/ad-link.js.map +1 -0
  11. package/dist/ad-progress.d.ts +18 -0
  12. package/dist/ad-progress.d.ts.map +1 -0
  13. package/dist/ad-progress.js +51 -0
  14. package/dist/ad-progress.js.map +1 -0
  15. package/dist/ad-skip-button.d.ts +21 -0
  16. package/dist/ad-skip-button.d.ts.map +1 -0
  17. package/dist/ad-skip-button.js +66 -0
  18. package/dist/ad-skip-button.js.map +1 -0
  19. package/dist/beacons.d.ts +29 -0
  20. package/dist/beacons.d.ts.map +1 -0
  21. package/dist/beacons.js +44 -0
  22. package/dist/beacons.js.map +1 -0
  23. package/dist/cdn/mattebox-player-ads.min.js +1 -0
  24. package/dist/component.d.ts +54 -0
  25. package/dist/component.d.ts.map +1 -0
  26. package/dist/component.js +95 -0
  27. package/dist/component.js.map +1 -0
  28. package/dist/controller.d.ts +178 -0
  29. package/dist/controller.d.ts.map +1 -0
  30. package/dist/controller.js +799 -0
  31. package/dist/controller.js.map +1 -0
  32. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/asyncToGenerator.js +26 -0
  33. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/defineProperty.js +11 -0
  34. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/objectSpread2.js +24 -0
  35. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/toPrimitive.js +13 -0
  36. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/toPropertyKey.js +8 -0
  37. package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/typeof.js +10 -0
  38. package/dist/es2015/ad-info.js +79 -0
  39. package/dist/es2015/ad-link.js +53 -0
  40. package/dist/es2015/ad-progress.js +51 -0
  41. package/dist/es2015/ad-skip-button.js +68 -0
  42. package/dist/es2015/beacons.js +42 -0
  43. package/dist/es2015/component.js +94 -0
  44. package/dist/es2015/controller.js +826 -0
  45. package/dist/es2015/host.js +18 -0
  46. package/dist/es2015/index.js +71 -0
  47. package/dist/es2015/locales/ar.js +13 -0
  48. package/dist/es2015/locales/ca.js +13 -0
  49. package/dist/es2015/locales/de.js +13 -0
  50. package/dist/es2015/locales/es.js +13 -0
  51. package/dist/es2015/locales/fr.js +13 -0
  52. package/dist/es2015/locales/hi.js +13 -0
  53. package/dist/es2015/locales/it.js +13 -0
  54. package/dist/es2015/locales/ja.js +13 -0
  55. package/dist/es2015/locales/ko.js +13 -0
  56. package/dist/es2015/locales/pt.js +13 -0
  57. package/dist/es2015/locales/zh.js +13 -0
  58. package/dist/es2015/shared.js +26 -0
  59. package/dist/es2015/vast.js +255 -0
  60. package/dist/es2015/vmap.js +73 -0
  61. package/dist/host.d.ts +14 -0
  62. package/dist/host.d.ts.map +1 -0
  63. package/dist/host.js +20 -0
  64. package/dist/host.js.map +1 -0
  65. package/dist/index.d.ts +40 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +73 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/locales/ar.d.ts +13 -0
  70. package/dist/locales/ar.d.ts.map +1 -0
  71. package/dist/locales/ar.js +15 -0
  72. package/dist/locales/ar.js.map +1 -0
  73. package/dist/locales/ca.d.ts +13 -0
  74. package/dist/locales/ca.d.ts.map +1 -0
  75. package/dist/locales/ca.js +15 -0
  76. package/dist/locales/ca.js.map +1 -0
  77. package/dist/locales/de.d.ts +13 -0
  78. package/dist/locales/de.d.ts.map +1 -0
  79. package/dist/locales/de.js +15 -0
  80. package/dist/locales/de.js.map +1 -0
  81. package/dist/locales/es.d.ts +13 -0
  82. package/dist/locales/es.d.ts.map +1 -0
  83. package/dist/locales/es.js +15 -0
  84. package/dist/locales/es.js.map +1 -0
  85. package/dist/locales/fr.d.ts +13 -0
  86. package/dist/locales/fr.d.ts.map +1 -0
  87. package/dist/locales/fr.js +15 -0
  88. package/dist/locales/fr.js.map +1 -0
  89. package/dist/locales/hi.d.ts +13 -0
  90. package/dist/locales/hi.d.ts.map +1 -0
  91. package/dist/locales/hi.js +15 -0
  92. package/dist/locales/hi.js.map +1 -0
  93. package/dist/locales/it.d.ts +13 -0
  94. package/dist/locales/it.d.ts.map +1 -0
  95. package/dist/locales/it.js +15 -0
  96. package/dist/locales/it.js.map +1 -0
  97. package/dist/locales/ja.d.ts +13 -0
  98. package/dist/locales/ja.d.ts.map +1 -0
  99. package/dist/locales/ja.js +15 -0
  100. package/dist/locales/ja.js.map +1 -0
  101. package/dist/locales/ko.d.ts +13 -0
  102. package/dist/locales/ko.d.ts.map +1 -0
  103. package/dist/locales/ko.js +15 -0
  104. package/dist/locales/ko.js.map +1 -0
  105. package/dist/locales/pt.d.ts +13 -0
  106. package/dist/locales/pt.d.ts.map +1 -0
  107. package/dist/locales/pt.js +15 -0
  108. package/dist/locales/pt.js.map +1 -0
  109. package/dist/locales/zh.d.ts +13 -0
  110. package/dist/locales/zh.d.ts.map +1 -0
  111. package/dist/locales/zh.js +15 -0
  112. package/dist/locales/zh.js.map +1 -0
  113. package/dist/messages.d.ts +30 -0
  114. package/dist/messages.d.ts.map +1 -0
  115. package/dist/shared.d.ts +7 -0
  116. package/dist/shared.d.ts.map +1 -0
  117. package/dist/shared.js +25 -0
  118. package/dist/shared.js.map +1 -0
  119. package/dist/vast.d.ts +94 -0
  120. package/dist/vast.d.ts.map +1 -0
  121. package/dist/vast.js +254 -0
  122. package/dist/vast.js.map +1 -0
  123. package/dist/vmap.d.ts +31 -0
  124. package/dist/vmap.d.ts.map +1 -0
  125. package/dist/vmap.js +71 -0
  126. package/dist/vmap.js.map +1 -0
  127. package/package.json +52 -6
@@ -0,0 +1,826 @@
1
+ import { expand, send } from "./beacons.js";
2
+ import _objectSpread2 from "./_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/objectSpread2.js";
3
+ import _asyncToGenerator from "./_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/asyncToGenerator.js";
4
+ import { fetchText, resolveVast } from "./vast.js";
5
+ import { offsetTime, parseVmap } from "./vmap.js";
6
+ // src/controller.ts
7
+ /** Seconds before a break at which its VAST is fetched: fresh, and in time for the preload. */
8
+ const LOOKAHEAD = 10;
9
+ const QUARTILE_EVENTS = [
10
+ "firstQuartile",
11
+ "midpoint",
12
+ "thirdQuartile"
13
+ ];
14
+ /** The keys that seek: the bar's arrows and media keys, the seek bar's Home, End and pages. */
15
+ const SEEK_KEYS = /* @__PURE__ */ new Set([
16
+ "ArrowLeft",
17
+ "ArrowRight",
18
+ "Home",
19
+ "End",
20
+ "PageUp",
21
+ "PageDown",
22
+ "MediaRewind",
23
+ "MediaFastForward",
24
+ "MediaTrackNext",
25
+ "MediaTrackPrevious",
26
+ "j",
27
+ "J",
28
+ "l",
29
+ "L",
30
+ ..."0123456789"
31
+ ]);
32
+ const STREAMING = /mpegurl|dash\+xml/i;
33
+ const controllers = /* @__PURE__ */ new WeakMap();
34
+ /** The ads of `player`, created on first use. */
35
+ function controllerFor(player, options) {
36
+ let found = controllers.get(player);
37
+ if (found === void 0) {
38
+ found = new Controller(player, options !== null && options !== void 0 ? options : {});
39
+ controllers.set(player, found);
40
+ } else if (options !== void 0) found.configure(options);
41
+ return found;
42
+ }
43
+ /** The time form of a scheduled offset. */
44
+ function offsetFrom(value) {
45
+ if (value === "start" || value === "end") return value;
46
+ if (typeof value === "number") return { time: value };
47
+ if (value.endsWith("%")) return { percent: Number(value.slice(0, -1)) };
48
+ return { time: value.split(":").map(Number).reduce((sum, part) => sum * 60 + part, 0) };
49
+ }
50
+ /** The file to play: a streaming one the engine reads, else the progressive one closest above the player's height. */
51
+ function pick(ad, engine, video, height) {
52
+ var _progressive$find;
53
+ const usable = ad.mediaFiles.filter((file) => file.apiFramework === void 0);
54
+ if (engine) {
55
+ const stream = usable.find((file) => STREAMING.test(file.type));
56
+ if (stream !== void 0) return stream;
57
+ }
58
+ const progressive = usable.filter((file) => !STREAMING.test(file.type) && video.canPlayType(file.type) !== "").sort((a, b) => {
59
+ var _a$height, _b$height;
60
+ return ((_a$height = a.height) !== null && _a$height !== void 0 ? _a$height : 0) - ((_b$height = b.height) !== null && _b$height !== void 0 ? _b$height : 0);
61
+ });
62
+ return (_progressive$find = progressive.find((file) => {
63
+ var _file$height;
64
+ return ((_file$height = file.height) !== null && _file$height !== void 0 ? _file$height : 0) >= height;
65
+ })) !== null && _progressive$find !== void 0 ? _progressive$find : progressive[progressive.length - 1];
66
+ }
67
+ var Controller = class {
68
+ constructor(player, options) {
69
+ this.onOpportunity = null;
70
+ this.player = player;
71
+ this.fetchDoc = fetchText;
72
+ this.progressive = "play";
73
+ this.send = send;
74
+ this.stitchedMode = "free";
75
+ this.own = /* @__PURE__ */ new Set();
76
+ this.configure(options);
77
+ this.plans = /* @__PURE__ */ new Map();
78
+ this.current = null;
79
+ this.watchers = /* @__PURE__ */ new Set();
80
+ this.offEngine = null;
81
+ this.lastTime = 0;
82
+ this.holding = false;
83
+ this.fetching = 0;
84
+ this.observer = null;
85
+ this.visibleSince = null;
86
+ this.seq = 0;
87
+ this.done = /* @__PURE__ */ new Set();
88
+ }
89
+ configure(options) {
90
+ var _options$fetch, _options$progressive, _options$beacon, _options$stitched;
91
+ this.fetchDoc = (_options$fetch = options.fetch) !== null && _options$fetch !== void 0 ? _options$fetch : fetchText;
92
+ this.progressive = (_options$progressive = options.progressive) !== null && _options$progressive !== void 0 ? _options$progressive : "play";
93
+ this.send = (_options$beacon = options.beacon) !== null && _options$beacon !== void 0 ? _options$beacon : send;
94
+ this.stitchedMode = (_options$stitched = options.stitched) !== null && _options$stitched !== void 0 ? _options$stitched : "free";
95
+ }
96
+ /** Follows the player until `detach`. */
97
+ attach() {
98
+ const player = this.player;
99
+ const video = player.video;
100
+ const onSource = () => {
101
+ this.reset();
102
+ this.bindEngine();
103
+ const schedule = player.getAttribute("ad-schedule");
104
+ if (schedule !== null && schedule !== "") this.load(schedule);
105
+ };
106
+ const onTime = () => this.tick();
107
+ const onMetadata = () => {
108
+ this.mark();
109
+ this.tick();
110
+ };
111
+ const onPlay = () => {
112
+ if (this.current === null && this.pending("start")) {
113
+ this.holding = true;
114
+ video.pause();
115
+ } else if (this.current === null) {
116
+ const pre = [...this.plans.values()].find((plan) => plan.offset === "start" && plan.status === "progressive");
117
+ if (pre !== void 0) {
118
+ video.pause();
119
+ this.startProgressive(pre);
120
+ }
121
+ }
122
+ this.tick();
123
+ };
124
+ const onEnded = () => {
125
+ var _this$current, _this$engineBreaks;
126
+ if (((_this$current = this.current) === null || _this$current === void 0 ? void 0 : _this$current.interrupt) === true) {
127
+ this.nextProgressive();
128
+ return;
129
+ }
130
+ if (this.current !== null || ((_this$engineBreaks = this.engineBreaks()) === null || _this$engineBreaks === void 0 ? void 0 : _this$engineBreaks.playing) != null) return;
131
+ const post = [...this.plans.values()].find((plan) => plan.offset === "end" && plan.status === "progressive");
132
+ if (post !== void 0) this.startProgressive(post);
133
+ };
134
+ const onError = () => {
135
+ var _current$plan;
136
+ const current = this.current;
137
+ if ((current === null || current === void 0 ? void 0 : current.interrupt) !== true) return;
138
+ if (current.ad !== void 0) for (const url of current.ad.errors) this.send(expand(url, _objectSpread2(_objectSpread2({}, this.macros(current)), {}, { errorCode: 405 })));
139
+ this.emit("aderror", {
140
+ id: (_current$plan = current.plan) === null || _current$plan === void 0 ? void 0 : _current$plan.id,
141
+ code: 405
142
+ });
143
+ this.nextProgressive();
144
+ };
145
+ const onSeeking = () => {
146
+ const current = this.current;
147
+ if ((current === null || current === void 0 ? void 0 : current.interrupt) !== true) return;
148
+ if (Math.abs(video.currentTime - current.last) > .5) video.currentTime = current.last;
149
+ };
150
+ const onAdPause = () => {
151
+ if (this.current !== null) this.adPaused(video);
152
+ };
153
+ const onAdPlay = () => {
154
+ if (this.current !== null) this.adResumed(video);
155
+ };
156
+ const onAdVolume = () => {
157
+ if (this.current !== null && !this.current.stitched) this.adVolume(video);
158
+ };
159
+ const onKey = (event) => {
160
+ if (this.current === null || this.free(this.current)) return;
161
+ if (!SEEK_KEYS.has(event.key)) return;
162
+ if (event.composedPath().some((node) => node instanceof HTMLElement && node.localName.startsWith("mbx-volume"))) return;
163
+ event.preventDefault();
164
+ event.stopImmediatePropagation();
165
+ };
166
+ player.addEventListener("keydown", onKey, true);
167
+ const onMarkers = () => this.spans();
168
+ player.addEventListener("markerschange", onMarkers);
169
+ const scrub = new MutationObserver(() => {
170
+ if (!player.hasAttribute("scrubbing")) this.landed();
171
+ });
172
+ scrub.observe(player, {
173
+ attributes: true,
174
+ attributeFilter: ["scrubbing"]
175
+ });
176
+ player.addEventListener("sourcechange", onSource);
177
+ video.addEventListener("timeupdate", onTime);
178
+ video.addEventListener("play", onPlay);
179
+ video.addEventListener("loadedmetadata", onMetadata);
180
+ video.addEventListener("ended", onEnded);
181
+ video.addEventListener("error", onError);
182
+ video.addEventListener("seeking", onSeeking);
183
+ video.addEventListener("pause", onAdPause);
184
+ video.addEventListener("playing", onAdPlay);
185
+ video.addEventListener("volumechange", onAdVolume);
186
+ this.bindEngine();
187
+ return () => {
188
+ var _this$offEngine;
189
+ scrub.disconnect();
190
+ player.removeEventListener("markerschange", onMarkers);
191
+ player.removeEventListener("keydown", onKey, true);
192
+ player.removeEventListener("sourcechange", onSource);
193
+ video.removeEventListener("timeupdate", onTime);
194
+ video.removeEventListener("play", onPlay);
195
+ video.removeEventListener("loadedmetadata", onMetadata);
196
+ video.removeEventListener("ended", onEnded);
197
+ video.removeEventListener("error", onError);
198
+ video.removeEventListener("seeking", onSeeking);
199
+ video.removeEventListener("pause", onAdPause);
200
+ video.removeEventListener("playing", onAdPlay);
201
+ video.removeEventListener("volumechange", onAdVolume);
202
+ this.reset();
203
+ (_this$offEngine = this.offEngine) === null || _this$offEngine === void 0 || _this$offEngine.call(this);
204
+ this.offEngine = null;
205
+ };
206
+ }
207
+ get state() {
208
+ var _current$plan$ads, _current$plan2, _current$entry$assets, _current$entry, _current$entry$start, _current$entry2, _current$entry$covers, _current$entry3, _ref, _current$ad$duration, _current$ad, _assets$current$index, _ref2, _current$ad$skipOffse, _current$ad2, _current$creative, _current$entry4, _current$ad$clickThro, _current$ad3, _current$creative2, _ref3, _current$plan$id, _current$plan3, _current$entry5, _current$entry6;
209
+ const current = this.current;
210
+ if (current === null) return null;
211
+ const media = this.player.video;
212
+ const ads = (_current$plan$ads = (_current$plan2 = current.plan) === null || _current$plan2 === void 0 ? void 0 : _current$plan2.ads) !== null && _current$plan$ads !== void 0 ? _current$plan$ads : [];
213
+ const assets = (_current$entry$assets = (_current$entry = current.entry) === null || _current$entry === void 0 ? void 0 : _current$entry.assets) !== null && _current$entry$assets !== void 0 ? _current$entry$assets : [];
214
+ const total = Math.max(1, ads.length || assets.length);
215
+ const time = current.stitched ? this.player.video.currentTime - ((_current$entry$start = (_current$entry2 = current.entry) === null || _current$entry2 === void 0 ? void 0 : _current$entry2.start) !== null && _current$entry$start !== void 0 ? _current$entry$start : 0) : media.currentTime;
216
+ const duration = current.stitched ? (_current$entry$covers = (_current$entry3 = current.entry) === null || _current$entry3 === void 0 ? void 0 : _current$entry3.covers) !== null && _current$entry$covers !== void 0 ? _current$entry$covers : NaN : Number.isFinite(media.duration) ? media.duration : (_ref = (_current$ad$duration = (_current$ad = current.ad) === null || _current$ad === void 0 ? void 0 : _current$ad.duration) !== null && _current$ad$duration !== void 0 ? _current$ad$duration : (_assets$current$index = assets[current.index]) === null || _assets$current$index === void 0 ? void 0 : _assets$current$index.duration) !== null && _ref !== void 0 ? _ref : NaN;
217
+ const later = (ads.length > 0 ? ads.map((ad) => ad.duration) : assets.map((asset) => {
218
+ var _asset$duration;
219
+ return (_asset$duration = asset.duration) !== null && _asset$duration !== void 0 ? _asset$duration : NaN;
220
+ })).slice(current.index + 1).reduce((sum, value) => sum + value, 0);
221
+ const free = this.free(current);
222
+ const skipOffset = (_ref2 = (_current$ad$skipOffse = (_current$ad2 = current.ad) === null || _current$ad2 === void 0 ? void 0 : _current$ad2.skipOffset) !== null && _current$ad$skipOffse !== void 0 ? _current$ad$skipOffse : (_current$creative = current.creative) === null || _current$creative === void 0 ? void 0 : _current$creative.skipOffset) !== null && _ref2 !== void 0 ? _ref2 : current.ad === void 0 && !current.stitched ? (_current$entry4 = current.entry) === null || _current$entry4 === void 0 ? void 0 : _current$entry4.skipAfter : void 0;
223
+ const clickThrough = (_current$ad$clickThro = (_current$ad3 = current.ad) === null || _current$ad3 === void 0 ? void 0 : _current$ad3.clickThrough) !== null && _current$ad$clickThro !== void 0 ? _current$ad$clickThro : (_current$creative2 = current.creative) === null || _current$creative2 === void 0 ? void 0 : _current$creative2.clickThrough;
224
+ return _objectSpread2(_objectSpread2(_objectSpread2({
225
+ breakId: (_ref3 = (_current$plan$id = (_current$plan3 = current.plan) === null || _current$plan3 === void 0 ? void 0 : _current$plan3.id) !== null && _current$plan$id !== void 0 ? _current$plan$id : (_current$entry5 = current.entry) === null || _current$entry5 === void 0 ? void 0 : _current$entry5.id) !== null && _ref3 !== void 0 ? _ref3 : "",
226
+ index: current.index + 1,
227
+ total,
228
+ currentTime: time,
229
+ duration,
230
+ remaining: duration - time + later,
231
+ skipIn: free ? 0 : skipOffset === void 0 ? null : Math.max(0, skipOffset - time)
232
+ }, clickThrough !== void 0 ? { clickThrough } : {}), current.ad !== void 0 ? { ad: current.ad } : {}), {}, {
233
+ stitched: current.stitched,
234
+ role: ((_current$entry6 = current.entry) === null || _current$entry6 === void 0 ? void 0 : _current$entry6.role) === "promo" ? "promo" : "ad",
235
+ free
236
+ });
237
+ }
238
+ /** A break the stream plays itself, with no rule of its own, under `stitched: 'free'`. */
239
+ free(current) {
240
+ var _current$creative3;
241
+ const entry = current.entry;
242
+ return current.stitched && this.stitchedMode === "free" && entry !== null && !entry.noSkip && !entry.noJump && ((_current$creative3 = current.creative) === null || _current$creative3 === void 0 ? void 0 : _current$creative3.skipOffset) === void 0;
243
+ }
244
+ watch(fn) {
245
+ this.watchers.add(fn);
246
+ return () => {
247
+ this.watchers.delete(fn);
248
+ };
249
+ }
250
+ load(source) {
251
+ var _this = this;
252
+ return _asyncToGenerator(function* () {
253
+ const url = new URL(source, location.href).href;
254
+ let text;
255
+ _this.fetching += 1;
256
+ try {
257
+ text = yield _this.fetchDoc(url, new AbortController().signal);
258
+ } catch (_unused) {
259
+ _this.fetching -= 1;
260
+ _this.emit("aderror", {
261
+ code: 900,
262
+ url
263
+ });
264
+ _this.release();
265
+ return;
266
+ }
267
+ _this.fetching -= 1;
268
+ const breaks = parseVmap(text, url);
269
+ if (breaks === null) _this.add({
270
+ id: "preroll",
271
+ offset: "start",
272
+ vast: { xml: text },
273
+ tracking: []
274
+ });
275
+ else for (const entry of breaks) _this.add(entry);
276
+ _this.release();
277
+ _this.tick();
278
+ })();
279
+ }
280
+ schedule(breaks) {
281
+ for (const entry of breaks) {
282
+ var _entry$id;
283
+ this.seq += 1;
284
+ this.add({
285
+ id: (_entry$id = entry.id) !== null && _entry$id !== void 0 ? _entry$id : `ad-break-${this.seq}`,
286
+ offset: offsetFrom(entry.offset),
287
+ vast: { url: new URL(entry.vast, location.href).href },
288
+ tracking: []
289
+ });
290
+ }
291
+ this.tick();
292
+ }
293
+ skip() {
294
+ var _this$engineBreaks2;
295
+ const current = this.current;
296
+ const state = this.state;
297
+ if (current === null || state === null || state.skipIn === null || state.skipIn > 0) return;
298
+ if (current.interrupt) {
299
+ current.skipped = true;
300
+ this.track(current, "skip");
301
+ this.emit("adskip", this.detail());
302
+ this.nextProgressive();
303
+ return;
304
+ }
305
+ current.skipped = true;
306
+ this.track(current, "skip");
307
+ this.emit("adskip", this.detail());
308
+ if (current.stitched && current.entry !== null) {
309
+ this.player.video.currentTime = current.entry.start + current.entry.covers;
310
+ return;
311
+ }
312
+ (_this$engineBreaks2 = this.engineBreaks()) === null || _this$engineBreaks2 === void 0 || _this$engineBreaks2.skip();
313
+ }
314
+ click() {
315
+ var _current$ad$clickThro2, _current$ad4, _current$creative4, _current$ad$clickTrac, _current$ad5;
316
+ const current = this.current;
317
+ const target = (_current$ad$clickThro2 = current === null || current === void 0 || (_current$ad4 = current.ad) === null || _current$ad4 === void 0 ? void 0 : _current$ad4.clickThrough) !== null && _current$ad$clickThro2 !== void 0 ? _current$ad$clickThro2 : current === null || current === void 0 || (_current$creative4 = current.creative) === null || _current$creative4 === void 0 ? void 0 : _current$creative4.clickThrough;
318
+ if (current === null || target === void 0) return;
319
+ for (const url of (_current$ad$clickTrac = (_current$ad5 = current.ad) === null || _current$ad5 === void 0 ? void 0 : _current$ad5.clickTracking) !== null && _current$ad$clickTrac !== void 0 ? _current$ad$clickTrac : []) this.send(expand(url, this.macros(current)));
320
+ this.track(current, "clickTracking");
321
+ window.open(target, "_blank", "noopener");
322
+ this.player.video.pause();
323
+ this.emit("adclick", this.detail());
324
+ }
325
+ add(entry) {
326
+ this.plans.set(entry.id, _objectSpread2(_objectSpread2({}, entry), {}, {
327
+ status: "waiting",
328
+ ads: [],
329
+ files: []
330
+ }));
331
+ this.mark();
332
+ }
333
+ /**
334
+ * The seek bar's ticks: the mid-roll breaks of the engine and of the
335
+ * schedule, spans where a break replaces a part, faded once played. A
336
+ * pre-roll and a post-roll sit at the ends and get none.
337
+ */
338
+ mark() {
339
+ var _engine$breaks;
340
+ const marks = [];
341
+ const engine = this.engineBreaks();
342
+ const ids = /* @__PURE__ */ new Set();
343
+ for (const entry of (_engine$breaks = engine === null || engine === void 0 ? void 0 : engine.breaks) !== null && _engine$breaks !== void 0 ? _engine$breaks : []) {
344
+ ids.add(entry.id);
345
+ if (entry.cue !== void 0) continue;
346
+ if (entry.role !== void 0 && entry.role !== "ad" && entry.role !== "promo") continue;
347
+ marks.push(_objectSpread2(_objectSpread2({ start: entry.start }, entry.covers > 0 ? { end: entry.start + entry.covers } : {}), this.done.has(entry.id) ? { played: true } : {}));
348
+ }
349
+ const duration = this.programmeDuration();
350
+ for (const plan of this.plans.values()) {
351
+ if (ids.has(plan.id) || plan.offset === "start" || plan.offset === "end") continue;
352
+ const at = offsetTime(plan.offset, duration);
353
+ if (at === null) continue;
354
+ marks.push(_objectSpread2({ start: at }, plan.status === "done" ? { played: true } : {}));
355
+ }
356
+ this.player.breaks.set("ads", marks);
357
+ this.spans();
358
+ }
359
+ /**
360
+ * The credits and chapters a SCTE-35 section names, as the player shows
361
+ * them: the credits as markers, which give Skip intro and Skip credits,
362
+ * beside the page's own, and the chapters in the chapter menu when nothing
363
+ * else gives chapters.
364
+ */
365
+ spans() {
366
+ var _this$engineBreaks$br, _this$engineBreaks3, _this$player$engine;
367
+ const breaks = (_this$engineBreaks$br = (_this$engineBreaks3 = this.engineBreaks()) === null || _this$engineBreaks3 === void 0 ? void 0 : _this$engineBreaks3.breaks) !== null && _this$engineBreaks$br !== void 0 ? _this$engineBreaks$br : [];
368
+ const credits = breaks.flatMap((entry) => (entry.role === "opening-credits" || entry.role === "closing-credits") && entry.covers > 0 ? [{
369
+ start: entry.start,
370
+ end: entry.start + entry.covers,
371
+ kind: entry.role
372
+ }] : []);
373
+ const markers = this.player.markers;
374
+ const theirs = markers.list.filter((marker) => !this.own.has(marker));
375
+ if (!(credits.length === this.own.size && [...this.own].every((marker, i) => {
376
+ const next = credits[i];
377
+ return next !== void 0 && next.start === marker.start && next.end === marker.end;
378
+ }) && [...this.own].every((marker) => markers.list.includes(marker)))) {
379
+ this.own = new Set(credits);
380
+ markers.set([...theirs, ...credits]);
381
+ }
382
+ const chapters = breaks.filter((entry) => entry.role === "chapter");
383
+ const api = (_this$player$engine = this.player.engine) === null || _this$player$engine === void 0 ? void 0 : _this$player$engine.chapters;
384
+ if (api !== void 0 && chapters.length > 0 && api.source !== "manifest" && api.source !== "app") api.set(chapters.map((entry, i) => _objectSpread2(_objectSpread2({ start: entry.start }, entry.covers > 0 ? { end: entry.start + entry.covers } : {}), {}, { title: `${i + 1}` })));
385
+ }
386
+ pending(offset) {
387
+ if (this.fetching > 0) return true;
388
+ return [...this.plans.values()].some((plan) => plan.offset === offset && (plan.status === "waiting" || plan.status === "resolving"));
389
+ }
390
+ engineBreaks() {
391
+ var _this$player$engine2;
392
+ return (_this$player$engine2 = this.player.engine) === null || _this$player$engine2 === void 0 ? void 0 : _this$player$engine2.interstitials;
393
+ }
394
+ /** The programme's own time, through a break. */
395
+ programmeTime() {
396
+ var _this$engineBreaks$pr, _this$engineBreaks4;
397
+ return (_this$engineBreaks$pr = (_this$engineBreaks4 = this.engineBreaks()) === null || _this$engineBreaks4 === void 0 ? void 0 : _this$engineBreaks4.primary.currentTime) !== null && _this$engineBreaks$pr !== void 0 ? _this$engineBreaks$pr : this.player.video.currentTime;
398
+ }
399
+ programmeDuration() {
400
+ var _this$engineBreaks$pr2, _this$engineBreaks5;
401
+ return (_this$engineBreaks$pr2 = (_this$engineBreaks5 = this.engineBreaks()) === null || _this$engineBreaks5 === void 0 ? void 0 : _this$engineBreaks5.primary.duration) !== null && _this$engineBreaks$pr2 !== void 0 ? _this$engineBreaks$pr2 : this.player.video.duration;
402
+ }
403
+ /** Fetches the VAST of each break as it comes near, and starts the progressive ones on time. */
404
+ tick() {
405
+ this.update();
406
+ if (this.current !== null) {
407
+ this.followAd(this.current);
408
+ return;
409
+ }
410
+ const time = this.programmeTime();
411
+ const duration = this.programmeDuration();
412
+ const seeked = Math.abs(time - this.lastTime) > 2;
413
+ for (const plan of this.plans.values()) {
414
+ const at = offsetTime(plan.offset, duration);
415
+ if (at === null) continue;
416
+ if (plan.status === "waiting" && (plan.offset === "start" || at - time <= LOOKAHEAD)) this.resolve(plan, at);
417
+ else if (plan.status === "progressive" && plan.offset !== "end" && !seeked && this.lastTime <= at && time >= at && plan.offset !== "start") {
418
+ this.startProgressive(plan);
419
+ break;
420
+ }
421
+ }
422
+ this.lastTime = time;
423
+ }
424
+ resolve(plan, at) {
425
+ var _this2 = this;
426
+ return _asyncToGenerator(function* () {
427
+ plan.status = "resolving";
428
+ const result = yield resolveVast(plan.vast, _this2.fetchDoc);
429
+ if (_this2.plans.get(plan.id) !== plan) return;
430
+ const engine = _this2.engineBreaks() !== void 0;
431
+ const height = _this2.player.clientHeight;
432
+ const playable = [];
433
+ const files = [];
434
+ for (const ad of result.ads) {
435
+ const file = pick(ad, engine, _this2.player.video, height);
436
+ if (file === void 0 || !STREAMING.test(file.type) && _this2.progressive === "skip") {
437
+ for (const url of ad.errors) _this2.send(expand(url, { errorCode: 403 }));
438
+ continue;
439
+ }
440
+ playable.push(ad);
441
+ files.push(file);
442
+ }
443
+ if (result.ads.length === 0) {
444
+ var _result$code;
445
+ for (const url of result.errors) _this2.send(expand(url, { errorCode: (_result$code = result.code) !== null && _result$code !== void 0 ? _result$code : 303 }));
446
+ }
447
+ plan.ads = playable;
448
+ plan.files = files;
449
+ plan.start = at;
450
+ if (playable.length === 0) {
451
+ var _result$code2;
452
+ plan.status = "done";
453
+ _this2.vmapTrack(plan, "error");
454
+ _this2.emit("aderror", {
455
+ id: plan.id,
456
+ code: (_result$code2 = result.code) !== null && _result$code2 !== void 0 ? _result$code2 : 403
457
+ });
458
+ _this2.release();
459
+ return;
460
+ }
461
+ const streams = files.every((file) => STREAMING.test(file.type));
462
+ if (engine && streams) {
463
+ var _this$engineBreaks6;
464
+ plan.status = "stream";
465
+ const offsets = playable.flatMap((ad) => ad.skipOffset === void 0 ? [] : [ad.skipOffset]);
466
+ if (_this2.holding) {
467
+ var _this$player$video$pl;
468
+ _this2.holding = false;
469
+ (_this$player$video$pl = _this2.player.video.play()) === null || _this$player$video$pl === void 0 || _this$player$video$pl.catch(() => {});
470
+ }
471
+ (_this$engineBreaks6 = _this2.engineBreaks()) === null || _this$engineBreaks6 === void 0 || _this$engineBreaks6.schedule(_objectSpread2(_objectSpread2(_objectSpread2({
472
+ id: plan.id,
473
+ start: at
474
+ }, plan.offset === "start" ? { cue: "pre" } : plan.offset === "end" ? { cue: "post" } : {}), plan.covers !== void 0 ? { covers: plan.covers } : {}), {}, {
475
+ assets: files.map((file, i) => {
476
+ var _playable$i$duration, _playable$i;
477
+ return {
478
+ url: file.url,
479
+ duration: (_playable$i$duration = (_playable$i = playable[i]) === null || _playable$i === void 0 ? void 0 : _playable$i.duration) !== null && _playable$i$duration !== void 0 ? _playable$i$duration : 0,
480
+ mimeType: /dash/i.test(file.type) ? "application/dash+xml" : "application/vnd.apple.mpegurl"
481
+ };
482
+ }),
483
+ noSkip: true,
484
+ noJump: true,
485
+ once: true
486
+ }, offsets.length > 0 ? { skipAfter: Math.min(...offsets) } : {}));
487
+ return;
488
+ }
489
+ plan.status = "progressive";
490
+ if (plan.offset === "start" && (_this2.holding || !_this2.player.video.paused)) {
491
+ _this2.holding = false;
492
+ _this2.player.video.pause();
493
+ _this2.startProgressive(plan);
494
+ }
495
+ })();
496
+ }
497
+ /** Lets the programme play once no pre-roll holds it. */
498
+ release() {
499
+ var _this$player$video$pl2;
500
+ if (!this.holding || this.pending("start")) return;
501
+ this.holding = false;
502
+ (_this$player$video$pl2 = this.player.video.play()) === null || _this$player$video$pl2 === void 0 || _this$player$video$pl2.catch(() => {});
503
+ }
504
+ bindEngine() {
505
+ var _this$offEngine2;
506
+ (_this$offEngine2 = this.offEngine) === null || _this$offEngine2 === void 0 || _this$offEngine2.call(this);
507
+ this.offEngine = null;
508
+ const engine = this.player.engine;
509
+ if (engine === null || this.engineBreaks() === void 0) return;
510
+ const on = (name, fn) => engine.on(`interstitials:${name}`, (payload) => fn(payload));
511
+ const offs = [
512
+ on("break-start", (p) => {
513
+ if (p.stitched === true && this.player.hasAttribute("scrubbing")) return;
514
+ this.engineBreakStart(p.break, p.stitched === true);
515
+ }),
516
+ on("asset-start", (p) => this.engineAssetStart(Number(p.asset))),
517
+ on("progress", (p) => this.quartile(Number(p.quartile))),
518
+ on("asset-end", () => this.adEnd()),
519
+ on("break-end", (p) => {
520
+ if (p.stitched === true && this.player.hasAttribute("scrubbing")) return;
521
+ this.breakEnd();
522
+ }),
523
+ on("warning", (p) => {
524
+ const current = this.current;
525
+ if ((current === null || current === void 0 ? void 0 : current.ad) !== void 0) for (const url of current.ad.errors) this.send(expand(url, _objectSpread2(_objectSpread2({}, this.macros(current)), {}, { errorCode: 405 })));
526
+ this.emit("aderror", {
527
+ id: p.id,
528
+ code: 405,
529
+ reason: p.reason
530
+ });
531
+ }),
532
+ on("opportunity", (p) => this.opportunity(p.break, Number(p.duration)))
533
+ ];
534
+ offs.push(engine.on("interstitials:changed", () => this.mark()));
535
+ this.offEngine = () => {
536
+ for (const off of offs) off();
537
+ };
538
+ this.mark();
539
+ }
540
+ /** After a drag: in an ad the stream plays, it starts; out of it, the ad that showed ends. */
541
+ landed() {
542
+ var _this$engineBreaks$br2, _this$engineBreaks7, _current$entry7;
543
+ const time = this.player.video.currentTime;
544
+ const inside = ((_this$engineBreaks$br2 = (_this$engineBreaks7 = this.engineBreaks()) === null || _this$engineBreaks7 === void 0 ? void 0 : _this$engineBreaks7.breaks) !== null && _this$engineBreaks$br2 !== void 0 ? _this$engineBreaks$br2 : []).find((entry) => entry.assets.length === 0 && entry.assetList === void 0 && time >= entry.start && time < entry.start + Math.max(entry.covers, .001));
545
+ const current = this.current;
546
+ if ((current === null || current === void 0 ? void 0 : current.stitched) === true && ((_current$entry7 = current.entry) === null || _current$entry7 === void 0 ? void 0 : _current$entry7.id) !== (inside === null || inside === void 0 ? void 0 : inside.id)) this.breakEnd();
547
+ if (inside !== void 0 && this.current === null) this.engineBreakStart(inside, true);
548
+ }
549
+ engineBreakStart(entry, stitched) {
550
+ var _this$plans$get;
551
+ const plan = (_this$plans$get = this.plans.get(entry.id)) !== null && _this$plans$get !== void 0 ? _this$plans$get : null;
552
+ this.current = {
553
+ plan,
554
+ entry,
555
+ stitched,
556
+ index: -1,
557
+ ad: void 0,
558
+ creative: void 0,
559
+ file: void 0,
560
+ skipped: false,
561
+ quartiles: 0,
562
+ sent: /* @__PURE__ */ new Set(),
563
+ viewable: false,
564
+ paused: false,
565
+ interrupt: false,
566
+ last: 0,
567
+ at: this.programmeTime()
568
+ };
569
+ this.breakStart();
570
+ if (stitched) this.engineAssetStart(0);
571
+ }
572
+ engineAssetStart(index) {
573
+ var _current$plan4, _current$entry8, _current$entry9, _current$plan5;
574
+ const current = this.current;
575
+ if (current === null) return;
576
+ current.index = index;
577
+ current.ad = (_current$plan4 = current.plan) === null || _current$plan4 === void 0 ? void 0 : _current$plan4.ads[index];
578
+ current.creative = current.stitched ? (_current$entry8 = current.entry) === null || _current$entry8 === void 0 || (_current$entry8 = _current$entry8.creatives) === null || _current$entry8 === void 0 ? void 0 : _current$entry8[0] : (_current$entry9 = current.entry) === null || _current$entry9 === void 0 || (_current$entry9 = _current$entry9.assets[index]) === null || _current$entry9 === void 0 || (_current$entry9 = _current$entry9.creatives) === null || _current$entry9 === void 0 ? void 0 : _current$entry9[0];
579
+ current.file = (_current$plan5 = current.plan) === null || _current$plan5 === void 0 ? void 0 : _current$plan5.files[index];
580
+ current.skipped = false;
581
+ current.quartiles = 0;
582
+ current.sent = /* @__PURE__ */ new Set();
583
+ current.viewable = false;
584
+ this.adStart(current);
585
+ }
586
+ opportunity(entry, duration) {
587
+ var _this$onOpportunity;
588
+ const url = (_this$onOpportunity = this.onOpportunity) === null || _this$onOpportunity === void 0 ? void 0 : _this$onOpportunity.call(this, {
589
+ id: entry.id,
590
+ start: entry.start,
591
+ duration
592
+ });
593
+ if (url === null || url === void 0) return;
594
+ this.add({
595
+ id: entry.id,
596
+ offset: { time: entry.start },
597
+ vast: { url: new URL(url, location.href).href },
598
+ tracking: [],
599
+ covers: duration
600
+ });
601
+ const plan = this.plans.get(entry.id);
602
+ if (plan !== void 0) this.resolve(plan, entry.start);
603
+ }
604
+ /** A break of progressive ads: each in the programme's place, then the programme again. */
605
+ startProgressive(plan) {
606
+ plan.status = "done";
607
+ this.current = {
608
+ plan,
609
+ entry: null,
610
+ stitched: false,
611
+ index: -1,
612
+ ad: void 0,
613
+ creative: void 0,
614
+ file: void 0,
615
+ skipped: false,
616
+ quartiles: 0,
617
+ sent: /* @__PURE__ */ new Set(),
618
+ viewable: false,
619
+ paused: false,
620
+ interrupt: true,
621
+ last: 0,
622
+ at: this.programmeTime()
623
+ };
624
+ this.breakStart();
625
+ this.nextProgressive();
626
+ }
627
+ nextProgressive() {
628
+ const current = this.current;
629
+ if (current === null || !current.interrupt || current.plan === null) return;
630
+ if (current.index >= 0) this.adEnd();
631
+ current.index += 1;
632
+ const ad = current.plan.ads[current.index];
633
+ const file = current.plan.files[current.index];
634
+ if (ad === void 0 || file === void 0) {
635
+ var _plan$start;
636
+ const plan = current.plan;
637
+ this.breakEnd();
638
+ this.player.resume(plan.offset === "end" ? {
639
+ at: (_plan$start = plan.start) !== null && _plan$start !== void 0 ? _plan$start : null,
640
+ playing: false
641
+ } : { playing: true });
642
+ return;
643
+ }
644
+ current.ad = ad;
645
+ current.file = file;
646
+ current.skipped = false;
647
+ current.quartiles = 0;
648
+ current.sent = /* @__PURE__ */ new Set();
649
+ current.viewable = false;
650
+ current.paused = false;
651
+ current.last = 0;
652
+ this.player.interrupt({
653
+ url: file.url,
654
+ type: file.type
655
+ }).catch(() => {
656
+ var _current$plan6;
657
+ if (this.current !== current) return;
658
+ for (const url of ad.errors) this.send(expand(url, _objectSpread2(_objectSpread2({}, this.macros(current)), {}, { errorCode: 405 })));
659
+ this.emit("aderror", {
660
+ id: (_current$plan6 = current.plan) === null || _current$plan6 === void 0 ? void 0 : _current$plan6.id,
661
+ code: 405
662
+ });
663
+ this.nextProgressive();
664
+ });
665
+ this.adStart(current);
666
+ }
667
+ breakStart() {
668
+ const current = this.current;
669
+ if (current === null) return;
670
+ this.player.setAttribute("ad", this.free(current) ? "stitched" : "");
671
+ if (current.plan !== null) this.vmapTrack(current.plan, "breakStart");
672
+ this.observe();
673
+ this.emit("adbreakstart", this.detail());
674
+ }
675
+ adStart(current) {
676
+ if (current.ad !== void 0) for (const url of current.ad.impressions) this.send(expand(url, this.macros(current)));
677
+ this.track(current, "impression");
678
+ this.track(current, "creativeView");
679
+ this.track(current, "start");
680
+ this.visibleSince = null;
681
+ this.emit("adstart", this.detail());
682
+ this.update();
683
+ }
684
+ quartile(n) {
685
+ const current = this.current;
686
+ if (current === null || n <= current.quartiles) return;
687
+ current.quartiles = n;
688
+ const event = QUARTILE_EVENTS[n - 1];
689
+ if (event !== void 0) this.track(current, event);
690
+ this.emit("adprogress", _objectSpread2(_objectSpread2({}, this.detail()), {}, { quartile: n }));
691
+ }
692
+ /** The ad's own time: progressive quartiles, progress beacons, the stream's callbacks, viewability. */
693
+ followAd(current) {
694
+ var _current$entry$start2, _current$entry10, _current$ad$tracking, _current$ad6, _current$entry$assets2, _current$entry11;
695
+ const media = this.player.video;
696
+ const time = current.stitched ? this.player.video.currentTime - ((_current$entry$start2 = (_current$entry10 = current.entry) === null || _current$entry10 === void 0 ? void 0 : _current$entry10.start) !== null && _current$entry$start2 !== void 0 ? _current$entry$start2 : 0) : media.currentTime;
697
+ if (current.interrupt && !media.seeking) current.last = time;
698
+ if (current.interrupt && Number.isFinite(media.duration) && media.duration > 0) {
699
+ const reached = Math.floor(time / media.duration * 4);
700
+ for (let n = current.quartiles + 1; n <= Math.min(3, reached); n += 1) this.quartile(n);
701
+ }
702
+ ((_current$ad$tracking = (_current$ad6 = current.ad) === null || _current$ad6 === void 0 ? void 0 : _current$ad6.tracking) !== null && _current$ad$tracking !== void 0 ? _current$ad$tracking : []).forEach((entry, i) => {
703
+ if (entry.event !== "progress" || entry.offset === void 0 || current.sent.has(i) || time < entry.offset) return;
704
+ current.sent.add(i);
705
+ this.send(expand(entry.url, this.macros(current)));
706
+ });
707
+ ((_current$entry$assets2 = (_current$entry11 = current.entry) === null || _current$entry11 === void 0 || (_current$entry11 = _current$entry11.assets[current.index]) === null || _current$entry11 === void 0 ? void 0 : _current$entry11.tracking) !== null && _current$entry$assets2 !== void 0 ? _current$entry$assets2 : []).forEach((entry, i) => {
708
+ const key = 1e3 + i;
709
+ if (current.sent.has(key) || time < entry.offset) return;
710
+ current.sent.add(key);
711
+ this.send(entry.url);
712
+ });
713
+ this.view(current);
714
+ }
715
+ adPaused(media) {
716
+ const current = this.current;
717
+ if (current === null || media.ended || current.paused) return;
718
+ current.paused = true;
719
+ this.track(current, "pause");
720
+ }
721
+ adResumed(media) {
722
+ const current = this.current;
723
+ if (current === null || !current.paused || media !== this.player.video) return;
724
+ current.paused = false;
725
+ this.track(current, "resume");
726
+ }
727
+ adVolume(media) {
728
+ const current = this.current;
729
+ if (current === null) return;
730
+ this.track(current, media.muted ? "mute" : "unmute");
731
+ }
732
+ adEnd() {
733
+ const current = this.current;
734
+ if (current === null || current.index < 0) return;
735
+ if (!current.skipped) this.track(current, "complete");
736
+ if (current.ad !== void 0 && !current.viewable) for (const url of current.ad.notViewable) this.send(expand(url, this.macros(current)));
737
+ this.emit("adend", this.detail());
738
+ }
739
+ breakEnd() {
740
+ var _current$plan$id2, _current$plan7, _current$entry12, _this$observer;
741
+ const current = this.current;
742
+ if (current === null) return;
743
+ if (current.stitched) this.adEnd();
744
+ if (current.plan !== null) {
745
+ current.plan.status = "done";
746
+ this.vmapTrack(current.plan, "breakEnd");
747
+ }
748
+ const detail = this.detail();
749
+ const id = (_current$plan$id2 = (_current$plan7 = current.plan) === null || _current$plan7 === void 0 ? void 0 : _current$plan7.id) !== null && _current$plan$id2 !== void 0 ? _current$plan$id2 : (_current$entry12 = current.entry) === null || _current$entry12 === void 0 ? void 0 : _current$entry12.id;
750
+ if (id !== void 0) this.done.add(id);
751
+ this.current = null;
752
+ this.player.removeAttribute("ad");
753
+ this.mark();
754
+ (_this$observer = this.observer) === null || _this$observer === void 0 || _this$observer.disconnect();
755
+ this.observer = null;
756
+ this.emit("adbreakend", detail);
757
+ this.update();
758
+ }
759
+ /** MRC: half the player in view for two seconds in a row. */
760
+ observe() {
761
+ if (typeof IntersectionObserver === "undefined" || this.observer !== null) return;
762
+ this.observer = new IntersectionObserver((entries) => {
763
+ var _this$visibleSince;
764
+ const seen = entries.some((entry) => entry.intersectionRatio >= .5);
765
+ this.visibleSince = seen ? (_this$visibleSince = this.visibleSince) !== null && _this$visibleSince !== void 0 ? _this$visibleSince : performance.now() : null;
766
+ }, { threshold: [0, .5] });
767
+ this.observer.observe(this.player);
768
+ }
769
+ view(current) {
770
+ if (current.viewable || current.ad === void 0 || this.visibleSince === null) return;
771
+ if (performance.now() - this.visibleSince < 2e3) return;
772
+ current.viewable = true;
773
+ for (const url of current.ad.viewable) this.send(expand(url, this.macros(current)));
774
+ this.emit("adviewable", this.detail());
775
+ }
776
+ macros(current) {
777
+ var _current$plan8;
778
+ const media = this.player.video;
779
+ const offset = (_current$plan8 = current.plan) === null || _current$plan8 === void 0 ? void 0 : _current$plan8.offset;
780
+ return _objectSpread2(_objectSpread2({
781
+ mediaPlayhead: current.at,
782
+ adPlayhead: media.currentTime
783
+ }, current.file !== void 0 ? { assetUri: current.file.url } : {}), {}, {
784
+ breakPosition: offset === "start" ? 1 : offset === "end" ? 3 : 2,
785
+ playerSize: [this.player.clientWidth, this.player.clientHeight]
786
+ });
787
+ }
788
+ track(current, event) {
789
+ var _current$ad$tracking2, _current$ad7;
790
+ for (const entry of (_current$ad$tracking2 = (_current$ad7 = current.ad) === null || _current$ad7 === void 0 ? void 0 : _current$ad7.tracking) !== null && _current$ad$tracking2 !== void 0 ? _current$ad$tracking2 : []) if (entry.event === event) this.send(expand(entry.url, this.macros(current)));
791
+ }
792
+ vmapTrack(plan, event) {
793
+ for (const entry of plan.tracking) if (entry.event === event) this.send(entry.url);
794
+ }
795
+ detail() {
796
+ var _ref4, _current$plan$id3, _current$plan9, _current$entry13, _current$index, _current$ad8, _current$stitched;
797
+ const current = this.current;
798
+ return {
799
+ breakId: (_ref4 = (_current$plan$id3 = current === null || current === void 0 || (_current$plan9 = current.plan) === null || _current$plan9 === void 0 ? void 0 : _current$plan9.id) !== null && _current$plan$id3 !== void 0 ? _current$plan$id3 : current === null || current === void 0 || (_current$entry13 = current.entry) === null || _current$entry13 === void 0 ? void 0 : _current$entry13.id) !== null && _ref4 !== void 0 ? _ref4 : null,
800
+ index: ((_current$index = current === null || current === void 0 ? void 0 : current.index) !== null && _current$index !== void 0 ? _current$index : 0) + 1,
801
+ ad: (_current$ad8 = current === null || current === void 0 ? void 0 : current.ad) !== null && _current$ad8 !== void 0 ? _current$ad8 : null,
802
+ stitched: (_current$stitched = current === null || current === void 0 ? void 0 : current.stitched) !== null && _current$stitched !== void 0 ? _current$stitched : false
803
+ };
804
+ }
805
+ emit(name, detail) {
806
+ this.player.dispatchEvent(new CustomEvent(name, { detail }));
807
+ }
808
+ update() {
809
+ for (const fn of [...this.watchers]) fn();
810
+ }
811
+ reset() {
812
+ var _this$observer2;
813
+ if (this.current !== null) this.player.removeAttribute("ad");
814
+ this.current = null;
815
+ this.plans = /* @__PURE__ */ new Map();
816
+ this.done = /* @__PURE__ */ new Set();
817
+ this.own = /* @__PURE__ */ new Set();
818
+ this.player.breaks.set("ads", []);
819
+ this.holding = false;
820
+ this.lastTime = 0;
821
+ (_this$observer2 = this.observer) === null || _this$observer2 === void 0 || _this$observer2.disconnect();
822
+ this.observer = null;
823
+ this.update();
824
+ }
825
+ };
826
+ export { Controller, controllerFor };