'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-0f211bec.js');
const resolveLocation = require('./resolve-location-afb71019.js');

const BceElementVideo = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.resolveLocation = resolveLocation.ResolveLocation.NOOP;
        this.url = '';
        this.youtube = false;
    }
    componentWillLoad() {
        if (!this.template)
            return;
        if (this.template.location.startsWith('BCE://')) {
            this.resolveLocation(this.template.location).then(url => (this.url = url));
            return;
        }
        const match1 = this.template.location.match(/youtube.com\/watch\?v=([^&]+)/);
        if (match1) {
            this.youtube = true;
            this.url = this.createYouTubeURL(match1[1]);
            return;
        }
        const match2 = this.template.location.match(/youtu\.be\/([^\?]+)/);
        if (match2) {
            this.youtube = true;
            this.url = this.createYouTubeURL(match2[1]);
            return;
        }
        this.url = this.template.location;
    }
    createYouTubeURL(id) {
        const url = this.template && new URL(this.template.location);
        const path = 'https://www.youtube.com/embed/' + id;
        const start = (url && url.searchParams.get('t')) || 0;
        const autoplay = this.template && this.template.autoplay;
        return `${path}?start=${start}&autoplay=${autoplay}`;
    }
    renderVideo() {
        if (!this.template || !this.url)
            return null;
        const { autoplay } = this.template;
        return (core.h("video", { key: this.url, autoplay: autoplay, muted: autoplay, playsInline: autoplay, controls: true }, core.h("source", { src: this.url })));
    }
    renderYouTube() {
        return [
            core.h("img", { src: "http://placehold.it/16x9" }),
            core.h("iframe", { src: this.url, frameborder: "0", allowFullScreen: true })
        ];
    }
    render() {
        return this.youtube ? this.renderYouTube() : this.renderVideo();
    }
    static get style() { return "bce-element-video{position:relative;display:block;margin:1rem auto;max-width:1200px;text-align:center}bce-element-video img,bce-element-video video{width:100%;height:auto}bce-element-video img{opacity:0}bce-element-video iframe{position:absolute;top:0;left:0;width:100%;height:100%}\@media screen and (min-width:1024px){bce-element-video{text-align:left;width:auto}}"; }
};

exports.bce_element_video = BceElementVideo;
