"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.RectangleSeriesDrawingProvider = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var ColumnMode_1 = require("../../../../types/ColumnMode");
var ValueName_1 = require("../../../../types/ValueName");
var BrushCache_1 = require("../../../Drawing/BrushCache");
var Pen2DCache_1 = require("../../../Drawing/Pen2DCache");
var constants_1 = require("../constants");
var BaseSeriesDrawingProvider_1 = require("./BaseSeriesDrawingProvider");
/**
 * Used internally - a drawing provider performs drawing for a {@link FastRectangleRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var RectangleSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(RectangleSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link RectangleSeriesDrawingProvider}
     * @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 FastRectangleRenderableSeries} which this drawing provider is attached to
     */
    function RectangleSeriesDrawingProvider(webAssemblyContext, parentSeries, ySelector, xSelector) {
        var _this = _super.call(this, webAssemblyContext, parentSeries, ySelector, xSelector) || this;
        _this.nativeDrawingProvider = new webAssemblyContext.SCRTRectangleSeriesDrawingProvider();
        _this.args = new _this.webAssemblyContext.SCRTRectangleDrawingParams();
        _this.strokePenCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.strokePenFillColoredCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.fillBrushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        return _this;
    }
    /**
     * @inheritDoc
     */
    RectangleSeriesDrawingProvider.prototype.onAttachSeries = function () {
        _super.prototype.onAttachSeries.call(this);
        var _a = this.getProperties(this.parentSeries), stroke = _a.stroke, strokeThickness = _a.strokeThickness, fill = _a.fill, opacity = _a.opacity;
        (0, Pen2DCache_1.createPenInCache)(this.strokePenCache, stroke, strokeThickness, opacity);
        // the pen is used to fix a bug when columns disappear when zooming out with zero stroke thickness
        if (strokeThickness === 0) {
            (0, Pen2DCache_1.createPenInCache)(this.strokePenFillColoredCache, fill, 1, opacity);
        }
        this.createBrush();
    };
    /**
     * @inheritDoc
     */
    RectangleSeriesDrawingProvider.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.strokePenFillColoredCache = (0, Deleter_1.deleteSafe)(this.strokePenFillColoredCache);
        this.fillBrushCache = (0, Deleter_1.deleteSafe)(this.fillBrushCache);
        _super.prototype.delete.call(this);
    };
    RectangleSeriesDrawingProvider.prototype.getProperties = function (parentSeries) {
        var stroke = parentSeries.stroke, strokeThickness = parentSeries.strokeThickness, opacity = parentSeries.opacity, fill = parentSeries.fill, fillLinearGradient = parentSeries.fillLinearGradient, customTextureOptions = parentSeries.customTextureOptions;
        return {
            stroke: stroke,
            strokeThickness: strokeThickness,
            opacity: opacity,
            fill: fill,
            fillLinearGradient: fillLinearGradient,
            customTextureOptions: customTextureOptions
        };
    };
    /**
     * @inheritDoc
     */
    RectangleSeriesDrawingProvider.prototype.draw = function (renderContext, renderPassData) {
        var pointSeries = renderPassData.pointSeries;
        var viewRect = this.parentSeries.parentSurface.seriesViewRect;
        var strokeThickness = this.getProperties(this.parentSeries).strokeThickness;
        this.args.Reset();
        this.args.verticalChart = renderPassData.isVerticalChart;
        this.args.columnWidth = this.parentSeries.getDataPointWidth(renderPassData.xCoordinateCalculator, this.parentSeries.dataPointWidth, this.parentSeries.dataPointWidthMode);
        // because Y coordinate calculator is flipped top is bottom and bottom is top
        this.args.bottomRadius = this.parentSeries.topCornerRadius;
        this.args.topRadius = this.parentSeries.bottomCornerRadius;
        this.args.xMode = (0, ColumnMode_1.convertColumnMode)(this.parentSeries.columnXMode, this.webAssemblyContext);
        this.args.defaultY2 = this.parentSeries.defaultY1;
        this.args.yMode = (0, ColumnMode_1.convertYColumnMode)(this.parentSeries.columnYMode, this.webAssemblyContext);
        var strokePenCache = this.strokePenCache;
        // ISSUE: If the strokeThickness property is not provided,
        // the fill will be disappeared with large zoom (when the column width will be small or zero)
        if (this.args.columnWidth === 1 && strokeThickness === 0) {
            this.args.columnWidth = 0;
            strokePenCache = this.strokePenFillColoredCache;
        }
        var linesPen = (0, Pen2DCache_1.getScrtPenFromCache)(strokePenCache);
        if (linesPen) {
            this.args.SetLinesPen(linesPen);
        }
        var fillBrush = (0, BrushCache_1.getScrtBrushFromCache)(this.fillBrushCache);
        if (fillBrush) {
            this.args.SetFillBrush(fillBrush);
        }
        this.args.viewportWidth = viewRect.width;
        this.args.viewportHeight = viewRect.height;
        // Paletting per point
        _super.prototype.applyStrokeFillPaletting.call(this, this.parentSeries.stroke, linesPen, this.parentSeries.fill, fillBrush, this.parentSeries.opacity, false, this.parentSeries.fillLinearGradient !== undefined);
        this.args.SetPalettedColors(this.palettingState.palettedColors);
        this.args.paletteStart = this.palettingState.paletteStartIndex;
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xValues = this.xSelector(pointSeries);
        var yValues = this.ySelector(pointSeries);
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : xValues;
        var _a = this.getStartAndCount(renderPassData, xDrawValues), startIndex = _a.startIndex, count = _a.count;
        this.args.count = count;
        this.args.startIndex = startIndex;
        this.args.SetXValues(xDrawValues);
        if (yValues) {
            this.args.SetYValues(yValues);
        }
        if (this.parentSeries.columnXMode === ColumnMode_1.EColumnMode.MidWidth ||
            this.parentSeries.columnXMode === ColumnMode_1.EColumnMode.StartEnd ||
            this.parentSeries.columnXMode === ColumnMode_1.EColumnMode.StartWidth) {
            var x1Values = pointSeries.getYValuesByName(ValueName_1.EValueName.X1);
            if (x1Values) {
                this.args.SetZValues(x1Values);
            }
            else {
                throw new Error("columnMode ".concat(this.parentSeries.columnXMode, " requires an DataSeries with a yValue called ").concat(ValueName_1.EValueName.X1));
            }
        }
        var y1Values = pointSeries.getYValuesByName(ValueName_1.EValueName.Y1);
        if (y1Values) {
            this.args.SetWValues(y1Values);
        }
        var _b = this.parentSeries.dataSeries, fifoCapacity = _b.fifoCapacity, fifoSweeping = _b.fifoSweeping, fifoSweepingGap = _b.fifoSweepingGap;
        var fifoStartIndex = pointSeries.fifoStartIndex;
        if (fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
            this.args.count = fifoStartIndex;
        }
        var nativeContext = renderContext.getNativeContext();
        this.args.SetNativeContext(nativeContext);
        this.args.SetXCoordinateCalculator(renderPassData.xCoordinateCalculator.nativeCalculator);
        this.args.SetYCoordinateCalculator(renderPassData.yCoordinateCalculator.nativeCalculator);
        this.nativeDrawingProvider.DrawPoints(this.args);
        if (fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
            this.args.startIndex = Math.min(yValues.size(), fifoStartIndex + fifoSweepingGap);
            this.args.count = Math.max(0, yValues.size() - fifoStartIndex - fifoSweepingGap);
            if (this.args.count > 0) {
                this.nativeDrawingProvider.DrawPoints(this.args);
            }
        }
    };
    /**
     * @inheritDoc
     */
    RectangleSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
    };
    /**
     * @inheritDoc
     */
    RectangleSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        var _a = this.parentSeries, stroke = _a.stroke, strokeThickness = _a.strokeThickness, opacity = _a.opacity, fill = _a.fill;
        if (propertyName === constants_1.PROPERTY.STROKE ||
            propertyName === constants_1.PROPERTY.STROKE_THICKNESS ||
            propertyName === constants_1.PROPERTY.OPACITY) {
            (0, Pen2DCache_1.createPenInCache)(this.strokePenCache, stroke, strokeThickness, opacity);
        }
        if (propertyName === constants_1.PROPERTY.FILL ||
            propertyName === constants_1.PROPERTY.OPACITY ||
            propertyName === constants_1.PROPERTY.CUSTOM_TEXTURE_OPTIONS) {
            this.createBrush();
        }
        if (strokeThickness === 0 &&
            (propertyName === constants_1.PROPERTY.STROKE_THICKNESS ||
                propertyName === constants_1.PROPERTY.STROKE ||
                propertyName === constants_1.PROPERTY.OPACITY ||
                propertyName === constants_1.PROPERTY.FILL)) {
            (0, Pen2DCache_1.createPenInCache)(this.strokePenFillColoredCache, fill, 1, opacity);
        }
    };
    RectangleSeriesDrawingProvider.prototype.createBrush = function () {
        var parentSurface = this.parentSeries.parentSurface;
        var _a = this.getProperties(this.parentSeries), fillLinearGradient = _a.fillLinearGradient, fill = _a.fill, opacity = _a.opacity, customTextureOptions = _a.customTextureOptions;
        // const textureHeightRatio = parentSurface?.isCopyCanvasSurface
        //     ? parentSurface.domCanvas2D.height / SciChartSurfaceBase.domMasterCanvas.height
        //     : 1;
        // const textureWidthRatio = parentSurface?.isCopyCanvasSurface
        //     ? parentSurface.domCanvas2D.width / SciChartSurfaceBase.domMasterCanvas.width
        //     : 1;
        var brush = this.fillBrushCache.create(fill, opacity, 1, 1, fillLinearGradient, customTextureOptions);
        return brush.scrtBrush;
    };
    return RectangleSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.RectangleSeriesDrawingProvider = RectangleSeriesDrawingProvider;
