"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.MountainSeriesDrawingProvider = 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 BaseMountainRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var MountainSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(MountainSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link MountainSeriesDrawingProvider}
     * @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 BaseMountainRenderableSeries} which this drawing provider is attached to
     */
    function MountainSeriesDrawingProvider(webAssemblyContext, parentSeries) {
        var _this = _super.call(this, webAssemblyContext, parentSeries) || this;
        _this.nativeDrawingProvider = new _this.webAssemblyContext.SCRTMountainSeriesDrawingProvider();
        _this.args = new _this.webAssemblyContext.SCRTMountainDrawingParams();
        _this.strokePenCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.fillBrushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        return _this;
    }
    /**
     * @inheritDoc
     */
    MountainSeriesDrawingProvider.prototype.delete = function () {
        this.nativeDrawingProvider = (0, Deleter_1.deleteSafe)(this.nativeDrawingProvider);
        this.args = (0, Deleter_1.deleteSafe)(this.args);
        this.strokePenCache = (0, Deleter_1.deleteSafe)(this.strokePenCache);
        this.fillBrushCache = (0, Deleter_1.deleteSafe)(this.fillBrushCache);
        _super.prototype.delete.call(this);
    };
    /**
     * @inheritDoc
     */
    MountainSeriesDrawingProvider.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.lineGaps = containsNaN
            ? this.parentSeries.drawNaNAs === WebGlRenderContext2D_1.ELineDrawMode.DiscontinuousLine
                ? this.webAssemblyContext.SCRTLineGapMode.DrawGaps
                : this.webAssemblyContext.SCRTLineGapMode.CloseGaps
            : this.webAssemblyContext.SCRTLineGapMode.Default;
        this.args.verticalChart = renderPassData.isVerticalChart;
        var _c = renderPassData.getyCoordinateCalculator(), visibleMin = _c.visibleMin, visibleMax = _c.visibleMax;
        this.args.zeroLineY = Math.max(Math.min(this.parentSeries.zeroLineY, visibleMax), visibleMin);
        this.args.isDigitalLine = this.parentSeries.isDigitalLine;
        if (this.parentSeries.xAxis.isLinearAxis) {
            this.args.forceClamp = !this.args.isDigitalLine;
        }
        else {
            // Log axes and non-linear baseValueAxes cause problems with viewportClamper
            this.args.forceClamp = false;
        }
        var fillBrush = this.createBrush();
        if (fillBrush) {
            this.args.SetFillBrush(fillBrush);
        }
        var strokePen = (0, Pen2DCache_1.getScrtPenFromCache)(this.strokePenCache);
        if (strokePen) {
            this.args.SetLinesPen(strokePen);
        }
        // Paletting per point
        _super.prototype.applyStrokeFillPaletting.call(this, this.parentSeries.stroke, strokePen, this.parentSeries.fill, fillBrush, this.parentSeries.opacity, true, this.parentSeries.fillLinearGradient !== undefined, renderPassData);
        // Use the per-point colour array (interleaved stroke,fill) + paletteStart, same as
        // line/column. (The old SCRTPalette/SetPalette path ignored the offset and 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 _d = this.parentSeries.dataSeries, fifoCapacity = _d.fifoCapacity, fifoSweeping = _d.fifoSweeping, fifoSweepingGap = _d.fifoSweepingGap;
        var fifoStartIndex = pointSeries.fifoStartIndex;
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xValues = pointSeries.xValues;
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : xValues;
        var yDrawValues = pointSeries.yValues;
        // const isSplineMountainSeries =
        //     this.parentSeries.type === ESeriesType.SplineMountainSeries && xValues.size() > 1;
        // if (isSplineMountainSeries) {
        //     const rs = this.parentSeries as any as ISpline;
        //     if (!this.parentSeries.isRunningAnimation) {
        //         rs.updateSplineValues();
        //     }
        //     xDrawValues = rs.xSplineValues;
        //     yDrawValues = rs.ySplineValues;
        // }
        var _e = this.getStartAndCount(
        //isSplineMountainSeries ? undefined : renderPassData,
        renderPassData, xDrawValues), startIndex = _e.startIndex, count = _e.count;
        this.args.count = count;
        this.args.startIndex = startIndex;
        if (fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
            this.args.count = fifoStartIndex;
        }
        var nativeContext = renderContext.getNativeContext();
        var viewRect = this.parentSeries.parentSurface.seriesViewRect;
        this.args.SetNativeContext(nativeContext);
        this.args.SetXValues(xDrawValues);
        this.args.SetYValues(yDrawValues);
        this.args.SetXCoordinateCalculator(renderPassData.xCoordinateCalculator.nativeCalculator);
        this.args.SetYCoordinateCalculator(renderPassData.yCoordinateCalculator.nativeCalculator);
        this.nativeDrawingProvider.DrawPointsVec(this.args);
        if (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).
            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
     */
    MountainSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
    };
    /**
     * @inheritDoc
     */
    MountainSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        if (propertyName === constants_1.PROPERTY.STROKE ||
            propertyName === constants_1.PROPERTY.STROKE_DASH_ARRAY ||
            propertyName === constants_1.PROPERTY.STROKE_THICKNESS ||
            propertyName === constants_1.PROPERTY.OPACITY) {
            this.createPen();
        }
        if (propertyName === constants_1.PROPERTY.FILL ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.FILL_LINEAR_GRADIENT) {
            this.createBrush();
        }
    };
    /**
     * @inheritDoc
     */
    MountainSeriesDrawingProvider.prototype.onAttachSeries = function () {
        _super.prototype.onAttachSeries.call(this);
        this.createPen();
        this.createBrush();
    };
    MountainSeriesDrawingProvider.prototype.createBrush = function () {
        this.palettingState.requiresUpdate = true;
        var _a = this.parentSeries, fill = _a.fill, opacity = _a.opacity, fillLinearGradient = _a.fillLinearGradient, parentSurface = _a.parentSurface, 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;
        this.fillBrushCache.create(fill, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradient, customTextureOptions);
        return (0, BrushCache_1.getScrtBrushFromCache)(this.fillBrushCache);
    };
    MountainSeriesDrawingProvider.prototype.createPen = function () {
        var _a = this.parentSeries, stroke = _a.stroke, strokeThickness = _a.strokeThickness, opacity = _a.opacity, strokeDashArray = _a.strokeDashArray;
        this.palettingState.requiresUpdate = true;
        return (0, Pen2DCache_1.createPenInCache)(this.strokePenCache, stroke, strokeThickness, opacity, strokeDashArray);
    };
    return MountainSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.MountainSeriesDrawingProvider = MountainSeriesDrawingProvider;
