"use strict";

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

/**
 * 激励监听器（含主流程与各广告源的回调）。
 * load/show 失败经此上报（不走 Promise.reject）。
 */

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

/**
 * 激励视频广告（一 placementId 一实例）。
 * 回调经唯一 ATReactNativeEventEmitter → ATAdEvents 按 placementID 分发到此实例；
 * 实例内按 callbackName 把 AdSource/MultipleLoaded 分流到对应 listener（ATAdEvents 不增机制）。
 */
export class ATRewardVideoAd {
  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 === RewardVideoCallback.multipleLoaded) {
      this.multipleLoadedListener?.(p.extraDic ?? {});
      return;
    }
    if (this.mainListener) {
      dispatchRewardEvent(this.mainListener, p);
    }
  }

  /** 加载（adRequest 可含 atAdRequest 底价/渠道；失败走 onRewardedVideoAdFailed）。 */
  load(adRequest) {
    ATLog.call('ATRewardVideoAd', 'load', this.placementId, adRequest);
    const extraDic = adRequest ? {
      atAdRequest: adRequest
    } : {};
    ATReactNativeBridge.loadRewardedVideo(this.placementId, extraDic);
  }

  /** 展示（失败走 Event）。scenarioOrConfig 为 string 时走 scenario 参数；为对象时走 showConfig。 */
  show(scenarioOrConfig) {
    ATLog.call('ATRewardVideoAd', 'show', this.placementId, scenarioOrConfig);
    if (typeof scenarioOrConfig === 'string') {
      ATReactNativeBridge.showRewardedVideo(this.placementId, scenarioOrConfig).catch(() => {});
      return;
    }
    if (scenarioOrConfig) {
      ATReactNativeBridge.showRewardedVideoWithConfig(this.placementId, scenarioOrConfig).catch(() => {});
    } else {
      ATReactNativeBridge.showRewardedVideo(this.placementId, '').catch(() => {});
    }
  }
  isAdReady() {
    ATLog.call('ATRewardVideoAd', 'isAdReady');
    return ATReactNativeBridge.rewardedVideoReady(this.placementId);
  }
  checkAdStatus() {
    ATLog.call('ATRewardVideoAd', 'checkAdStatus');
    return ATReactNativeBridge.checkRewardedVideoLoadStatus(this.placementId);
  }
  checkValidAdCaches() {
    ATLog.call('ATRewardVideoAd', 'checkValidAdCaches');
    return ATReactNativeBridge.getRewardedVideoValidAds(this.placementId);
  }
  static entryAdScenario(placementId, scenarioId, tkExtra) {
    ATLog.call('ATRewardVideoAd', 'entryAdScenario', placementId, scenarioId);
    ATReactNativeBridge.entryRewardedVideoScenario(placementId, scenarioId, tkExtra ?? {});
  }
  setLocalExtra(map) {
    ATLog.call('ATRewardVideoAd', 'setLocalExtra');
    ATReactNativeBridge.setRewardedVideoLocalExtra(this.placementId, map);
  }
  setTKExtra(map) {
    ATLog.call('ATRewardVideoAd', 'setTKExtra');
    ATReactNativeBridge.setRewardedVideoTKExtra(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 dispatchRewardEvent(l, p) {
  const info = p.extraDic ?? {};
  switch (p.callbackName) {
    case RewardVideoCallback.loaded:
      l.onRewardedVideoAdLoaded?.();
      break;
    case RewardVideoCallback.loadFail:
      l.onRewardedVideoAdFailed?.(asError(p.requestMessage));
      break;
    case RewardVideoCallback.playStart:
      l.onRewardedVideoAdPlayStart?.(info);
      break;
    case RewardVideoCallback.playEnd:
      l.onRewardedVideoAdPlayEnd?.(info);
      break;
    case RewardVideoCallback.playFail:
      l.onRewardedVideoAdPlayFailed?.(asError(p.requestMessage), info);
      break;
    case RewardVideoCallback.close:
      l.onRewardedVideoAdClosed?.(info);
      break;
    case RewardVideoCallback.click:
      l.onRewardedVideoAdPlayClicked?.(info);
      break;
    case RewardVideoCallback.reward:
      l.onReward?.(info);
      break;
    case RewardVideoCallback.deeplink:
      l.onDeeplinkCallback?.(info, p.isDeeplinkSuccess ?? false);
      break;
    case RewardVideoCallback.againPlayStart:
      l.onRewardedVideoAdAgainPlayStart?.(info);
      break;
    case RewardVideoCallback.againPlayEnd:
      l.onRewardedVideoAdAgainPlayEnd?.(info);
      break;
    case RewardVideoCallback.againPlayFail:
      l.onRewardedVideoAdAgainPlayFailed?.(asError(p.requestMessage), info);
      break;
    case RewardVideoCallback.againClick:
      l.onRewardedVideoAdAgainPlayClicked?.(info);
      break;
    case RewardVideoCallback.againReward:
      l.onAgainReward?.(info);
      break;
    case RewardVideoCallback.rewardFailed:
      l.onRewardFailed?.(info);
      break;
    case RewardVideoCallback.againRewardFailed:
      l.onAgainRewardFailed?.(info);
      break;
    case RewardVideoCallback.downloadConfirm:
      l.onDownloadConfirm?.(info);
      break;
    default:
      break;
  }
}
//# sourceMappingURL=ATRewardVideoAd.js.map