export var getClosestPairDiffAndRange = function (data) {
    if (data.length < 2) {
        return;
    }
    data = data.sort(function (a, b) {
        return a instanceof Date ? a.getTime() - b.getTime() : a - b;
    });
    var minDiff = Number.MAX_VALUE;
    for (var i = 1; i < data.length; i++) {
        var diff = data[i] instanceof Date
            ? data[i].getTime() - data[i - 1].getTime()
            : data[i] - data[i - 1];
        minDiff = Math.min(minDiff, diff);
    }
    var range = data[0] instanceof Date
        ? data[data.length - 1].getTime() - data[0].getTime()
        : data[data.length - 1] - data[0];
    return [minDiff, range];
};
export var calculateAppropriateBarWidth = function (data, totalWidth, innerPadding) {
    var result = getClosestPairDiffAndRange(data);
    if (!result || result[1] === 0) {
        return 16;
    }
    var closestPairDiff = result[0], range = result[1];
    // Refer to https://microsoft.github.io/fluentui-charting-contrib/docs/rfcs/fix-overlapping-bars-on-continuous-axes
    // for the derivation of the following formula.
    var barWidth = Math.floor((totalWidth * closestPairDiff * (1 - innerPadding)) / (range + closestPairDiff * (1 - innerPadding)));
    return barWidth;
};
/**
 * Calculates the total width available for rendering bars.
 */
export var calcTotalWidth = function (containerWidth, margins, extraMargin) {
    if (extraMargin === void 0) { extraMargin = 0; }
    return containerWidth - (margins.left || 0) - (margins.right || 0) - extraMargin * 2;
};
/**
 * Calculates the combined size of all bands and the gaps between them, in units relative to the bandwidth.
 */
export var calcTotalBandUnits = function (numBands, innerPadding) {
    // inner_padding = space_between_bands / (space_between_bands + bandwidth)
    // => space_between_bands = (inner_padding / (1 - inner_padding)) * bandwidth
    var gapToBandRatio = innerPadding / (1 - innerPadding);
    return numBands + (numBands - 1) * gapToBandRatio;
};
/**
 * Calculates the total width required to render all bands including the gaps between them.
 */
export var calcRequiredWidth = function (bandwidth, numBands, innerPadding) {
    return bandwidth * calcTotalBandUnits(numBands, innerPadding);
};
/**
 * Calculates the maximum possible bandwidth such that the combined widths of all bands
 * and the gaps between them exactly fill the total available width.
 */
export var calcBandwidth = function (totalWidth, numBands, innerPadding) {
    return totalWidth / calcTotalBandUnits(numBands, innerPadding);
};
//# sourceMappingURL=vbc-utils.js.map