"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.YAxisDragModifier = void 0;
var ChartModifierType_1 = require("../../types/ChartModifierType");
var CursorStyle_1 = require("../../types/CursorStyle");
var DragMode_1 = require("../../types/DragMode");
var translate_1 = require("../../utils/translate");
var AxisBase2D_1 = require("../Visuals/Axis/AxisBase2D");
var ChartModifierBase2D_1 = require("./ChartModifierBase2D");
var PinchZoomModifier_1 = require("./PinchZoomModifier");
/**
 * The YAxisDragModifier provides scaling/panning behavior for Y axis {@link AxisBase2D}
 * and pinch zoom support on mobile devices
 * within SciChart - High Performance {@link https://www.scichart.com/javascript-chart-features | JavaScript Charts}
 * @remarks
 *
 * To apply the YAxisDragModifier to a {@link SciChartSurface} and add scaling behavior,
 * use the following code:
 *
 * ```ts
 * sciChartSurface.chartModifiers.add(new YAxisDragModifier());
 * ```
 *
 * ---
 * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-modifier-api/zooming-and-panning/y-axis-drag-modifier/}
 */
var YAxisDragModifier = /** @class */ (function (_super) {
    __extends(YAxisDragModifier, _super);
    /**
     * Creates an instance of a XAxisDragModifier
     * @param options optional parameters to pass to the XAxisDragModifier to configure it upon construction
     *
     * ---
     * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-modifier-api/zooming-and-panning/y-axis-drag-modifier/}
     */
    function YAxisDragModifier(options) {
        var _this = this;
        var _a, _b;
        _this = _super.call(this, options) || this;
        _this.type = ChartModifierType_1.EChart2DModifierType.YAxisDrag;
        _this.dragMode = DragMode_1.EDragMode.Scaling;
        /**
         * Sets whether to enable pinch zoom behavior of {@link SciChartSurface} on touchscreen devices
         */
        _this.enableZoom = true;
        _this.isClickedOverYAxis = false;
        _this.isVerticalChart = false;
        _this.cursorStyle = CursorStyle_1.ECursorStyle.Default;
        _this.dragMode = (_a = options === null || options === void 0 ? void 0 : options.dragMode) !== null && _a !== void 0 ? _a : _this.dragMode;
        _this.enableZoom = (_b = options === null || options === void 0 ? void 0 : options.enableZoom) !== null && _b !== void 0 ? _b : _this.enableZoom;
        return _this;
    }
    /**
     * Not supported for {@link YAxisDragModifier}, use includeYAxis instead
     */
    YAxisDragModifier.prototype.includeXAxis = function (axis, isIncluded) {
        throw Error("method includeXAxis() is not supported for YAxisDragModifier, use includeYAxis() instead");
    };
    /**
     * Not supported for {@link YAxisDragModifier}, use getIncludedYAxis instead
     */
    YAxisDragModifier.prototype.getIncludedXAxis = function () {
        throw Error("method getIncludedXAxis() is not supported for YAxisDragModifier, use getIncludedXAxis() instead");
    };
    /** @inheritDoc */
    YAxisDragModifier.prototype.modifierMouseDown = function (args) {
        _super.prototype.modifierMouseDown.call(this, args);
        if (!this.isAttached) {
            throw new Error("Should not call YAxisDragModifier.modifierMouseDown if not attached");
        }
        if ((0, ChartModifierBase2D_1.testIsOverAxes)(this.getVerticalYAxes(), args.mousePoint)) {
            // Horizontal chart
            this.isVerticalChart = false;
            this.startDragging(this.getVerticalYAxes(), args);
        }
        else if ((0, ChartModifierBase2D_1.testIsOverAxes)(this.getHorizontalYAxes(), args.mousePoint)) {
            // Vertical chart
            this.isVerticalChart = true;
            this.startDragging(this.getHorizontalYAxes(), args);
        }
    };
    /** @inheritDoc */
    YAxisDragModifier.prototype.modifierMouseMove = function (args) {
        if (this.isClickedOverYAxis) {
            _super.prototype.modifierMouseMove.call(this, args);
            if (this.getIsActionAllowed(args)) {
                return;
            }
            if (this.pointFrom === undefined) {
                return;
            }
            if (this.dragMode === DragMode_1.EDragMode.Panning) {
                this.doPanning(args.mousePoint);
            }
            else if (this.dragMode === DragMode_1.EDragMode.Scaling) {
                this.doScaling(args.mousePoint);
            }
        }
        else {
            this.updateCursor(args.mousePoint);
        }
    };
    /** @inheritDoc */
    YAxisDragModifier.prototype.modifierMouseUp = function (args) {
        _super.prototype.modifierMouseUp.call(this, args);
        this.updateCursor(args.mousePoint);
        this.pointFrom = undefined;
        this.isClickedOverYAxis = false;
    };
    /** @inheritDoc */
    YAxisDragModifier.prototype.toJSON = function () {
        var json = _super.prototype.toJSON.call(this);
        var options = {
            dragMode: this.dragMode,
            enableZoom: this.enableZoom
        };
        Object.assign(json.options, options);
        return json;
    };
    YAxisDragModifier.prototype.getIsActionAllowed = function (args) {
        return this.enableZoom && _super.prototype.getIsActionAllowed.call(this, args);
    };
    YAxisDragModifier.prototype.performModifierAction = function (args) {
        var _this = this;
        var _a = Array.from(this.activePointerEvents.values()), firstTouchPoint = _a[0].mousePoint, secondTouchPoint = _a[1].mousePoint;
        var currentHorizontalTouchPointsDistance = Math.abs(firstTouchPoint.x - secondTouchPoint.x);
        var currentVerticalTouchPointsDistance = Math.abs(firstTouchPoint.y - secondTouchPoint.y);
        var zoomCenterMousePoint = this.getMiddlePoint(firstTouchPoint, secondTouchPoint);
        var anyUpdated = false;
        if (this.previousHorizontalTouchPointsDistance !== undefined &&
            this.previousVerticalTouchPointsDistance !== undefined) {
            var verticalDistanceDiff = this.previousVerticalTouchPointsDistance - currentVerticalTouchPointsDistance;
            var translatedPoint_1 = (0, translate_1.translateFromCanvasToSeriesViewRect)(zoomCenterMousePoint, this.parentSurface.seriesViewRect, true);
            var activeYAxis = (0, ChartModifierBase2D_1.getActiveAxes)(this.getIncludedYAxis(), zoomCenterMousePoint);
            var verticalFraction_1 = this.verticalGrowFactor * verticalDistanceDiff;
            activeYAxis.forEach(function (axis) {
                _this.growBy(translatedPoint_1, axis, verticalFraction_1);
                anyUpdated = true;
            });
        }
        this.previousHorizontalTouchPointsDistance = currentHorizontalTouchPointsDistance;
        this.previousVerticalTouchPointsDistance = currentVerticalTouchPointsDistance;
        return anyUpdated;
    };
    YAxisDragModifier.prototype.updateCursor = function (mousePoint) {
        var canvas = this.parentSurface.getMainCanvas();
        if ((0, ChartModifierBase2D_1.testIsOverAxes)(this.getVerticalYAxes(), mousePoint)) {
            if (this.cursorStyle !== CursorStyle_1.ECursorStyle.NS) {
                canvas.style.cursor = CursorStyle_1.ECursorStyle.NS;
                this.cursorStyle = CursorStyle_1.ECursorStyle.NS;
            }
        }
        else if ((0, ChartModifierBase2D_1.testIsOverAxes)(this.getHorizontalYAxes(), mousePoint)) {
            if (this.cursorStyle !== CursorStyle_1.ECursorStyle.EW) {
                canvas.style.cursor = CursorStyle_1.ECursorStyle.EW;
                this.cursorStyle = CursorStyle_1.ECursorStyle.EW;
            }
        }
        else {
            if (this.cursorStyle !== CursorStyle_1.ECursorStyle.Default) {
                canvas.style.cursor = CursorStyle_1.ECursorStyle.Default;
                this.cursorStyle = CursorStyle_1.ECursorStyle.Default;
            }
        }
    };
    YAxisDragModifier.prototype.doPanning = function (mousePoint) {
        var pointTo = mousePoint;
        if (this.isVerticalChart) {
            var xDelta_1 = pointTo.x - this.pointFrom.x;
            this.activeAxes.forEach(function (y) {
                var delta = y.flippedCoordinates ? xDelta_1 : -xDelta_1;
                y.scroll(delta, AxisBase2D_1.EClipMode.None);
            });
        }
        else {
            var yDelta_1 = pointTo.y - this.pointFrom.y;
            this.activeAxes.forEach(function (y) {
                var delta = y.flippedCoordinates ? yDelta_1 : -yDelta_1;
                y.scroll(delta, AxisBase2D_1.EClipMode.None);
            });
        }
        this.pointFrom = pointTo;
    };
    YAxisDragModifier.prototype.doScaling = function (pointTo) {
        var _this = this;
        if (this.isVerticalChart) {
            this.activeAxes.forEach(function (axis, index) {
                var axisViewRect = axis.viewRect;
                var xFromTrans = (0, translate_1.translateFromCanvasToSeriesViewRectX)(_this.pointFrom.x, axisViewRect);
                var isMoreThanHalf = xFromTrans <= axisViewRect.width / 2;
                var xDelta = -(pointTo.x - _this.pointFrom.x) / axisViewRect.width;
                axis.scale(_this.initialVisibleRanges[index], xDelta, isMoreThanHalf);
            });
        }
        else {
            this.activeAxes.forEach(function (axis, index) {
                var axisViewRect = axis.viewRect;
                var yFromTrans = (0, translate_1.translateFromCanvasToSeriesViewRectY)(_this.pointFrom.y, axisViewRect);
                var isMoreThanHalf = yFromTrans <= axisViewRect.height / 2;
                var yDelta = -(pointTo.y - _this.pointFrom.y) / axisViewRect.height;
                axis.scale(_this.initialVisibleRanges[index], yDelta, isMoreThanHalf);
            });
        }
    };
    YAxisDragModifier.prototype.getVerticalYAxes = function () {
        return this.getIncludedYAxis().filter(function (el) { return !el.isHorizontalAxis; });
    };
    YAxisDragModifier.prototype.getHorizontalYAxes = function () {
        return this.getIncludedYAxis().filter(function (el) { return el.isHorizontalAxis; });
    };
    YAxisDragModifier.prototype.startDragging = function (axes, args) {
        this.isClickedOverYAxis = true;
        var activeAxes = (0, ChartModifierBase2D_1.getActiveAxes)(axes, args.mousePoint);
        var initialVisibleRanges = [];
        activeAxes.forEach(function (el) {
            initialVisibleRanges.push(el.visibleRange);
        });
        this.activeAxes = activeAxes;
        this.initialVisibleRanges = initialVisibleRanges;
        this.pointFrom = args.mousePoint;
        args.handled = true;
    };
    return YAxisDragModifier;
}(PinchZoomModifier_1.PinchZoomModifier));
exports.YAxisDragModifier = YAxisDragModifier;
