"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.UniformContoursRenderableSeries = exports.EContourColorMapMode = exports.COLOR_MAP_PREFIX = void 0;
var classFactory_1 = require("../../../Core/classFactory");
var Deleter_1 = require("../../../Core/Deleter");
var BaseType_1 = require("../../../types/BaseType");
var SeriesType_1 = require("../../../types/SeriesType");
var isRealNumber_1 = require("../../../utils/isRealNumber");
var NumericLabelProvider_1 = require("../Axis/LabelProvider/NumericLabelProvider");
var BaseRenderableSeries_1 = require("./BaseRenderableSeries");
var constants_1 = require("./constants");
var ContoursDataLabelProvider_1 = require("./DataLabels/ContoursDataLabelProvider");
var UniformContoursDrawingProvider_1 = require("./DrawingProviders/UniformContoursDrawingProvider");
var HeatmapColorMap_1 = require("./HeatmapColorMap");
var UniformHeatmapHitTestProvider_1 = require("./HitTest/UniformHeatmapHitTestProvider");
/** @ignore */
exports.COLOR_MAP_PREFIX = "colorMap.";
var EContourColorMapMode;
(function (EContourColorMapMode) {
    /**
     *  Applies the Gradient Colors from {@link UniformContoursRenderableSeries} ColorMap individually,
     *  ignoring offsets, e.g. with Gradient Stops Red, Green, Blue then
     *  contour lines will appear Red Green or Blue
     */
    EContourColorMapMode["AlternateColors"] = "AlternateColors";
    /**
     * Applies the Gradient Colors from {@link UniformContoursRenderableSeries} ColorMap according to the data on the chart
     */
    EContourColorMapMode["GradientColors"] = "GradientColors";
})(EContourColorMapMode = exports.EContourColorMapMode || (exports.EContourColorMapMode = {}));
/**
 * Defines a JavaScript Uniform Contours series or Contour Lines chart type in SciChart's High Performance Real-time
 * {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/uniform-contours-renderable-series/}
 */
