"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.PolarStackedColumnRenderableSeries = void 0;
var Deleter_1 = require("../../../../Core/Deleter");
var ColumnMode_1 = require("../../../../types/ColumnMode");
var DataPointWidthMode_1 = require("../../../../types/DataPointWidthMode");
var SeriesType_1 = require("../../../../types/SeriesType");
var XyPointSeriesWrapped_1 = require("../../../Model/PointSeries/XyPointSeriesWrapped");
var IThemeProvider_1 = require("../../../Themes/IThemeProvider");
var SciChartSurfaceBase_1 = require("../../SciChartSurfaceBase");
var BaseStackedRenderableSeries_1 = require("../BaseStackedRenderableSeries");
var constants_1 = require("../constants");
var PointMarkerDrawingProvider_1 = require("../DrawingProviders/PointMarkerDrawingProvider");
var FastColumnRenderableSeries_1 = require("../FastColumnRenderableSeries");
var RectangleSeriesHitTestProvider_1 = require("../HitTest/RectangleSeriesHitTestProvider");
var PolarColumnSeriesDrawingProvider_1 = require("./DrawingProviders/PolarColumnSeriesDrawingProvider");
/**
 * Defines a polar stacked column-series or polar stacked column chart type in the SciChart's High Performance Real-time
 * {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}, which needs to be paired within a
 * {@link PolarStackedColumnCollection | Collection} to function correctly.
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/polar-stacked-column-renderable-series/}
 */
