import _objectDestructuringEmpty from "@babel/runtime/helpers/esm/objectDestructuringEmpty";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _excluded = ["className", "style", "children", "rootNodeRef"];
var _class, _CommonWrapper;
import React from 'react';
import { isFunction, isRefableElement } from "../../../lib/utils";
import { cx } from "../../../lib/theming/Emotion";
import { getRootNode, isInstanceWithRootNode, rootNode } from "../../../lib/rootNode";
import { callChildRef } from "../../../lib/callChildRef/callChildRef";
import { extractCommonProps } from "../utils/extractCommonProps";
import { getCommonVisualStateDataAttributes } from "../utils/getCommonVisualStateDataAttributes";
export var CommonWrapper = rootNode(_class = (_CommonWrapper = /*#__PURE__*/function (_React$Component) {
  function CommonWrapper() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.rootNodeSubscription = null;
    _this.ref = function (instance) {
      var _this$rootNodeSubscri, _this$child;
      _this.setRootNode(instance);
      _this.props.rootNodeRef == null || _this.props.rootNodeRef(getRootNode(instance));

      // refs are called when instances change,
      // so we have to renew or remove old subscription
      (_this$rootNodeSubscri = _this.rootNodeSubscription) == null || _this$rootNodeSubscri.remove();
      _this.rootNodeSubscription = null;
      if (instance && isInstanceWithRootNode(instance)) {
        _this.rootNodeSubscription = instance.addRootNodeChangeListener == null ? void 0 : instance.addRootNodeChangeListener(function (node) {
          _this.setRootNode(node);
          _this.props.rootNodeRef == null || _this.props.rootNodeRef(node);
        });
      }
      var originalRef = (_this$child = _this.child) == null ? void 0 : _this$child.ref;
      if (typeof originalRef === 'function' || originalRef && typeof originalRef === 'object') {
        originalRef && callChildRef(originalRef, instance);
      }
    };
    return _this;
  }
  _inheritsLoose(CommonWrapper, _React$Component);
  var _proto = CommonWrapper.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _extractCommonProps = extractCommonProps(this.props),
      _extractCommonProps$ = _extractCommonProps[0],
      className = _extractCommonProps$.className,
      style = _extractCommonProps$.style,
      children = _extractCommonProps$.children,
      rootNodeRef = _extractCommonProps$.rootNodeRef,
      dataProps = _objectWithoutPropertiesLoose(_extractCommonProps$, _excluded),
      rest = _extends({}, (_objectDestructuringEmpty(_extractCommonProps[1]), _extractCommonProps[1]));
    this.child = isFunction(children) ? children(rest) : children;
    var getChildProps = function getChildProps(child) {
      var childProps = _extends({}, getCommonVisualStateDataAttributes(rest), dataProps);
      isRefableElement(child) && (childProps.ref = _this2.ref);
      var classNames = cx(child.props.className, className);
      classNames && (childProps.className = classNames);
      var styles = _extends({}, child.props.style, style);
      Object.keys(styles).length && (childProps.style = styles);
      return childProps;
    };
    return /*#__PURE__*/React.isValidElement(this.child) ? /*#__PURE__*/React.cloneElement(this.child, getChildProps(this.child)) : this.child;
  };
  return CommonWrapper;
}(React.Component), _CommonWrapper.__KONTUR_REACT_UI__ = 'CommonWrapper', _CommonWrapper.displayName = 'CommonWrapper', _CommonWrapper)) || _class;