'use strict';

var eventInfo = require('../events/event-info.js');
var Feature = require('../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/Feature.js');
var index = require('../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/gestures/press/index.js');
var frame = require('../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/frameloop/frame.js');

function handlePressEvent(node, event, lifecycle) {
    const { props } = node;
    if (node.current instanceof HTMLButtonElement && node.current.disabled) {
        return;
    }
    if (node.animationState && props.whileTap) {
        node.animationState.setActive("whileTap", lifecycle === "Start");
    }
    const eventName = ("onTap" + (lifecycle === "End" ? "" : lifecycle));
    const callback = props[eventName];
    if (callback) {
        frame.frame.postRender(() => callback(event, eventInfo.extractEventInfo(event)));
    }
}
class PressGesture extends Feature.Feature {
    mount() {
        const { current } = this.node;
        if (!current)
            return;
        const { globalTapTarget, propagate } = this.node.props;
        this.unmount = index.press(current, (_element, startEvent) => {
            handlePressEvent(this.node, startEvent, "Start");
            return (endEvent, { success }) => handlePressEvent(this.node, endEvent, success ? "End" : "Cancel");
        }, {
            useGlobalTarget: globalTapTarget,
            stopPropagation: propagate?.tap === false,
        });
    }
    unmount() { }
}

exports.PressGesture = PressGesture;
