"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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getYyYRange = exports.XyyDataSeries = void 0;
var Deleter_1 = require("../../Core/Deleter");
var Guard_1 = require("../../Core/Guard");
var NumberRange_1 = require("../../Core/NumberRange");
var SearchMode_1 = require("../../types/SearchMode");
var ValueName_1 = require("../../types/ValueName");
var YRangeMode_1 = require("../../types/YRangeMode");
var isRealNumber_1 = require("../../utils/isRealNumber");
var vectorToArray_1 = require("../../utils/vectorToArray");
var BaseDataSeries_1 = require("./BaseDataSeries");
var IDataSeries_1 = require("./IDataSeries");
/**
 * XyyDataSeries is a DataSeries for holding X, Y1, Y2 data in SciChart's 2D
 * {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 * @remarks
 * The XyyDataSeries is primarily used with our {@link FastBandRenderableSeries | JavaScript Band Chart},
 * which draws a High-Low fill between two lines, where the fill changes color depending on whether line Y2 > Y1
 *
 * A DataSeries stores the data to render. This is independent from the {@link IRenderableSeries | RenderableSeries}
 * which defines how that data should be rendered.
 *
 * See derived types of {@link BaseDataSeries} to find out what data-series are available.
 * See derived types of {@link IRenderableSeries} to find out what 2D JavaScript Chart types are available.
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/data-series-api/data-series-api-overview/}
 */
