"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.CommonWrapper = void 0;var _objectDestructuringEmpty2 = _interopRequireDefault(require("@babel/runtime/helpers/objectDestructuringEmpty"));var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));

var _utils = require("../../lib/utils");
var _Emotion = require("../../lib/theming/Emotion");


var _rootNode = require("../../lib/rootNode");
var _callChildRef = require("../../lib/callChildRef/callChildRef");


var _extractCommonProps2 = require("./utils/extractCommonProps");
var _getCommonVisualStateDataAttributes = require("./utils/getCommonVisualStateDataAttributes");var _excluded = ["className", "style", "children", "rootNodeRef"];var _class, _CommonWrapper;var




CommonWrapper = exports.CommonWrapper = (0, _rootNode.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((0, _rootNode.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 && (0, _rootNode.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 && (0, _callChildRef.callChildRef)(originalRef, instance);
      }
    };return _this;}(0, _inheritsLoose2.default)(CommonWrapper, _React$Component);var _proto = CommonWrapper.prototype;_proto.render = function render() {var _this2 = this;var _extractCommonProps = (0, _extractCommonProps2.extractCommonProps)(this.props),_extractCommonProps$ = _extractCommonProps[0],className = _extractCommonProps$.className,style = _extractCommonProps$.style,children = _extractCommonProps$.children,rootNodeRef = _extractCommonProps$.rootNodeRef,dataProps = (0, _objectWithoutPropertiesLoose2.default)(_extractCommonProps$, _excluded),rest = (0, _extends2.default)({}, ((0, _objectDestructuringEmpty2.default)(_extractCommonProps[1]), _extractCommonProps[1]));this.child = (0, _utils.isFunction)(children) ? children(rest) : children;var getChildProps = function getChildProps(child) {var childProps = (0, _extends2.default)({}, (0, _getCommonVisualStateDataAttributes.getCommonVisualStateDataAttributes)(rest), dataProps);(0, _utils.isRefableElement)(child) && (childProps.ref = _this2.ref);var classNames = (0, _Emotion.cx)(child.props.className, className);classNames && (childProps.className = classNames);var styles = (0, _extends2.default)({}, child.props.style, style);Object.keys(styles).length && (childProps.style = styles);return childProps;};return /*#__PURE__*/_react.default.isValidElement(this.child) ? /*#__PURE__*/_react.default.cloneElement(this.child, getChildProps(this.child)) : this.child;};return CommonWrapper;}(_react.default.Component), _CommonWrapper.__KONTUR_REACT_UI__ = 'CommonWrapper', _CommonWrapper.displayName = 'CommonWrapper', _CommonWrapper)) || _class;