import { StatusBar, Style } from '@capacitor/status-bar';
export { Style };

const _origin = (() => {
  try { if (document.referrer) return new URL(document.referrer).origin; } catch {}
  return window.location.origin;
})();

/** Set status bar icon/text colour. Use instead of StatusBar.setStyle directly. */
export async function setStatusBarStyle(style: Style): Promise<void> {
  try { await StatusBar.setStyle({ style }); } catch {}
  if (window.parent !== window) {
    window.parent.postMessage({ type: '__apsuite_statusbar', style }, _origin);
  }
}

/** Set status bar background colour (Android). Use instead of StatusBar.setBackgroundColor directly. */
export async function setStatusBarBackground(color: string): Promise<void> {
  try { await (StatusBar as any).setBackgroundColor({ color }); } catch {}
  if (window.parent !== window) {
    window.parent.postMessage({ type: '__apsuite_statusbar_bg', color }, _origin);
  }
}
