import type { MobileCallKitAdapter } from '../../contracts/mobile.contracts';
import { loadMetoyouMobilePlugin } from './metoyou-mobile.plugin';

/** iOS CallKit bridge via the MetoyouMobile native plugin. */
export class CapacitorMobileCallKitAdapter implements MobileCallKitAdapter {
  async startActiveCall(callId: string, displayName: string): Promise<void> {
    const MetoyouMobile = await loadMetoyouMobilePlugin();

    if (!MetoyouMobile) {
      return;
    }

    try {
      const result = await MetoyouMobile.startCallKitSession({ callId, displayName });

      if (!result.supported) {
        console.info('[mobile] CallKit is unavailable on this iOS build');
      }
    } catch (error) {
      console.info('[mobile] CallKit start skipped', error);
    }
  }

  async endActiveCall(callId: string): Promise<void> {
    const MetoyouMobile = await loadMetoyouMobilePlugin();

    if (!MetoyouMobile) {
      return;
    }

    try {
      await MetoyouMobile.endCallKitSession({ callId });
    } catch (error) {
      console.info('[mobile] CallKit end skipped', error);
    }
  }
}
