import { HTMLElement, h, proxyCustomElement } from '@stencil/core/internal/client';
import { f as firebase } from './chunk-J3IR2QPV.js';

var i=({id:r,src:o,module:d})=>new Promise((n,e)=>{if(document?.getElementById(r)){n("JS already loaded.");return}let t=document.createElement("script");t.id=r,t.src=o,d?t.type="module":(t.async=!0,t.defer=!0),t.addEventListener("load",()=>n("JS loaded."),{once:!0}),t.addEventListener("error",()=>e("Error loading script."),{once:!0}),t.addEventListener("abort",()=>e("Script loading aborted."),{once:!0}),document.head.appendChild(t);}),s=({id:r,src:o})=>new Promise((d,n)=>{if(document?.getElementById(r)){d("CSS already loaded.");return}let e=document.createElement("link");e.id=r,e.setAttribute("rel","stylesheet"),e.setAttribute("href",o),e.addEventListener("load",()=>d("CSS loaded."),{once:!0}),e.addEventListener("error",()=>n("Error loading css."),{once:!0}),e.addEventListener("abort",()=>n("CSS loading aborted."),{once:!0}),document.head.appendChild(e);});

const firebaseSigninCss = "deckgo-firebase-signin{display:block}deckgo-firebase-signin .firebaseui-container{background:transparent}deckgo-firebase-signin form button.firebaseui-idp-password,deckgo-firebase-signin form button.firebaseui-idp-password:hover{background-color:var(--ion-color-primary) !important}deckgo-firebase-signin .mdl-textfield--floating-label.is-focused .mdl-textfield__label{color:var(--ion-color-medium)}deckgo-firebase-signin .firebaseui-textfield.mdl-textfield .firebaseui-label::after{background-color:var(--ion-color-primary)}deckgo-firebase-signin .mdl-button--primary.mdl-button--primary{color:var(--ion-color-primary)}deckgo-firebase-signin .mdl-button--primary.mdl-button--primary:hover,deckgo-firebase-signin .mdl-button--primary.mdl-button--primary:active{background:transparent}deckgo-firebase-signin .mdl-button--raised.mdl-button--colored{background:var(--ion-color-primary);border-radius:64px}deckgo-firebase-signin .mdl-button--raised.mdl-button--colored:hover,deckgo-firebase-signin .mdl-button--raised.mdl-button--colored:active{background:var(--ion-color-primary)}deckgo-firebase-signin button.firebaseui-idp-google>.firebaseui-idp-text{color:black}deckgo-firebase-signin .firebaseui-idp-button{font-family:var(--ion-font-family, inherit);border-radius:64px;padding:8px 16px 8px 30px}deckgo-firebase-signin p.firebaseui-tos{color:black;font-family:var(--ion-font-family, inherit);padding-top:16px}deckgo-firebase-signin p.firebaseui-tos a,deckgo-firebase-signin p.firebaseui-tos a:hover,deckgo-firebase-signin p.firebaseui-tos a:active,deckgo-firebase-signin p.firebaseui-tos a:focus{text-decoration:none}deckgo-firebase-signin a.firebaseui-link{text-decoration:none;color:var(--ion-color-primary)}deckgo-firebase-signin div.mdl-shadow--2dp{box-shadow:none}deckgo-firebase-signin div.mdl-shadow--2dp div.firebaseui-card-header{display:none}";

let FirebaseSignIn = class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    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 i({
      id: 'firebase-ui-script',
      src: 'https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.js'
    });
    await s({
      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 style() { return firebaseSigninCss; }
};
FirebaseSignIn = /*@__PURE__*/ proxyCustomElement(FirebaseSignIn, [0, "deckgo-firebase-signin", {
    "config": [16],
    "signInSuccess": [16]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["deckgo-firebase-signin"];
  components.forEach(tagName => { switch (tagName) {
    case "deckgo-firebase-signin":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, FirebaseSignIn);
      }
      break;
  } });
}

const DeckgoFirebaseSignin = FirebaseSignIn;
const defineCustomElement = defineCustomElement$1;

export { DeckgoFirebaseSignin, defineCustomElement };
