"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.LineSeriesHitTestProvider = void 0;
var BaseHitTestProvider_1 = require("./BaseHitTestProvider");
var hitTestHelpers_1 = require("./hitTestHelpers");
var HitTestInfo_1 = require("./HitTestInfo");
/**
 * Hit-test provider for {@link BaseLineRenderableSeries}. See base class {@link BaseHitTestProvider} for further info
 */
var LineSeriesHitTestProvider = /** @class */ (function (_super) {
    __extends(LineSeriesHitTestProvider, _super);
    function LineSeriesHitTestProvider() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /**
     * @inheritDoc
     */
    LineSeriesHitTestProvider.prototype.hitTest = function (x, y, hitTestRadius) {
        if (hitTestRadius === void 0) { hitTestRadius = BaseHitTestProvider_1.BaseHitTestProvider.DEFAULT_HIT_TEST_RADIUS; }
        var hitTestPoint = this.getTranslatedHitTestPoint(x, y);
        if (!hitTestPoint) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var isVerticalChart = this.currentRenderPassData.isVerticalChart;
        var xHitCoord = isVerticalChart ? hitTestPoint.y : hitTestPoint.x;
        var yHitCoord = isVerticalChart ? hitTestPoint.x : hitTestPoint.y;
        var isSortedData = this.parentSeries.dataSeries.dataDistributionCalculator.isSortedAscending;
        if (isSortedData) {
            return this.hitTestSorted(xHitCoord, yHitCoord, hitTestRadius);
        }
        else {
            return this.hitTestUnsorted(xHitCoord, yHitCoord, hitTestRadius);
        }
    };
    LineSeriesHitTestProvider.prototype.hitTestSorted = function (xHitCoord, yHitCoord, hitTestRadius) {
        var _a = this.currentRenderPassData, xCoordinateCalculator = _a.xCoordinateCalculator, yCoordinateCalculator = _a.yCoordinateCalculator, isVerticalChart = _a.isVerticalChart;
        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();
        // Call method on RS as this resolves arrayFilter stuff
        var yNativeValues = this.parentSeries.getNativeYValues();
        var hitTestInfo = hitTestHelpers_1.hitTestHelpers.createHitTestInfo(this.parentSeries, xCoordinateCalculator, yCoordinateCalculator, isVerticalChart, dataSeries, xNativeValues, yNativeValues, xHitCoord, yHitCoord, nearestPointIndex, hitTestRadius, undefined);
        if (nearestPointIndex >= 0) {
            var dataSeriesCount = xNativeValues.size();
            var nearestXCoord = xCoordinateCalculator.getCoordinate(xCoordinateCalculator.isCategoryCoordinateCalculator
                ? nearestPointIndex
                : xNativeValues.get(nearestPointIndex));
            if ((nearestPointIndex === dataSeriesCount - 1 &&
                (xCoordinateCalculator.hasFlippedCoordinates
                    ? xHitCoord >= nearestXCoord
                    : xHitCoord <= nearestXCoord)) ||
                (nearestPointIndex === 0 &&
                    (xCoordinateCalculator.hasFlippedCoordinates
                        ? xHitCoord <= nearestXCoord
                        : xHitCoord >= nearestXCoord))) {
                hitTestInfo.isHit = hitTestHelpers_1.hitTestHelpers.testIsHitForPoint(xCoordinateCalculator, yCoordinateCalculator, xNativeValues, yNativeValues, nearestPointIndex, xHitCoord, yHitCoord, hitTestRadius, dataSeries);
            }
            else {
                var hitRes = hitTestHelpers_1.hitTestHelpers.testIsHitForLine(xCoordinateCalculator, yCoordinateCalculator, xNativeValues, yNativeValues, nearestPointIndex, xHitCoord, yHitCoord, hitTestRadius, dataSeries);
                hitTestInfo.isHit = hitRes.isHit;
                hitTestInfo.point2dataSeriesIndex = hitRes.secondPointIndex;
                hitTestInfo.point2xValue = xNativeValues.get(hitRes.secondPointIndex);
                hitTestInfo.point2xCoord = xCoordinateCalculator.getCoordinate(hitTestInfo.point2xValue);
                hitTestInfo.point2yValue = yNativeValues.get(hitRes.secondPointIndex);
                hitTestInfo.point2yCoord = yCoordinateCalculator.getCoordinate(hitTestInfo.point2yValue);
                hitTestInfo.point2metadata = dataSeries.getMetadataAt(hitRes.secondPointIndex);
                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;
    };
    LineSeriesHitTestProvider.prototype.hitTestUnsorted = function (xHitCoord, yHitCoord, hitTestRadius) {
        var _a = this.currentRenderPassData, xCoordinateCalculator = _a.xCoordinateCalculator, yCoordinateCalculator = _a.yCoordinateCalculator, isVerticalChart = _a.isVerticalChart;
        var dataSeries = this.parentSeries.dataSeries;
        if (!dataSeries)
            return HitTestInfo_1.HitTestInfo.empty();
        var nearest = hitTestHelpers_1.hitTestHelpers.getNearestXyPoint(this.webAssemblyContext, xCoordinateCalculator, yCoordinateCalculator, dataSeries, xHitCoord, yHitCoord, hitTestRadius);
        var xNativeValues = dataSeries.getNativeXValues();
        // Call method on RS as this resolves arrayFilter stuff
        var yNativeValues = this.parentSeries.getNativeYValues();
        var hitTestInfo = hitTestHelpers_1.hitTestHelpers.createHitTestInfo(this.parentSeries, xCoordinateCalculator, yCoordinateCalculator, isVerticalChart, dataSeries, xNativeValues, yNativeValues, xHitCoord, yHitCoord, nearest.nearestPointIndex, hitTestRadius, nearest.distance);
        if (nearest.nearestPointIndex >= 0) {
            // Test for the point
            // const distance = calcDistance(xHitCoord, yHitCoord, hitTestInfo.xCoord, hitTestInfo.yCoord);
            // if (distance < hitTestRadius) {
            //     hitTestInfo.isHit = true;
            //     return hitTestInfo;
            // }
            var dataSeriesCount = xNativeValues.size();
            var getXFn = function (i) { return xNativeValues.get(i); };
            var getYFn = function (i) { return yNativeValues.get(i); };
            var getX1Fn = function (i) { return xNativeValues.get(i + 1); };
            var getY1Fn = function (i) { return yNativeValues.get(i + 1); };
            var _b = hitTestHelpers_1.hitTestHelpers.getNearestLineSegment(xCoordinateCalculator, yCoordinateCalculator, dataSeriesCount - 1, getXFn, getYFn, getX1Fn, getY1Fn, xHitCoord, yHitCoord, hitTestRadius), isHit = _b.isHit, isWithinDataBounds = _b.isWithinDataBounds, nearestPointIndex = _b.nearestPointIndex;
            if (nearestPointIndex !== -1) {
                hitTestInfo.dataSeriesIndex = nearestPointIndex;
                hitTestInfo.isWithinDataBounds = isWithinDataBounds;
                hitTestInfo.isHit = isHit;
                hitTestInfo.xValue = xNativeValues.get(nearestPointIndex);
                hitTestInfo.xCoord = xCoordinateCalculator.getCoordinate(hitTestInfo.xValue);
                hitTestInfo.yValue = yNativeValues.get(nearestPointIndex);
                hitTestInfo.yCoord = yCoordinateCalculator.getCoordinate(hitTestInfo.yValue);
                hitTestInfo.point2dataSeriesIndex = nearestPointIndex + 1;
                hitTestInfo.point2xValue = xNativeValues.get(nearestPointIndex + 1);
                hitTestInfo.point2xCoord = xCoordinateCalculator.getCoordinate(hitTestInfo.point2xValue);
                hitTestInfo.point2yValue = yNativeValues.get(nearestPointIndex + 1);
                hitTestInfo.point2yCoord = yCoordinateCalculator.getCoordinate(hitTestInfo.point2yValue);
                hitTestInfo.point2metadata = dataSeries.getMetadataAt(nearestPointIndex + 1);
                var point2yValueByName = dataSeries.valueNames.reduce(function (map, name) {
                    map[name] = dataSeries.getNativeValue(dataSeries.getYValuesByName(name), hitTestInfo.point2dataSeriesIndex);
                    return map;
                }, {});
                hitTestInfo.point2yValueByName = point2yValueByName;
                return hitTestInfo;
            }
        }
        hitTestInfo.isHit = false;
        return hitTestInfo;
    };
    return LineSeriesHitTestProvider;
}(BaseHitTestProvider_1.BaseHitTestProvider));
exports.LineSeriesHitTestProvider = LineSeriesHitTestProvider;
