import * as tslib_1 from "tslib";
import { NumbersColumn, isNumbersColumn } from '../model';
import { CANVAS_HEIGHT, cssClass } from '../styles';
import { ANumbersCellRenderer } from './ANumbersCellRenderer';
import { toHeatMapColor } from './BrightnessCellRenderer';
import { ERenderMode } from './interfaces';
import { forEachChild, noRenderer } from './utils';
/** @internal */
var VerticalBarCellRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(VerticalBarCellRenderer, _super);
    function VerticalBarCellRenderer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.title = 'Bar Chart';
        return _this;
    }
    VerticalBarCellRenderer.prototype.canRender = function (col, mode) {
        return isNumbersColumn(col) && Boolean(col.dataLength) && mode === ERenderMode.CELL;
    };
    VerticalBarCellRenderer.compute = function (v, threshold, domain) {
        if (v < threshold) {
            //threshold to down
            return { height: (threshold - v), bottom: (v - domain[0]) };
        }
        //from top to down
        return { height: (v - threshold), bottom: (threshold - domain[0]) };
    };
    VerticalBarCellRenderer.prototype.createContext = function (col, context, imposer) {
        var cellDimension = context.colWidth(col) / col.dataLength;
        var threshold = col.getMapping().apply(NumbersColumn.CENTER);
        var range = 1;
        var templateRows = '';
        for (var i = 0; i < col.dataLength; ++i) {
            templateRows += "<div class=\"" + cssClass('heatmap-cell') + "\" style=\"background-color: white\" title=\"\"></div>";
        }
        var formatter = col.getNumberFormat();
        return {
            clazz: cssClass('heatmap'),
            templateRow: templateRows,
            update: function (row, data, raw, item) {
                var zero = toHeatMapColor(0, item, col, imposer);
                var one = toHeatMapColor(1, item, col, imposer);
                forEachChild(row, function (d, i) {
                    var v = data[i];
                    var _a = VerticalBarCellRenderer.compute(v, threshold, [0, 1]), bottom = _a.bottom, height = _a.height;
                    d.title = formatter(raw[i]);
                    d.style.backgroundColor = v < threshold ? zero : one;
                    d.style.bottom = Math.round((100 * bottom) / range) + "%";
                    d.style.height = Math.round((100 * height) / range) + "%";
                });
            },
            render: function (ctx, data, item) {
                var zero = toHeatMapColor(0, item, col, imposer);
                var one = toHeatMapColor(1, item, col, imposer);
                var scale = CANVAS_HEIGHT / range;
                data.forEach(function (v, j) {
                    ctx.fillStyle = v < threshold ? zero : one;
                    var xpos = (j * cellDimension);
                    var _a = VerticalBarCellRenderer.compute(v, threshold, [0, 1]), bottom = _a.bottom, height = _a.height;
                    ctx.fillRect(xpos, (range - height - bottom) * scale, cellDimension, height * scale);
                });
            }
        };
    };
    VerticalBarCellRenderer.prototype.createSummary = function () {
        return noRenderer;
    };
    return VerticalBarCellRenderer;
}(ANumbersCellRenderer));
export default VerticalBarCellRenderer;
//# sourceMappingURL=VerticalBarCellRenderer.js.map