"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.TriangleSeriesDrawingProvider = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var BrushCache_1 = require("../../../Drawing/BrushCache");
var constants_1 = require("../constants");
var BaseSeriesDrawingProvider_1 = require("./BaseSeriesDrawingProvider");
var TriangleSeriesDrawMode_1 = require("../../../../types/TriangleSeriesDrawMode");
var ValueName_1 = require("../../../../types/ValueName");
var app_1 = require("../../../../constants/app");
/**
 * Used internally - a drawing provider performs drawing for a {@link FastTriangleRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var TriangleSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(TriangleSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link TriangleSeriesDrawingProvider}
     * @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 FastTriangleRenderableSeries} which this drawing provider is attached to
     */
    function TriangleSeriesDrawingProvider(webAssemblyContext, parentSeries, ySelector, xSelector) {
        var _this = _super.call(this, webAssemblyContext, parentSeries, ySelector, xSelector) || this;
        _this.brushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        _this.args = new webAssemblyContext.SCRTTriangleDrawingParams();
        _this.neutralColorSCRTBrush = app_1.IS_TEST_ENV ? undefined : new webAssemblyContext.SCRTSolidBrush(app_1.NEUTRAL_COLOR, false);
        return _this;
    }
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
    };
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        if (propertyName === constants_1.PROPERTY.PALETTE_PROVIDER) {
            this.brushCache.invalidateCache();
        }
        if (propertyName === constants_1.PROPERTY.FILL ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.FILL_LINEAR_GRADIENT) {
            this.palettingState.requiresUpdate = true;
            var _a = this.getProperties(this.parentSeries), fill = _a.fill, opacity = _a.opacity, fillLinearGradient = _a.fillLinearGradient, customTextureOptions = _a.customTextureOptions;
            //if (fill) {
            this.brushCache.create(fill, opacity !== null && opacity !== void 0 ? opacity : 1, 1, 1, fillLinearGradient, customTextureOptions);
            //}
        }
    };
    /** A mapping function to get the pen properties from the parent series */
    TriangleSeriesDrawingProvider.prototype.getProperties = function (parentSeries) {
        var fill = parentSeries.fill, opacity = parentSeries.opacity, drawMode = parentSeries.drawMode, polygonVertices = parentSeries.polygonVertices, fillLinearGradient = parentSeries.fillLinearGradient, customTextureOptions = parentSeries.customTextureOptions;
        return {
            fill: fill,
            opacity: opacity,
            drawMode: drawMode,
            polygonVertices: polygonVertices,
            fillLinearGradient: fillLinearGradient,
            customTextureOptions: customTextureOptions
        };
    };
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.onAttachSeries = function () {
        _super.prototype.onAttachSeries.call(this);
        this.nativeDrawingProvider = new this.webAssemblyContext.SCRTTriangleSeriesDrawingProvider();
        var _a = this.getProperties(this.parentSeries), fill = _a.fill, opacity = _a.opacity, fillLinearGradient = _a.fillLinearGradient, customTextureOptions = _a.customTextureOptions;
        //if (fill) {
        this.brushCache.create(fill, opacity !== null && opacity !== void 0 ? opacity : 1, 1, 1, fillLinearGradient, customTextureOptions);
        //}
    };
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.onDetachSeries = function () {
        _super.prototype.onDetachSeries.call(this);
        this.nativeDrawingProvider = (0, Deleter_1.deleteSafe)(this.nativeDrawingProvider);
    };
    TriangleSeriesDrawingProvider.prototype.getBrushForDrawing = function () {
        return this.brushCache;
    };
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.draw = function (renderContext, renderPassData) {
        var _a = this.getProperties(this.parentSeries), fill = _a.fill, drawMode = _a.drawMode, polygonVertices = _a.polygonVertices;
        var fillBrush = (0, BrushCache_1.getScrtBrushFromCache)(this.getBrushForDrawing());
        if (!fillBrush || !fill) {
            return;
        }
        var pointSeries = renderPassData.pointSeries;
        this.args.Reset();
        if (drawMode === TriangleSeriesDrawMode_1.ETriangleSeriesDrawMode.Polygon) {
            this.args.drawMode = this.webAssemblyContext.SCRTTriangleDrawMode.Polygon;
        }
        else if (drawMode === TriangleSeriesDrawMode_1.ETriangleSeriesDrawMode.Strip) {
            this.args.drawMode = this.webAssemblyContext.SCRTTriangleDrawMode.Strip;
        }
        else {
            this.args.drawMode = this.webAssemblyContext.SCRTTriangleDrawMode.List;
        }
        this.args.iPolygonVertices = polygonVertices;
        var hasPP = this.parentSeries.hasFillPaletteProvider();
        // For paletted series, we must pass NEUTRAL_COLOR brush
        if (hasPP) {
            this.args.SetFillBrush(this.neutralColorSCRTBrush);
        }
        else {
            this.args.SetFillBrush(fillBrush);
        }
        this.args.verticalChart = renderPassData.isVerticalChart;
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xValues = this.xSelector(pointSeries);
        if (hasPP) {
            _super.prototype.applyStrokeFillPaletting.call(this, this.parentSeries.stroke, undefined, this.parentSeries.fill, fillBrush, this.parentSeries.opacity, false, true);
            // paletted colors has length twice as the count, first value is stroke and second is fill
            this.args.SetPalettedColors(this.palettingState.palettedColors);
        }
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : xValues;
        var yDrawValues = this.ySelector(pointSeries);
        var _b = this.getStartAndCount(renderPassData, xDrawValues), startIndex = _b.startIndex, count = _b.count;
        this.args.count = count;
        this.args.startIndex = startIndex;
        var x1Values = pointSeries.getYValuesByName(ValueName_1.EValueName.X1);
        var y1Values = pointSeries.getYValuesByName(ValueName_1.EValueName.Y1);
        if (x1Values && y1Values) {
            this.args.SetZValues(x1Values);
            this.args.SetYValues(y1Values);
        }
        else if (x1Values || y1Values) {
            throw new Error("You must either supply both x1 values and y1 values for texture coordinates, or neither to use screen coordinates for the texture");
        }
        this.updateArgs(renderPassData);
        var nativeContext = renderContext.getNativeContext();
        this.drawTriangles(renderContext, nativeContext, xDrawValues, yDrawValues, renderPassData.xCoordinateCalculator.nativeCalculator, renderPassData.yCoordinateCalculator.nativeCalculator, this.args, this.parentSeries.parentSurface.seriesViewRect);
    };
    /**
     * @inheritDoc
     */
    TriangleSeriesDrawingProvider.prototype.delete = function () {
        this.nativeDrawingProvider = (0, Deleter_1.deleteSafe)(this.nativeDrawingProvider);
        this.args = (0, Deleter_1.deleteSafe)(this.args);
        this.brushCache = (0, Deleter_1.deleteSafe)(this.brushCache);
        this.neutralColorSCRTBrush = (0, Deleter_1.deleteSafe)(this.neutralColorSCRTBrush);
        _super.prototype.delete.call(this);
    };
    TriangleSeriesDrawingProvider.prototype.drawTriangles = function (renderContext, nativeContext, xValues, yValues, xCoordCalc, yCoordCalc, args, viewRect) {
        args.SetNativeContext(nativeContext);
        args.SetXValues(xValues);
        args.SetYValues(yValues);
        args.SetXCoordinateCalculator(xCoordCalc);
        args.SetYCoordinateCalculator(yCoordCalc);
        this.nativeDrawingProvider.DrawTrianglesVec(args);
    };
    return TriangleSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.TriangleSeriesDrawingProvider = TriangleSeriesDrawingProvider;
