import o from "./index-asyncToGenerator.js";
import i from "./index-defineProperty.js";
import a, { Component as f } from "react";
import d from "./index-Initialize.js";
import l from "./index-Subscribe.js";
import u from "./index-Fields.js";
import p from "./index-LoginStatus.js";
class h extends f {
  constructor(...t) {
    var e;
    super(...t), e = this, i(this, "state", {
      loading: !0
    }), i(
      this,
      "handleReady",
      /* @__PURE__ */ (function() {
        var n = o(function* (r) {
          e.api = r, e.updateProfile();
        });
        return function(r) {
          return n.apply(this, arguments);
        };
      })()
    ), i(this, "handleStatusChange", () => {
      this.updateProfile();
    });
  }
  updateProfile() {
    var t = this;
    return o(function* () {
      const {
        api: e,
        props: {
          fields: n
        }
      } = t;
      if (e)
        try {
          if ((yield e.getLoginStatus()).status !== p.CONNECTED) {
            t.setState({
              profile: void 0,
              loading: !1,
              error: void 0
            });
            return;
          }
          const s = yield e.getProfile({
            fields: n
          });
          t.setState({
            profile: s,
            loading: !1,
            error: void 0
          });
        } catch (r) {
          t.setState({
            profile: void 0,
            loading: !1,
            error: r
          });
        }
    })();
  }
  render() {
    const {
      children: t
    } = this.props, {
      profile: e,
      loading: n,
      error: r
    } = this.state;
    return a.createElement(d, {
      onReady: this.handleReady
    }, a.createElement(l, {
      event: "auth.statusChange",
      onChange: this.handleStatusChange
    }, t({
      profile: e,
      loading: n,
      error: r
    })));
  }
}
i(h, "defaultProps", {
  fields: u
});
