import { DEFAULT_REFRESH_INTERVAL } from "../constants";
import { createCompositeElement, createElement, createMetricTitle, isExtendedMode, } from "../utils";
export default function constructDebugGeneralInfo(instance, parentElt, cancelSignal) {
    var generalInfoElt = createElement("div");
    var adaptationsElt = createElement("div");
    var representationsElt = createElement("div");
    updateGeneralInfo();
    var generalInfoItv = setInterval(function () {
        updateGeneralInfo();
    }, DEFAULT_REFRESH_INTERVAL);
    cancelSignal.register(function () {
        clearInterval(generalInfoItv);
    });
    return createCompositeElement("div", [
        generalInfoElt,
        adaptationsElt,
        representationsElt,
    ]);
    function updateGeneralInfo() {
        var _a, _b, _c, _d;
        var videoElement = instance.getVideoElement();
        if (videoElement === null) {
            // disposed player. Clean-up everything
            generalInfoElt.innerHTML = "";
            adaptationsElt.innerHTML = "";
            representationsElt.innerHTML = "";
            clearInterval(generalInfoItv);
            return;
        }
        else {
            var currentTime = instance.getPosition();
            var bufferGap = instance.getVideoBufferGap();
            var bufferGapStr = bufferGap === Infinity ? "0" : bufferGap.toFixed(2);
            var valuesLine1 = [
                ["ct", currentTime.toFixed(2)],
                ["bg", bufferGapStr],
                ["rs", String(videoElement.readyState)],
                ["pr", String(videoElement.playbackRate)],
                ["sp", String(instance.getPlaybackRate())],
                ["pa", String(videoElement.paused ? 1 : 0)],
                ["en", String(videoElement.ended ? 1 : 0)],
                ["li", String(instance.isLive() ? 1 : 0)],
                ["wba", String(instance.getWantedBufferAhead())],
                ["st", "\"".concat(instance.getPlayerState(), "\"")],
            ];
            var valuesLine2 = [];
            var ks = instance.getKeySystemConfiguration();
            if (ks !== null) {
                valuesLine2.push(["ks", ks.keySystem]);
            }
            var mbb = instance.getMaxBufferBehind();
            if (mbb !== Infinity) {
                valuesLine2.push(["mbb", String(mbb)]);
            }
            var mba = instance.getMaxBufferAhead();
            if (mba !== Infinity) {
                valuesLine2.push(["mba", String(mba)]);
            }
            var mia = instance.getMinAudioBitrate();
            if (mia !== 0) {
                valuesLine2.push(["mia", String(mia)]);
            }
            var miv = instance.getMinVideoBitrate();
            if (miv !== 0) {
                valuesLine2.push(["miv", String(miv)]);
            }
            var maa = instance.getMaxAudioBitrate();
            if (maa !== Infinity) {
                valuesLine2.push(["maa", String(maa)]);
            }
            var mav = instance.getMaxVideoBitrate();
            if (mav !== Infinity) {
                valuesLine2.push(["mav", String(mav)]);
            }
            var fab = instance.getManualAudioBitrate();
            if (fab >= 0) {
                valuesLine2.push(["fab", String(fab)]);
            }
            var fvb = instance.getManualVideoBitrate();
            if (fvb >= 0) {
                valuesLine2.push(["fvb", String(fvb)]);
            }
            var mbs = instance.getMaxVideoBufferSize();
            if (mbs !== Infinity) {
                valuesLine2.push(["mbs", String(mbs)]);
            }
            var minPos = instance.getMinimumPosition();
            if (minPos !== null) {
                valuesLine1.push(["mip", minPos.toFixed(2)]);
                valuesLine2.push(["dmi", (currentTime - minPos).toFixed(2)]);
            }
            var maxPos = instance.getMaximumPosition();
            if (maxPos !== null) {
                valuesLine1.push(["map", maxPos.toFixed(2)]);
                valuesLine2.push(["dma", (maxPos - currentTime).toFixed(2)]);
            }
            var valuesLine3 = [];
            var error = instance.getError();
            if (error !== null) {
                valuesLine3.push(["er", "\"".concat(String(error), "\"")]);
            }
            generalInfoElt.innerHTML = "";
            for (var _i = 0, _e = [valuesLine1, valuesLine2, valuesLine3]; _i < _e.length; _i++) {
                var valueSet = _e[_i];
                if (valueSet.length > 0) {
                    var lineInfoElt = createElement("div");
                    for (var _f = 0, valueSet_1 = valueSet; _f < valueSet_1.length; _f++) {
                        var value = valueSet_1[_f];
                        lineInfoElt.appendChild(createMetricTitle(value[0]));
                        lineInfoElt.appendChild(createElement("span", {
                            textContent: value[1] + " ",
                        }));
                    }
                    generalInfoElt.appendChild(lineInfoElt);
                }
            }
            if (isExtendedMode(parentElt)) {
                var url = instance.getUrl();
                if (url !== undefined) {
                    var reducedUrl = url.length > 100 ?
                        url.substring(0, 99) + "…" :
                        url;
                    generalInfoElt.appendChild(createCompositeElement("div", [
                        createMetricTitle("url"),
                        createElement("span", {
                            textContent: reducedUrl,
                        }),
                    ]));
                }
            }
        }
        if (isExtendedMode(parentElt)) {
            var videoId = instance.getAvailableVideoTracks().map(function (_a) {
                var id = _a.id, active = _a.active;
                return active ? "*".concat(id) : id;
            });
            var audioId = instance.getAvailableAudioTracks().map(function (_a) {
                var id = _a.id, active = _a.active;
                return active ? "*".concat(id) : id;
            });
            var textId = instance.getAvailableTextTracks().map(function (_a) {
                var id = _a.id, active = _a.active;
                return active ? "*".concat(id) : id;
            });
            adaptationsElt.innerHTML = "";
            if (videoId.length > 0) {
                var textContent = "".concat(videoId.length, ":").concat(videoId.join(" "), " ");
                if (textContent.length > 100) {
                    textContent = textContent.substring(0, 98) + "… ";
                }
                var videoAdaps = createCompositeElement("div", [
                    createMetricTitle("vt"),
                    createElement("span", { textContent: textContent }),
                ]);
                adaptationsElt.appendChild(videoAdaps);
            }
            if (audioId.length > 0) {
                var textContent = "".concat(audioId.length, ":").concat(audioId.join(" "), " ");
                if (textContent.length > 100) {
                    textContent = textContent.substring(0, 98) + "… ";
                }
                var audioAdaps = createCompositeElement("div", [
                    createMetricTitle("at"),
                    createElement("span", { textContent: textContent }),
                ]);
                adaptationsElt.appendChild(audioAdaps);
            }
            if (textId.length > 0) {
                var textContent = "".concat(textId.length, ":").concat(textId.join(" "), " ");
                if (textContent.length > 100) {
                    textContent = textContent.substring(0, 98) + "… ";
                }
                var textAdaps = createCompositeElement("div", [
                    createMetricTitle("tt"),
                    createElement("span", { textContent: textContent }),
                ]);
                adaptationsElt.appendChild(textAdaps);
            }
            var adaptations = instance.getCurrentAdaptations();
            var videoBitratesStr = (_b = (_a = adaptations === null || adaptations === void 0 ? void 0 : adaptations.video) === null || _a === void 0 ? void 0 : _a.representations.map(function (r) {
                return String(r.bitrate) +
                    (r.isSupported ? "" : " U!") +
                    (r.decipherable !== false ? "" : " E!");
            })) !== null && _b !== void 0 ? _b : [];
            var audioBitratesStr = (_d = (_c = adaptations === null || adaptations === void 0 ? void 0 : adaptations.video) === null || _c === void 0 ? void 0 : _c.representations.map(function (r) {
                return String(r.bitrate) +
                    (r.isSupported ? "" : " U!") +
                    (r.decipherable !== false ? "" : " E!");
            })) !== null && _d !== void 0 ? _d : [];
            representationsElt.innerHTML = "";
            if (videoBitratesStr.length > 0) {
                representationsElt.appendChild(createMetricTitle("vb"));
                representationsElt.appendChild(createElement("span", {
                    textContent: videoBitratesStr.join(" ") + " ",
                }));
            }
            if (audioBitratesStr.length > 0) {
                representationsElt.appendChild(createMetricTitle("ab"));
                representationsElt.appendChild(createElement("span", {
                    textContent: audioBitratesStr.join(" ") + " ",
                }));
            }
        }
        else {
            adaptationsElt.innerHTML = "";
            representationsElt.innerHTML = "";
        }
    }
}
