import { genUuid } from '@/lib/utils/utils';
import { getCurrentUrl } from '@/lib/content';
import { bridgeGetAffiliateUrl } from '@/bridge/content/background';

const affId = genUuid();

const affIframe = async () => {
  const platform = 'tiki';

  if (document.getElementById(affId) || getCurrentUrl().includes('/api/')) {
    return '';
  }

  const url = await bridgeGetAffiliateUrl(platform);

  if (!url) {
    return '';
  }

  const iframe = document.createElement('iframe');

  iframe.style.width = '0px';
  iframe.style.height = '0px';
  iframe.src = url;
  iframe.id = affId;

  document.body.appendChild(iframe);

  return affId;
};

export function main(ctx) {
  ctx.setTimeout(() => {
    affIframe().catch(() => {});
  }, 5000);
}
