import '../../assert/js/assert.js';
import '../../globals.js';
import optionize from '../../phet-core/js/optionize.js';
import { Color } from '../../scenery/js/imports.js';
import bamboo from './bamboo.js';
import CanvasPainter from './CanvasPainter.js';
export default class CanvasLinePlot extends CanvasPainter {
    constructor(chartTransform, dataSet, providedOptions) {
        const options = optionize()({
            // SelfOptions
            stroke: 'black',
            lineWidth: 1,
            lineDash: [] // solid
        }, providedOptions);
        super();
        this.chartTransform = chartTransform;
        this.dataSet = dataSet;
        this.lineWidth = options.lineWidth;
        this.lineDash = options.lineDash;
        this.strokeCSS = null; // updated in setStroke
        this.setStroke(options.stroke);
    }
    /**
     * Sets the stroke.
     * @param stroke - If you call setStroke, you are responsible for calling update on the associated ChartCanvasNode(s).
     */
    setStroke(stroke) {
        if (assert && typeof stroke === 'string') {
            assert && assert(Color.isCSSColorString(stroke), 'invalid stroke');
        }
        this.strokeCSS = stroke instanceof Color ? stroke.toCSS() : stroke;
    }
    set stroke(stroke) {
        this.setStroke(stroke);
    }
    dispose() {
        assert && assert(!this.isDisposed, 'already disposed');
        this.isDisposed = true;
    }
    // Sets dataSet. You are responsible for calling update on the associated ChartCanvasNode(s).
    setDataSet(dataSet) {
        this.dataSet = dataSet;
    }
    // Intended to be called by ChartCanvasNode.
    paintCanvas(context) {
        context.beginPath();
        if (this.strokeCSS) {
            context.strokeStyle = this.strokeCSS;
            context.lineWidth = this.lineWidth;
            context.setLineDash(this.lineDash);
            let moveToNextPoint = true;
            // Only access the data set length once for performance
            const length = this.dataSet.length;
            for (let i = 0; i < length; i++) {
                const dataPoint = this.dataSet[i];
                assert && assert(dataPoint === null || dataPoint.isFinite(), 'data points must be finite Vector2 or null');
                // Draw a line segment to the next non-null value. Null values result in a gap (via move) in the plot.
                if (dataPoint) {
                    const viewPoint = this.chartTransform.modelToViewPosition(dataPoint);
                    if (moveToNextPoint) {
                        context.moveTo(viewPoint.x, viewPoint.y);
                        moveToNextPoint = false;
                    }
                    else {
                        context.lineTo(viewPoint.x, viewPoint.y);
                    }
                }
                else {
                    moveToNextPoint = true;
                }
            }
            context.stroke();
        }
    }
}
bamboo.register('CanvasLinePlot', CanvasLinePlot);
//# sourceMappingURL=CanvasLinePlot.js.map