import '../../assert/js/assert.js';
import '../../globals.js';
import { Shape } from '../../kite/js/imports.js';
import optionize from '../../phet-core/js/optionize.js';
import { Path } from '../../scenery/js/imports.js';
import bamboo from './bamboo.js';
export default class AreaPlot extends Path {
    constructor(chartTransform, dataSet, providedOptions) {
        const options = optionize()({
            // SelfOptions
            baseline: 0,
            // PathOptions
            fill: 'black'
        }, providedOptions);
        super(null, options);
        this.chartTransform = chartTransform;
        this.dataSet = dataSet;
        this.baseline = options.baseline;
        // Initialize
        this.update();
        // Update when the transform changes.
        const changedListener = () => this.update();
        chartTransform.changedEmitter.addListener(changedListener);
        this.disposeAreaChart = () => chartTransform.changedEmitter.removeListener(changedListener);
    }
    dispose() {
        this.disposeAreaChart();
        super.dispose();
    }
    // Recomputes the rendered shape.
    update() {
        const shape = new Shape();
        let moveToNextPoint = true;
        // previous dataPoint
        let oldDataPoint = null;
        for (let i = 0; i < this.dataSet.length; i++) {
            const dataPoint = this.dataSet[i];
            assert && assert(dataPoint === null || dataPoint.isFinite(), 'data points must be finite Vector2 or null');
            if (dataPoint) {
                const viewPoint = this.chartTransform.modelToViewPosition(dataPoint);
                if (moveToNextPoint) {
                    // basePoint for the shape
                    const startBasePoint = this.chartTransform.modelToViewXY(dataPoint.x, this.baseline);
                    shape.moveToPoint(startBasePoint);
                    // move to the actual data point
                    shape.lineToPoint(viewPoint);
                    moveToNextPoint = false;
                }
                else {
                    shape.lineToPoint(viewPoint);
                }
                // keep a record of the last datapoint
                oldDataPoint = dataPoint;
            }
            else {
                // if oldDataPoint exists, it must be the first null value of dataPoint,
                if (oldDataPoint) {
                    // go straight down to baseline using the last valid value and close the shape.
                    this.closeShape(oldDataPoint, shape);
                }
                // set oldDataPoint to null
                oldDataPoint = null;
                moveToNextPoint = true;
            }
        }
        // close the shape associated with the last point (if non-null)
        if (oldDataPoint) {
            this.closeShape(oldDataPoint, shape);
        }
        this.shape = shape.makeImmutable();
    }
    /**
     * Given a point and a shape, first go to the baseline of the point and then close the shape.
     */
    closeShape(dataPoint, shape) {
        const endBasePoint = this.chartTransform.modelToViewXY(dataPoint.x, this.baseline);
        shape.lineToPoint(endBasePoint);
        shape.close();
    }
    /**
     * Sets the dataSet and redraws the plot. If instead the dataSet array is mutated, it is the client's responsibility
     * to call `update` or make sure `update` is called elsewhere (say, if the chart scrolls in that frame).
     */
    setDataSet(dataSet) {
        this.dataSet = dataSet;
        this.update();
    }
    /**
     * Sets the baseline value for the area plot.
     */
    setBaseline(baseline) {
        this.baseline = baseline;
        this.update();
    }
}
bamboo.register('AreaPlot', AreaPlot);
//# sourceMappingURL=AreaPlot.js.map