"use strict";

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

/**
 * Banner 监听器。Banner 自动刷新，有 AutoRefresh 回调。
 */

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

/**
 * Banner 命令式 API。视图渲染走 Fabric `<ATBannerViewComponent>`；
 * 本类用于命令式控制（load/查询/destroy/监听）。实例内按 callbackName 分流。
 */
export class ATBannerView {
  placementId = '';
  setPlacementId(placementId) {
    ATLog.call('ATBannerView', 'setPlacementId');
    if (this.placementId) {
      ATAdEvents.removeAdListener(this.placementId);
      ATAdEvents.removeRevenueListener(this.placementId);
    }
    this.placementId = placementId;
    ATAdEvents.setAdListener(placementId, p => this.route(p));
  }
  route(p) {
    if (p.callbackName.indexOf('AdSource') >= 0) {
      this.adSourceListener?.(p);
      return;
    }
    if (p.callbackName === BannerCallback.multipleLoaded) {
      this.multipleLoadedListener?.(p.extraDic ?? {});
      return;
    }
    if (this.mainListener) {
      dispatchBannerEvent(this.mainListener, p);
    }
  }

  /**
   * @param bannerSize 与 Fabric `width`/`height` 一致（pt/dp），避免 load 与 show 尺寸不一致导致空白。
   */
  loadAd(adRequest, bannerSize) {
    ATLog.call('ATBannerView', 'loadAd', this.placementId, adRequest, bannerSize);
    const w = bannerSize?.width ?? 320;
    const h = bannerSize?.height ?? 50;
    const extraDic = {
      size: {
        x: 0,
        y: 0,
        width: w,
        height: h
      }
    };
    if (adRequest) {
      extraDic.atAdRequest = adRequest;
    }
    ATReactNativeBridge.loadBannerAd(this.placementId, extraDic);
  }
  setShowConfig(config) {
    ATLog.call('ATBannerView', 'setShowConfig');
    ATReactNativeBridge.setBannerShowConfig(this.placementId, config);
  }
  setLocalExtra(map) {
    ATLog.call('ATBannerView', 'setLocalExtra');
    ATReactNativeBridge.setBannerLocalExtra(this.placementId, map);
  }
  setTKExtra(map) {
    ATLog.call('ATBannerView', 'setTKExtra');
    ATReactNativeBridge.setBannerTKExtra(this.placementId, map);
  }
  checkAdStatus() {
    ATLog.call('ATBannerView', 'checkAdStatus');
    return ATReactNativeBridge.checkBannerLoadStatus(this.placementId);
  }
  checkValidAdCaches() {
    ATLog.call('ATBannerView', 'checkValidAdCaches');
    return ATReactNativeBridge.getBannerValidAds(this.placementId);
  }
  static entryAdScenario(placementId, scenarioId, tkExtra) {
    ATLog.call('ATBannerView', 'entryAdScenario', placementId, scenarioId);
    ATReactNativeBridge.entryBannerScenario(placementId, scenarioId, tkExtra ?? {});
  }
  setBannerAdListener(listener) {
    this.mainListener = listener;
  }
  setAdRevenueListener(listener) {
    ATAdEvents.setRevenueListener(this.placementId, listener);
  }
  setAdMultipleLoadedListener(listener) {
    this.multipleLoadedListener = listener;
  }
  setAdSourceStatusListener(listener) {
    this.adSourceListener = listener;
  }
  destroy() {
    ATLog.call('ATBannerView', 'destroy');
    ATReactNativeBridge.destroyBanner(this.placementId);
    if (this.placementId) {
      ATAdEvents.removeAdListener(this.placementId);
      ATAdEvents.removeRevenueListener(this.placementId);
    }
  }
}
export function dispatchBannerEvent(l, p) {
  const info = p.extraDic ?? {};
  switch (p.callbackName) {
    case BannerCallback.loaded:
      l.onBannerAdLoaded?.();
      break;
    case BannerCallback.loadFail:
      l.onBannerAdLoadFail?.(asError(p.requestMessage));
      break;
    case BannerCallback.show:
      l.onBannerAdShow?.(info);
      break;
    case BannerCallback.click:
      l.onBannerAdClicked?.(info);
      break;
    case BannerCallback.close:
      l.onBannerAdClose?.(info);
      break;
    case BannerCallback.refresh:
      l.onBannerAdAutoRefreshed?.(info);
      break;
    case BannerCallback.refreshFail:
      l.onBannerAdAutoRefreshFail?.(asError(p.requestMessage));
      break;
    case BannerCallback.deeplink:
      l.onDeeplinkCallback?.(info, p.isDeeplinkSuccess ?? false);
      break;
    case BannerCallback.downloadConfirm:
      l.onDownloadConfirm?.(info);
      break;
    default:
      break;
  }
}
//# sourceMappingURL=ATBannerView.js.map