import isNullOrUndefined from "../../../../utils/is_null_or_undefined";
import SegmentBufferGraph from "../buffer_graph";
import { DEFAULT_REFRESH_INTERVAL } from "../constants";
import { createElement, createGraphCanvas, createMetricTitle, isExtendedMode, } from "../utils";
export default function createSegmentBufferGraph(instance, bufferType, title, parentElt, cancelSignal) {
    var bufferGraphWrapper = createElement("div");
    var bufferTitle = createMetricTitle(title);
    var canvasElt = createGraphCanvas();
    var currentRangeRepInfoElt = createElement("div");
    var loadingRangeRepInfoElt = createElement("div");
    var bufferGraph = new SegmentBufferGraph(canvasElt);
    var intervalId = setInterval(update, DEFAULT_REFRESH_INTERVAL);
    cancelSignal.register(function () {
        clearInterval(intervalId);
    });
    bufferGraphWrapper.appendChild(bufferTitle);
    bufferGraphWrapper.appendChild(canvasElt);
    bufferGraphWrapper.appendChild(currentRangeRepInfoElt);
    bufferGraphWrapper.appendChild(loadingRangeRepInfoElt);
    bufferGraphWrapper.style.padding = "5px 0px";
    update();
    return bufferGraphWrapper;
    function update() {
        var _a, _b, _c, _d;
        if (instance.getVideoElement() === null) {
            // disposed player. Clean-up everything
            bufferGraphWrapper.style.display = "none";
            bufferGraphWrapper.innerHTML = "";
            clearInterval(intervalId);
            return;
        }
        var showAllInfo = isExtendedMode(parentElt);
        var inventory = instance.__priv_getSegmentBufferContent(bufferType);
        if (inventory === null) {
            bufferGraphWrapper.style.display = "none";
            currentRangeRepInfoElt.innerHTML = "";
            loadingRangeRepInfoElt.innerHTML = "";
        }
        else {
            bufferGraphWrapper.style.display = "block";
            var currentTime = instance.getPosition();
            var width = Math.min(parentElt.clientWidth - 150, 600);
            bufferGraph.update({
                currentTime: currentTime,
                minimumPosition: (_a = instance.getMinimumPosition()) !== null && _a !== void 0 ? _a : undefined,
                maximumPosition: (_b = instance.getMaximumPosition()) !== null && _b !== void 0 ? _b : undefined,
                inventory: inventory,
                width: width,
                height: 10,
            });
            if (!showAllInfo) {
                currentRangeRepInfoElt.innerHTML = "";
                loadingRangeRepInfoElt.innerHTML = "";
                return;
            }
            currentRangeRepInfoElt.innerHTML = "";
            for (var i = 0; i < inventory.length; i++) {
                var rangeInfo = inventory[i];
                var bufferedStart = rangeInfo.bufferedStart, bufferedEnd = rangeInfo.bufferedEnd, infos = rangeInfo.infos;
                if (bufferedStart !== undefined && bufferedEnd !== undefined &&
                    currentTime >= bufferedStart && currentTime < bufferedEnd) {
                    currentRangeRepInfoElt.appendChild(createMetricTitle("play"));
                    currentRangeRepInfoElt.appendChild(createElement("span", {
                        textContent: constructRepresentationInfo(infos),
                    }));
                    break;
                }
            }
            loadingRangeRepInfoElt.innerHTML = "";
            var rep = (_c = instance.getCurrentRepresentations()) === null || _c === void 0 ? void 0 : _c[bufferType];
            var adap = (_d = instance.getCurrentAdaptations()) === null || _d === void 0 ? void 0 : _d[bufferType];
            var manifest = instance.getManifest();
            if (manifest !== null && !isNullOrUndefined(rep) && !isNullOrUndefined(adap)) {
                var period = manifest.getPeriodForTime(currentTime);
                if (period !== undefined) {
                    loadingRangeRepInfoElt.appendChild(createMetricTitle("load"));
                    loadingRangeRepInfoElt.appendChild(createElement("span", {
                        textContent: constructRepresentationInfo({
                            period: period,
                            adaptation: adap,
                            representation: rep,
                        }),
                    }));
                }
            }
        }
    }
}
function constructRepresentationInfo(content) {
    var _a;
    var period = content.period;
    var _b = content.adaptation, language = _b.language, isAudioDescription = _b.isAudioDescription, isClosedCaption = _b.isClosedCaption, isTrickModeTrack = _b.isTrickModeTrack, isSignInterpreted = _b.isSignInterpreted, bufferType = _b.type;
    var _c = content.representation, id = _c.id, height = _c.height, width = _c.width, bitrate = _c.bitrate, codec = _c.codec;
    var representationInfo = "\"".concat(id, "\" ");
    if (height !== undefined && width !== undefined) {
        representationInfo += "".concat(width, "x").concat(height, " ");
    }
    if (bitrate !== undefined) {
        representationInfo += "(".concat((bitrate / 1000).toFixed(0), "kbps) ");
    }
    if (codec !== undefined) {
        representationInfo += "c:\"".concat(codec, "\" ");
    }
    if (language !== undefined) {
        representationInfo += "l:\"".concat(language, "\" ");
    }
    if (bufferType === "video" && typeof isSignInterpreted === "boolean") {
        representationInfo += "si:".concat(isSignInterpreted ? 1 : 0, " ");
    }
    if (bufferType === "video" && typeof isTrickModeTrack === "boolean") {
        representationInfo += "tm:".concat(isTrickModeTrack ? 1 : 0, " ");
    }
    if (bufferType === "audio" && typeof isAudioDescription === "boolean") {
        representationInfo += "ad:".concat(isAudioDescription ? 1 : 0, " ");
    }
    if (bufferType === "text" && typeof isClosedCaption === "boolean") {
        representationInfo += "cc:".concat(isClosedCaption ? 1 : 0, " ");
    }
    representationInfo += "p:".concat(period.start, "-").concat((_a = period.end) !== null && _a !== void 0 ? _a : "?");
    return representationInfo;
}