var XyyDataSeries = /** @class */ (function (_super) {
    __extends(XyyDataSeries, _super);
    /**
     * Creates an instance of {@link XyyDataSeries}
     * @param webAssemblyContext the {@link TSciChart | SciChart WebAssembly Context} containing native methods
     * and access to our underlying WebGL2 rendering engine
     * @param options the {@link IXyyDataSeriesOptions} which can be passed to configure the DataSeries at construct time
     *
     * ---
     * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/data-series-api/data-series-api-overview/}
     */
    function XyyDataSeries(webAssemblyContext, options) {
        var _this = this;
        var baseOptions = __assign(__assign({}, options), { arrayCount: 2, valueNames: [ValueName_1.EValueName.Y, ValueName_1.EValueName.Y1], includeInYRange: [true, true] });
        _this = _super.call(this, webAssemblyContext, baseOptions) || this;
        /** @inheritDoc */
        _this.type = IDataSeries_1.EDataSeriesType.Xyy;
        if (baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.xValues) {
            Guard_1.Guard.notNull(baseOptions.yValues, "baseOptions.yValues");
            Guard_1.Guard.notNull(baseOptions.y1Values, "baseOptions.y1Values");
            _this.appendRange(baseOptions.xValues, baseOptions.yValues, baseOptions.y1Values, baseOptions.metadata);
            if ((baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoCapacity) && (baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex)) {
                _this.xValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex);
                _this.yValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex);
                _this.y1Values.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex);
            }
        }
        return _this;
    }
    Object.defineProperty(XyyDataSeries.prototype, "yValues", {
        get: function () {
            return this.getNativeYValues();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(XyyDataSeries.prototype, "y1Values", {
        get: function () {
            return this.getNativeY1Values();
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Gets a native / WebAssembly vector of Y1-values in the DataSeries
     */
    XyyDataSeries.prototype.getNativeY1Values = function () {
        return this.getNativeYValues(1);
    };
    /**
     * Appends a single X, Y, Y1 point to the DataSeries
     * @remarks
     * For best performance on drawing large datasets, use the {@link appendRange} method
     *
     * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param x The X-value
     * @param y The Y1-value
     * @param y1 The Y2-value
     * @param metadata The point metadata
     */
    XyyDataSeries.prototype.append = function (x, y, y1, metadata) {
        _super.prototype.appendN.call(this, x, [y, y1], metadata);
    };
    /**
     * Appends a range of X, Y, Y1 points to the DataSeries
     * @remarks
     * This method is considerably higher performance than {@link append} which appends a single point
     *
     * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param xValues The X-values
     * @param yValues The Y-values
     * @param y1Values The Y1-values
     * @param metadata The array of point metadata
     */
    XyyDataSeries.prototype.appendRange = function (xValues, yValues, y1Values, metadata) {
        _super.prototype.appendRangeN.call(this, xValues, [yValues, y1Values], metadata);
    };
    /**
     * Updates a single Y, Y1-value by X-index
     * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param index the index to update
     * @param y The new Y value
     * @param y1 The new Y1 value
     * @param metadata The point metadata
     */
    XyyDataSeries.prototype.update = function (index, y, y1, metadata) {
        _super.prototype.updateN.call(this, index, [y, y1], metadata);
    };
    /**
     * Updates a single X, Y, Y1-value by X-index. Might also need to set isSorted = false
     * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param index the index to update
     * @param x The new X value
     * @param y The new Y value
     * @param y1 The new Y1 value
     * @param metadata The point metadata
     */
    XyyDataSeries.prototype.updateXyy1 = function (index, x, y, y1, metadata) {
        _super.prototype.updateXyN.call(this, index, x, [y, y1], metadata);
    };
    /**
     * Inserts a single X,Y1,Y2 value at the start index
     * @remarks
     * For best performance on drawing large datasets, use the {@link insertRange} method
     *
     * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param startIndex the index to insert at
     * @param x the Xvalue
     * @param y the Y1Value
     * @param y1 the Y2Value
     * @param metadata The point metadata
     */
    XyyDataSeries.prototype.insert = function (startIndex, x, y, y1, metadata) {
        _super.prototype.insertN.call(this, startIndex, x, [y, y1], metadata);
    };
    /**
     * Inserts a ragne of X,Y1,Y2 values at the startIndex
     * @remarks
     * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param startIndex the index to insert at
     * @param xValues the XValues
     * @param yValues the YValues
     * @param y1Values the Y1Values
     * @param metadata The array of point metadata
     */
    XyyDataSeries.prototype.insertRange = function (startIndex, xValues, yValues, y1Values, metadata) {
        _super.prototype.insertRangeN.call(this, startIndex, xValues, [yValues, y1Values], metadata);
    };
    /** @inheritDoc */
    XyyDataSeries.prototype.getWindowedYRange = function (xRange, getPositiveRange, isXCategoryAxis, dataSeriesValueType, yRangeMode) {
        if (isXCategoryAxis === void 0) { isXCategoryAxis = false; }
        if (dataSeriesValueType === void 0) { dataSeriesValueType = IDataSeries_1.EDataSeriesValueType.Default; }
        if (yRangeMode === void 0) { yRangeMode = YRangeMode_1.EYRangeMode.Visible; }
        var _a = this.getYY1Values(dataSeriesValueType), yValues = _a.yValues, y1Values = _a.y1Values;
        // TODO: getPositiveRange
        // if one point
        if (this.count() === 1) {
            var min = Math.min(yValues.get(0), y1Values.get(0));
            var max = Math.max(yValues.get(0), y1Values.get(0));
            return new NumberRange_1.NumberRange(min, max);
        }
        var indicesRange = isXCategoryAxis
            ? xRange
            : this.getIndicesRange(xRange, false, yRangeMode === YRangeMode_1.EYRangeMode.Visible ? SearchMode_1.ESearchMode.RoundUp : SearchMode_1.ESearchMode.RoundDown, yRangeMode === YRangeMode_1.EYRangeMode.Visible ? SearchMode_1.ESearchMode.RoundDown : SearchMode_1.ESearchMode.RoundUp);
        return getYyYRange(this.webAssemblyContext, indicesRange, yValues, y1Values, this.dataDistributionCalculator.containsNaN);
    };
    /** @inheritDoc */
    XyyDataSeries.prototype.getOptions = function (excludeData) {
        if (excludeData === void 0) { excludeData = false; }
        var json = _super.prototype.getOptions.call(this, excludeData);
        if (!excludeData) {
            // const dataSize = this.count();
            // const xValues: number[] = new Array(dataSize);
            // const yValues: number[] = new Array(dataSize);
            // const y1Values: number[] = new Array(dataSize);
            // if (this.fifoCapacity && this.fifoSweeping) {
            //     for (let i = 0; i < dataSize; i++) {
            //         xValues[i] = (this.xValues as SCRTFifoVector).getRaw(i);
            //         yValues[i] = (this.yValues as SCRTFifoVector).getRaw(i);
            //         y1Values[i] = (this.y1Values as SCRTFifoVector).getRaw(i);
            //     }
            // } else {
            //     for (let i = 0; i < dataSize; i++) {
            //         xValues[i] = this.xValues.get(i);
            //         yValues[i] = this.yValues.get(i);
            //         y1Values[i] = this.y1Values.get(i);
            //     }
            // }
            //  Vroom Vroom!
            var xValues = (0, vectorToArray_1.vectorToArray)(this.xValues, this.webAssemblyContext);
            var yValues = (0, vectorToArray_1.vectorToArray)(this.yValues, this.webAssemblyContext);
            var y1Values = (0, vectorToArray_1.vectorToArray)(this.y1Values, this.webAssemblyContext);
            var options = {
                xValues: xValues,
                yValues: yValues,
                y1Values: y1Values
            };
            Object.assign(json, options);
        }
        return json;
    };
    XyyDataSeries.prototype.getYY1Values = function (dataSeriesValueType) {
        var yValues;
        var y1Values;
        switch (dataSeriesValueType) {
            case IDataSeries_1.EDataSeriesValueType.FinalAnimationValues:
                yValues = this.yFinalAnimationValuesArray[0];
                y1Values = this.yFinalAnimationValuesArray[1];
                break;
            case IDataSeries_1.EDataSeriesValueType.InitialAnimationValues:
                yValues = this.yInitialAnimationValuesArray[0];
                y1Values = this.yInitialAnimationValuesArray[1];
                break;
            default:
                yValues = this.yValues;
                y1Values = this.y1Values;
        }
        return { yValues: yValues, y1Values: y1Values };
    };
    return XyyDataSeries;
}(BaseDataSeries_1.BaseDataSeries));
exports.XyyDataSeries = XyyDataSeries;
function getYyYRange(webAssemblyContext, indicesRange, yValues, y1Values, containsNaN) {
    var iMin = Math.max(Math.floor(indicesRange.min), 0);
    var iMax = Math.min(Math.ceil(indicesRange.max), yValues.size() - 1);
    if (iMax < iMin) {
        return undefined;
    }
    var minMax;
    var minMaxy1;
    try {
        minMax = webAssemblyContext.NumberUtil.MinMaxWithIndex(yValues, iMin, iMax - iMin + 1, containsNaN);
        if (!(0, isRealNumber_1.isRealNumber)(minMax.minD) || !(0, isRealNumber_1.isRealNumber)(minMax.maxD)) {
            return undefined;
        }
        minMaxy1 = webAssemblyContext.NumberUtil.MinMaxWithIndex(y1Values, iMin, iMax - iMin + 1, containsNaN);
        if (!(0, isRealNumber_1.isRealNumber)(minMaxy1.minD) || !(0, isRealNumber_1.isRealNumber)(minMaxy1.maxD)) {
            return undefined;
        }
        return new NumberRange_1.NumberRange(Math.min(minMax.minD, minMaxy1.minD), Math.max(minMax.maxD, minMaxy1.maxD));
    }
    finally {
        (0, Deleter_1.deleteSafe)(minMax);
        (0, Deleter_1.deleteSafe)(minMaxy1);
    }
}
exports.getYyYRange = getYyYRange;
