"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.XyNDataSeries = void 0;
var Guard_1 = require("../../Core/Guard");
var vectorToArray_1 = require("../../utils/vectorToArray");
var ResamplingMode_1 = require("../Numerics/Resamplers/ResamplingMode");
var BaseDataSeries_1 = require("./BaseDataSeries");
var IDataSeries_1 = require("./IDataSeries");
var BasePointSeriesWrapped_1 = require("./PointSeries/BasePointSeriesWrapped");
var XyNPointSeriesResampled_1 = require("./PointSeries/XyNPointSeriesResampled");
/**
 * XyNDataSeries is a DataSeries for holding X, and an arbitrary number of sets of Y values in SciChart's 2D
 * {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 * @remarks
 *
 * 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 XyNDataSeries = /** @class */ (function (_super) {
    __extends(XyNDataSeries, _super);
    /**
     * Creates an instance of {@link XyNDataSeries}
     * @param webAssemblyContext the {@link TSciChart | SciChart WebAssembly Context} containing native methods
     * and access to our underlying WebGL2 rendering engine
     * @param options the {@link IXyNDataSeriesOptions} 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 XyNDataSeries(webAssemblyContext, options) {
        var _this = this;
        var _a, _b, _c;
        var baseOptions = __assign(__assign({}, options), { arrayCount: (_b = (_a = options.yValuesArray) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : options.arrayCount });
        _this = _super.call(this, webAssemblyContext, baseOptions) || this;
        _this.type = IDataSeries_1.EDataSeriesType.XyN;
        if (!baseOptions.arrayCount && !baseOptions.yValuesArray) {
            throw new Error("XyNDataSeries requres either arrayCount or yValuesArray to initialize the size");
        }
        _this.resamplingModes = (_c = baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.resamplingModes) !== null && _c !== void 0 ? _c : _this.valueNames.map(function (v) { return ResamplingMode_1.EResamplingMode.Auto; });
        if ((baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.xValues) && (baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.yValuesArray)) {
            _this.appendRangeN(baseOptions.xValues, baseOptions.yValuesArray, 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.yValuesArray.forEach(function (yValues) {
                    yValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex);
                });
            }
        }
        return _this;
    }
    XyNDataSeries.prototype.notifyDataChanged = function (changeType, index, count, name) {
        this.resamplingParams = undefined;
        _super.prototype.notifyDataChanged.call(this, changeType, index, count, name);
    };
    XyNDataSeries.prototype.toPointSeries = function (rp, _pointSeries, resamplerHelper) {
        if (rp && (rp.dataIsFifo || rp.resamplingMode !== ResamplingMode_1.EResamplingMode.None)) {
            if (!this.pointSeries) {
                this.pointSeries = new XyNPointSeriesResampled_1.XyNPointSeriesResampled(this.webAssemblyContext, rp.xVisibleRange, this.arrayCount, this.valueNames);
            }
            else {
                this.pointSeries.xRange = rp.xVisibleRange;
            }
            if (!this.resamplingParams) {
                this.resamplingParams = rp;
            }
            else {
                if (this.resamplingParams.indexesRange.equals(rp.indexesRange) &&
                    this.resamplingParams.precision == rp.precision &&
                    this.resamplingParams.viewportSize === rp.viewportSize) {
                    return this.pointSeries;
                }
            }
            this.resamplingParams = rp;
            var ps = this.pointSeries;
            Guard_1.Guard.notNull(resamplerHelper, "resamplerHelper");
            for (var i = 0; i < this.arrayCount; i++) {
                var subPS = ps.subSeries[i];
                var rpTemp = rp.clone({
                    resamplingMode: this.resamplingModes[i]
                });
                var result = resamplerHelper.resampleIntoPointSeries(this.webAssemblyContext, rpTemp, this.getNativeXValues(), this.getNativeYValues(i), subPS.intIndexes, subPS.indexes, subPS.xValues, subPS.yValues, false);
                subPS.fifoStartIndex = result.OutputSplitIndex;
                subPS.clearIntIndexes();
            }
            return this.pointSeries;
        }
        else {
            return new BasePointSeriesWrapped_1.BasePointSeriesWrapped(this);
        }
    };
    /** @inheritDoc */
    XyNDataSeries.prototype.getOptions = function (excludeData) {
        if (excludeData === void 0) { excludeData = false; }
        var json = _super.prototype.getOptions.call(this, excludeData);
        if (!excludeData) {
            // const dataSize = this.count();
            // let yValuesArray: number[][] = Array.from(Array(this.arrayCount)).map(i => new Array(dataSize));
            // const xValues: number[] = new Array(dataSize);
            // if (this.fifoCapacity && this.fifoSweeping) {
            //     for (let i = 0; i < dataSize; i++) {
            //         xValues[i] = (this.xValues as SCRTFifoVector).getRaw(i);
            //         for (let j = 0; j < this.arrayCount; j++) {
            //             yValuesArray[j][i] = (this.yValuesArray[j] as SCRTFifoVector).getRaw(i);
            //         }
            //     }
            // } else {
            //     for (let i = 0; i < dataSize; i++) {
            //         xValues[i] = this.xValues.get(i);
            //         for (let j = 0; j < this.arrayCount; j++) {
            //             yValuesArray[j][i] = this.yValuesArray[j].get(i);
            //         }
            //     }
            // }
            // Vroom Vroom!
            var xValues = (0, vectorToArray_1.vectorToArray)(this.xValues, this.webAssemblyContext);
            var yValuesArray = [];
            for (var j = 0; j < this.arrayCount; j++) {
                yValuesArray[j] = (0, vectorToArray_1.vectorToArray)(this.yValuesArray[j], this.webAssemblyContext);
            }
            var options = {
                xValues: xValues,
                yValuesArray: yValuesArray,
                resamplingModes: this.resamplingModes,
                yValues: undefined
            };
            Object.assign(json, options);
        }
        return json;
    };
    return XyNDataSeries;
}(BaseDataSeries_1.BaseDataSeries));
exports.XyNDataSeries = XyNDataSeries;
