"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.StackedMountainSeriesHitTestProvider = void 0;
var BaseHitTestProvider_1 = require("./BaseHitTestProvider");
var hitTestHelpers_1 = require("./hitTestHelpers");
var HitTestInfo_1 = require("./HitTestInfo");
/**
 * Hit-test provider for {@link StackedMountainRenderableSeries}. See base class {@link BaseHitTestProvider} for further info
 */
var StackedMountainSeriesHitTestProvider = /** @class */ (function (_super) {
    __extends(StackedMountainSeriesHitTestProvider, _super);
    function StackedMountainSeriesHitTestProvider() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /**
     * @inheritDoc
     */
    StackedMountainSeriesHitTestProvider.prototype.hitTest = function (x, y) {
        var hitTestPoint = this.getTranslatedHitTestPoint(x, y);
        if (!hitTestPoint) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var _a = this.currentRenderPassData, xCoordinateCalculator = _a.xCoordinateCalculator, yCoordinateCalculator = _a.yCoordinateCalculator, isVerticalChart = _a.isVerticalChart;
        var xHitCoord = isVerticalChart ? hitTestPoint.y : hitTestPoint.x;
        var yHitCoord = isVerticalChart ? hitTestPoint.x : hitTestPoint.y;
        var dataSeries = this.parentSeries.dataSeries;
        if (!dataSeries) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var nearestPointIndex = hitTestHelpers_1.hitTestHelpers.getNearestXPoint(this.webAssemblyContext, xCoordinateCalculator, dataSeries, xHitCoord, dataSeries.dataDistributionCalculator.isSortedAscending);
        var xNativeValues = dataSeries.getNativeXValues();
        var accumulatedValues = this.parentSeries.accumulatedValues;
        var hitTestInfo = hitTestHelpers_1.hitTestHelpers.createHitTestInfo(this.parentSeries, xCoordinateCalculator, yCoordinateCalculator, isVerticalChart, dataSeries, xNativeValues, accumulatedValues, xHitCoord, yHitCoord, nearestPointIndex, 0);
        if (nearestPointIndex >= 0) {
            var bottomAccumulatedValues_1 = this.parentSeries.bottomAccumulatedValues;
            var separateStacking_1 = bottomAccumulatedValues_1.size() > 0;
            var getTopLine = function (index) { return accumulatedValues.get(index); };
            var getBottomLine = function (index) {
                return separateStacking_1
                    ? bottomAccumulatedValues_1.get(index)
                    : accumulatedValues.get(index) - dataSeries.getNativeYValues().get(index);
            };
            var hitRes = hitTestHelpers_1.hitTestHelpers.testIsHitForBand(this.parentSeries.isDigitalLine, xCoordinateCalculator, yCoordinateCalculator, xNativeValues, getTopLine, getBottomLine, nearestPointIndex, xHitCoord, yHitCoord, dataSeries);
            hitTestInfo.isHit = hitRes.isHit;
            hitTestInfo.point2dataSeriesIndex = hitRes.secondPointIndex;
            // Place the rollover marker at the outer tip of the band.
            // For negative values with separate stacking, the outer tip is the bottom edge.
            if (separateStacking_1 && dataSeries.getNativeYValues().get(nearestPointIndex) < 0) {
                hitTestInfo.yValue = bottomAccumulatedValues_1.get(nearestPointIndex);
            }
            var point2yValueByName = dataSeries.valueNames.reduce(function (map, name) {
                map[name] = dataSeries.getNativeValue(dataSeries.getYValuesByName(name), hitTestInfo.point2dataSeriesIndex);
                return map;
            }, {});
            hitTestInfo.point2yValueByName = point2yValueByName;
        }
        else {
            hitTestInfo.isHit = false;
        }
        return hitTestInfo;
    };
    /**
     * @inheritDoc
     */
    StackedMountainSeriesHitTestProvider.prototype.hitTestXSlice = function (x, y) {
        var hitTestPoint = this.getTranslatedHitTestPoint(x, y);
        if (!hitTestPoint) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var _a = this.currentRenderPassData, xCoordinateCalculator = _a.xCoordinateCalculator, yCoordinateCalculator = _a.yCoordinateCalculator, isVerticalChart = _a.isVerticalChart;
        var xHitCoord = isVerticalChart ? hitTestPoint.y : hitTestPoint.x;
        var yHitCoord = isVerticalChart ? hitTestPoint.x : hitTestPoint.y;
        var dataSeries = this.parentSeries.dataSeries;
        if (!dataSeries) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var nearestPointIndex = hitTestHelpers_1.hitTestHelpers.getNearestXPoint(this.webAssemblyContext, xCoordinateCalculator, dataSeries, xHitCoord, dataSeries.dataDistributionCalculator.isSortedAscending);
        var xNativeValues = dataSeries.getNativeXValues();
        var yNativeValues = dataSeries.getNativeYValues();
        var accumulatedValues = this.parentSeries.accumulatedValues;
        var hitTestInfo = hitTestHelpers_1.hitTestHelpers.createHitTestInfo(this.parentSeries, xCoordinateCalculator, yCoordinateCalculator, isVerticalChart, dataSeries, xNativeValues, accumulatedValues, xHitCoord, yHitCoord, nearestPointIndex, 0);
        if (nearestPointIndex >= 0) {
            // Hit-test on StackedColumnSeries returns Y accumulated, Y1 original
            hitTestInfo.y1Value = yNativeValues.get(nearestPointIndex);
            hitTestInfo.isHit = hitTestInfo.isWithinDataBounds;
            // Place the rollover marker at the outer tip of the band.
            // For negative values with separate stacking, the outer tip is the bottom edge.
            var bottomAccumulatedValues = this.parentSeries.bottomAccumulatedValues;
            if (bottomAccumulatedValues.size() > 0 && yNativeValues.get(nearestPointIndex) < 0) {
                hitTestInfo.yValue = bottomAccumulatedValues.get(nearestPointIndex);
            }
        }
        else {
            hitTestInfo.isHit = false;
        }
        return hitTestInfo;
    };
    /**
     * @inheritDoc
     */
    StackedMountainSeriesHitTestProvider.prototype.hitTestDataPoint = function (x, y, hitTestRadius) {
        if (hitTestRadius === void 0) { hitTestRadius = BaseHitTestProvider_1.BaseHitTestProvider.DEFAULT_HIT_TEST_RADIUS; }
        return this.hitTest(x, y);
    };
    return StackedMountainSeriesHitTestProvider;
}(BaseHitTestProvider_1.BaseHitTestProvider));
exports.StackedMountainSeriesHitTestProvider = StackedMountainSeriesHitTestProvider;
