"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Cursor
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursorInformation = exports.CursorUpdatedEvent = exports.CursorDirection = exports.CursorDirectionParts = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
/** Enum for Cursor Direction parts
 *  @public
 */
var CursorDirectionParts;
(function (CursorDirectionParts) {
    CursorDirectionParts[CursorDirectionParts["Top"] = 4096] = "Top";
    CursorDirectionParts[CursorDirectionParts["Left"] = 256] = "Left";
    CursorDirectionParts[CursorDirectionParts["Right"] = 16] = "Right";
    CursorDirectionParts[CursorDirectionParts["Bottom"] = 1] = "Bottom";
})(CursorDirectionParts = exports.CursorDirectionParts || (exports.CursorDirectionParts = {}));
/** Enum for Cursor Direction
 * @public
 */
var CursorDirection;
(function (CursorDirection) {
    CursorDirection[CursorDirection["None"] = 0] = "None";
    CursorDirection[CursorDirection["Top"] = 4096] = "Top";
    CursorDirection[CursorDirection["Left"] = 256] = "Left";
    CursorDirection[CursorDirection["Right"] = 16] = "Right";
    CursorDirection[CursorDirection["Bottom"] = 1] = "Bottom";
    CursorDirection[CursorDirection["TopLeft"] = 4352] = "TopLeft";
    CursorDirection[CursorDirection["TopRight"] = 4112] = "TopRight";
    CursorDirection[CursorDirection["BottomLeft"] = 257] = "BottomLeft";
    CursorDirection[CursorDirection["BottomRight"] = 17] = "BottomRight";
})(CursorDirection = exports.CursorDirection || (exports.CursorDirection = {}));
/** Cursor Updated Event class.
 * @public
 */
class CursorUpdatedEvent extends appui_abstract_1.UiEvent {
}
exports.CursorUpdatedEvent = CursorUpdatedEvent;
/** Cursor Information class
 * @public
 */
class CursorInformation {
    /** The cursor position. */
    static get cursorPosition() {
        return this._cursorPosition;
    }
    static set cursorPosition(pt) {
        this._cursorPosition = core_react_1.Point.create(pt);
    }
    /** Gets the cursor X position. */
    static get cursorX() {
        return this._cursorPosition.x;
    }
    /** Gets the cursor Y position. */
    static get cursorY() {
        return this._cursorPosition.y;
    }
    /** Gets the general cursor movement direction. */
    static get cursorDirection() {
        return this._cursorDirection;
    }
    /** Handles the mouse movement.  Sets the cursor position and direction and emits onCursorUpdatedEvent. */
    static handleMouseMove(point) {
        const oldPt = CursorInformation.cursorPosition;
        const direction = this._determineMostFrequentDirection(this._cursorDirections, this._cursorPosition, core_react_1.Point.create(point));
        this.cursorPosition = point;
        this._cursorDirection = direction;
        this.onCursorUpdatedEvent.emit({ oldPt, newPt: point, direction });
    }
    static _determineMostFrequentDirection(cursorDirections, oldPt, newPt) {
        const cursorDirection = this._determineCursorDirection(oldPt, newPt);
        cursorDirections.push(cursorDirection);
        if (cursorDirections.length > 10)
            cursorDirections.shift();
        return this._mostFrequent(cursorDirections);
    }
    static _mostFrequent(array) {
        const hashMap = new Map();
        let maxCount = 0, mostFrequent = CursorDirection.None;
        array.forEach((value) => {
            const key = value;
            let frequency = hashMap.get(key);
            if (frequency !== undefined)
                frequency++;
            else
                frequency = 1;
            hashMap.set(key, frequency);
            if (frequency > maxCount) {
                mostFrequent = key;
                maxCount = frequency;
            }
        });
        return mostFrequent;
    }
    static _determineCursorDirection(oldPt, newPt) {
        let directionParts = 0;
        if (newPt.x < oldPt.x)
            directionParts |= CursorDirectionParts.Left;
        else if (newPt.x > oldPt.x)
            directionParts |= CursorDirectionParts.Right;
        if (newPt.y < oldPt.y)
            directionParts |= CursorDirectionParts.Top;
        else if (newPt.y > oldPt.y)
            directionParts |= CursorDirectionParts.Bottom;
        const direction = directionParts;
        return direction;
    }
    /** Gets the relative position based on the cursor direction. */
    static getRelativePositionFromCursorDirection(cursorDirection) {
        let relativePosition = appui_abstract_1.RelativePosition.BottomRight;
        switch (cursorDirection) {
            case CursorDirection.Top:
                relativePosition = appui_abstract_1.RelativePosition.Top;
                break;
            case CursorDirection.Left:
                relativePosition = appui_abstract_1.RelativePosition.Left;
                break;
            case CursorDirection.Right:
                relativePosition = appui_abstract_1.RelativePosition.Right;
                break;
            case CursorDirection.Bottom:
                relativePosition = appui_abstract_1.RelativePosition.Bottom;
                break;
            case CursorDirection.TopLeft:
                relativePosition = appui_abstract_1.RelativePosition.TopLeft;
                break;
            case CursorDirection.TopRight:
                relativePosition = appui_abstract_1.RelativePosition.TopRight;
                break;
            case CursorDirection.BottomLeft:
                relativePosition = appui_abstract_1.RelativePosition.BottomLeft;
                break;
            case CursorDirection.BottomRight:
                relativePosition = appui_abstract_1.RelativePosition.BottomRight;
                break;
        }
        return relativePosition;
    }
    /** @internal - for testing */
    static clearCursorDirections() {
        this._cursorDirections.length = 0;
    }
}
CursorInformation._cursorPosition = new core_react_1.Point();
CursorInformation._cursorDirection = CursorDirection.BottomRight;
CursorInformation._cursorDirections = new Array();
/** Gets the [[CursorUpdatedEvent]]. */
CursorInformation.onCursorUpdatedEvent = new CursorUpdatedEvent();
exports.CursorInformation = CursorInformation;
//# sourceMappingURL=CursorInformation.js.map