"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.PolarLineSeriesHitTestProvider = void 0;
var Point_1 = require("../../../../../Core/Point");
var HitTestInfo_1 = require("../../HitTest/HitTestInfo");
var hitTestHelpers_1 = require("../../HitTest/hitTestHelpers");
var annotationHelpers_1 = require("../../../Annotations/annotationHelpers");
var ECoordinateMode_1 = require("../../../Annotations/types/ECoordinateMode");
var pointUtil_1 = require("../../../../../utils/pointUtil");
var PolarDataPointHitTestProvider_1 = require("./PolarDataPointHitTestProvider");
var DpiHelper_1 = require("../../../TextureManager/DpiHelper");
var DEFAULT_RADIUS = 10;
/**
 * Hit-test provider for {@link PolarLineRenderableSeries}. See base class {@link BaseHitTestProvider} for further info
 */
var PolarLineSeriesHitTestProvider = /** @class */ (function (_super) {
    __extends(PolarLineSeriesHitTestProvider, _super);
    function PolarLineSeriesHitTestProvider() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /** @inheritDoc */
    PolarLineSeriesHitTestProvider.prototype.hitTest = function (x, y, hitTestRadius) {
        var _this = this;
        if (hitTestRadius === void 0) { hitTestRadius = DEFAULT_RADIUS; }
        // convert to polar and add necessary offset
        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 = hitTestPoint.x;
        var yHitCoord = hitTestPoint.y;
        var dataSeries = this.parentSeries.dataSeries;
        if (!dataSeries) {
            return HitTestInfo_1.HitTestInfo.empty();
        }
        var xNativeValues = dataSeries.getNativeXValues();
        var yNativeValues = dataSeries.getNativeYValues();
        var xPolarAxis = this.parentSeries.xAxis;
        var getCartesianCoordsFn = function (index$) {
            var x = xNativeValues.get(index$);
            var y = yNativeValues.get(index$);
            var xCoord = xCoordinateCalculator.getCoordinate(x);
            var yCoord = yCoordinateCalculator.getCoordinate(y);
            var angle = isVerticalChart ? yCoord : xCoord;
            var radius = isVerticalChart ? xCoord : yCoord;
            var res = annotationHelpers_1.annotationHelpers.convertPolarToCartesian(xPolarAxis, false, _this.webAssemblyContext, ECoordinateMode_1.ECoordinateMode.Pixel, angle * DpiHelper_1.DpiHelper.PIXEL_RATIO, radius);
            return new Point_1.Point(res.x, res.y);
        };
        var minDistance = Number.MAX_VALUE;
        var minDistanceIndex1 = -1;
        var minDistanceIndex2 = -1;
        var updateMinDistFn = function (dist$, ind1$, ind2$) {
            if (ind2$ === void 0) { ind2$ = -1; }
            if (dist$ < minDistance) {
                minDistance = dist$;
                minDistanceIndex1 = ind1$;
                minDistanceIndex2 = ind2$;
            }
        };
        if (dataSeries.count() > 0) {
            var point1Coords = getCartesianCoordsFn(0);
            var distanceToPoint1 = (0, pointUtil_1.calcDistance)(xHitCoord, yHitCoord, point1Coords.x, point1Coords.y);
            updateMinDistFn(distanceToPoint1, 0);
            for (var i = 1; i < dataSeries.count(); i++) {
                var point2Coords = getCartesianCoordsFn(i);
                var lineSegmentLength = (0, pointUtil_1.calcDistance)(point1Coords.x, point1Coords.y, point2Coords.x, point2Coords.y);
                var distanceToPoint2 = (0, pointUtil_1.calcDistance)(xHitCoord, yHitCoord, point2Coords.x, point2Coords.y);
                updateMinDistFn(distanceToPoint2, i);
                var isHitPointInLineSegmentVicinity = distanceToPoint1 < lineSegmentLength && distanceToPoint2 < lineSegmentLength;
                if (isHitPointInLineSegmentVicinity) {
                    var distanceToLine = (0, pointUtil_1.calcDistanceFromLine)(xHitCoord, yHitCoord, point1Coords.x, point1Coords.y, point2Coords.x, point2Coords.y);
                    if (distanceToLine < minDistance) {
                        if (distanceToPoint1 < distanceToPoint2) {
                            updateMinDistFn(distanceToLine, i - 1, i);
                        }
                        else {
                            updateMinDistFn(distanceToLine, i, i - 1);
                        }
                    }
                }
                point1Coords = point2Coords;
                distanceToPoint1 = distanceToPoint2;
            }
        }
        var polarHitTestPoint = xPolarAxis.reverseTransform(hitTestPoint.x, hitTestPoint.y);
        var hitTestInfo = hitTestHelpers_1.hitTestHelpers.createHitTestInfo(this.parentSeries, xCoordinateCalculator, yCoordinateCalculator, isVerticalChart, dataSeries, xNativeValues, yNativeValues, polarHitTestPoint.x, polarHitTestPoint.y, minDistanceIndex1, hitTestRadius);
        hitTestInfo.isHit = minDistance < hitTestRadius;
        return hitTestInfo;
    };
    return PolarLineSeriesHitTestProvider;
}(PolarDataPointHitTestProvider_1.PolarDataPointHitTestProvider));
exports.PolarLineSeriesHitTestProvider = PolarLineSeriesHitTestProvider;
