import type { MobileMediaAdapter } from '../../contracts/mobile.contracts';

/** Web fallback for mobile media affordances. */
export class WebMobileMediaAdapter implements MobileMediaAdapter {
  async pickAttachments(): Promise<File[]> {
    return new Promise((resolve) => {
      const input = document.createElement('input');

      input.type = 'file';
      input.multiple = true;
      input.accept = 'image/*,video/*,audio/*,.pdf,.txt,.zip,.rar,.7z,.doc,.docx,.xls,.xlsx,.ppt,.pptx';
      input.style.display = 'none';

      input.addEventListener('change', () => {
        const files = input.files ? Array.from(input.files) : [];

        input.remove();
        resolve(files);
      }, { once: true });

      document.body.appendChild(input);
      input.click();
    });
  }

  async setSpeakerphoneEnabled(_enabled: boolean): Promise<void> {
    return;
  }

  async startBackgroundAudioSession(): Promise<void> {
    return;
  }

  async stopBackgroundAudioSession(): Promise<void> {
    return;
  }

  isScreenShareSupported(): boolean {
    return typeof navigator !== 'undefined'
      && !!navigator.mediaDevices?.getDisplayMedia
      && !/iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
  }

  isPictureInPictureSupported(): boolean {
    return typeof document !== 'undefined'
      && 'pictureInPictureEnabled' in document
      && document.pictureInPictureEnabled === true;
  }
}
