import {getEventTarget as $d8ac7ed472840322$export$e58f029f0fbfdb29, nodeContains as $d8ac7ed472840322$export$4282f70798064fe0} from "../utils/shadowdom/DOMFunctions.js";
import {getOwnerDocument as $cc3c3666b64debad$export$b204af158042fbac} from "../utils/domHelpers.js";
import {useGlobalListeners as $0d742958be022209$export$4eaf04e54aa8eed6} from "../utils/useGlobalListeners.js";
import {useState as $4dh1a$useState, useRef as $4dh1a$useRef, useEffect as $4dh1a$useEffect, useMemo as $4dh1a$useMemo} from "react";

/*
 * Copyright 2020 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ // Portions of the code in this file are based on code from react.
// Original licensing for the following can be found in the
// NOTICE file in the root directory of this source tree.
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions




// iOS fires onPointerEnter twice: once with pointerType="touch" and again with pointerType="mouse".
// We want to ignore these emulated events so they do not trigger hover behavior.
// See https://bugs.webkit.org/show_bug.cgi?id=214609.
let $f7f05710dfc01c4c$var$globalIgnoreEmulatedMouseEvents = false;
let $f7f05710dfc01c4c$var$hoverCount = 0;
function $f7f05710dfc01c4c$var$setGlobalIgnoreEmulatedMouseEvents() {
    $f7f05710dfc01c4c$var$globalIgnoreEmulatedMouseEvents = true;
    // Clear globalIgnoreEmulatedMouseEvents after a short timeout. iOS fires onPointerEnter
    // with pointerType="mouse" immediately after onPointerUp and before onFocus. On other
    // devices that don't have this quirk, we don't want to ignore a mouse hover sometime in
    // the distant future because a user previously touched the element.
    setTimeout(()=>{
        $f7f05710dfc01c4c$var$globalIgnoreEmulatedMouseEvents = false;
    }, 500);
}
function $f7f05710dfc01c4c$var$handleGlobalPointerEvent(e) {
    if (e.pointerType === 'touch') $f7f05710dfc01c4c$var$setGlobalIgnoreEmulatedMouseEvents();
}
function $f7f05710dfc01c4c$var$setupGlobalTouchEvents() {
    let ownerDocument = (0, $cc3c3666b64debad$export$b204af158042fbac)(null);
    if (typeof ownerDocument === 'undefined') return;
    if ($f7f05710dfc01c4c$var$hoverCount === 0) {
        if (typeof PointerEvent !== 'undefined') ownerDocument.addEventListener('pointerup', $f7f05710dfc01c4c$var$handleGlobalPointerEvent);
        else if (process.env.NODE_ENV === 'test') ownerDocument.addEventListener('touchend', $f7f05710dfc01c4c$var$setGlobalIgnoreEmulatedMouseEvents);
    }
    $f7f05710dfc01c4c$var$hoverCount++;
    return ()=>{
        $f7f05710dfc01c4c$var$hoverCount--;
        if ($f7f05710dfc01c4c$var$hoverCount > 0) return;
        if (typeof PointerEvent !== 'undefined') ownerDocument.removeEventListener('pointerup', $f7f05710dfc01c4c$var$handleGlobalPointerEvent);
        else if (process.env.NODE_ENV === 'test') ownerDocument.removeEventListener('touchend', $f7f05710dfc01c4c$var$setGlobalIgnoreEmulatedMouseEvents);
    };
}
function $f7f05710dfc01c4c$export$ae780daf29e6d456(props) {
    let { onHoverStart: onHoverStart, onHoverChange: onHoverChange, onHoverEnd: onHoverEnd, isDisabled: isDisabled } = props;
    let [isHovered, setHovered] = (0, $4dh1a$useState)(false);
    let state = (0, $4dh1a$useRef)({
        isHovered: false,
        ignoreEmulatedMouseEvents: false,
        pointerType: '',
        target: null
    }).current;
    (0, $4dh1a$useEffect)($f7f05710dfc01c4c$var$setupGlobalTouchEvents, []);
    let { addGlobalListener: addGlobalListener, removeAllGlobalListeners: removeAllGlobalListeners } = (0, $0d742958be022209$export$4eaf04e54aa8eed6)();
    let { hoverProps: hoverProps, triggerHoverEnd: triggerHoverEnd } = (0, $4dh1a$useMemo)(()=>{
        let triggerHoverStart = (event, pointerType)=>{
            state.pointerType = pointerType;
            if (isDisabled || pointerType === 'touch' || state.isHovered || !(0, $d8ac7ed472840322$export$4282f70798064fe0)(event.currentTarget, (0, $d8ac7ed472840322$export$e58f029f0fbfdb29)(event))) return;
            state.isHovered = true;
            let target = event.currentTarget;
            state.target = target;
            // When an element that is hovered over is removed, no pointerleave event is fired by the browser,
            // even though the originally hovered target may have shrunk in size so it is no longer hovered.
            // However, a pointerover event will be fired on the new target the mouse is over.
            // In Chrome this happens immediately. In Safari and Firefox, it happens upon moving the mouse one pixel.
            addGlobalListener((0, $cc3c3666b64debad$export$b204af158042fbac)((0, $d8ac7ed472840322$export$e58f029f0fbfdb29)(event)), 'pointerover', (e)=>{
                if (state.isHovered && state.target && !(0, $d8ac7ed472840322$export$4282f70798064fe0)(state.target, (0, $d8ac7ed472840322$export$e58f029f0fbfdb29)(e))) triggerHoverEnd(e, e.pointerType);
            }, {
                capture: true
            });
            if (onHoverStart) onHoverStart({
                type: 'hoverstart',
                target: target,
                pointerType: pointerType
            });
            if (onHoverChange) onHoverChange(true);
            setHovered(true);
        };
        let triggerHoverEnd = (event, pointerType)=>{
            let target = state.target;
            state.pointerType = '';
            state.target = null;
            if (pointerType === 'touch' || !state.isHovered || !target) return;
            state.isHovered = false;
            removeAllGlobalListeners();
            if (onHoverEnd) onHoverEnd({
                type: 'hoverend',
                target: target,
                pointerType: pointerType
            });
            if (onHoverChange) onHoverChange(false);
            setHovered(false);
        };
        let hoverProps = {};
        if (typeof PointerEvent !== 'undefined') {
            hoverProps.onPointerEnter = (e)=>{
                if ($f7f05710dfc01c4c$var$globalIgnoreEmulatedMouseEvents && e.pointerType === 'mouse') return;
                triggerHoverStart(e, e.pointerType);
            };
            hoverProps.onPointerLeave = (e)=>{
                if (!isDisabled && (0, $d8ac7ed472840322$export$4282f70798064fe0)(e.currentTarget, (0, $d8ac7ed472840322$export$e58f029f0fbfdb29)(e))) triggerHoverEnd(e, e.pointerType);
            };
        } else if (process.env.NODE_ENV === 'test') {
            hoverProps.onTouchStart = ()=>{
                state.ignoreEmulatedMouseEvents = true;
            };
            hoverProps.onMouseEnter = (e)=>{
                if (!state.ignoreEmulatedMouseEvents && !$f7f05710dfc01c4c$var$globalIgnoreEmulatedMouseEvents) triggerHoverStart(e, 'mouse');
                state.ignoreEmulatedMouseEvents = false;
            };
            hoverProps.onMouseLeave = (e)=>{
                if (!isDisabled && (0, $d8ac7ed472840322$export$4282f70798064fe0)(e.currentTarget, (0, $d8ac7ed472840322$export$e58f029f0fbfdb29)(e))) triggerHoverEnd(e, 'mouse');
            };
        }
        return {
            hoverProps: hoverProps,
            triggerHoverEnd: triggerHoverEnd
        };
    }, [
        onHoverStart,
        onHoverChange,
        onHoverEnd,
        isDisabled,
        state,
        addGlobalListener,
        removeAllGlobalListeners
    ]);
    (0, $4dh1a$useEffect)(()=>{
        // Call the triggerHoverEnd as soon as isDisabled changes to true
        // Safe to call triggerHoverEnd, it will early return if we aren't currently hovering
        if (isDisabled) triggerHoverEnd({
            currentTarget: state.target
        }, state.pointerType);
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        isDisabled
    ]);
    return {
        hoverProps: hoverProps,
        isHovered: isHovered
    };
}


export {$f7f05710dfc01c4c$export$ae780daf29e6d456 as useHover};
//# sourceMappingURL=useHover.js.map
