import { round } from '../internal';
import { isNumbersColumn, isNumberColumn, DEFAULT_COLOR } from '../model';
import { setText, adaptDynamicColorToBgColor, noRenderer } from './utils';
import { CANVAS_HEIGHT, cssClass } from '../styles';
import { colorOf } from './impose';
import { ERenderMode } from './interfaces';
import { renderMissingCanvas, renderMissingDOM } from './missing';
/** @internal */
var BarCellRenderer = /** @class */ (function () {
    /**
     * flag to always render the value
     * @type {boolean}
     */
    function BarCellRenderer(renderValue) {
        if (renderValue === void 0) { renderValue = false; }
        this.renderValue = renderValue;
        this.title = 'Bar';
    }
    BarCellRenderer.prototype.canRender = function (col, mode) {
        return mode === ERenderMode.CELL && isNumberColumn(col) && !isNumbersColumn(col);
    };
    BarCellRenderer.prototype.create = function (col, context, imposer) {
        var width = context.colWidth(col);
        return {
            template: "<div title=\"\">\n          <div class=\"" + cssClass('bar-label') + "\" style='background-color: " + DEFAULT_COLOR + "'>\n            <span " + (this.renderValue ? '' : "class=\"" + cssClass('hover-only') + "\"") + "></span>\n          </div>\n        </div>",
            update: function (n, d) {
                var value = col.getNumber(d);
                var missing = renderMissingDOM(n, col, d);
                var w = isNaN(value) ? 0 : round(value * 100, 2);
                var title = col.getLabel(d);
                n.title = title;
                var bar = n.firstElementChild;
                bar.style.width = missing ? '100%' : w + "%";
                var color = colorOf(col, d, imposer, value);
                bar.style.backgroundColor = missing ? null : color;
                setText(bar.firstElementChild, title);
                var item = bar.firstElementChild;
                setText(item, title);
                adaptDynamicColorToBgColor(item, color || DEFAULT_COLOR, title, w / 100);
            },
            render: function (ctx, d) {
                if (renderMissingCanvas(ctx, col, d, width)) {
                    return;
                }
                var value = col.getNumber(d);
                ctx.fillStyle = colorOf(col, d, imposer, value) || DEFAULT_COLOR;
                var w = width * value;
                ctx.fillRect(0, 0, isNaN(w) ? 0 : w, CANVAS_HEIGHT);
            }
        };
    };
    BarCellRenderer.prototype.createGroup = function () {
        return noRenderer;
    };
    BarCellRenderer.prototype.createSummary = function () {
        return noRenderer;
    };
    return BarCellRenderer;
}());
export default BarCellRenderer;
//# sourceMappingURL=BarCellRenderer.js.map