import p from "./index-_virtual8.js";
import k, { useState as I, useRef as S, useEffect as W } from "react";
import { canUseDOM as T, useShallowCompareMemoize as m, removeChildrenWithAttribute as C, twLoad as M, cloneShallow as h, twWidgetFactory as R } from "./index-utils5.js";
function b(f, t, o, i, s, u, n) {
  try {
    var r = f[u](n), a = r.value;
  } catch (c) {
    o(c);
    return;
  }
  r.done ? t(a) : Promise.resolve(a).then(i, s);
}
function A(f) {
  return function() {
    var t = this, o = arguments;
    return new Promise(function(i, s) {
      var u = f.apply(t, o);
      function n(a) {
        b(u, i, s, n, r, "next", a);
      }
      function r(a) {
        b(u, i, s, n, r, "throw", a);
      }
      n(void 0);
    });
  };
}
T && M();
var g = "twdiv";
function D(f, t, o, i) {
  var s = I(null), u = s[0], n = s[1], r = S(null);
  if (!T)
    return {
      ref: r,
      error: u
    };
  var a = [f, m(t), m(o)];
  return W(function() {
    n(null);
    var c = !1;
    if (r.current) {
      var E = /* @__PURE__ */ (function() {
        var y = A(/* @__PURE__ */ p.mark(function d() {
          var l, w, v;
          return p.wrap(function(e) {
            for (; ; )
              switch (e.prev = e.next) {
                case 0:
                  if (!(!r || !r.current)) {
                    e.next = 2;
                    break;
                  }
                  return e.abrupt("return");
                case 2:
                  return l = document.createElement("div"), l.setAttribute(g, "yes"), r.current.appendChild(l), e.prev = 5, e.next = 8, R();
                case 8:
                  return w = e.sent, e.next = 11, w[f](h(t), l, h(o));
                case 11:
                  if (v = e.sent, !(!v && !c)) {
                    e.next = 14;
                    break;
                  }
                  throw new Error("Twitter could not create widget. If it is a Timeline or Tweet, ensure the screenName/tweetId exists.");
                case 14:
                  e.next = 21;
                  break;
                case 16:
                  return e.prev = 16, e.t0 = e.catch(5), n(e.t0), e.abrupt("return");
                case 21:
                  if (!(!r || !r.current)) {
                    e.next = 23;
                    break;
                  }
                  return e.abrupt("return");
                case 23:
                  if (!c) {
                    e.next = 26;
                    break;
                  }
                  return l && l.remove(), e.abrupt("return");
                case 26:
                  i && i();
                case 27:
                case "end":
                  return e.stop();
              }
          }, d, null, [[5, 16]]);
        }));
        return function() {
          return y.apply(this, arguments);
        };
      })();
      C(r.current, g), E();
    }
    return function() {
      c = !0;
    };
  }, a), {
    ref: r,
    error: u
  };
}
var O = function(t) {
  var o = t.tweetId, i = t.options, s = t.onLoad, u = t.renderError, n = D("createTweet", o, i, s), r = n.ref, a = n.error;
  return /* @__PURE__ */ k.createElement("div", {
    ref: r
  }, a && u && u(a));
};
export {
  O as Tweet
};
