"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.StackedColumnSeriesDrawingProvider = exports.StackedColumnRenderPassData = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var BrushCache_1 = require("../../../Drawing/BrushCache");
var Pen2DCache_1 = require("../../../Drawing/Pen2DCache");
var RenderPassData_1 = require("../../../Services/RenderPassData");
var SciChartSurfaceBase_1 = require("../../SciChartSurfaceBase");
var constants_1 = require("../constants");
var BaseSeriesDrawingProvider_1 = require("./BaseSeriesDrawingProvider");
var StackedColumnRenderPassData = /** @class */ (function (_super) {
    __extends(StackedColumnRenderPassData, _super);
    function StackedColumnRenderPassData(indexRange, getxCoordinateCalculator, getyCoordinateCalculator, isVerticalChart, columnWidth, stackedGroupCount, groupIndex, spacing, pointSeries, resamplingHash) {
        var _this = _super.call(this, indexRange, getxCoordinateCalculator, getyCoordinateCalculator, isVerticalChart, pointSeries, resamplingHash) || this;
        _this.columnWidth = columnWidth;
        _this.stackedGroupCount = stackedGroupCount;
        _this.groupIndex = groupIndex;
        _this.spacing = spacing;
        return _this;
    }
    return StackedColumnRenderPassData;
}(RenderPassData_1.RenderPassData));
exports.StackedColumnRenderPassData = StackedColumnRenderPassData;
/**
 * Used internally - a drawing provider performs drawing for a {@link StackedColumnRenderableSeries} using
 * our WebAssembly WebGL rendering engine
 */
var StackedColumnSeriesDrawingProvider = /** @class */ (function (_super) {
    __extends(StackedColumnSeriesDrawingProvider, _super);
    /**
     * Creates an instance of the {@link StackedColumnSeriesDrawingProvider}
     * @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 StackedColumnRenderableSeries} which this drawing provider is attached to
     */
    function StackedColumnSeriesDrawingProvider(webAssemblyContext, parentSeries, ySelector, xSelector) {
        var _this = _super.call(this, webAssemblyContext, parentSeries, ySelector, xSelector) || this;
        _this.nativeDrawingProvider = new webAssemblyContext.SCRTStackedColumnSeriesDrawingProvider();
        _this.args = new _this.webAssemblyContext.SCRTStackedColumnDrawingParams();
        _this.strokePenCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.strokePenFillColoredCache = new Pen2DCache_1.Pen2DCache(webAssemblyContext);
        _this.fillBrushCache = new BrushCache_1.BrushCache(webAssemblyContext);
        return _this;
    }
    /**
     * @inheritDoc
     */
    StackedColumnSeriesDrawingProvider.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
     */
    StackedColumnSeriesDrawingProvider.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);
    };
    StackedColumnSeriesDrawingProvider.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
     */
    StackedColumnSeriesDrawingProvider.prototype.draw = function (renderContext, renderPassData) {
        var pointSeries = renderPassData.pointSeries;
        var viewRect = this.parentSeries.parentSurface.seriesViewRect;
        var _a = this.getProperties(this.parentSeries), strokeThickness = _a.strokeThickness, fill = _a.fill;
        this.args.Reset();
        this.args.forceShaderMethod = true;
        this.args.verticalChart = renderPassData.isVerticalChart;
        this.args.columnWidth = renderPassData.columnWidth;
        this.args.spacing = renderPassData.spacing;
        this.args.stackedGroupCount = renderPassData.stackedGroupCount;
        this.args.stackedGroupIndex = renderPassData.groupIndex;
        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 (renderPassData.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, renderPassData);
        this.args.SetPalettedColors(this.palettingState.palettedColors);
        this.args.paletteStart = this.palettingState.paletteStartIndex;
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        var xDrawValues = isCategoryAxis ? pointSeries.indexes : pointSeries.xValues;
        var _b = this.getStartAndCount(renderPassData, xDrawValues), startIndex = _b.startIndex, count = _b.count;
        this.args.count = count;
        this.args.startIndex = startIndex;
        // const { fifoCapacity, fifoSweeping, fifoSweepingGap } = this.parentSeries.dataSeries;
        // const fifoStartIndex = pointSeries.fifoStartIndex;
        // if (fifoSweeping && fifoCapacity === this.parentSeries.dataSeries.count()) {
        //     this.args.count = fifoStartIndex;
        // }
        var nativeContext = renderContext.getNativeContext();
        this.args.SetNativeContext(nativeContext);
        this.args.SetXValues(xDrawValues);
        this.args.SetYValues(pointSeries.yValues);
        this.args.SetZValues(pointSeries.y1Values);
        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(yValues.size(), fifoStartIndex + fifoSweepingGap);
        //     this.args.count = Math.max(0, yValues.size() - fifoStartIndex - fifoSweepingGap);
        //     if (this.args.count > 0) {
        //         this.nativeDrawingProvider.DrawPointsVec(this.args);
        //     }
        // }
    };
    /**
     * @inheritDoc
     */
    StackedColumnSeriesDrawingProvider.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.onSeriesPropertyChange(constants_1.PROPERTY.STROKE);
    };
    /**
     * @inheritDoc
     */
    StackedColumnSeriesDrawingProvider.prototype.onSeriesPropertyChange = function (propertyName) {
        _super.prototype.onSeriesPropertyChange.call(this, propertyName);
        var _a = this.getProperties(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);
        }
    };
    StackedColumnSeriesDrawingProvider.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;
        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;
        var brush = this.fillBrushCache.create(fill, opacity, textureHeightRatio, textureWidthRatio, fillLinearGradient, customTextureOptions);
        return brush.scrtBrush;
    };
    return StackedColumnSeriesDrawingProvider;
}(BaseSeriesDrawingProvider_1.BaseSeriesDrawingProvider));
exports.StackedColumnSeriesDrawingProvider = StackedColumnSeriesDrawingProvider;
