import { SystemBars, SystemBarsStyle } from '@capacitor/core';

export async function initializeAndroidSystemBars(): Promise<void> {
  const darkTheme = (globalThis as unknown as {
    matchMedia(query: string): { matches: boolean; addEventListener(type: 'change', listener: () => void): void };
  }).matchMedia('(prefers-color-scheme: dark)');
  const applyStyle = () => {
    const style = systemBarsStyleForDarkTheme(darkTheme.matches);
    void SystemBars.setStyle({ style }).catch((error: unknown) => {
      console.warn('Could not synchronize Android system-bar icon style', error);
    });
  };

  applyStyle();
  darkTheme.addEventListener('change', applyStyle);
}

export function systemBarsStyleForDarkTheme(dark: boolean): SystemBarsStyle {
  return dark ? SystemBarsStyle.Dark : SystemBarsStyle.Light;
}
