"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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XyTextDataSeries = void 0;
var Guard_1 = require("../../Core/Guard");
var array_1 = require("../../utils/array");
var vectorToArray_1 = require("../../utils/vectorToArray");
var BaseDataSeries_1 = require("./BaseDataSeries");
var IDataSeries_1 = require("./IDataSeries");
/**
 * @summary XyTextDataSeries is a DataSeries for holding X, Y, Text data in SciChart's 2D
 * {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 * @description
 * The XyTextDataSeries is primarily used with our {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/fast-text-renderable-series/ | FastTextRenderableSeries} and
 * is used to display word-clouds or custom text annotations on a chart.
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/data-series-api/data-series-api-overview/}
 */
var XyTextDataSeries = /** @class */ (function (_super) {
    __extends(XyTextDataSeries, _super);
    /**
     * Creates an instance of {@link XyDataSeries}
     * @param webAssemblyContext the {@link TSciChart | SciChart WebAssembly Context} containing native methods
     * and access to our underlying WebGL2 WebAssembly rendering engine
     * @param options the {@link IXyDataSeriesOptions} 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 XyTextDataSeries(webAssemblyContext, options) {
        var _this = _super.call(this, webAssemblyContext, options) || this;
        /**
         * @inheritDoc
         */
        _this.type = IDataSeries_1.EDataSeriesType.XyText;
        _this.textValuesProperty = [];
        if (options === null || options === void 0 ? void 0 : options.xValues) {
            Guard_1.Guard.notNull(options.yValues, "options.yValues");
            _this.appendRange(options.xValues, options.yValues, options.textValues, options.metadata);
        }
        return _this;
    }
    Object.defineProperty(XyTextDataSeries.prototype, "textValues", {
        /** The text values for this series.  Manipulate using append, insert, update etc on the XyTextDataSeries  */
        get: function () {
            return this.textValuesProperty;
        },
        enumerable: false,
        configurable: true
    });
    /** Get the text value at an index, unwrapping the fifo buffer if fifoCapacity is set */
    XyTextDataSeries.prototype.getTextValue = function (index) {
        if (!this.fifoCapacity)
            return this.textValuesProperty[index];
        if (!this.fifoSweeping) {
            var fifoIndex = (this.xValues.getStartIndex() + index) % this.fifoCapacity;
            return this.textValuesProperty[fifoIndex];
        }
        else {
            if (this.fifoCapacity - index < this.fifoSweepingGap) {
                return "";
            }
            return this.textValuesProperty[index];
        }
    };
    /**
     * Appends a single X, Y, Text 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 Y-value
     * @param text The text-value
     * @param metadata The point metadata
     */
    XyTextDataSeries.prototype.append = function (x, y, text, metadata) {
        var _this = this;
        var onAppend = function () {
            // Fifo text also before values as startIndex gets updated by nativeX push
            if (_this.fifoCapacity && _this.count() === _this.fifoCapacity) {
                _this.textValuesProperty[_this.xValues.getStartIndex()] = text;
            }
            else {
                _this.textValuesProperty.push(text);
            }
        };
        _super.prototype.appendN.call(this, x, [y], metadata, onAppend);
    };
    /**
     * Appends a range of X, Y, Text 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 textValues The text values
     * @param metadata The array of point metadata
     */
    XyTextDataSeries.prototype.appendRange = function (xValues, yValues, textValues, metadata) {
        var _this = this;
        var onAppend = function () {
            if (_this.fifoCapacity) {
                (0, array_1.appendRangeFifo)(textValues, _this.textValuesProperty, _this.fifoCapacity, _this.xValues.getStartIndex());
            }
            else {
                _this.textValuesProperty = _this.textValuesProperty.concat(textValues);
            }
        };
        _super.prototype.appendRangeN.call(this, xValues, [yValues], metadata, onAppend);
    };
    /**
     * Updates a single Y-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 text The new text value
     * @param metadata The point metadata
     */
    XyTextDataSeries.prototype.update = function (index, y, text, metadata) {
        var _this = this;
        var onUpdate = function () { return (_this.textValuesProperty[index] = text); };
        _super.prototype.updateN.call(this, index, [y], metadata, onUpdate);
    };
    /**
     * Updates a single X, Y, Text 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 text The new text value
     * @param metadata The point metadata
     */
    XyTextDataSeries.prototype.updateXyText = function (index, x, y, text, metadata) {
        var _this = this;
        var onUpdate = function () { return (_this.textValuesProperty[index] = text); };
        _super.prototype.updateXyN.call(this, index, x, [y], metadata, onUpdate);
    };
    /**
     * @summary Inserts a single X,Y,Text 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 X value
     * @param y the Y value
     * @param text The new text value
     * @param metadata The point metadata
     */
    XyTextDataSeries.prototype.insert = function (startIndex, x, y, text, metadata) {
        var _this = this;
        var onInsert = function () { return _this.textValuesProperty.splice(startIndex, 0, text); };
        _super.prototype.insertN.call(this, startIndex, x, [y], metadata, onInsert);
    };
    /**
     * @summary Inserts a range of X,Y 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 textValues The text values
     * @param metadata The array of point metadata
     */
    XyTextDataSeries.prototype.insertRange = function (startIndex, xValues, yValues, textValues, metadata) {
        var _this = this;
        var onInsert = function () {
            var _a;
            return (_a = _this.textValuesProperty).splice.apply(_a, __spreadArray([startIndex, 0], textValues, false));
        };
        _super.prototype.insertRangeN.call(this, startIndex, xValues, [yValues], metadata, onInsert);
    };
    /**
     * Removes an X,Y value at the specified index
     * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param index the index to remove at
     */
    XyTextDataSeries.prototype.removeAt = function (index) {
        if (!this.getIsDeleted()) {
            this.textValuesProperty.splice(index, 1);
            _super.prototype.removeAt.call(this, index);
        }
    };
    /**
     * @summary Removes a range of X,Y values at the specified index
     * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     * @param startIndex the start index to remove at
     * @param count the number of points to remove
     */
    XyTextDataSeries.prototype.removeRange = function (startIndex, count) {
        if (!this.getIsDeleted()) {
            this.textValuesProperty.splice(startIndex, count);
            _super.prototype.removeRange.call(this, startIndex, count);
        }
    };
    /**
     * Clears the entire DataSeries.
     * @remarks
     * Note this does not free memory, WebAssembly/Native memory is released by calling {@link delete}, after which the
     * DataSeries is no longer usable.
     *
     * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface}
     */
    XyTextDataSeries.prototype.clear = function () {
        if (!this.getIsDeleted()) {
            this.textValuesProperty = [];
            _super.prototype.clear.call(this);
        }
    };
    Object.defineProperty(XyTextDataSeries.prototype, "yValues", {
        get: function () {
            return this.getNativeYValues();
        },
        enumerable: false,
        configurable: true
    });
    XyTextDataSeries.prototype.getOptions = function (excludeData) {
        if (excludeData === void 0) { excludeData = false; }
        var json = _super.prototype.getOptions.call(this);
        if (!excludeData) {
            // const dataSize = this.count();
            // const xValues: number[] = new Array(dataSize);
            // const yValues: number[] = new Array(dataSize);
            // for (let i = 0; i < dataSize; i++) {
            //     xValues[i] = this.xValues.get(i);
            //     yValues[i] = this.yValues.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 options = {
                xValues: xValues,
                yValues: yValues,
                textValues: this.textValuesProperty
            };
            Object.assign(json, options);
        }
        return json;
    };
    return XyTextDataSeries;
}(BaseDataSeries_1.BaseDataSeries));
exports.XyTextDataSeries = XyTextDataSeries;
