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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React from 'react';
import { isFunction, isRefableElement } from '../../lib/utils.js';
import { getRootNode, isInstanceWithRootNode, rootNode } from '../../lib/rootNode/index.js';
import { callChildRef } from '../../lib/callChildRef/callChildRef.js';
import { getElementRef } from '../../lib/getElementRef.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { extractCommonProps } from './utils/extractCommonProps.js';
import { getCommonVisualStateDataAttributes } from './utils/getCommonVisualStateDataAttributes.js';
var CommonWrapper = /** @class */ (function (_super) {
    __extends(CommonWrapper, _super);
    function CommonWrapper() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.rootNodeSubscription = null;
        _this.ref = function (instance) {
            var _a, _b, _c, _d;
            _this.setRootNode(instance);
            (_b = (_a = _this.props).rootNodeRef) === null || _b === void 0 ? void 0 : _b.call(_a, getRootNode(instance));
            // refs are called when instances change,
            // so we have to renew or remove old subscription
            (_c = _this.rootNodeSubscription) === null || _c === void 0 ? void 0 : _c.remove();
            _this.rootNodeSubscription = null;
            if (instance && isInstanceWithRootNode(instance)) {
                _this.rootNodeSubscription = (_d = instance.addRootNodeChangeListener) === null || _d === void 0 ? void 0 : _d.call(instance, function (node) {
                    var _a, _b;
                    _this.setRootNode(node);
                    (_b = (_a = _this.props).rootNodeRef) === null || _b === void 0 ? void 0 : _b.call(_a, node);
                });
            }
            var originalRef = getElementRef(_this.child);
            if (typeof originalRef === 'function' || (originalRef && typeof originalRef === 'object')) {
                originalRef && callChildRef(originalRef, instance);
            }
        };
        return _this;
    }
    CommonWrapper.prototype.render = function () {
        var _this = this;
        var _a = extractCommonProps(this.props), _b = _a[0], _c = _a[1], className = _b.className, style = _b.style, children = _b.children, rootNodeRef = _b.rootNodeRef, dataProps = __rest(_b, ["className", "style", "children", "rootNodeRef"]), rest = __rest(_c, []);
        this.child = isFunction(children) ? children(rest) : children;
        var getChildProps = function (child) {
            var childProps = __assign(__assign({}, getCommonVisualStateDataAttributes(rest)), dataProps);
            isRefableElement(child) && (childProps.ref = _this.ref);
            var classNames = _this.cx(child.props.className, className);
            classNames && (childProps.className = classNames);
            var styles = __assign(__assign({}, child.props.style), style);
            Object.keys(styles).length && (childProps.style = styles);
            return childProps;
        };
        return React.isValidElement(this.child)
            ? React.cloneElement(this.child, getChildProps(this.child))
            : this.child;
    };
    CommonWrapper.__KONTUR_REACT_UI__ = 'CommonWrapper';
    CommonWrapper.displayName = 'CommonWrapper';
    CommonWrapper = __decorate([
        withRenderEnvironment,
        rootNode
    ], CommonWrapper);
    return CommonWrapper;
}(React.Component));
export { CommonWrapper };
//# sourceMappingURL=CommonWrapper.js.map