// The submitting platform ("channel") reported with a feedback message. Detected
// from the runtime environment: Telegram Mini App, the Capacitor native shell
// (iOS/Android), or a plain web browser. No new dependency — the Capacitor runtime
// global is feature-detected.

import { insideTelegram } from './telegram';

export type Channel = 'telegram' | 'ios' | 'android' | 'web';

/**
 * detectChannel picks the channel from the runtime signals: telegram wins, then a
 * native Capacitor platform (ios/android), else web. Pure, so it is unit-tested
 * without a DOM.
 */
export function detectChannel(signals: { telegram: boolean; capacitorPlatform?: string }): Channel {
  if (signals.telegram) return 'telegram';
  if (signals.capacitorPlatform === 'ios' || signals.capacitorPlatform === 'android') {
    return signals.capacitorPlatform;
  }
  return 'web';
}

/** clientChannel returns the submitting platform from the current runtime. */
export function clientChannel(): Channel {
  const capacitorPlatform =
    typeof window === 'undefined'
      ? undefined
      : (window as unknown as { Capacitor?: { getPlatform?: () => string } }).Capacitor?.getPlatform?.();
  return detectChannel({ telegram: insideTelegram(), capacitorPlatform });
}
