"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataLabelState = void 0;
var NumberRange_1 = require("../../../../Core/NumberRange");
var parseColor_1 = require("../../../../utils/parseColor");
var vectorToArray_1 = require("../../../../utils/vectorToArray");
var DpiHelper_1 = require("../../TextureManager/DpiHelper");
var DataLabelState = /** @class */ (function () {
    function DataLabelState(renderContext, renderPassData, style, color, yValues, parentSeries) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
        this.dataLabels = [];
        this.indexProperty = 0;
        this.xCoordCache = undefined;
        this.yCoordCache = undefined;
        var pointSeries = renderPassData.pointSeries;
        var isCategoryAxis = renderPassData.xCoordinateCalculator.isCategoryCoordinateCalculator;
        this.style = DpiHelper_1.DpiHelper.adjustTextStyle(style);
        this.font = renderContext.getFont(this.style);
        this.color = (0, parseColor_1.parseColorToUIntArgb)(color);
        this.parentSeries = parentSeries;
        this.renderPassData = renderPassData;
        // Acquire font BEFORE creating Float64Array views over WASM memory.
        // getFont() may trigger native allocations that grow WASM memory,
        // which detaches all existing ArrayBuffer views.
        this.font = renderContext.getFont(this.style);
        if ((_a = this.parentSeries.dataSeries) === null || _a === void 0 ? void 0 : _a.yFinalAnimationValues) {
            this.yFinalValues = (0, vectorToArray_1.vectorToArrayViewF64)((_b = this.parentSeries.dataSeries) === null || _b === void 0 ? void 0 : _b.yFinalAnimationValues, renderContext.webAssemblyContext);
        }
        this.xValues = (0, vectorToArray_1.vectorToArrayViewF64)(isCategoryAxis ? pointSeries.indexes : pointSeries.xValues, renderContext.webAssemblyContext);
        this.yValues = (0, vectorToArray_1.vectorToArrayViewF64)(yValues, renderContext.webAssemblyContext);
        this.indexes = (0, vectorToArray_1.vectorToArrayViewF64)((_c = renderPassData.pointSeries) === null || _c === void 0 ? void 0 : _c.indexes, renderContext.webAssemblyContext);
        // the value is inverted (by default X coord calc is not flipped, but it returns flipped)
        this.isXFlipped = (renderPassData.isVerticalChart
            ? this.renderPassData.yCoordinateCalculator
            : this.renderPassData.xCoordinateCalculator).hasFlippedCoordinates;
        // the value is inverted (by default Y coord calc is flipped, but it returns not flipped)
        this.isYFlipped = (renderPassData.isVerticalChart
            ? this.renderPassData.xCoordinateCalculator
            : this.renderPassData.yCoordinateCalculator).hasFlippedCoordinates;
        if (!renderPassData.indexRange) {
            var indicesRange = this.parentSeries.getIndicesRange(new NumberRange_1.NumberRange(renderPassData.xCoordinateCalculator.visibleMin, renderPassData.xCoordinateCalculator.visibleMax), isCategoryAxis);
            this.indexStart = indicesRange.min;
            this.indexEnd = indicesRange.max;
            this.pointCount = renderPassData.indexRange.diff + 1;
            if (this.pointCount > 1) {
                this.pointGap = renderPassData.isVerticalChart
                    ? Math.abs(this.yCoord(this.indexStart) - this.yCoord(this.indexEnd)) / this.pointCount
                    : Math.abs(this.xCoord(this.indexStart) - this.xCoord(this.indexEnd)) / this.pointCount;
            }
            else
                this.pointGap = Infinity; // Single point
        }
        else {
            this.indexStart = pointSeries.resampled ? 0 : renderPassData.indexRange.min;
            this.pointCount = pointSeries.resampled ? pointSeries.count : renderPassData.indexRange.diff + 1;
            this.indexEnd = pointSeries.resampled ? pointSeries.count - 1 : renderPassData.indexRange.max;
            if (this.pointCount > 1) {
                this.pointGap = renderPassData.isVerticalChart
                    ? Math.abs(this.yCoord(this.indexStart) - this.yCoord(this.indexEnd)) / this.pointCount
                    : Math.abs(this.xCoord(this.indexStart) - this.xCoord(this.indexEnd)) / this.pointCount;
            }
            else
                this.pointGap = Infinity; // Single point
        }
        this.index = this.indexStart;
        this.xPadding = ((_e = (_d = this.style.padding) === null || _d === void 0 ? void 0 : _d.left) !== null && _e !== void 0 ? _e : 0) + ((_g = (_f = this.style.padding) === null || _f === void 0 ? void 0 : _f.right) !== null && _g !== void 0 ? _g : 0);
        this.yPadding = ((_j = (_h = this.style.padding) === null || _h === void 0 ? void 0 : _h.top) !== null && _j !== void 0 ? _j : 0) + ((_l = (_k = this.style.padding) === null || _k === void 0 ? void 0 : _k.bottom) !== null && _l !== void 0 ? _l : 0);
        this.fifoSweeping = parentSeries.dataSeries.fifoSweeping;
    }
    Object.defineProperty(DataLabelState.prototype, "index", {
        get: function () {
            return this.indexProperty;
        },
        set: function (value) {
            this.xCoordCache = undefined;
            this.yCoordCache = undefined;
            this.indexProperty = value;
        },
        enumerable: false,
        configurable: true
    });
    DataLabelState.prototype.getIndexInternal = function (index) {
        var i = index !== null && index !== void 0 ? index : this.indexProperty;
        if (!this.isXFlipped) {
            i = this.indexEnd - (i - this.indexStart);
        }
        return i;
    };
    DataLabelState.prototype.xVal = function (index) {
        return this.xValues[this.getIndexInternal(index)];
        //return this.parentSeries.dataSeries.getNativeValue(this.xValues, this.getIndexInternal(index));
    };
    DataLabelState.prototype.yVal = function (index) {
        return this.yValues[this.getIndexInternal(index)];
        //return this.parentSeries.dataSeries.getNativeValue(this.yValues, this.getIndexInternal(index));
    };
    DataLabelState.prototype.yValAfterAnimation = function (index) {
        if (!this.yFinalValues)
            return this.yVal(index);
        return this.yFinalValues[this.getIndexInternal(index)];
    };
    DataLabelState.prototype.xCoord = function (index) {
        if (this.renderPassData.isVerticalChart)
            return this.yCoordInternal(index);
        else
            return this.xCoordInternal(index);
    };
    DataLabelState.prototype.yCoord = function (index) {
        if (this.renderPassData.isVerticalChart)
            return this.xCoordInternal(index);
        else
            return this.yCoordInternal(index);
    };
    DataLabelState.prototype.hasNext = function (index) {
        var i = index !== null && index !== void 0 ? index : this.indexProperty;
        if (!this.isXFlipped)
            return i > 0;
        else
            return i < this.indexEnd;
    };
    Object.defineProperty(DataLabelState.prototype, "lastLabel", {
        get: function () {
            if (this.dataLabels.length > 0) {
                return this.dataLabels[this.dataLabels.length - 1];
            }
            else {
                return undefined;
            }
        },
        enumerable: false,
        configurable: true
    });
    DataLabelState.prototype.getMetaData = function () {
        var index = this.indexes[this.getIndexInternal()];
        return this.parentSeries.dataSeries.getMetadataAt(index, this.fifoSweeping);
    };
    Object.defineProperty(DataLabelState.prototype, "isVerticalChart", {
        get: function () {
            return this.renderPassData.isVerticalChart;
        },
        enumerable: false,
        configurable: true
    });
    DataLabelState.prototype.getXCoord = function (xValue) {
        return this.renderPassData.xCoordinateCalculator.getCoordinate(xValue);
    };
    DataLabelState.prototype.getYCoord = function (yValue) {
        return this.renderPassData.yCoordinateCalculator.getCoordinate(yValue);
    };
    DataLabelState.prototype.xCoordInternal = function (index) {
        if (index !== undefined)
            return this.renderPassData.xCoordinateCalculator.getCoordinate(this.xVal(index));
        else {
            if (this.xCoordCache !== undefined)
                return this.xCoordCache;
            else
                this.xCoordCache = this.renderPassData.xCoordinateCalculator.getCoordinate(this.xVal());
            return this.xCoordCache;
        }
    };
    DataLabelState.prototype.yCoordInternal = function (index) {
        if (index !== undefined)
            return this.renderPassData.yCoordinateCalculator.getCoordinate(this.yVal(index));
        else {
            if (this.yCoordCache !== undefined)
                return this.yCoordCache;
            else
                this.yCoordCache = this.renderPassData.yCoordinateCalculator.getCoordinate(this.yVal());
            return this.yCoordCache;
        }
    };
    return DataLabelState;
}());
exports.DataLabelState = DataLabelState;
