import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import EventEmitter from 'eventemitter3';
import { getRootNode } from "../getRootNode";
export function rootNode(Component) {
  var _Class;
  var rootNode = (_Class = /*#__PURE__*/function (_Component) {
    function rootNode() {
      var _this;
      _this = _Component.call(this, arguments.length <= 0 ? undefined : arguments[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 remove() {
            _this.rootNodeEmitter.removeListener('change', callback);
          }
        };
      };
      return _this;
    }
    _inheritsLoose(rootNode, _Component);
    return rootNode;
  }(Component), _Class.__KONTUR_REACT_UI__ = Component.__KONTUR_REACT_UI__, _Class.displayName = Component.displayName, _Class);
  var nameDescriptor = Object.getOwnPropertyDescriptor(rootNode, 'name');
  if (!nameDescriptor || nameDescriptor.configurable) {
    Object.defineProperty(rootNode, 'name', {
      value: Component.name
    });
  }
  return rootNode;
}
export var isInstanceWithRootNode = function isInstanceWithRootNode(instance) {
  return Boolean(instance) && Object.prototype.hasOwnProperty.call(instance, 'getRootNode');
};