'use strict';

var Feature = require('../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/Feature.js');
var pipe = require('../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/pipe.js');
var addDomEvent = require('../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/events/add-dom-event.js');

class FocusGesture extends Feature.Feature {
    constructor() {
        super(...arguments);
        this.isActive = false;
    }
    onFocus() {
        let isFocusVisible = false;
        /**
         * If this element doesn't match focus-visible then don't
         * apply whileHover. But, if matches throws that focus-visible
         * is not a valid selector then in that browser outline styles will be applied
         * to the element by default and we want to match that behaviour with whileFocus.
         */
        try {
            isFocusVisible = this.node.current.matches(":focus-visible");
        }
        catch (e) {
            isFocusVisible = true;
        }
        if (!isFocusVisible || !this.node.animationState)
            return;
        this.node.animationState.setActive("whileFocus", true);
        this.isActive = true;
    }
    onBlur() {
        if (!this.isActive || !this.node.animationState)
            return;
        this.node.animationState.setActive("whileFocus", false);
        this.isActive = false;
    }
    mount() {
        this.unmount = pipe.pipe(addDomEvent.addDomEvent(this.node.current, "focus", () => this.onFocus()), addDomEvent.addDomEvent(this.node.current, "blur", () => this.onBlur()));
    }
    unmount() { }
}

exports.FocusGesture = FocusGesture;
