/**
 * DevExtreme (esm/events/click.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../core/renderer";
import eventsEngine from "../events/core/events_engine";
import devices from "../core/devices";
import domAdapter from "../core/dom_adapter";
import {
    resetActiveElement
} from "../core/utils/dom";
import {
    requestAnimationFrame,
    cancelAnimationFrame
} from "../animation/frame";
import {
    addNamespace,
    fireEvent
} from "./utils/index";
import {
    subscribeNodesDisposing,
    unsubscribeNodesDisposing
} from "./utils/event_nodes_disposing";
import pointerEvents from "./pointer";
import Emitter from "./core/emitter";
import registerEmitter from "./core/emitter_registrator";
var CLICK_EVENT_NAME = "dxclick";
var misc = {
    requestAnimationFrame: requestAnimationFrame,
    cancelAnimationFrame: cancelAnimationFrame
};
var prevented = null;
var lastFiredEvent = null;
var onNodeRemove = () => {
    lastFiredEvent = null
};
var clickHandler = function(e) {
    var originalEvent = e.originalEvent;
    var eventAlreadyFired = lastFiredEvent === originalEvent || originalEvent && originalEvent.DXCLICK_FIRED;
    var leftButton = !e.which || 1 === e.which;
    if (leftButton && !prevented && !eventAlreadyFired) {
        if (originalEvent) {
            originalEvent.DXCLICK_FIRED = true
        }
        unsubscribeNodesDisposing(lastFiredEvent, onNodeRemove);
        lastFiredEvent = originalEvent;
        subscribeNodesDisposing(lastFiredEvent, onNodeRemove);
        fireEvent({
            type: CLICK_EVENT_NAME,
            originalEvent: e
        })
    }
};
var ClickEmitter = Emitter.inherit({
    ctor: function(element) {
        this.callBase(element);
        eventsEngine.on(this.getElement(), "click", clickHandler)
    },
    start: function(e) {
        prevented = null
    },
    cancel: function() {
        prevented = true
    },
    dispose: function() {
        eventsEngine.off(this.getElement(), "click", clickHandler)
    }
});
! function() {
    var desktopDevice = devices.real().generic;
    if (!desktopDevice) {
        var startTarget = null;
        var blurPrevented = false;
        var document = domAdapter.getDocument();
        eventsEngine.subscribeGlobal(document, addNamespace(pointerEvents.down, "NATIVE_CLICK_FIXER"), (function(e) {
            startTarget = e.target;
            blurPrevented = e.isDefaultPrevented()
        }));
        eventsEngine.subscribeGlobal(document, addNamespace("click", "NATIVE_CLICK_FIXER"), (function(e) {
            var $target = function(e) {
                var _originalEvent$target;
                var originalEvent = e.originalEvent;
                if (null !== originalEvent && void 0 !== originalEvent && null !== (_originalEvent$target = originalEvent.target) && void 0 !== _originalEvent$target && _originalEvent$target.shadowRoot) {
                    var _originalEvent$path, _originalEvent$compos;
                    var path = null !== (_originalEvent$path = originalEvent.path) && void 0 !== _originalEvent$path ? _originalEvent$path : null === (_originalEvent$compos = originalEvent.composedPath) || void 0 === _originalEvent$compos ? void 0 : _originalEvent$compos.call(originalEvent);
                    return $(path[0])
                }
                return $(e.target)
            }(e);
            if (!blurPrevented && startTarget && !$target.is(startTarget) && !$(startTarget).is("label") && (element = $target, $(element).is("input, textarea, select, button ,:focus, :focus *"))) {
                resetActiveElement()
            }
            var element;
            startTarget = null;
            blurPrevented = false
        }))
    }
}();
registerEmitter({
    emitter: ClickEmitter,
    bubble: true,
    events: [CLICK_EVENT_NAME]
});
export {
    CLICK_EVENT_NAME as name
};
