import { NativeModules, Platform } from 'react-native';
import { BannerSize } from './banner/BannerSize';
import type { ReactNativeTapsell } from '../ReactNativeTapsell';

const LINKING_ERROR =
  `The package '@react-native-tapsell-mediation/tapsell' doesn't seem to be linked. Make sure: \n\n` +
  Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
  '- You rebuilt the app after installing the package\n' +
  '- You are not using Expo Go\n';

const TapsellMediatorModule = NativeModules.RNTapsellMediation
  ? NativeModules.RNTapsellMediation
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

/** @internal */
class RequestCourier implements ReactNativeTapsell.RequestCourier {
  private static _instance: RequestCourier;

  public static getInstance(): RequestCourier {
    if (!RequestCourier._instance) {
      RequestCourier._instance = new RequestCourier();
    }
    return RequestCourier._instance;
  }

  setUserConsent(consent: boolean): Promise<void> {
    return TapsellMediatorModule.setUserConsent(consent);
  }

  requestRewardedAd(zoneId: string): Promise<string> {
    return TapsellMediatorModule.requestRewardedAd(zoneId);
  }

  requestInterstitialAd(zoneId: string): Promise<string> {
    return TapsellMediatorModule.requestInterstitialAd(zoneId);
  }

  requestBannerAd(zoneId: string, bannerSize: BannerSize): Promise<string> {
    return TapsellMediatorModule.requestBannerAd(zoneId, bannerSize);
  }

  requestNativeAd(zoneId: string): Promise<string> {
    return TapsellMediatorModule.requestNativeAd(zoneId);
  }

  requestMultipleNativeAd(
    zoneId: string,
    maximumCount: number
  ): Promise<string> {
    return TapsellMediatorModule.requestMultipleNativeAd(zoneId, maximumCount);
  }
}

/** @internal */
export const RequestCourierInstance = RequestCourier.getInstance();
