import { Base } from '@dojo/framework/core/meta/Base';
import global from '@dojo/framework/shim/global';
import Set from '@dojo/framework/shim/Set';
const defaultResults = {
    active: false,
    containsFocus: false
};
export class Focus extends Base {
    constructor() {
        super(...arguments);
        this._nodeset = new Set();
        this._onFocusChange = () => {
            if ((this._activeElement && this._nodeset.has(this._activeElement)) ||
                this._nodeset.has(global.document.activeElement) ||
                [...this._nodeset].some((node) => !!(node.contains(global.document.activeElement) ||
                    (this._activeElement && node.contains(this._activeElement))))) {
                this.invalidate();
            }
            this._activeElement = global.document.activeElement;
        };
    }
    get(key) {
        const node = this.getNode(key);
        if (!node) {
            return Object.assign({}, defaultResults);
        }
        this._nodeset.add(node);
        if (!this._activeElement) {
            this._activeElement = global.document.activeElement;
            this._createListener();
        }
        return {
            active: node === this._activeElement,
            containsFocus: !!this._activeElement && node.contains(this._activeElement)
        };
    }
    set(key) {
        const node = this.getNode(key);
        node && node.focus();
    }
    _createListener() {
        global.document.addEventListener('focusin', this._onFocusChange);
        global.document.addEventListener('focusout', this._onFocusChange);
        this.own({
            destroy: () => {
                this._removeListener();
            }
        });
    }
    _removeListener() {
        global.document.removeEventListener('focusin', this._onFocusChange);
        global.document.removeEventListener('focusout', this._onFocusChange);
    }
}
export default Focus;

/*# sourceMappingURL=Focus.mjs.map*/