import { sourcepoint as sourcepointAtom } from "../../../../atoms/tracking/index.js";
import Script from 'next/script';
import { useEffect } from 'react';
import { useSetRecoilState } from 'recoil';
import useWaitForWindowProperty from "../../../../hooks/useWaitForWindowProperty/index.js";
const SourcepointConfigScript = function SourcepointConfigScript() {
  const setSourcepoint = useSetRecoilState(sourcepointAtom);
  const spService = useWaitForWindowProperty('_sp_.executeMessaging');
  const spId = useWaitForWindowProperty('pm_id', 10000);
  useEffect(() => {
    if (spId) {
      setSourcepoint(true);
    }
  }, [spId]);
  useEffect(() => {
    if (spService) {
      spService.executeMessaging();
    }
  }, [spService]);
  return <Script id="sourcepoint-config-script" type="text/javascript">
      {`
      window._sp_queue = [];
      window._sp_ = {
        config: {
          accountId: 1962,
          baseEndpoint: 'https://cdn.privacy-mgmt.com',
          usnat: { },
          isSPA: true,
          events: {
            onMessageReceiveData(_type, data) {
              console.log({sp: data, type: _type});
              try {
                if (data.msgDescription === "LINK: US Applicable States") {
                  window.pm_id = 1237172;
                }
              } catch (error) {
                console.error('Failed to initialize Sourcepoint: ', error);
              }
            },
          },
        }
      }
    `}
    </Script>;
};
export default SourcepointConfigScript;