import v from "./index-objectWithoutProperties.js";
import n from "./index-asyncToGenerator.js";
import y from "./index-defineProperty.js";
import l from "./index-LoginStatus.js";
function m(o, t) {
  var e = Object.keys(o);
  if (Object.getOwnPropertySymbols) {
    var r = Object.getOwnPropertySymbols(o);
    t && (r = r.filter(function(s) {
      return Object.getOwnPropertyDescriptor(o, s).enumerable;
    })), e.push.apply(e, r);
  }
  return e;
}
function d(o) {
  for (var t = 1; t < arguments.length; t++) {
    var e = arguments[t] != null ? arguments[t] : {};
    t % 2 ? m(e, !0).forEach(function(r) {
      y(o, r, e[r]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(o, Object.getOwnPropertyDescriptors(e)) : m(e).forEach(function(r) {
      Object.defineProperty(o, r, Object.getOwnPropertyDescriptor(e, r));
    });
  }
  return o;
}
const p = {
  GET: "get",
  POST: "post",
  DELETE: "delete"
};
let k = class {
  constructor(t = {}) {
    if (this.options = d({
      domain: "connect.facebook.net",
      version: "v3.2",
      cookie: !1,
      status: !1,
      xfbml: !1,
      language: "en_US",
      frictionlessRequests: !1,
      debug: !1,
      chatSupport: !1
    }, t), !this.options.appId)
      throw new Error("You need to set appId");
    this.options.wait || this.init();
  }
  getAppId() {
    return this.options.appId;
  }
  init() {
    var t = this;
    return n(function* () {
      return t.loadingPromise || (t.loadingPromise = new Promise((e) => {
        const r = t.options, {
          domain: s,
          language: c,
          debug: f,
          chatSupport: u
        } = r, i = v(r, ["domain", "language", "debug", "chatSupport"]);
        if (window.fbAsyncInit = () => {
          window.FB.init({
            appId: i.appId,
            version: i.version,
            cookie: i.cookie,
            status: i.status,
            xfbml: i.xfbml,
            frictionlessRequests: t.frictionlessRequests
          }), e(window.FB);
        }, window.document.getElementById("facebook-jssdk"))
          return e(window.FB);
        const a = window.document.createElement("script");
        a.id = "facebook-jssdk", a.async = !0, a.defer = !0, a.src = `https://${s}/${c}/sdk${u ? "/xfbml.customerchat" : ""}${f ? "/debug" : ""}.js`, window.document.body.appendChild(a);
      })), t.loadingPromise;
    })();
  }
  process(t, e = [], r = []) {
    var s = this;
    return n(function* () {
      const c = yield s.init();
      return new Promise((f, u) => {
        c[t](...e, (i) => {
          if (i)
            if (i.error) {
              const {
                code: a,
                type: g,
                message: b
              } = i.error, h = new Error(b);
              h.code = a, h.type = g, u(h);
            } else
              f(i);
          else {
            if (t === "ui") return;
            u(new Error("Response is undefined"));
          }
        }, ...r);
      });
    })();
  }
  ui(t) {
    var e = this;
    return n(function* () {
      return e.process("ui", [t]);
    })();
  }
  api(t, e = p.GET, r = {}) {
    var s = this;
    return n(function* () {
      return s.process("api", [t, e, r]);
    })();
  }
  login(t = null) {
    var e = this;
    return n(function* () {
      return e.process("login", [], [t]);
    })();
  }
  logout() {
    var t = this;
    return n(function* () {
      return t.process("logout");
    })();
  }
  getLoginStatus() {
    var t = this;
    return n(function* () {
      return t.process("getLoginStatus");
    })();
  }
  getAuthResponse() {
    var t = this;
    return n(function* () {
      return t.process("getAuthResponse");
    })();
  }
  getTokenDetail(t) {
    var e = this;
    return n(function* () {
      if (t.status === l.CONNECTED && t.authResponse)
        return t.authResponse;
      const r = yield e.getLoginStatus();
      if (r.status === l.CONNECTED && r.authResponse)
        return r.authResponse;
      throw new Error("Token is undefined");
    })();
  }
  getProfile(t) {
    var e = this;
    return n(function* () {
      return e.api("/me", p.GET, t);
    })();
  }
  getTokenDetailWithProfile(t, e) {
    var r = this;
    return n(function* () {
      const s = yield r.getTokenDetail(e);
      return {
        profile: yield r.getProfile(t),
        tokenDetail: s
      };
    })();
  }
  getToken() {
    var t = this;
    return n(function* () {
      return (yield t.getTokenDetail()).accessToken;
    })();
  }
  getUserId() {
    var t = this;
    return n(function* () {
      return (yield t.getTokenDetail()).userID;
    })();
  }
  sendInvite(t, e) {
    var r = this;
    return n(function* () {
      return r.ui(d({
        to: t,
        method: "apprequests"
      }, e));
    })();
  }
  postAction(t, e, r, s, c) {
    var f = this;
    return n(function* () {
      let u = `/me/${t}:${e}?${r}=${encodeURIComponent(s)}`;
      return c === !0 && (u += "&no_feed_story=true"), f.api(u, p.POST);
    })();
  }
  getPermissions() {
    var t = this;
    return n(function* () {
      return (yield t.api("/me/permissions")).data;
    })();
  }
  hasPermissions(t) {
    var e = this;
    return n(function* () {
      const r = yield e.getPermissions();
      return t.filter((c) => !!r.find((u) => {
        const {
          permission: i,
          status: a
        } = u;
        return a === "granted" && i === c;
      })).length === t.length;
    })();
  }
  subscribe(t, e) {
    var r = this;
    return n(function* () {
      (yield r.init()).Event.subscribe(t, e);
    })();
  }
  unsubscribe(t, e) {
    var r = this;
    return n(function* () {
      (yield r.init()).Event.unsubscribe(t, e);
    })();
  }
  parse(t) {
    var e = this;
    return n(function* () {
      const r = yield e.init();
      typeof t > "u" ? r.XFBML.parse() : r.XFBML.parse(t);
    })();
  }
  getRequests() {
    var t = this;
    return n(function* () {
      return t.api("/me/apprequests");
    })();
  }
  removeRequest(t) {
    var e = this;
    return n(function* () {
      return e.api(t, p.DELETE);
    })();
  }
  setAutoGrow() {
    var t = this;
    return n(function* () {
      (yield t.init()).Canvas.setAutoGrow();
    })();
  }
  paySimple(t, e = 1) {
    var r = this;
    return n(function* () {
      return r.ui({
        method: "pay",
        action: "purchaseitem",
        product: t,
        quantity: e
      });
    })();
  }
  pay(t, e) {
    var r = this;
    return n(function* () {
      return r.ui(d({
        method: "pay",
        action: "purchaseitem",
        product: t
      }, e));
    })();
  }
};
export {
  p as Method,
  k as default
};
