"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BandSeriesDrawingProvider = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var BrushCache_1 = require("../../../Drawing/BrushCache");
var Pen2DCache_1 = require("../../../Drawing/Pen2DCache");
var WebGlRenderContext2D_1 = require("../../../Drawing/WebGlRenderContext2D");
var SciChartSurfaceBase_1 = require("../../SciChartSurfaceBase");
var constants_1 = require("../constants");
var BaseSeriesDrawingProvider_1 = require("./BaseSeriesDrawingProvider");
/**
 * Used internally - a drawing provider performs drawing for a {@link BaseBandRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var BandSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(BandSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link BandSeriesDrawingProvider}
     * @param webAssemblyContext The {@link TSciChart | SciChart 2D WebAssembly Context} containing native methods and
     * access to our WebGL2 Engine and WebAssembly numerical methods
     * @param parentSeries the parent {@link BaseBandRenderableSeries} which this drawing provider is attached to
     */
    function BandSeriesDrawingProvider(webAssemblyContext, parentSeries, ySelector, y1Selector) {
        var _this = _super.call(this, webAssemblyContext, parentSeries, ySelector) || this;
        _this.args = new _this.webAssemblyContext.SCRTBandDrawingParams();
        _this.y1Selector = y1Selector !== null && y1Selector !== void 0 ? y1Selector : (function (ps) { return ps.y1Values; });
        _this.linesPenCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.strokePenY1Cache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.fillBrushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        _this.fillBrushY1Cache = new BrushCache_1.BrushCache(webAssemblyContext);
        return _this;
    }
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.onAttachSeries = function () {
        _super.prototype.onAttachSeries.call(this);
        this.nativeDrawingProvider = new this.webAssemblyContext.SCRTBandSeriesDrawingProvider();
        var _a = this.parentSeries, parentSurface = _a.parentSurface, stroke = _a.stroke, strokeThickness = _a.strokeThickness, strokeY1Thickness = _a.strokeY1Thickness, fill = _a.fill, strokeY1 = _a.strokeY1, fillY1 = _a.fillY1, opacity = _a.opacity, strokeDashArray = _a.strokeDashArray, strokeY1DashArray = _a.strokeY1DashArray, fillLinearGradient = _a.fillLinearGradient, fillLinearGradientY1 = _a.fillLinearGradientY1, customTextureOptions = _a.customTextureOptions;
        var textureHeightRatio = (parentSurface === null || parentSurface === void 0 ? void 0 : parentSurface.isCopyCanvasSurface)
            ? parentSurface.domCanvas2D.height / SciChartSurfaceBase_1.SciChartSurfaceBase.domMasterCanvas.height
            : 1;
        var textureWidthRatio = (parentSurface === null || parentSurface === void 0 ? void 0 : parentSurface.isCopyCanvasSurface)
            ? parentSurface.domCanvas2D.width / SciChartSurfaceBase_1.SciChartSurfaceBase.domMasterCanvas.width
            : 1;
        (0, Pen2DCache_1.createPenInCache)(this.linesPenCache, stroke, strokeThickness, opacity, strokeDashArray);
        (0, Pen2DCache_1.createPenInCache)(this.strokePenY1Cache, strokeY1, strokeY1Thickness, opacity, strokeY1DashArray);
        this.fillBrushCache.create(fill, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradient, customTextureOptions);
        this.fillBrushY1Cache.create(fillY1, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradientY1, customTextureOptions);
    };
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.onDetachSeries = function () {
        _super.prototype.onDetachSeries.call(this);
        this.nativeDrawingProvider = (0, Deleter_1.deleteSafe)(this.nativeDrawingProvider);
    };
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.delete = function () {
        this.nativeDrawingProvider = (0, Deleter_1.deleteSafe)(this.nativeDrawingProvider);
        this.args = (0, Deleter_1.deleteSafe)(this.args);
        this.linesPenCache = (0, Deleter_1.deleteSafe)(this.linesPenCache);
        this.fillBrushCache = (0, Deleter_1.deleteSafe)(this.fillBrushCache);
        this.strokePenY1Cache = (0, Deleter_1.deleteSafe)(this.strokePenY1Cache);
        this.fillBrushY1Cache = (0, Deleter_1.deleteSafe)(this.fillBrushY1Cache);
        _super.prototype.delete.call(this);
    };
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.draw = function (renderContext, renderPassData) {
        var _a, _b;
        var pointSeries = renderPassData.pointSeries;
        var containsNaN = this.parentSeries.dataSeries.dataDistributionCalculator.containsNaN;
        this.args.Reset();
        this.args.forceShaderMethod = true;
        this.args.verticalChart = renderPassData.isVerticalChart;
        this.args.lineGaps = containsNaN
            ? this.parentSeries.drawNaNAs === WebGlRenderContext2D_1.ELineDrawMode.DiscontinuousLine
                ? this.webAssemblyContext.SCRTLineGapMode.DrawGaps
                : this.webAssemblyContext.SCRTLineGapMode.CloseGaps
            : this.webAssemblyContext.SCRTLineGapMode.Default;
        this.args.isDigitalLine = this.parentSeries.isDigitalLine;
        var fillBrush = (0, BrushCache_1.getScrtBrushFromCache)(this.fillBrushCache);
        if (fillBrush) {
            this.args.SetFillBrush(fillBrush);
        }
        var linesPen = (0, Pen2DCache_1.getScrtPenFromCache)(this.linesPenCache);
        if (linesPen) {
            this.args.SetLinesPen(linesPen);
        }
        var fillBrushY1 = (0, BrushCache_1.getScrtBrushFromCache)(this.fillBrushY1Cache);
        if (fillBrushY1) {
            this.args.SetFillBrush1(fillBrushY1);
        }
        var strokePenY1 = (0, Pen2DCache_1.getScrtPenFromCache)(this.strokePenY1Cache);
        if (strokePenY1) {
            this.args.SetLinesPen1(strokePenY1);
        }
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xValues = pointSeries.xValues;
        var _c = this.parentSeries.dataSeries, fifoCapacity = _c.fifoCapacity, fifoSweeping = _c.fifoSweeping, fifoSweepingGap = _c.fifoSweepingGap;
        var fifoStartIndex = pointSeries.fifoStartIndex;
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : xValues;
        var yDrawValues = this.ySelector(pointSeries);
        var y1DrawValues = this.y1Selector(pointSeries);
        // const isSplineBandSeries = this.parentSeries.type === ESeriesType.SplineBandSeries && xValues.size() > 1;
        // if (isSplineBandSeries) {
        //     const splineBandSeries = this.parentSeries as SplineBandRenderableSeries;
        //     if (!this.parentSeries.isRunningAnimation) {
        //         splineBandSeries.updateSplineValues();
        //     }
        //     xDrawValues = splineBandSeries.xSplineValues;
        //     yDrawValues = splineBandSeries.ySplineValues;
        //     y1DrawValues = splineBandSeries.y1SplineValues;
        // }
        var _d = this.getStartAndCount(
        //isSplineBandSeries ? undefined : renderPassData,
        renderPassData, xDrawValues), startIndex = _d.startIndex, count = _d.count;
        this.args.count = count;
        this.args.startIndex = startIndex;
        if (fifoCapacity > 0 && fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
            this.args.count = fifoStartIndex;
        }
        // Paletting per point
        _super.prototype.applyStrokeFillPaletting.call(this, undefined, undefined, undefined, undefined, this.parentSeries.opacity, true, true, renderPassData);
        // Use the per-point colour array (interleaved stroke,fill) + paletteStart, the same mechanism
        // as line/column. (The old SCRTPalette/SetPalette path read colours from offset 0 and so
        // re-used pass 1's colours on the second sweep pass.)
        if (this.palettingState.palettedColors) {
            this.args.SetPalettedColors(this.palettingState.palettedColors);
            this.args.paletteStart = (_a = this.palettingState.paletteStartIndex) !== null && _a !== void 0 ? _a : 0;
        }
        this.args.isSmoothColors = this.palettingState.gradientPaletting;
        var nativeContext = renderContext.getNativeContext();
        this.args.SetNativeContext(nativeContext);
        this.args.SetXValues(xDrawValues);
        this.args.SetYValues(yDrawValues);
        this.args.SetZValues(y1DrawValues);
        this.args.SetXCoordinateCalculator(renderPassData.xCoordinateCalculator.nativeCalculator);
        this.args.SetYCoordinateCalculator(renderPassData.yCoordinateCalculator.nativeCalculator);
        this.nativeDrawingProvider.DrawPointsVec(this.args);
        if (fifoCapacity > 0 && fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
            this.args.startIndex = Math.min(yDrawValues.size(), fifoStartIndex + fifoSweepingGap);
            this.args.count = Math.max(0, yDrawValues.size() - fifoStartIndex - fifoSweepingGap);
            // Re-base the palette for the second sweep pass (interleaved stroke+fill, so *2), so it
            // doesn't re-read pass 1's colours.
            this.args.paletteStart = ((_b = this.palettingState.paletteStartIndex) !== null && _b !== void 0 ? _b : 0) + (this.args.startIndex - startIndex) * 2;
            if (this.args.count > 0) {
                this.nativeDrawingProvider.DrawPointsVec(this.args);
            }
        }
    };
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE_Y1);
    };
    /**
     * @inheritDoc
     */
    BandSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        var _a = this.parentSeries, parentSurface = _a.parentSurface, stroke = _a.stroke, strokeY1 = _a.strokeY1, strokeThickness = _a.strokeThickness, strokeY1Thickness = _a.strokeY1Thickness, fill = _a.fill, fillY1 = _a.fillY1, opacity = _a.opacity, strokeDashArray = _a.strokeDashArray, strokeY1DashArray = _a.strokeY1DashArray, fillLinearGradient = _a.fillLinearGradient, fillLinearGradientY1 = _a.fillLinearGradientY1, customTextureOptions = _a.customTextureOptions;
        if (propertyName === constants_1.PROPERTY.STROKE ||
            propertyName === constants_1.PROPERTY.STROKE_THICKNESS ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.STROKE_DASH_ARRAY) {
            this.palettingState.requiresUpdate = true;
            (0, Pen2DCache_1.createPenInCache)(this.linesPenCache, stroke, strokeThickness, opacity, strokeDashArray);
            return;
        }
        if (propertyName === constants_1.PROPERTY.STROKE_Y1 ||
            propertyName === constants_1.PROPERTY.STROKE_THICKNESS ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.STROKE_Y1_DASH_ARRAY ||
            propertyName === constants_1.PROPERTY.STROKE_THICKNESS_Y1) {
            this.palettingState.requiresUpdate = true;
            (0, Pen2DCache_1.createPenInCache)(this.strokePenY1Cache, strokeY1, strokeY1Thickness, opacity, strokeY1DashArray);
            return;
        }
        var textureHeightRatio = (parentSurface === null || parentSurface === void 0 ? void 0 : parentSurface.isCopyCanvasSurface)
            ? parentSurface.domCanvas2D.height / SciChartSurfaceBase_1.SciChartSurfaceBase.domMasterCanvas.height
            : 1;
        var textureWidthRatio = (parentSurface === null || parentSurface === void 0 ? void 0 : parentSurface.isCopyCanvasSurface)
            ? parentSurface.domCanvas2D.width / SciChartSurfaceBase_1.SciChartSurfaceBase.domMasterCanvas.width
            : 1;
        if (propertyName === constants_1.PROPERTY.FILL ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.FILL_LINEAR_GRADIENT ||
            propertyName == constants_1.PROPERTY.CUSTOM_TEXTURE_OPTIONS) {
            this.palettingState.requiresUpdate = true;
            this.fillBrushCache.create(fill, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradient, customTextureOptions);
        }
        if (propertyName === constants_1.PROPERTY.FILL_Y1 ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.FILL_LINEAR_GRADIENT_Y1 ||
            propertyName == constants_1.PROPERTY.CUSTOM_TEXTURE_OPTIONS) {
            this.palettingState.requiresUpdate = true;
            this.fillBrushY1Cache.create(fillY1, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradientY1, customTextureOptions);
        }
    };
    return BandSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.BandSeriesDrawingProvider = BandSeriesDrawingProvider;
