/**
 * DevExtreme (esm/__internal/events/dblclick.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 Class from "../../core/class";
import domAdapter from "../../core/dom_adapter";
import {
    closestCommonParent
} from "../../core/utils/dom";
import {
    name as clickEventName
} from "../../events/click";
import eventsEngine from "../../events/core/events_engine";
import {
    addNamespace,
    fireEvent
} from "../../events/utils/index";
var DBLCLICK_EVENT_NAME = "dxdblclick";
var DBLCLICK_NAMESPACE = "dxDblClick";
var NAMESPACED_CLICK_EVENT = addNamespace(clickEventName, DBLCLICK_NAMESPACE);
var DBLCLICK_TIMEOUT = 300;
var DblClick = Class.inherit({
    ctor() {
        this._handlerCount = 0;
        this._forgetLastClick()
    },
    _forgetLastClick() {
        this._firstClickTarget = null;
        this._lastClickTimeStamp = -DBLCLICK_TIMEOUT
    },
    add() {
        if (this._handlerCount <= 0) {
            eventsEngine.on(domAdapter.getDocument(), NAMESPACED_CLICK_EVENT, this._clickHandler.bind(this))
        }
        this._handlerCount += 1
    },
    _clickHandler(e) {
        var timeStamp = e.timeStamp || Date.now();
        var timeBetweenClicks = timeStamp - this._lastClickTimeStamp;
        var isSimulated = timeBetweenClicks < 0;
        var isDouble = !isSimulated && timeBetweenClicks < DBLCLICK_TIMEOUT;
        if (isDouble) {
            fireEvent({
                type: DBLCLICK_EVENT_NAME,
                target: closestCommonParent(this._firstClickTarget, e.target),
                originalEvent: e
            });
            this._forgetLastClick()
        } else {
            this._firstClickTarget = e.target;
            this._lastClickTimeStamp = timeStamp
        }
    },
    remove() {
        this._handlerCount -= 1;
        if (this._handlerCount <= 0) {
            this._forgetLastClick();
            eventsEngine.off(domAdapter.getDocument(), NAMESPACED_CLICK_EVENT, void 0);
            this._handlerCount = 0
        }
    }
});
var dblClick = new DblClick;
export {
    dblClick,
    DBLCLICK_EVENT_NAME as name
};
