import { useState, useEffect } from 'react';
const useCustomAd = function useCustomAd({
  type,
  retryLimit = 10
}) {
  const [id, setId] = useState();
  useEffect(() => {
    if (id) return undefined;
    let timer;
    const mountAd = (retry = 0) => {
      if (window.politicoAds?.generateDynamicAdSlot) {
        try {
          const adId = window.politicoAds.generateDynamicAdSlot(type);
          setId(adId);
          return;
        } catch (error) {
          /* eslint-disable-next-line no-console */
          console.error('Ad: Error Generating Ad: ', error);
        }
      } else if (retry < retryLimit) {
        timer = setTimeout(() => mountAd(retry + 1), 500);
      } else {
        /* eslint-disable-next-line no-console */
        console.error('Ad: Advertising network not loaded.');
      }
    };
    mountAd();
    return () => {
      if (timer) {
        clearTimeout(timer);
      }
    };
  }, [id]);
  return id;
};
export default useCustomAd;