// Which copy of the loader on this page is the current one: each init() takes
// the next number, and anything a superseded copy does late (drawing a banner)
// checks isCurrentInstance() first, so a duplicate snippet cannot draw twice.

const INSTANCE_KEY = "__consent_hub_instance__";
let instanceId = 0;

export function claimInstance(): void {
  const page = window as unknown as Record<string, number | undefined>;
  page[INSTANCE_KEY] = (page[INSTANCE_KEY] ?? 0) + 1;
  instanceId = page[INSTANCE_KEY];
}

export function isCurrentInstance(): boolean {
  // No copy has claimed the page (code run without init): nothing replaced it.
  return (
    ((window as unknown as Record<string, number | undefined>)[INSTANCE_KEY] ??
      0) === instanceId
  );
}
