"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Base_1 = require("@dojo/framework/core/meta/Base");
var global_1 = require("@dojo/framework/shim/global");
var Set_1 = require("@dojo/framework/shim/Set");
var defaultResults = {
    active: false,
    containsFocus: false
};
var Focus = /** @class */ (function (_super) {
    tslib_1.__extends(Focus, _super);
    function Focus() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._nodeset = new Set_1.default();
        _this._onFocusChange = function () {
            if ((_this._activeElement && _this._nodeset.has(_this._activeElement)) ||
                _this._nodeset.has(global_1.default.document.activeElement) ||
                tslib_1.__spread(_this._nodeset).some(function (node) {
                    return !!(node.contains(global_1.default.document.activeElement) ||
                        (_this._activeElement && node.contains(_this._activeElement)));
                })) {
                _this.invalidate();
            }
            _this._activeElement = global_1.default.document.activeElement;
        };
        return _this;
    }
    Focus.prototype.get = function (key) {
        var node = this.getNode(key);
        if (!node) {
            return tslib_1.__assign({}, defaultResults);
        }
        this._nodeset.add(node);
        if (!this._activeElement) {
            this._activeElement = global_1.default.document.activeElement;
            this._createListener();
        }
        return {
            active: node === this._activeElement,
            containsFocus: !!this._activeElement && node.contains(this._activeElement)
        };
    };
    Focus.prototype.set = function (key) {
        var node = this.getNode(key);
        node && node.focus();
    };
    Focus.prototype._createListener = function () {
        var _this = this;
        global_1.default.document.addEventListener('focusin', this._onFocusChange);
        global_1.default.document.addEventListener('focusout', this._onFocusChange);
        this.own({
            destroy: function () {
                _this._removeListener();
            }
        });
    };
    Focus.prototype._removeListener = function () {
        global_1.default.document.removeEventListener('focusin', this._onFocusChange);
        global_1.default.document.removeEventListener('focusout', this._onFocusChange);
    };
    return Focus;
}(Base_1.Base));
exports.Focus = Focus;
exports.default = Focus;

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