import C from "./index-extends.js";
import m from "./index-asyncToGenerator.js";
import u from "./index-defineProperty.js";
import y, { forwardRef as D, Component as S } from "react";
import x from "./index-Process.js";
import k from "./index-Fields.js";
function g(n, t) {
  var e = Object.keys(n);
  if (Object.getOwnPropertySymbols) {
    var r = Object.getOwnPropertySymbols(n);
    t && (r = r.filter(function(o) {
      return Object.getOwnPropertyDescriptor(n, o).enumerable;
    })), e.push.apply(e, r);
  }
  return e;
}
function q(n) {
  for (var t = 1; t < arguments.length; t++) {
    var e = arguments[t] != null ? arguments[t] : {};
    t % 2 ? g(e, !0).forEach(function(r) {
      u(n, r, e[r]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(n, Object.getOwnPropertyDescriptors(e)) : g(e).forEach(function(r) {
      Object.defineProperty(n, r, Object.getOwnPropertyDescriptor(e, r));
    });
  }
  return n;
}
class O extends S {
  constructor(...t) {
    var e;
    super(...t), e = this, u(
      this,
      "handleClick",
      /* @__PURE__ */ (function() {
        var r = m(function* (o) {
          o.preventDefault();
          const {
            handleProcess: i,
            onCompleted: p,
            onError: l,
            onSuccess: P
          } = e.props;
          try {
            yield i(
              /* @__PURE__ */ (function() {
                var c = m(function* (a) {
                  const {
                    scope: _,
                    fields: v,
                    returnScopes: w,
                    rerequest: b,
                    reauthorize: j,
                    eventKey: E
                  } = e.props, f = {
                    scope: _
                  }, s = [];
                  w && (f.return_scopes = !0), b && s.push("rerequest"), j && s.push("reauthenticate"), s.length && (f.auth_type = s.join(","));
                  const d = yield a.login(f);
                  if (d.status !== "connected")
                    throw new Error("Unauthorized user");
                  const h = yield a.getTokenDetailWithProfile({
                    fields: v
                  }, d);
                  return p && (yield p(q({}, h, {
                    eventKey: E
                  }))), h;
                });
                return function(a) {
                  return c.apply(this, arguments);
                };
              })(),
              P
            );
          } catch (c) {
            l && l(c);
          }
        });
        return function(o) {
          return r.apply(this, arguments);
        };
      })()
    );
  }
  render() {
    const {
      children: t,
      loading: e,
      error: r,
      data: o
    } = this.props;
    return t({
      loading: e,
      handleClick: this.handleClick,
      error: r,
      data: o
    });
  }
}
u(O, "defaultProps", {
  scope: "",
  fields: k,
  returnScopes: !1,
  rerequest: !1,
  reauthorize: !1,
  onCompleted: void 0,
  onError: void 0,
  eventKey: void 0
});
const G = D((n, t) => y.createElement(x, null, ({
  loading: e,
  handleProcess: r,
  data: o,
  error: i
}) => y.createElement(O, C({}, n, {
  loading: e,
  handleProcess: r,
  data: o,
  error: i,
  ref: t
}))));
export {
  G as default
};
