"use strict";
// Copyright (c) Rotorz Limited and portions by original markdown-it-video authors
// Licensed under the MIT license. See LICENSE file in the project root.
Object.defineProperty(exports, "__esModule", { value: true });
const defaultUrlFilter = (url) => url;
class VideoServiceBase {
    constructor(name, options, env) {
        this.name = name;
        this.options = Object.assign(this.getDefaultOptions(), options);
        this.env = env;
    }
    getDefaultOptions() {
        return {};
    }
    /**
     * Overridden by child classes to extract the ID from a URL/reference.
     */
    extractVideoID(reference) {
        return reference;
    }
    getFilteredVideoUrl(videoID) {
        let filterUrlDelegate = typeof this.env.options.filterUrl === 'function'
            ? this.env.options.filterUrl
            : defaultUrlFilter;
        let videoUrl = this.getVideoUrl(videoID);
        return filterUrlDelegate(videoUrl, this.name, videoID, this.env.options);
    }
    getEmbedCode(videoID) {
        let containerClassNames = [];
        if (this.env.options.containerClassName && this.options.ignoreStyle !== true) {
            containerClassNames.push(this.env.options.containerClassName);
        }
        let escapedServiceName = this.env.md.utils.escapeHtml(this.name);
        containerClassNames.push(this.env.options.serviceClassPrefix + escapedServiceName);
        const containerStyles = [];
        if (this.options.ignoreStyle !== true) {
            containerStyles.push('position: relative;');
            if (this.options.height !== undefined && this.options.width !== undefined) {
                containerStyles.push(`padding-bottom: ${this.options.height / this.options.width * 100}%`);
            }
        }
        let iframeAttributeList = [];
        iframeAttributeList.push(["type", "text/html"]);
        iframeAttributeList.push(["src", this.getFilteredVideoUrl(videoID)]);
        iframeAttributeList.push(["frameborder", 0]);
        if (this.env.options.outputPlayerSize === true && this.options.ignoreStyle === true) {
            if (this.options.width !== undefined && this.options.width !== null) {
                iframeAttributeList.push(["width", this.options.width]);
            }
            if (this.options.height !== undefined && this.options.height !== null) {
                iframeAttributeList.push(["height", this.options.height]);
            }
        }
        if (this.env.options.allowFullScreen === true) {
            iframeAttributeList.push(["webkitallowfullscreen"]);
            iframeAttributeList.push(["mozallowfullscreen"]);
            iframeAttributeList.push(["allowfullscreen"]);
        }
        let iframeAttributes = iframeAttributeList
            .map(([key, value]) => (value !== undefined ? `${key}="${value}"` : key))
            .join(" ");
        return `<div class="${containerClassNames.join(" ")}" style="${containerStyles.join(" ")}">`
            + `<iframe ${iframeAttributes}></iframe>`
            + `</div>\n`;
    }
}
exports.default = VideoServiceBase;
//# sourceMappingURL=VideoServiceBase.js.map