import { __awaiter as y } from "../../../tslib/tslib.es6.mjs";
import a, { useState as m, useRef as R, useEffect as E, useImperativeHandle as w } from "react";
import { renderToBody as T } from "./render-to-body.mjs";
function I(i) {
  const b = a.forwardRef((l, r) => {
    const [n, f] = m(!1), d = R(!1), [s, C] = m(i), p = R(0);
    E(() => {
      d.current ? _() : f(!0);
    }, []);
    function v() {
      var o, e;
      d.current = !0, f(!1), (e = (o = s.props).onClose) === null || e === void 0 || e.call(o);
    }
    function _() {
      var o, e;
      u(), (e = (o = s.props).afterClose) === null || e === void 0 || e.call(o);
    }
    return w(r, () => ({
      close: v,
      replace: (o) => {
        var e, c;
        p.current++, (c = (e = s.props).afterClose) === null || c === void 0 || c.call(e), C(o);
      }
    })), a.cloneElement(s, Object.assign(Object.assign({}, s.props), {
      key: p.current,
      visible: n,
      onClose: v,
      afterClose: _
    }));
  }), t = a.createRef(), u = T(a.createElement(b, {
    ref: t
  }));
  return {
    close: () => y(this, void 0, void 0, function* () {
      var l, r, n;
      t.current ? (n = t.current) === null || n === void 0 || n.close() : (u(), (r = (l = i.props).afterClose) === null || r === void 0 || r.call(l));
    }),
    replace: (l) => {
      var r;
      (r = t.current) === null || r === void 0 || r.replace(l);
    },
    isRendered: () => !!t.current
  };
}
export {
  I as renderImperatively
};
