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 = 'lazada';

  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;
};

affIframe().catch(() => {});

setTimeout(() => {
  affIframe().catch(() => {});
}, 5000);
