import { Component, h, Prop } from '@stencil/core';
import { injectJS, injectCSS } from '@deckdeckgo/editor';
import firebase from '@firebase/app';
import '@firebase/auth';
export class FirebaseSignIn {
  constructor() {
    this.signInSuccessWithAuthResult = (userCred, _redirectUrl) => {
      var _a;
      const githubAccessToken = this.getGithubAccessToken(userCred);
      this.signInSuccess({ uid: (_a = userCred === null || userCred === void 0 ? void 0 : userCred.user) === null || _a === void 0 ? void 0 : _a.uid, githubAccessToken });
      return false;
    };
  }
  async componentDidLoad() {
    await this.setupFirebaseUI();
  }
  async disconnectedCallback() {
    const ui = firebaseui.auth.AuthUI.getInstance();
    if (ui) {
      await ui.delete();
    }
  }
  async setupFirebaseUI() {
    await injectJS({
      id: 'firebase-ui-script',
      src: 'https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.js'
    });
    await injectCSS({
      id: 'firebase-ui-css',
      src: 'https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.css'
    });
    const signInOptions = [];
    // GitHub scope
    signInOptions.push({
      provider: firebase.auth.GithubAuthProvider.PROVIDER_ID,
      scopes: ['public_repo']
    });
    signInOptions.push(firebase.auth.GoogleAuthProvider.PROVIDER_ID);
    signInOptions.push(firebase.auth.EmailAuthProvider.PROVIDER_ID);
    const uiConfig = {
      signInFlow: 'redirect',
      signInSuccessUrl: this.config.appUrl,
      signInOptions: signInOptions,
      // tosUrl and privacyPolicyUrl accept either url string or a callback
      // function.
      // Terms of service url/callback.
      tosUrl: 'https://deckdeckgo.com/terms',
      // Privacy policy url/callback.
      privacyPolicyUrl: 'https://deckdeckgo.com/privacy',
      credentialHelper: firebaseui.auth.CredentialHelper.GOOGLE_YOLO,
      autoUpgradeAnonymousUsers: false,
      callbacks: {
        signInSuccessWithAuthResult: this.signInSuccessWithAuthResult
      }
    };
    // @ts-ignore
    window['firebase'] = firebase;
    const ui = firebaseui.auth.AuthUI.getInstance() || new firebaseui.auth.AuthUI(firebase.auth());
    if (!ui.isPendingRedirect()) {
      ui.reset();
    }
    // The start method will wait until the DOM is loaded.
    ui.start('#firebaseui-auth-container', uiConfig);
  }
  getGithubAccessToken(userCred) {
    if (!userCred) {
      return undefined;
    }
    if (!userCred.credential || userCred.credential.providerId !== 'github.com' || !userCred.credential.accessToken) {
      return undefined;
    }
    return userCred.credential.accessToken;
  }
  render() {
    return h("div", { id: "firebaseui-auth-container" });
  }
  static get is() { return "deckgo-firebase-signin"; }
  static get originalStyleUrls() { return {
    "$": ["firebase-signin.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["firebase-signin.css"]
  }; }
  static get properties() { return {
    "config": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Record<string, string>",
        "resolved": "{ [x: string]: string; }",
        "references": {
          "Record": {
            "location": "global"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "signInSuccess": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "(credentials: {uid: string | undefined; githubAccessToken: string | undefined} | undefined) => void",
        "resolved": "(credentials: { uid: string; githubAccessToken: string; }) => void",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
}
