window.debug = {};

import { h, render } from 'preact';
import App from './components/App';

// import "mini.css/src/flavors/mini-dark.scss"
import "mini.css/src/flavors/mini-default.scss"
import "uikit";
import "uikit/dist/css/uikit.css"
import "./index.scss"
import Navigation from './navigation';
import VaultsPage from './components/routes/vaults/Vaults';
import { Page } from './page';

import Notes from "./notes"
import DemoPage from './components/demo';
import VaultPage from './components/routes/vault/Vault';
import SharePage from './components/routes/share/Share';
import Notifications from './notifications';
console.log(Notes);

(async () => {
   // Initialize notes provider
   if (Notes.loginRequired()) {
      let url = new URL(location.href)
      let code = url.searchParams.get("code");

      if (code) {
         let err = await Notes.getToken(code)
         if (err) {
            Notifications.sendError("Login failed: " + err)
            Notes.login()
         } else {
            window.history.replaceState(null, document.title, "/" + window.location.hash);
         }
      } else {
         Notes.login()
      }
   }

   await Notes.start();

   Navigation.default = VaultsPage as typeof Page;
   Navigation.addPage("/vault", VaultPage as typeof Page)
   Navigation.addPage("/demo", DemoPage as typeof Page)
   Navigation.addPage("/share", SharePage as typeof Page)
   Navigation.start();

   render(<App />, document.body, document.getElementById('app'));
})()
