// Capacitor bootstrap — no-ops on the web, configures native shell on mobile.
// Plugins are dynamic-imported so the web bundle isn't penalized.

let initStarted = false;

export function isNativeApp() {
  return typeof window !== 'undefined' && Boolean(window.Capacitor?.isNativePlatform?.());
}

export async function initNativeShell() {
  if (initStarted || !isNativeApp()) return;
  initStarted = true;

  try {
    const { StatusBar, Style } = await import('@capacitor/status-bar');
    await StatusBar.setBackgroundColor({ color: '#27633a' }).catch(() => {});
    await StatusBar.setStyle({ style: Style.Light }).catch(() => {});
  } catch {
    /* plugin not present — fine on web */
  }

  try {
    const { SplashScreen } = await import('@capacitor/splash-screen');
    await SplashScreen.hide({ fadeOutDuration: 250 }).catch(() => {});
  } catch {
    /* plugin not present — fine on web */
  }

  try {
    const { App } = await import('@capacitor/app');
    App.addListener('backButton', ({ canGoBack }) => {
      if (canGoBack) window.history.back();
      else App.exitApp();
    });
  } catch {
    /* plugin not present — fine on web */
  }
}