var UniformContoursRenderableSeries = /** @class */ (function (_super) {
    __extends(UniformContoursRenderableSeries, _super);
    // private drawingModeProperty: EContourDrawingMode;
    /**
     * Creates an instance of the {@link UniformHeatmapRenderableSeries}
     * @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 IHeatmapRenderableSeriesOptions} applied when constructing the series type
     *
     * ---
     * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/uniform-contours-renderable-series/}
     */
    function UniformContoursRenderableSeries(webAssemblyContext, options) {
        var _this = this;
        var _a, _b, _c, _d;
        _this = _super.call(this, webAssemblyContext, options) || this;
        _this.type = SeriesType_1.ESeriesType.UniformContoursSeries;
        _this.colorMapPropertyChanged = _this.colorMapPropertyChanged.bind(_this);
        if (options === null || options === void 0 ? void 0 : options.colorMap) {
            if (!("toJSON" in options.colorMap)) {
                options.colorMap = new HeatmapColorMap_1.HeatmapColorMap(options.colorMap);
            }
        }
        _this.colorMap = options === null || options === void 0 ? void 0 : options.colorMap;
        _this.dataSeries = options === null || options === void 0 ? void 0 : options.dataSeries;
        _this.xAxisId = options === null || options === void 0 ? void 0 : options.xAxisId;
        _this.yAxisId = options === null || options === void 0 ? void 0 : options.yAxisId;
        _this.zMin = options === null || options === void 0 ? void 0 : options.zMin;
        _this.zMax = options === null || options === void 0 ? void 0 : options.zMax;
        _this.zStep = (options === null || options === void 0 ? void 0 : options.zStep) || NaN;
        _this.xOffsetProperty = (_a = options === null || options === void 0 ? void 0 : options.zOffset) !== null && _a !== void 0 ? _a : 0;
        _this.zLevelsProperty = options === null || options === void 0 ? void 0 : options.zLevels;
        _this.majorLineStyle = (options === null || options === void 0 ? void 0 : options.majorLineStyle) || UniformContoursRenderableSeries.DEFAULT_MAJOR_LINE_STYLE;
        _this.minorLineStyle = (options === null || options === void 0 ? void 0 : options.minorLineStyle) || UniformContoursRenderableSeries.DEFAULT_MINOR_LINE_STYLE;
        _this.minorsPerMajor = (options === null || options === void 0 ? void 0 : options.minorsPerMajor) || 0;
        _this.majorLineStyleProperty = options === null || options === void 0 ? void 0 : options.majorLineStyle;
        _this.minorLineStyleProperty = options === null || options === void 0 ? void 0 : options.minorLineStyle;
        if (options === null || options === void 0 ? void 0 : options.zLabelProvider) {
            if (!("getLabels" in (options === null || options === void 0 ? void 0 : options.zLabelProvider))) {
                options.zLabelProvider = (0, classFactory_1.createType)(BaseType_1.EBaseType.LabelProvider, options.zLabelProvider.type, webAssemblyContext, options.zLabelProvider.options);
            }
        }
        _this.zLabelProviderProperty =
            (_b = options === null || options === void 0 ? void 0 : options.zLabelProvider) !== null && _b !== void 0 ? _b : new NumericLabelProvider_1.NumericLabelProvider({ labelPrecision: 2 });
        // Must be called here for the series type to be available
        if ((_c = _this.paletteProvider) === null || _c === void 0 ? void 0 : _c.onAttached) {
            (_d = _this.paletteProvider) === null || _d === void 0 ? void 0 : _d.onAttached(_this);
        }
        if (!_this.dataLabelProviderProperty) {
            _this.dataLabelProviderProperty = new ContoursDataLabelProvider_1.ContoursDataLabelProvider(options === null || options === void 0 ? void 0 : options.dataLabels);
            _this.dataLabelProviderProperty.onAttach(_this.webAssemblyContext, _this);
        }
        _this.drawingProviders = [];
        _this.drawingProviders.push(new UniformContoursDrawingProvider_1.UniformContoursDrawingProvider(webAssemblyContext, _this));
        return _this;
    }
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zMin", {
        /**
         * The minimum Z value for which contours will be drawn.  Defaults to the data z minimum.
         */
        get: function () {
            return this.zMinProperty;
        },
        set: function (zMin) {
            this.zMinProperty = zMin;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_MIN);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zMax", {
        /**
         * The maximum Z value for which contours will be drawn.  Defaults to the data z maximum.
         */
        get: function () {
            return this.zMaxProperty;
        },
        set: function (zMax) {
            this.zMaxProperty = zMax;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_MAX);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zStep", {
        /**
         * Gets of sets the step on Z, used to calculate spacing between contours
         */
        get: function () {
            return this.zStepProperty;
        },
        set: function (zStep) {
            this.zStepProperty = zStep;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_STEP);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zOffset", {
        /**
         * Gets of sets the offset of the first contour, can take positive or negative values.
         * The contours will not drawn outsize the zMin, zMax limits.
         * zOffset larger than zStep will be treated as zOffset mod zStep.
         * If you want to draw a contour at a specific value, set the zMin and zMax either side of it and use zOffset.
         * eg to draw a single contour at 10, set zMin = 9, zMax = 11, zOffset = 1
         */
        get: function () {
            return this.xOffsetProperty;
        },
        set: function (value) {
            this.xOffsetProperty = value;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_OFFSET);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zLevels", {
        /**
         * Explicit list of Z values at which to draw contour lines.
         * When set and non-empty, takes precedence over zMin/zMax/zStep/zOffset.
         */
        get: function () {
            return this.zLevelsProperty;
        },
        set: function (value) {
            this.zLevelsProperty = value;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_LEVELS);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "colorMap", {
        /**
         * Gets or sets the {@link HeatmapColorMap}, which maps heatmap z-values to colors
         */
        get: function () {
            return this.colorMapProperty;
        },
        /**
         * Gets or sets the {@link HeatmapColorMap}, which maps heatmap z-values to colors
         */
        set: function (colorMap) {
            var _a, _b;
            (_a = this.colorMapProperty) === null || _a === void 0 ? void 0 : _a.propertyChanged.unsubscribe(this.colorMapPropertyChanged);
            this.colorMapProperty = colorMap;
            (_b = this.colorMapProperty) === null || _b === void 0 ? void 0 : _b.propertyChanged.subscribe(this.colorMapPropertyChanged);
            this.notifyPropertyChanged(constants_1.PROPERTY.COLOR_MAP);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "colorMapMode", {
        get: function () {
            return this.colorMapModeProperty;
        },
        set: function (colorMapMode) {
            this.colorMapModeProperty = colorMapMode;
            this.notifyPropertyChanged(constants_1.PROPERTY.CONTOUR_COLOR_MAP_MODE);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "zLabelProvider", {
        get: function () {
            return this.zLabelProviderProperty;
        },
        set: function (labelProvider) {
            this.zLabelProviderProperty = labelProvider;
            this.notifyPropertyChanged(constants_1.PROPERTY.Z_LABEL_PROVIDER);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "majorLineStyle", {
        get: function () {
            return this.majorLineStyleProperty;
        },
        set: function (majorLineStyle) {
            this.majorLineStyleProperty = majorLineStyle;
            this.notifyPropertyChanged(constants_1.PROPERTY.CONTOUR_MAJOR_LINE_STYLE);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "minorLineStyle", {
        get: function () {
            return this.minorLineStyleProperty;
        },
        set: function (minorLineStyle) {
            this.minorLineStyleProperty = minorLineStyle;
            this.notifyPropertyChanged(constants_1.PROPERTY.CONTOUR_MINOR_LINE_STYLE);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UniformContoursRenderableSeries.prototype, "minorsPerMajor", {
        get: function () {
            return this.minorsPerMajorProperty;
        },
        set: function (minorsPerMajor) {
            this.minorsPerMajorProperty = minorsPerMajor;
            this.notifyPropertyChanged(constants_1.PROPERTY.CONTOUR_MINORS_PER_MAJOR);
        },
        enumerable: false,
        configurable: true
    });
    UniformContoursRenderableSeries.prototype.getContourDrawingParams = function () {
        var _a, _b, _c;
        var heatmapSeries = this.dataSeries;
        var dataZRange = heatmapSeries.zRange;
        var zMin = (0, isRealNumber_1.isRealNumber)(this.zMin) ? this.zMin : dataZRange.min;
        var zMax = (0, isRealNumber_1.isRealNumber)(this.zMax) ? this.zMax : dataZRange.max;
        zMin = Math.max(zMin, dataZRange.min);
        zMax = Math.min(zMax, dataZRange.max);
        var majorStepZ = this.zStep && (0, isRealNumber_1.isRealNumber)(this.zStep)
            ? this.zStep
            : Math.abs(zMax - zMin) / UniformContoursRenderableSeries.DEFAULT_CONTOURS_COUNT;
        var minorStepZ = this.minorsPerMajor && this.minorsPerMajor > 0 ? majorStepZ / (this.minorsPerMajor + 1) : majorStepZ;
        var minorLineStyle = (_a = this.minorLineStyle) !== null && _a !== void 0 ? _a : {
            strokeThickness: this.strokeThickness,
            color: this.stroke
        };
        var majorLineStyle = (_b = this.majorLineStyle) !== null && _b !== void 0 ? _b : {
            strokeThickness: this.strokeThickness,
            color: this.stroke
        };
        var zOffset = this.zOffset;
        var zLevels = ((_c = this.zLevelsProperty) === null || _c === void 0 ? void 0 : _c.length)
            ? this.zLevelsProperty.filter(function (z) { return (0, isRealNumber_1.isRealNumber)(z) && z >= dataZRange.min && z <= dataZRange.max; })
            : undefined;
        var result = {
            xMax: heatmapSeries.xMax,
            yMin: heatmapSeries.yMin,
            yMax: heatmapSeries.yMax,
            xMin: heatmapSeries.xMin,
            zMin: zMin,
            zMax: zMax,
            majorStepZ: majorStepZ,
            minorStepZ: minorStepZ,
            minorLineStyle: minorLineStyle,
            majorLineStyle: majorLineStyle,
            zOffset: zOffset,
            dataZRange: dataZRange,
            zLevels: zLevels
        };
        return result;
    };
    /** @inheritDoc */
    UniformContoursRenderableSeries.prototype.toPointSeries = function (resamplingParams) {
        // not used for Contours
        return undefined;
    };
    /** @inheritDoc */
    UniformContoursRenderableSeries.prototype.delete = function () {
        _super.prototype.delete.call(this);
        this.zLabelProviderProperty = (0, Deleter_1.deleteSafe)(this.zLabelProviderProperty);
    };
    /** @inheritDoc */
    UniformContoursRenderableSeries.prototype.toJSON = function (excludeData) {
        var _a, _b;
        if (excludeData === void 0) { excludeData = false; }
        var json = _super.prototype.toJSON.call(this, excludeData);
        var options = {
            colorMap: (_a = this.colorMap) === null || _a === void 0 ? void 0 : _a.toJSON(),
            majorLineStyle: this.majorLineStyle,
            minorLineStyle: this.minorLineStyle,
            zOffset: this.zOffset,
            zStep: this.zStep,
            zMax: this.zMax,
            zMin: this.zMin,
            zLevels: this.zLevels,
            minorsPerMajor: this.minorsPerMajor,
            //@ts-ignore
            zLabelProvider: (_b = this.zLabelProvider) === null || _b === void 0 ? void 0 : _b.toJSON()
        };
        Object.assign(json.options, options);
        return json;
    };
    /**
     * Called when a property changes on {@link HeatmapColorMap}, and notifies the parent {@link SciChartSurface}
     * that a redraw is required.
     * @param args
     */
    UniformContoursRenderableSeries.prototype.colorMapPropertyChanged = function (args) {
        this.notifyPropertyChanged(exports.COLOR_MAP_PREFIX + args.propertyName);
    };
    UniformContoursRenderableSeries.prototype.newHitTestProvider = function () {
        return new UniformHeatmapHitTestProvider_1.UniformHeatmapHitTestProvider(this, this.webAssemblyContext);
    };
    UniformContoursRenderableSeries.DEFAULT_CONTOURS_COUNT = 15;
    UniformContoursRenderableSeries.DEFAULT_MAJOR_LINE_STYLE = {
        strokeThickness: 2,
        color: "white"
    };
    UniformContoursRenderableSeries.DEFAULT_MINOR_LINE_STYLE = {
        strokeThickness: 1,
        color: "white"
    };
    return UniformContoursRenderableSeries;
}(BaseRenderableSeries_1.BaseRenderableSeries));
exports.UniformContoursRenderableSeries = UniformContoursRenderableSeries;
