var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
    if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
    return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
import { EventEmitter } from 'eventemitter3';
import { getRootNode } from './getRootNode.js';
export function rootNode(Component) {
    var _a;
    var rootNode = (_a = /** @class */ (function (_super) {
            __extends(class_1, _super);
            function class_1() {
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                var _this = _super.call(this, args[0]) || this;
                _this.rootNode = Component.defaultRootNode;
                _this.rootNodeEmitter = new EventEmitter();
                _this.setRootNode = function (instance) {
                    var rootNode = getRootNode(instance);
                    if (rootNode !== _this.rootNode) {
                        _this.rootNode = rootNode;
                        _this.rootNodeEmitter.emit('change', rootNode);
                    }
                };
                _this.getRootNode = function () {
                    return _this.rootNode;
                };
                _this.addRootNodeChangeListener = function (callback) {
                    _this.rootNodeEmitter.addListener('change', callback);
                    return {
                        remove: function () {
                            _this.rootNodeEmitter.removeListener('change', callback);
                        },
                    };
                };
                return _this;
            }
            return class_1;
        }(Component)),
        __setFunctionName(_a, "rootNode"),
        _a.__KONTUR_REACT_UI__ = Component.__KONTUR_REACT_UI__,
        _a.displayName = Component.displayName,
        _a);
    var nameDescriptor = Object.getOwnPropertyDescriptor(rootNode, 'name');
    if (!nameDescriptor || nameDescriptor.configurable) {
        Object.defineProperty(rootNode, 'name', { value: Component.name });
    }
    return rootNode;
}
export var isInstanceWithRootNode = function (instance) {
    return Boolean(instance) && Object.prototype.hasOwnProperty.call(instance, 'getRootNode');
};
//# sourceMappingURL=rootNodeDecorator.js.map