import { NumbersColumn, isNumbersColumn } from '../model';
import { matchRows } from './ANumbersCellRenderer';
import { ERenderMode } from './interfaces';
import { renderMissingDOM } from './missing';
import { forEachChild, noRenderer } from './utils';
/** @internal */
export function line(data) {
    if (data.length === 0) {
        return '';
    }
    var p = '';
    var moveNext = true;
    data.forEach(function (d, i) {
        if (isNaN(d)) {
            moveNext = true;
        }
        else if (moveNext) {
            p += "M" + i + "," + (1 - d) + " ";
            moveNext = false;
        }
        else {
            p += "L" + i + "," + (1 - d) + " ";
        }
    });
    return p;
}
/** @internal */
var SparklineCellRenderer = /** @class */ (function () {
    function SparklineCellRenderer() {
        this.title = 'Sparkline';
    }
    SparklineCellRenderer.prototype.canRender = function (col, mode) {
        return isNumbersColumn(col) && mode !== ERenderMode.SUMMARY;
    };
    SparklineCellRenderer.prototype.create = function (col) {
        var dataLength = col.dataLength;
        var yPos = 1 - col.getMapping().apply(NumbersColumn.CENTER);
        return {
            template: "<svg viewBox=\"0 0 " + (dataLength - 1) + " 1\" preserveAspectRatio=\"none meet\"><line x1=\"0\" x2=\"" + (dataLength - 1) + "\" y1=\"" + yPos + "\" y2=\"" + yPos + "\"></line><path></path></svg>",
            update: function (n, d) {
                if (renderMissingDOM(n, col, d)) {
                    return;
                }
                var data = col.getNumbers(d);
                n.lastElementChild.setAttribute('d', line(data));
            }
        };
    };
    SparklineCellRenderer.prototype.createGroup = function (col, context) {
        var dataLength = col.dataLength;
        var yPos = 1 - col.getMapping().apply(NumbersColumn.CENTER);
        return {
            template: "<svg viewBox=\"0 0 " + dataLength + " 1\" preserveAspectRatio=\"none meet\"><line x1=\"0\" x2=\"" + (dataLength - 1) + "\" y1=\"" + yPos + "\" y2=\"" + yPos + "\"></line><path></path></svg>",
            update: function (n, group) {
                //overlapping ones
                matchRows(n, group.order.length, "<path></path>");
                return context.tasks.groupRows(col, group, 'numbers', function (r) { return Array.from(r.map(function (d) { return col.getNumbers(d); })); }).then(function (vs) {
                    if (typeof vs === 'symbol') {
                        return;
                    }
                    forEachChild(n, (function (row, i) {
                        row.setAttribute('d', line(vs[i]));
                    }));
                });
            }
        };
    };
    SparklineCellRenderer.prototype.createSummary = function () {
        return noRenderer;
    };
    return SparklineCellRenderer;
}());
export default SparklineCellRenderer;
//# sourceMappingURL=SparklineCellRenderer.js.map