{"version":3,"file":"video-d3r.js","names":[],"sources":["../resources/javascript/video-d3r.js"],"sourcesContent":["// Shared across every video-d3r instance so hls.js is fetched at most once,\n// only for browsers (e.g. Firefox) that can't play HLS natively.\nlet hlsModulePromise = null;\n\nfunction loadHls() {\n    hlsModulePromise ??= import('hls.js').then(m => m.default);\n    return hlsModulePromise;\n}\n\nclass VideoD3R extends HTMLElement {\n    #hls = null;\n    #hlsGeneration = 0;\n\n    connectedCallback() {\n        const root = this.#attachShadowRoot();\n        this.#setUpHls();\n    }\n\n    disconnectedCallback() {\n        this.#hlsGeneration++;\n        this.#hls?.destroy();\n        this.#hls = null;\n\n        const video = this.shadowRoot?.querySelector('video');\n        if (video) delete video.dataset.hlsChecked;\n    }\n\n    // Declarative shadow DOM (<template shadowrootmode>) is only promoted\n    // automatically by the browser's streaming HTML parser. Markup inserted\n    // later via innerHTML/AJAX (e.g. a dialog or overlay) leaves the template\n    // inert in the light DOM, so fall back to attaching it manually.\n    #attachShadowRoot() {\n        if (this.shadowRoot) return this.shadowRoot;\n\n        const template = this.querySelector('template[shadowrootmode]');\n        if (!template) return null;\n\n        const root = this.attachShadow({\n            mode: template.getAttribute('shadowrootmode'),\n        });\n        root.appendChild(template.content);\n        template.remove();\n        return root;\n    }\n\n    #setUpHls() {\n        const video = this.video;\n        const source = video?.querySelector('source');\n        if (!video || !source || video.dataset.hlsChecked) return;\n        video.dataset.hlsChecked = 'true';\n\n        if (\n            ![\n                'application/x-mpegurl',\n                'application/vnd.apple.mpegurl',\n            ].includes(source.type.toLowerCase())\n        ) {\n            return; // e.g. mp4 — leave it to the browser's native handling\n        }\n\n        const src = source.src;\n        // `force-hls` opts out of native playback even where it's supported (e.g.\n        // Safari) so hls.js's ABR can pick a better starting quality than the\n        // native heuristic, which tends to start conservative.\n        if (\n            !this.hasAttribute('force-hls') &&\n            video.canPlayType('application/vnd.apple.mpegurl')\n        ) {\n            return; // Safari / Chrome 142+ already picked up the <source> natively\n        }\n\n        const generation = this.#hlsGeneration;\n        loadHls().then(Hls => {\n            if (\n                !this.isConnected ||\n                generation !== this.#hlsGeneration ||\n                !Hls.isSupported()\n            ) {\n                return;\n            }\n            this.#hls = new Hls();\n            this.#hls.loadSource(src);\n            this.#hls.attachMedia(video);\n        });\n    }\n\n    // The one deliberate escape hatch: JS can still get a real handle on the\n    // video (e.g. for a player SDK), even though CSS and querySelector from\n    // outside cannot.\n    get video() {\n        return this.shadowRoot.querySelector('video');\n    }\n}\n\ncustomElements.define('video-d3r', VideoD3R);\n"],"mappings":"AAEA,IAAI,EAAmB,KAEvB,SAAS,GAAU,CAEf,MADA,KAAqB,OAAO,0BAAS,CAAC,KAAK,GAAK,EAAE,OAAO,EAClD,CACX,CAEA,IAAM,EAAN,cAAuB,WAAY,CAC/B,GAAO,KACP,GAAiB,EAEjB,mBAAoB,CACH,KAAK,GAAkB,EACpC,KAAK,GAAU,CACnB,CAEA,sBAAuB,CACnB,KAAK,KACL,KAAK,IAAM,QAAQ,EACnB,KAAK,GAAO,KAEZ,IAAM,EAAQ,KAAK,YAAY,cAAc,OAAO,EAChD,GAAO,OAAO,EAAM,QAAQ,UACpC,CAMA,IAAoB,CAChB,GAAI,KAAK,WAAY,OAAO,KAAK,WAEjC,IAAM,EAAW,KAAK,cAAc,0BAA0B,EAC9D,GAAI,CAAC,EAAU,OAAO,KAEtB,IAAM,EAAO,KAAK,aAAa,CAC3B,KAAM,EAAS,aAAa,gBAAgB,CAChD,CAAC,EAGD,OAFA,EAAK,YAAY,EAAS,OAAO,EACjC,EAAS,OAAO,EACT,CACX,CAEA,IAAY,CACR,IAAM,EAAQ,KAAK,MACb,EAAS,GAAO,cAAc,QAAQ,EAI5C,GAHI,CAAC,GAAS,CAAC,GAAU,EAAM,QAAQ,aACvC,EAAM,QAAQ,WAAa,OAGvB,CAAC,CACG,wBACA,+BACJ,CAAC,CAAC,SAAS,EAAO,KAAK,YAAY,CAAC,GAEpC,OAGJ,IAAM,EAAM,EAAO,IAInB,GACI,CAAC,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