var PolarStackedColumnRenderableSeries = /** @class */ (function (_super) {
    __extends(PolarStackedColumnRenderableSeries, _super);
    /**
     * Creates an instance of the {@link PolarStackedColumnRenderableSeries}
     * @param webAssemblyContext The {@link TSciChart | SciChart WebAssembly Context} containing
     * native methods and access to our WebGL2 WebAssembly Drawing Engine
     * @param options optional parameters of type {@link IPolarColumnRenderableSeriesOptions} applied when constructing the series type
     *
     * ---
     * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/polar-stacked-column-renderable-series/}
     */
    function PolarStackedColumnRenderableSeries(webAssemblyContext, options) {
        var _this = this;
        var _a, _b, _c, _d, _e;
        _this = _super.call(this, webAssemblyContext, options) || this;
        _this.type = SeriesType_1.ESeriesType.PolarStackedColumnSeries;
        _this.isPolar = true;
        _this.fillProperty = SciChartSurfaceBase_1.SciChartSurfaceBase.DEFAULT_THEME.columnFillBrush;
        _this.polarColumnXModeProperty = ColumnMode_1.EColumnMode.Mid;
        _this.polarColumnYModeProperty = ColumnMode_1.EColumnYMode.TopBottom;
        _this.dataPointWidthProperty = 0.8;
        _this.dataPointWidthModeProperty = DataPointWidthMode_1.EDataPointWidthMode.Range;
        _this.fill = (_a = options === null || options === void 0 ? void 0 : options.fill) !== null && _a !== void 0 ? _a : _this.fillProperty;
        _this.fillLinearGradientProperty = (_b = options === null || options === void 0 ? void 0 : options.fillLinearGradient) !== null && _b !== void 0 ? _b : _this.fillLinearGradientProperty;
        _this.polarColumnXModeProperty = (_c = options === null || options === void 0 ? void 0 : options.columnXMode) !== null && _c !== void 0 ? _c : _this.polarColumnXModeProperty;
        _this.polarColumnYModeProperty = (_d = options === null || options === void 0 ? void 0 : options.columnYMode) !== null && _d !== void 0 ? _d : _this.polarColumnYModeProperty;
        _this.dataPointWidthModeProperty = (_e = options === null || options === void 0 ? void 0 : options.dataPointWidthMode) !== null && _e !== void 0 ? _e : _this.dataPointWidthModeProperty;
        _this.customTextureOptionsProperty = options === null || options === void 0 ? void 0 : options.customTextureOptions;
        return _this;
    }
    PolarStackedColumnRenderableSeries.prototype.addDrawingProviders = function (webAssemblyContext, options) {
        this.drawingProviders.push(new PolarColumnSeriesDrawingProvider_1.PolarColumnSeriesDrawingProvider(webAssemblyContext, this));
        this.drawingProviders.push(new PointMarkerDrawingProvider_1.PointMarkerDrawingProvider(webAssemblyContext, this));
    };
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "columnXMode", {
        /**
         * Gets or Sets how the x values and optional x1 values are interpreted
         */
        get: function () {
            return this.polarColumnXModeProperty;
        },
        set: function (value) {
            if (this.polarColumnXModeProperty !== value) {
                this.polarColumnXModeProperty = value;
                this.notifyPropertyChanged("POLAR_COLUMN_MODE");
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "dataPointWidthMode", {
        /**
         * Gets or sets the mode which determines how dataPointWidth in X direction is interpreted. Available values are {@link EDataPointWidthMode}.  Default Range.
         */
        get: function () {
            return this.dataPointWidthModeProperty;
        },
        set: function (value) {
            this.dataPointWidthModeProperty = value;
            this.notifyPropertyChanged(constants_1.PROPERTY.DATA_POINT_WIDTH_MODE);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "columnYMode", {
        get: function () {
            return ColumnMode_1.EColumnYMode.TopBottom;
        },
        /**
         * Setting columnYMode is not supported for PolarColumnRenderableSeries
         */
        set: function (value) {
            throw Error("columnYMode is not supported for PolarColumnRenderableSeries");
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "defaultY1", {
        /**
         * Gets or sets the Zero-line Y, the Y-value for the base of the columns. Default is 0
         */
        get: function () {
            return 0;
        },
        /**
         * Gets or sets the Zero-line Y, the Y-value for the base of the columns. Default is 0
         */
        set: function (zeroLineY) { },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "dataPointWidth", {
        /**
         * Gets or sets the width of columns as a fraction of available space. Valid values range from 0.0 - 1.0
         */
        get: function () {
            return this.dataPointWidthProperty;
        },
        /**
         * Gets or sets the width of columns as a fraction of available space. Valid values range from 0.0 - 1.0
         */
        set: function (dataPointWidth) {
            this.dataPointWidthProperty = dataPointWidth;
            this.notifyPropertyChanged(constants_1.PROPERTY.DATA_POINT_WIDTH);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "fillLinearGradient", {
        /**
         * Gets or sets the fill as a gradient brush
         */
        get: function () {
            return this.fillLinearGradientProperty;
        },
        /**
         * Gets or sets the fill as a gradient brush
         */
        set: function (gradientBrushParams) {
            // We need this check, to prevent creating new Texture if we set the same value
            if (gradientBrushParams !== this.fillLinearGradientProperty) {
                this.fillLinearGradientProperty = gradientBrushParams;
                this.notifyPropertyChanged(constants_1.PROPERTY.FILL_LINEAR_GRADIENT);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "customTextureOptions", {
        /**
         * Gets or sets options to use a custom texture brush
         */
        get: function () {
            return this.customTextureOptionsProperty;
        },
        /**
         * Gets or sets options to use a custom texture brush
         */
        set: function (customTextureOptions) {
            // We need this check, to prevent creating new Texture if we set the same value
            if (customTextureOptions !== this.customTextureOptionsProperty) {
                this.customTextureOptionsProperty = customTextureOptions;
                this.notifyPropertyChanged(constants_1.PROPERTY.CUSTOM_TEXTURE_OPTIONS);
            }
        },
        enumerable: false,
        configurable: true
    });
    // PUBLIC
    /**
     * Called internally when the {@link PolarStackedColumnRenderableSeries} is attached to a parent {@link PolarStackedColumnCollection}
     * @param parentCollection the parent {@link BaseStackedCollection}
     * @param getParentSurfaceFn function to get the parent {@link SciChartSurface}
     * @param notifyPropertyChangedFn function to notify property has changed
     */
    PolarStackedColumnRenderableSeries.prototype.onAttachToParentCollection = function (parentCollection, getParentSurfaceFn, notifyPropertyChangedFn) {
        if (this.parentCollection) {
            throw new Error("Invalid operation in StackedMountainRenderableSeries.onAttachToParentCollection, this series has been already attached to collection. Please detach it from the collection before attaching to another");
        }
        this.parentCollection = parentCollection;
        this.getParentSurfaceFn = getParentSurfaceFn;
        this.notifyParentPropertyChangedFn = notifyPropertyChangedFn;
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.onAttach = function (scs) {
        if (this.invalidateParentCallback) {
            throw new Error("Invalid operation in sciChartSurface.attachSeries, this series has already been attached to a SciChartSurface. Please detach it from a SciChartSurface before attaching to another");
        }
        this.invalidateParentCallback = scs.invalidateElement;
        this.drawingProviders.forEach(function (dp) { return dp.onAttachSeries(); });
        this.rolloverModifierProps.setInvalidateParentCallback(scs.invalidateElement);
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.onDetach = function () {
        this.invalidateParentCallback = undefined;
        this.drawingProviders.forEach(function (dp) { return dp.onDetachSeries(); });
        this.rolloverModifierProps.setInvalidateParentCallback(undefined);
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.draw = function (renderContext, renderPassData) {
        var _a;
        this.currentRenderPassData = renderPassData;
        (_a = this.hitTestProvider) === null || _a === void 0 ? void 0 : _a.update(renderPassData);
        try {
            renderContext.pushShaderEffect(this.effect);
            this.drawingProviders.forEach(function (dp) { return dp.draw(renderContext, renderPassData); });
        }
        finally {
            renderContext.popShaderEffect();
        }
    };
    /**
     * @inheritDoc
     */
    PolarStackedColumnRenderableSeries.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.notifyPropertyChanged(constants_1.PROPERTY.STROKE);
    };
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "fill", {
        // PROPERTIES
        /**
         * Gets or sets the fill brush of the column as an HTML color code
         */
        get: function () {
            return (0, IThemeProvider_1.stripAutoColor)(this.fillProperty);
        },
        /**
         * Gets or sets the fill brush of the column as an HTML color code
         */
        set: function (fill) {
            if (this.fillProperty !== fill) {
                this.fillProperty = fill;
                this.drawingProviders.forEach(function (dp) { return dp.onSeriesPropertyChange(constants_1.PROPERTY.FILL); });
                // If fill changes, we need to redo both brushes on the drawing provider
                this.drawingProviders.forEach(function (dp) { return dp.onSeriesPropertyChange(constants_1.PROPERTY.FILL_Y1); });
                this.notifyPropertyChanged(constants_1.PROPERTY.FILL);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarStackedColumnRenderableSeries.prototype, "fillY1", {
        /**
         * Gets or sets the fill color for when Y1 is less than Y as an HTML Color code
         */
        set: function (fillY1) {
            throw new Error("Setting fillY1 property is not supported on StackedMountainRenderableSeries");
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Gets the RenderPassData instance used for this render pass
     */
    PolarStackedColumnRenderableSeries.prototype.getCurrentRenderPassData = function () {
        return this.currentRenderPassData;
    };
    // PROPERTIES END
    PolarStackedColumnRenderableSeries.prototype.toJSON = function (excludeData) {
        if (excludeData === void 0) { excludeData = false; }
        var json = _super.prototype.toJSON.call(this, excludeData);
        var options = {
            columnXMode: this.columnXMode,
            columnYMode: this.columnYMode,
            dataPointWidthMode: this.dataPointWidthMode,
            stroke: this.stroke,
            strokeThickness: this.strokeThickness,
            fill: this.fill,
            fillLinearGradient: this.fillLinearGradient
        };
        Object.assign(json.options, options);
        return json;
    };
    PolarStackedColumnRenderableSeries.prototype.delete = function () {
        // TODO this could just call delete of BaseRenderableSeries
        this.drawingProviders.forEach(function (dp) { return dp.delete(); });
        this.drawingProviders = [];
        this.effect = (0, Deleter_1.deleteSafe)(this.effect);
        _super.prototype.delete.call(this);
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.toPointSeries = function (resamplingParams) {
        return new XyPointSeriesWrapped_1.XyPointSeriesWrapped(this.dataSeries);
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.hasStrokePaletteProvider = function () {
        var strokePalette = this.paletteProvider;
        return (strokePalette === null || strokePalette === void 0 ? void 0 : strokePalette.overrideStrokeArgb) !== undefined;
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.hasFillPaletteProvider = function () {
        var fillPalette = this.paletteProvider;
        return (fillPalette === null || fillPalette === void 0 ? void 0 : fillPalette.overrideFillArgb) !== undefined;
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.hasPointMarkerPaletteProvider = function () {
        var pointMarkerPalette = this.paletteProvider;
        return (pointMarkerPalette === null || pointMarkerPalette === void 0 ? void 0 : pointMarkerPalette.overridePointMarkerArgb) !== undefined;
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.resolveAutoColors = function (index, maxSeries, theme) {
        _super.prototype.resolveAutoColors.call(this, index, maxSeries, theme);
        if (this.fillProperty.startsWith(IThemeProvider_1.AUTO_COLOR)) {
            var color = theme.getFillColor(index, maxSeries, this.webAssemblyContext);
            this.fill = IThemeProvider_1.AUTO_COLOR + this.adjustAutoColor("fill", color);
        }
        // No need to resolve strokeY1 as the collection sets it based on previous series
    };
    /**
     * @inheritDoc
     */
    PolarStackedColumnRenderableSeries.prototype.getXRange = function () {
        var range = this.dataSeries.xRange;
        var count = this.dataSeries.count();
        return (0, FastColumnRenderableSeries_1.getXRange)(range, count, this.dataPointWidth);
    };
    PolarStackedColumnRenderableSeries.prototype.getDataPointWidth = function (xCoordCalc, widthFraction) {
        var xValues = this.dataSeries.getNativeXValues();
        var seriesViewRectWidth = xCoordCalc.viewportDimension;
        var isCategoryAxis = xCoordCalc.isCategoryCoordinateCalculator;
        if (widthFraction < 0 || widthFraction > 1) {
            throw new Error("WidthFraction should be between 0.0 and 1.0 inclusive");
        }
        // TODO: vertical chart
        var count = xValues.size();
        var dataPointWidth = seriesViewRectWidth;
        var barsAmount = count;
        // TODO: logarithmic axis
        if (barsAmount > 1) {
            var max = Number.NEGATIVE_INFINITY;
            var min = Number.MAX_VALUE;
            if (isCategoryAxis) {
                max = xCoordCalc.getCoordinate(count - 1);
                min = xCoordCalc.getCoordinate(0);
            }
            else {
                var minMax = void 0;
                try {
                    // containsNaN is always false for xValues
                    minMax = this.webAssemblyContext.NumberUtil.MinMax(xValues, false);
                    // if (!isRealNumber(minMax.minD) || !isRealNumber(minMax.maxD)) {
                    //     return new NumberRange(0, 0);
                    // }
                    min = minMax.minD;
                    max = minMax.maxD;
                }
                finally {
                    (0, Deleter_1.deleteSafe)(minMax);
                }
                max = xCoordCalc.getCoordinate(max);
                min = xCoordCalc.getCoordinate(min);
            }
            dataPointWidth = Math.abs(max - min) / (barsAmount - 1);
        }
        else if (barsAmount === 1) {
            dataPointWidth = Math.min(dataPointWidth, xCoordCalc.getCoordinate(xValues.get(0)) * 2);
        }
        return dataPointWidth * widthFraction;
    };
    /** @inheritDoc */
    PolarStackedColumnRenderableSeries.prototype.newHitTestProvider = function () {
        return new RectangleSeriesHitTestProvider_1.RectangleSeriesHitTestProvider(this, this.webAssemblyContext);
    };
    return PolarStackedColumnRenderableSeries;
}(BaseStackedRenderableSeries_1.BaseStackedRenderableSeries));
exports.PolarStackedColumnRenderableSeries = PolarStackedColumnRenderableSeries;
