import BufferSizeGraph from "../buffer_size_graph";
import { DEFAULT_REFRESH_INTERVAL } from "../constants";
import { createElement, createGraphCanvas, createMetricTitle, } from "../utils";
export default function createSegmentBufferSizeGraph(instance, parentElt, cancelSignal) {
    var bufferSizeGraphWrapperElt = createElement("div");
    var bufferSizeTitle = createMetricTitle("bgap");
    var canvasElt = createGraphCanvas();
    var bufferSizeGraph = new BufferSizeGraph(canvasElt);
    var intervalId = setInterval(addBufferSize, DEFAULT_REFRESH_INTERVAL);
    cancelSignal.register(function () {
        clearInterval(intervalId);
    });
    bufferSizeGraphWrapperElt.appendChild(bufferSizeTitle);
    bufferSizeGraphWrapperElt.appendChild(canvasElt);
    bufferSizeGraphWrapperElt.style.padding = "7px 0px";
    addBufferSize();
    return bufferSizeGraphWrapperElt;
    function addBufferSize() {
        if (instance.getVideoElement() === null) {
            // disposed player. Clean-up everything
            bufferSizeGraphWrapperElt.innerHTML = "";
            clearInterval(intervalId);
            return;
        }
        var bufferGap = instance.getVideoBufferGap();
        if (bufferGap === Infinity) {
            bufferSizeGraph.pushBufferSize(0);
        }
        else {
            bufferSizeGraph.pushBufferSize(bufferGap);
        }
        var width = Math.min(parentElt.clientWidth - 150, 600);
        bufferSizeGraph.reRender(width, 10);
    }
}
