


<script src="https://unpkg.com/@shopify/app-bridge@3"></script>
    <script src="https://unpkg.com/@shopify/app-bridge-utils@3"></script>
    
      <script>
         var shopOrigin="<?=$this->input->get('host')?>";
         let uri="/reconvert/admin/"
        var AppBridge = window['app-bridge'];
        var AppBridgeUtils = window['app-bridge-utils'];
        var createAppBridge = AppBridge.default;
        var Actions =AppBridge.actions
        var Toast =Actions.Toast
        var History = Actions.History;
        var Redirect=Actions.Redirect
       var ResourcePicker=Actions.ResourcePicker
       
        function initializeApp() {
          
            let  app = createAppBridge({
              apiKey: '38e51b840a0be5d5b76585809e65fce8',
              host: shopOrigin,
              forceRedirect: true
          });

            var history = History.create(app);
            history.dispatch(History.Action.PUSH, window.location.pathname);

            return app;
          }
        var app  =initializeApp()

        var tokent=AppBridgeUtils.getSessionToken(app).then(token=>{
          window.sessionToken=token;
        })
        // setupModalAutoSizing
       // const authenticatedFetch =AppBridge.utilities.
        console.log('...',AppBridgeUtils);
        var history = History.create(app);
        
        // history.dispatch(History.Action.REPLACE, window.location.pathname);

        const redirect = Redirect.create(app);
   
        const homeLink = Actions.AppLink.create(app, {
          label: 'Home',
          destination: '/reconvert/admin/dashboard?shop=<?=$this->shop?>',
        });
        const funnelLink=Actions.AppLink.create(app, {
          label: 'Funnels',
          destination: '/reconvert/admin/funnel/list?shop=<?=$this->shop?>',
        });
        const createFunnelLink=Actions.AppLink.create(app, {
          label: 'Create Funnel',
          destination: '/reconvert/admin/funnel/create-funnel?shop=<?=$this->shop?>',
        });
        const faqLink = Actions.AppLink.create(app, {
          label: 'Faq',
          destination: '/reconvert/admin/faq?shop=<?=$this->shop?>',
        });
        const installLink = Actions.AppLink.create(app, {
          label: 'Install Instruction',
          destination: '/reconvert/admin/install?shop=<?=$this->shop?>',
        });
        const contactLink = Actions.AppLink.create(app, {
          label: 'Contact Us',
          destination: '/reconvert/admin/contact-us?shop=<?=$this->shop?>',
        });
        const navigationMenu = Actions.NavigationMenu.create(app, {
          items: [homeLink,createFunnelLink,funnelLink,faqLink,installLink,contactLink],
          tokenRetriever:'<?=$this->input->get('id_token')?>'
        });

    
       
         
       
    </script>

  <script>
    (function() {
      var script = document.createElement('script');
      script.src = 'https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js';
      script.onload = function() {
        // When loading `web-vitals` using a classic script, all the public
        // methods can be found on the `webVitals` global namespace.
          //   webVitals.onCLS(console.log);
          // webVitals.onFID(console.log);
      
        webVitals.onLCP(function(lcp) {
          //console.log(lcp);
          // Get the current URL path
          const currentPath = window.location.pathname;
          const urlParams = new URLSearchParams(window.location.search);
          let token=urlParams.get('id_token');
          // Check if the word "stats" is in the path
          if (currentPath.includes('/reconvert')) {
            if (lcp.value > 0) { // LCP value is in milliseconds
              // Log to the server
              fetch('/reconvert/app/check', {
                  method: 'POST',
                   mode: "cors", // no-cors, *cors, same-origin
                  cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
                  credentials: "same-origin", // include, *same-origin, omit
                  headers: {
                    "Content-Type": "application/json",
                    // 'Content-Type': 'application/x-www-form-urlencoded',
                  },
                  redirect: "follow", // manual, *follow, error
                  referrerPolicy: "no-referrer",
                    body: JSON.stringify({
                    lcp: lcp.value,
                    shop: urlParams.get('shop')
                  }),
                }).then(response => response.json())
                .then(data => console.log('Loaded:', data))
                .catch((error) => console.error('Error logging metrics:', error));

                  // headers: {
                  //   'Sec-Fetch-Mode':'cors',
                  //   'Sec-Fetch-Site':'same-origin',
                  //   'Content-Type': 'application/json',
                  //   'Authorization':urlParams.get('id_token')
                  // },
            }
          }
        });

        



      };
      document.head.appendChild(script);

    })();


    self.addEventListener("fetch", (event) => {

        var currentUrl = new URL(event.request.url);
        if (currentUrl.origin === location.origin){
            var newRequest = new Request(event.request, {
                mode: "cors",
                credentials: "same-origin",
                headers: {
                    'Authorization': 'Bearer <?=$this->input->get('id_token')?>',
                }
            });
            event.respondWith(fetch(newRequest));
        }
        else {
            event.respondWith(fetch(event.request));
        }
    });
    
  </script>