let initialized = false;
let initializing: Promise<void> | null = null;

async function setupAdminRuntime() {
  const [{ initComponentAdapter }, formModule, { setupZqTable }] =
    await Promise.all([
      import('#/adapter/component'),
      import('#/adapter/form'),
      import('#/components/zq-table/init'),
    ]);

  await initComponentAdapter();
  await formModule.initSetupVbenForm();
  setupZqTable({
    useVbenForm: formModule.useVbenForm,
  });

  initialized = true;
}

async function ensureAdminRuntime() {
  if (initialized) return;

  initializing ??= setupAdminRuntime().finally(() => {
    initializing = null;
  });

  await initializing;
}

export { ensureAdminRuntime };
