import { getURLSearchParams } from '@/tools';

type Lib = 'eruda' | 'vconsole';

export default function debugLoader(tag = 'debug', ignoreDevelop = true): DEV.Loader {
  return async function (app, config) {
    const debug = config.image.debug;
    if (import.meta.env.DEV && ignoreDevelop && !debug) return;

    const query = getURLSearchParams();
    let lib = query.get(tag) as Lib;
    if (!lib && debug) lib = 'eruda';
    switch (lib) {
      case 'eruda':
        const { default: eruda } = await import('eruda');
        // @ts-ignore
        const { default: erudaMonitor } = await import('eruda-monitor');
        // @ts-ignore
        const { default: erudaFeatures } = await import('eruda-features');
        // @ts-ignore
        const { default: erudaTiming } = await import('eruda-timing');
        eruda.init();
        eruda.add(erudaMonitor);
        eruda.add(erudaFeatures);
        eruda.add(erudaTiming);
        break;
      case 'vconsole':
        const { default: VConsole } = await import('vconsole');
        new VConsole();
        break;
    }
  };
}
