import { AdPlatformAdapter } from "../types";
import { openWithMraidOrWindow, setupMraidCustomClose } from "./helpers";
import { getStoreUrl } from "./storeUrls";

export function createApplovinAdapter(): AdPlatformAdapter {
  let started = false;
  let ended = false;

  return {
    platform: "applovin",

    init() {
      setupMraidCustomClose();
    },

    onResourcesLoaded() {},

    onGameStart() {
      if (started) return;
      started = true;
    },

    onGameEnd() {
      if (ended) return;
      ended = true;
    },

    openStore() {
      if (window.ExitApi) {
        window.ExitApi.exit();
        return;
      }
      openWithMraidOrWindow(getStoreUrl());
    },

    shouldUseCustomLoading() {
      return true;
    },

    shouldShowAdBadge() {
      return true;
    },
  };
}

export const adPlatform = createApplovinAdapter();
