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