import type { Component } from 'vue';
import { createApp } from 'vue';

import { Toast } from '@/platform/toast.ui';
import { getApplicationMethod } from '@/request/api';

import router from '@/router';
import pinia from '@/stores';

export default async function Loader(component: Component, ...loader: DEV.Loader[]) {
  const app = createApp(component);
  app.use(router);
  app.use(pinia);

  const toast = Toast.loading(500);

  try {
    const config = await getApplicationMethod().send(true);
    for (const fn of loader) await fn(app, config);
  } catch (error) {
    toast.close();
    throw error;
  }
  toast.close();
  return app;
}

export { default as launchLoader } from './launch.loader';
export { default as bridgeLoader } from './bridge.loader';
export { default as debugLoader } from './debug.loader';
