/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { ListDropdownAwareFoundation } from './list-dropdown-aware-foundation';
export class CascadingListDropdownAwareFoundation extends ListDropdownAwareFoundation {
    constructor(_config) {
        super();
        this._config = _config;
        this._open = false;
        this._options = [];
        this._childOpen = false;
        this._popupHasMouse = false; // Used to determine if the popup element is under the users mouse
        this._targetMouseEnterListener = evt => this._onTargetMouseEnter(evt);
        this._targetMouseLeaveListener = evt => this._onTargetMouseLeave(evt);
        this._childPopupMouseEnterListener = () => this._onChildPopupMouseEnter();
        this._childPopupMouseLeaveListener = () => this._onChildPopupMouseLeave();
        this._documentMouseMoveListener = evt => this._mouseCoords = { x: evt.pageX, y: evt.pageY };
    }
    _onChildPopupMouseEnter() {
        this._popupHasMouse = true;
    }
    _onChildPopupMouseLeave() {
        this._popupHasMouse = false;
        setTimeout(() => {
            const mouseElement = document.elementFromPoint(this._mouseCoords.x, this._mouseCoords.y);
            if (!this._popupHasMouse && !this._childOpen && (!mouseElement || !this._isOwnElement(mouseElement))) {
                this._setCascadeTargetInactive();
                this._closeDropdown();
            }
        }, this._config.popupTimeout);
    }
    _onTargetMouseEnter(evt) {
        if (!this._options.length) {
            return;
        }
        this._openDropdown({ fromKeyboard: false });
    }
    _onTargetMouseLeave(evt) {
        // We wrap this in a timeout to allow for the user to take an indirect path toward an open child menu. This
        // allows for the popup to stay open while the user is moving their mouse to it (makes the transition easier and more smooth)
        this._activeMouseLeaveTimeout = window.setTimeout(() => {
            // Ignore this if our open state has changed, or if we have a child popup open now
            if (!this._open || this._childOpen) {
                return;
            }
            if (this._mouseCoords) {
                // We ignore this event if the mouse moved to an element within our own popup
                const mouseElement = document.elementFromPoint(this._mouseCoords.x, this._mouseCoords.y);
                if (mouseElement && this._isOwnElement(mouseElement)) {
                    return;
                }
            }
            this._setCascadeTargetInactive();
            this._closeDropdown();
        }, this._config.targetTimeout);
    }
    _clearMouseLeaveTimeout() {
        if (this._activeMouseLeaveTimeout !== undefined) {
            window.clearTimeout(this._activeMouseLeaveTimeout);
        }
    }
}
