"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.HeightSeriesDrawingProvider = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var HeightColorSettings_1 = require("../../../../types/HeightColorSettings");
var HeightColorUtils_1 = require("../../../../utils/HeightColorUtils");
var parseColor_1 = require("../../../../utils/parseColor");
var BrushCache_1 = require("../../../Drawing/BrushCache");
var Pen2DCache_1 = require("../../../Drawing/Pen2DCache");
var IPaletteProvider_1 = require("../../../Model/IPaletteProvider");
var NativeObject_1 = require("../../Helpers/NativeObject");
var constants_1 = require("../constants");
var BaseSeriesDrawingProvider_1 = require("./BaseSeriesDrawingProvider");
var HeightSeriesMode_1 = require("../../../../types/HeightSeriesMode");
/**
 * Used internally - a drawing provider performs drawing for a {@link HeightRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var HeightSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(HeightSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link HeightSeriesDrawingProvider}
     * @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 IHeightRenderableSeries} which this drawing provider is attached to
     */
    function HeightSeriesDrawingProvider(webAssemblyContext, parentSeries, ySelector, zSelector) {
        var _this = _super.call(this, webAssemblyContext, parentSeries, ySelector) || this;
        _this.args = new _this.webAssemblyContext.SCRTHeightSeriesParams();
        _this.colorSettings = new _this.webAssemblyContext.SCRTHeightColorSettings();
        _this.zSelector = zSelector !== null && zSelector !== void 0 ? zSelector : (function (ps) { return ps.zValues; });
        _this.linesPenCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.fillBrushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        return _this;
    }
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.prototype.onAttachSeries = function () {
        _super.prototype.onAttachSeries.call(this);
        this.nativeDrawingProvider = new this.webAssemblyContext.SCRTHeightSeriesDrawingProvider();
        var _a = this.parentSeries, stroke = _a.stroke, strokeThickness = _a.strokeThickness, fill = _a.fill, opacity = _a.opacity, strokeDashArray = _a.strokeDashArray, customTextureOptions = _a.customTextureOptions;
        (0, Pen2DCache_1.createPenInCache)(this.linesPenCache, stroke, strokeThickness, opacity, strokeDashArray);
        this.fillBrushCache.create(fill, opacity, 1, 1, undefined, customTextureOptions);
    };
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.prototype.onDetachSeries = function () {
        _super.prototype.onDetachSeries.call(this);
        this.delete();
    };
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.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);
        _super.prototype.delete.call(this);
    };
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.prototype.draw = function (renderContext, renderPassData) {
        var _a;
        var pointSeries = renderPassData.pointSeries;
        this.args.Reset();
        this.colorSettings.Reset();
        var fillBrush = (0, BrushCache_1.getScrtBrushFromCache)(this.fillBrushCache);
        if (fillBrush) {
            this.args.SetFillBrush(fillBrush);
        }
        var _b = this.parentSeries, zMultiplier = _b.zMultiplier, zLimits = _b.zLimits, paletteProvider = _b.paletteProvider, normalise = _b.normalise, isInverseHeight = _b.isInverseHeight, strokeThickness = _b.strokeThickness, heightMode = _b.heightMode, dataSeries = _b.dataSeries;
        this.args.zMultiplier = zMultiplier;
        this.args.isInverseHeight = isInverseHeight;
        this.args.normaliseHeights = normalise;
        var linesPen = (0, Pen2DCache_1.getScrtPenFromCache)(this.linesPenCache);
        if (linesPen && strokeThickness > 0) {
            this.args.SetEdgePen(linesPen);
        }
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xValues = pointSeries.xValues;
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : xValues;
        var yDrawValues = this.ySelector(pointSeries);
        var zDrawValues = this.zSelector(pointSeries);
        if (normalise && (!zLimits || zLimits.max === Number.MAX_VALUE)) {
            var minMax = void 0;
            try {
                minMax = this.webAssemblyContext.NumberUtil.MinMax(zDrawValues, dataSeries === null || dataSeries === void 0 ? void 0 : dataSeries.dataDistributionCalculator.containsNaN);
                this.args.minHeight = minMax.minD;
                this.args.maxHeight = minMax.maxD;
            }
            finally {
                (0, Deleter_1.deleteSafe)(minMax);
            }
        }
        else {
            this.args.minHeight = zLimits.min;
            this.args.maxHeight = zLimits.max;
        }
        var _c = this.getStartAndCount(renderPassData, xDrawValues), startIndex = _c.startIndex, count = _c.count;
        //console.log(startIndex, count);
        //logDoubleVector(zDrawValues, "", 0, 100);
        this.args.startIndex = Math.max(0, startIndex - 1);
        this.args.count = Math.min(xDrawValues.size(), count + 1);
        this.args.verticalChart = renderPassData.isVerticalChart;
        if (heightMode === HeightSeriesMode_1.EHeightSeriesMode.Horizontal) {
            this.args.SetNormalDirection(renderPassData.isVerticalChart
                ? (0, NativeObject_1.getVector3)(this.webAssemblyContext, 0, 1, 0)
                : (0, NativeObject_1.getVector3)(this.webAssemblyContext, 1, 0, 0));
        }
        else if (heightMode === HeightSeriesMode_1.EHeightSeriesMode.Vertical) {
            this.args.SetNormalDirection(renderPassData.isVerticalChart
                ? (0, NativeObject_1.getVector3)(this.webAssemblyContext, 1, 0, 0)
                : (0, NativeObject_1.getVector3)(this.webAssemblyContext, 0, 1, 0));
        }
        // leave undefined for usual perpendicular mode
        var hasPaletteProvider = paletteProvider.overrideFillArgb !== undefined;
        if (hasPaletteProvider) {
            // Paletting per point
            _super.prototype.applyStrokeFillPaletting.call(this, this.parentSeries.stroke, linesPen, this.parentSeries.fill, fillBrush, this.parentSeries.opacity, false, true, renderPassData);
            this.args.SetPalettedColors(this.palettingState.palettedColors);
            this.args.paletteStart = this.palettingState.paletteStartIndex;
            this.args.interpolate = this.palettingState.gradientPaletting;
        }
        if (this.parentSeries.colorSettings) {
            var colorStops = this.parentSeries.colorSettings.colorStops;
            (0, HeightColorUtils_1.validateColorStops)(this.parentSeries.colorSettings);
            for (var _i = 0, colorStops_1 = colorStops; _i < colorStops_1.length; _i++) {
                var colorStop = colorStops_1[_i];
                this.colorSettings.AddColorStop(colorStop.dataValue, (0, parseColor_1.parseColorToUIntArgb)(colorStop.color));
            }
            if (this.parentSeries.colorSettings.infraColor) {
                this.colorSettings.m_uiInfra = (0, parseColor_1.parseColorToUIntArgb)(this.parentSeries.colorSettings.infraColor);
            }
            if (this.parentSeries.colorSettings.ultraColor) {
                this.colorSettings.m_uiUltra = (0, parseColor_1.parseColorToUIntArgb)(this.parentSeries.colorSettings.ultraColor);
            }
            this.colorSettings.m_fMinHeight = colorStops[0].dataValue;
            this.colorSettings.m_fMaxHeight = colorStops[colorStops.length - 1].dataValue;
            this.colorSettings.m_bIsGradient =
                this.parentSeries.colorSettings.colorPickMode === HeightColorSettings_1.EColorPickMode.Interpolated;
            this.args.interpolate = this.parentSeries.colorSettings.fillMode === IPaletteProvider_1.EFillPaletteMode.GRADIENT;
            this.colorSettings.m_fMaxTextureSize = (_a = this.parentSeries.colorSettings.maxTextureSize) !== null && _a !== void 0 ? _a : 0;
            this.args.SetHeightColors(this.colorSettings);
        }
        if (this.parentSeries.heightsForColoringDataSeries) {
            this.args.SetHeightsForColoring(this.parentSeries.getHeightsForColoring(pointSeries.resampled));
        }
        this.args.SetXValues(xDrawValues);
        this.args.SetYValues(yDrawValues);
        this.args.SetZValues(zDrawValues);
        this.args.SetXCoordinateCalculator(renderPassData.xCoordinateCalculator.nativeCalculator);
        this.args.SetYCoordinateCalculator(renderPassData.yCoordinateCalculator.nativeCalculator);
        this.args.SetNativeContext(renderContext.getNativeContext());
        this.nativeDrawingProvider.DrawPointsVec(this.args);
    };
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
    };
    /**
     * @inheritDoc
     */
    HeightSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        var _a = this.parentSeries, stroke = _a.stroke, strokeThickness = _a.strokeThickness, fill = _a.fill, opacity = _a.opacity, strokeDashArray = _a.strokeDashArray, 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);
        }
        if (propertyName === constants_1.PROPERTY.FILL ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.CUSTOM_TEXTURE_OPTIONS) {
            this.palettingState.requiresUpdate = true;
            this.fillBrushCache.create(fill, opacity, 1, 1, undefined, customTextureOptions);
        }
    };
    return HeightSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.HeightSeriesDrawingProvider = HeightSeriesDrawingProvider;
