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

/** Document Picture-in-Picture API adapter for supported browsers. */
export class WebMobilePictureInPictureAdapter implements MobilePictureInPictureAdapter {
  isSupported(): boolean {
    return typeof document !== 'undefined'
      && 'pictureInPictureEnabled' in document
      && document.pictureInPictureEnabled === true;
  }

  async enter(videoElement: HTMLVideoElement): Promise<void> {
    if (!this.isSupported() || document.pictureInPictureElement) {
      return;
    }

    await videoElement.requestPictureInPicture();
  }

  async exit(): Promise<void> {
    if (!document.pictureInPictureElement) {
      return;
    }

    await document.exitPictureInPicture();
  }
}
