"use strict";

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

/**
 * 开屏监听器。load 失败/超时经此上报（不走 Promise.reject）。
 */

/** 开屏 show 配置 = ATShowConfig + 桥内容器高度（heightRatio 优先；都不传则全屏）。 */

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

/**
 * 开屏广告（一 placementId 一实例）。show 渲染进容器（高度可配，通过 heightRatio 设置）。
 */
export class ATSplashAd {
  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 === SplashCallback.multipleLoaded) {
      this.multipleLoadedListener?.(p.extraDic ?? {});
      return;
    }
    if (this.mainListener) {
      dispatchSplashEvent(this.mainListener, p);
    }
  }

  /** 加载（adRequest 底价/渠道；fetchAdTimeout 毫秒；失败/超时走 Event）。 */
  load(adRequest, fetchAdTimeout) {
    ATLog.call('ATSplashAd', 'load', this.placementId, adRequest, fetchAdTimeout);
    const extraDic = adRequest ? {
      atAdRequest: adRequest
    } : {};
    if (fetchAdTimeout != null) {
      extraDic.fetchAdTimeout = fetchAdTimeout;
    }
    ATReactNativeBridge.loadSplash(this.placementId, extraDic);
  }

  /** 展示。scenarioOrConfig 为 string 时走 scenario；为对象时走 showConfig（可含 heightRatio/heightPx）。 */
  show(scenarioOrConfig) {
    ATLog.call('ATSplashAd', 'show', this.placementId, scenarioOrConfig);
    if (typeof scenarioOrConfig === 'string') {
      ATReactNativeBridge.showSplash(this.placementId, scenarioOrConfig).catch(() => {});
      return;
    }
    if (scenarioOrConfig) {
      ATReactNativeBridge.showSplashWithConfig(this.placementId, scenarioOrConfig).catch(() => {});
    } else {
      ATReactNativeBridge.showSplash(this.placementId, '').catch(() => {});
    }
  }
  isAdReady() {
    ATLog.call('ATSplashAd', 'isAdReady');
    return ATReactNativeBridge.splashReady(this.placementId);
  }
  checkAdStatus() {
    ATLog.call('ATSplashAd', 'checkAdStatus');
    return ATReactNativeBridge.checkSplashLoadStatus(this.placementId);
  }
  checkValidAdCaches() {
    ATLog.call('ATSplashAd', 'checkValidAdCaches');
    return ATReactNativeBridge.getSplashValidAds(this.placementId);
  }
  static entryAdScenario(placementId, scenarioId, tkExtra) {
    ATLog.call('ATSplashAd', 'entryAdScenario', placementId, scenarioId);
    ATReactNativeBridge.entrySplashScenario(placementId, scenarioId, tkExtra ?? {});
  }
  setLocalExtra(map) {
    ATLog.call('ATSplashAd', 'setLocalExtra');
    ATReactNativeBridge.setSplashLocalExtra(this.placementId, map);
  }
  setTKExtra(map) {
    ATLog.call('ATSplashAd', 'setTKExtra');
    ATReactNativeBridge.setSplashTKExtra(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 dispatchSplashEvent(l, p) {
  const info = p.extraDic ?? {};
  switch (p.callbackName) {
    case SplashCallback.loaded:
      l.onSplashAdLoaded?.(p.isTimeout ?? false);
      break;
    case SplashCallback.loadFail:
      l.onSplashAdLoadFail?.(asError(p.requestMessage));
      break;
    case SplashCallback.timeout:
      l.onSplashAdTimeout?.();
      break;
    case SplashCallback.show:
      l.onSplashAdShow?.(info);
      break;
    case SplashCallback.showFailed:
      l.onSplashAdShowFailed?.(asError(p.requestMessage));
      break;
    case SplashCallback.click:
      l.onSplashAdClick?.(info);
      break;
    case SplashCallback.close:
      l.onSplashAdClose?.(info);
      break;
    case SplashCallback.deeplink:
      l.onDeeplinkCallback?.(info, p.isDeeplinkSuccess ?? false);
      break;
    case SplashCallback.downloadConfirm:
      l.onDownloadConfirm?.(info);
      break;
    default:
      break;
  }
}
//# sourceMappingURL=ATSplashAd.js.map