// What the WordPress plugin's script has in place of cdn-code.ts: the plugin serves the script
// and its chunks from the site itself and updates them only with the plugin, so it takes no
// code from a CDN and never refreshes itself from one.
import type { ConsentStoreConfig } from "./consent-store";

/** See cdn-code.ts: the plugin's own copy may always switch the "Powered by" line off. */
export const PLUGIN_BUILD = true;

export function cdnChunkUrls(_base: string, _file: string): string[] {
  return [];
}

export function refreshLoaderIfStale(_params: {
  config: ConsentStoreConfig;
  latestBuild: string | null | undefined;
  scriptSrc: string | null;
}): void {}

// A page that stayed open while the plugin was updated asks for chunk files whose names (they
// carry a content hash) the new plugin folder no longer has, and cannot learn the new names
// without a fresh cmp.js. One reload per browser session fetches it, so the visitor's next
// click on the preference center works; the guard keeps a server that is really down from
// causing more than that one reload.
const RELOADED_KEY = "__consent_hub_chunk_reload__";

export function onChunkFailure(
  page: Pick<Window, "navigator" | "sessionStorage" | "location"> = window,
): void {
  try {
    // Offline, a reload would only lose the page.
    if (page.navigator.onLine === false) return;
    if (page.sessionStorage.getItem(RELOADED_KEY)) return;
    page.sessionStorage.setItem(RELOADED_KEY, "1");
    page.location.reload();
  } catch {
    // No storage to remember it in: do not risk reloading in a loop.
  }
}
