"use strict";

import ATReactNativeBridge from "../specs/NativeATBridge.js";
import { ATLog } from "../utils/ATLog.js";
import { ATAdEvents } from "../events/ATAdEvents.js";
import { InterstitialCallback } from "../constants/Const.js";
import { toAdError } from "../types/index.js";

/**
 * 插屏监听器。无 reward/again，有 show + 视频播放回调。
 */

function asError(message) {
  return toAdError(message) ?? {
    code: '',
    desc: '',
    fullErrorInfo: message ?? ''
  };
}

/**
 * 插屏广告（一 placementId 一实例）。
 * 实例内按 callbackName 把 AdSource/MultipleLoaded 分流到对应 listener。
 */
export class ATInterstitialAd {
  registered = false;
  constructor(placementId) {
    this.placementId = placementId;
  }
  ensureRegistered() {
    if (this.registered) {
      return;
    }
    ATAdEvents.setAdListener(this.placementId, p => this.route(p));
    this.registered = true;
  }
  route(p) {
    if (p.callbackName.indexOf('AdSource') >= 0) {
      this.adSourceListener?.(p);
      return;
    }
    if (p.callbackName === InterstitialCallback.multipleLoaded) {
      this.multipleLoadedListener?.(p.extraDic ?? {});
      return;
    }
    if (this.mainListener) {
      dispatchInterstitialEvent(this.mainListener, p);
    }
  }
  load(adRequest) {
    ATLog.call('ATInterstitialAd', 'load', this.placementId, adRequest);
    const extraDic = adRequest ? {
      atAdRequest: adRequest
    } : {};
    ATReactNativeBridge.loadInterstitial(this.placementId, extraDic);
  }

  /** 展示（失败走 Event）。scenarioOrConfig 为 string 时走 scenario；为对象时走 showConfig。 */
  show(scenarioOrConfig) {
    ATLog.call('ATInterstitialAd', 'show', this.placementId, scenarioOrConfig);
    if (typeof scenarioOrConfig === 'string') {
      ATReactNativeBridge.showInterstitial(this.placementId, scenarioOrConfig).catch(() => {});
      return;
    }
    if (scenarioOrConfig) {
      ATReactNativeBridge.showInterstitialWithConfig(this.placementId, scenarioOrConfig).catch(() => {});
    } else {
      ATReactNativeBridge.showInterstitial(this.placementId, '').catch(() => {});
    }
  }
  isAdReady() {
    ATLog.call('ATInterstitialAd', 'isAdReady');
    return ATReactNativeBridge.interstitialReady(this.placementId);
  }
  checkAdStatus() {
    ATLog.call('ATInterstitialAd', 'checkAdStatus');
    return ATReactNativeBridge.checkInterstitialLoadStatus(this.placementId);
  }
  checkValidAdCaches() {
    ATLog.call('ATInterstitialAd', 'checkValidAdCaches');
    return ATReactNativeBridge.getInterstitialValidAds(this.placementId);
  }
  static entryAdScenario(placementId, scenarioId, tkExtra) {
    ATLog.call('ATInterstitialAd', 'entryAdScenario', placementId, scenarioId);
    ATReactNativeBridge.entryInterstitialScenario(placementId, scenarioId, tkExtra ?? {});
  }
  setLocalExtra(map) {
    ATLog.call('ATInterstitialAd', 'setLocalExtra');
    ATReactNativeBridge.setInterstitialLocalExtra(this.placementId, map);
  }
  setTKExtra(map) {
    ATLog.call('ATInterstitialAd', 'setTKExtra');
    ATReactNativeBridge.setInterstitialTKExtra(this.placementId, map);
  }
  setAdListener(listener) {
    this.mainListener = listener;
    this.ensureRegistered();
  }
  setAdRevenueListener(listener) {
    ATAdEvents.setRevenueListener(this.placementId, listener);
  }
  setAdMultipleLoadedListener(listener) {
    this.multipleLoadedListener = listener;
    this.ensureRegistered();
  }
  setAdSourceStatusListener(listener) {
    this.adSourceListener = listener;
    this.ensureRegistered();
  }
  removeAdListener() {
    ATAdEvents.removeAdListener(this.placementId);
    ATAdEvents.removeRevenueListener(this.placementId);
    this.registered = false;
  }
}
export function dispatchInterstitialEvent(l, p) {
  const info = p.extraDic ?? {};
  switch (p.callbackName) {
    case InterstitialCallback.loaded:
      l.onInterstitialAdLoaded?.();
      break;
    case InterstitialCallback.loadFail:
      l.onInterstitialAdLoadFail?.(asError(p.requestMessage));
      break;
    case InterstitialCallback.show:
      l.onInterstitialAdShow?.(info);
      break;
    case InterstitialCallback.showFail:
      l.onInterstitialAdShowFailed?.(asError(p.requestMessage));
      break;
    case InterstitialCallback.close:
      l.onInterstitialAdClose?.(info);
      break;
    case InterstitialCallback.click:
      l.onInterstitialAdClicked?.(info);
      break;
    case InterstitialCallback.playStart:
      l.onInterstitialAdVideoStart?.(info);
      break;
    case InterstitialCallback.playEnd:
      l.onInterstitialAdVideoEnd?.(info);
      break;
    case InterstitialCallback.playFail:
      l.onInterstitialAdVideoError?.(asError(p.requestMessage));
      break;
    case InterstitialCallback.deeplink:
      l.onDeeplinkCallback?.(info, p.isDeeplinkSuccess ?? false);
      break;
    case InterstitialCallback.downloadConfirm:
      l.onDownloadConfirm?.(info);
      break;
    default:
      break;
  }
}
//# sourceMappingURL=ATInterstitialAd.js.map