"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Wrapper = void 0;
var _findDOMNode = _interopRequireWildcard(require("rc-util/lib/Dom/findDOMNode"));
var _ref = require("rc-util/lib/ref");
var React = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
class LegacyWrapper extends React.Component {
  constructor() {
    super(...arguments);
    this.element = null;
  }
  componentDidMount() {
    this.componentDidUpdate();
  }
  componentDidUpdate() {
    // eslint-disable-next-line
    const node = (0, _findDOMNode.default)(this);
    if (node instanceof Element) {
      this.element = node;
    } else {
      this.element = null;
    }
  }
  render() {
    return this.props.children;
  }
}
const Wrapper = exports.Wrapper = React.forwardRef(({
  children
}, ref) => {
  const elementRef = React.useRef(null);
  const legacyWrapperRef = React.createRef();
  const child = React.Children.only(children);
  const canUseRef = (0, _ref.supportRef)(children);
  const getElement = () => {
    var _a;
    if (canUseRef) {
      return (0, _findDOMNode.getDOM)(elementRef.current);
    }
    return (_a = legacyWrapperRef.current) === null || _a === void 0 ? void 0 : _a.element;
  };
  React.useImperativeHandle(ref, () => ({
    element: getElement()
  }));
  const composedRef = (0, _ref.composeRef)(elementRef, (0, _ref.getNodeRef)(child));
  return canUseRef ? React.cloneElement(child, {
    ref: composedRef
  }) : React.createElement(LegacyWrapper, {
    ref: legacyWrapperRef
  }, child);
});