define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.calcBandwidth = exports.calcRequiredWidth = exports.calcTotalBandUnits = exports.calcTotalWidth = exports.calculateAppropriateBarWidth = exports.getClosestPairDiffAndRange = void 0;
    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];
    };
    exports.getClosestPairDiffAndRange = getClosestPairDiffAndRange;
    var calculateAppropriateBarWidth = function (data, totalWidth, innerPadding) {
        var result = (0, exports.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;
    };
    exports.calculateAppropriateBarWidth = calculateAppropriateBarWidth;
    /**
     * Calculates the total width available for rendering bars.
     */
    var calcTotalWidth = function (containerWidth, margins, extraMargin) {
        if (extraMargin === void 0) { extraMargin = 0; }
        return containerWidth - (margins.left || 0) - (margins.right || 0) - extraMargin * 2;
    };
    exports.calcTotalWidth = calcTotalWidth;
    /**
     * Calculates the combined size of all bands and the gaps between them, in units relative to the bandwidth.
     */
    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;
    };
    exports.calcTotalBandUnits = calcTotalBandUnits;
    /**
     * Calculates the total width required to render all bands including the gaps between them.
     */
    var calcRequiredWidth = function (bandwidth, numBands, innerPadding) {
        return bandwidth * (0, exports.calcTotalBandUnits)(numBands, innerPadding);
    };
    exports.calcRequiredWidth = calcRequiredWidth;
    /**
     * Calculates the maximum possible bandwidth such that the combined widths of all bands
     * and the gaps between them exactly fill the total available width.
     */
    var calcBandwidth = function (totalWidth, numBands, innerPadding) {
        return totalWidth / (0, exports.calcTotalBandUnits)(numBands, innerPadding);
    };
    exports.calcBandwidth = calcBandwidth;
});
//# sourceMappingURL=vbc-utils.js.map