1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[45558],{45558:(e,t,n)=>{n.d(t,{$:()=>b,IntegrationsProvider:()=>v});var o=n(22342),a=n(33670),r=n(20063),i=n(62833),l=n(80817),s=n(67469);let c={google:{connected:!1,accounts:[],loading:!1},microsoft:{connected:!1,accounts:[],loading:!1}},d={facebook:{connected:!1,tokenExpired:!1,profile:null,connectedAccountsCount:0,connectedPages:[],availablePages:[],connectedAdAccounts:[]}},u={id:null,name:null,availablePixels:[]},f={connected:!1,phoneNumber:null,tfvStatus:null,cardState:"not_started",onboardingStatus:"not_started",hasApprovedBrand:!1,hasApprovedCampaign:!1,hasNumberOnMS:!1},g={calendar:"ul-integrations-calendar",socialMedia:"ul-integrations-social",pixel:"ul-integrations-pixel",businessPhone:"ul-integrations-phone",leadForms:"ul-integrations-leadforms"},h=(e,t)=>{try{let t=localStorage.getItem(e);if(t)return JSON.parse(t)}catch(t){console.error("Error loading from cache:",e,t)}return t},p=(e,t)=>{try{localStorage.setItem(e,JSON.stringify(t))}catch(t){console.error("Error saving to cache:",e,t)}},m=(0,a.createContext)(void 0);function v(e){let{children:t}=e,n=(0,r.usePathname)(),{isAuthenticated:v,user:b,handleSessionExpired:k}=(0,i.A)(),[S,x]=(0,a.useState)(c),[P,A]=(0,a.useState)(d),[E,I]=(0,a.useState)(u),[w,C]=(0,a.useState)(f),[y,N]=(0,a.useState)([]),[F,L]=(0,a.useState)(!0),[M,O]=(0,a.useState)(!1),[_,j]=(0,a.useState)(!1),[J,D]=(0,a.useState)(!1);(0,a.useLayoutEffect)(()=>{D(!0);let e=h(g.calendar,c),t=h(g.socialMedia,d),n=h(g.pixel,u),o=h(g.businessPhone,f),a=h(g.leadForms,[]),r=!1;(e.google.connected||e.microsoft.connected)&&(x(e),r=!0),t.facebook.connected&&(A(t),r=!0),n.id&&(I(n),r=!0),o.connected&&(C(o),r=!0),a.length>
10&&(N(a),r=!0),r?L(!1):v||L(!1)},[]),(0,a.useEffect)(()=>{let e=()=>{O(!1),Z(!0)},t=()=>{O(!0)};return window.addEventListener("online",e),window.addEventListener("offline",t),O(!navigator.onLine),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",t)}},[]);let Z=(0,a.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(!v){x(c),A(d),t||L(!1);return}if(!navigator.onLine){O(!0),t||L(!1);return}try{var n,o,a,r,i,l;t||L(!0),O(!1);let[s,d,f,h,m,v]=await Promise.all([fetch("/api/integrations/calendars",{credentials:"include"}),fetch(e?"/api/integrations/social-media?refresh=true":"/api/integrations/social-media",{credentials:"include"}),fetch("/api/integrations/meta-pixel",{credentials:"include"}),fetch("/api/integrations/business-phone",{credentials:"include"}),fetch("/api/facebook/forms?includeArchived=true",{credentials:"include"}),fetch("/api/integrations/token-health",{credentials:"include"})]);if(401===s.status)return void k();if(s.ok){let e,t=await s.json();if(t.success&&(null==(n=t.integrations)?void 0:n.length)>0){let n=[],o=[];t.integrations.forEach(e=>{"google"===e.provider?n.push({email:e.provider_email,id:e.id}):"microsoft"===e.provider&&o.push({email:e.provider_email,id:e.id})}),e={google:{connected:n.length>0,accounts:n,loading:!1},microsoft:{connected:o.length>0,accounts:o,loading:!1}}}else e=c;x(e),p(g.calendar,e)}let b=!1;if(v.ok){let e=await v.json();b="expired"===e.tokenHealth}if(401===d.status)return void k();if(d.ok){let e=await d.json(),t=+!!e.facebookProfile,n=(null==(o=e.connectedPages)?void 0:o.length)||0,r=(null==(a=e.connectedAdAccounts)?void 0:a.length)||0,i=(e.connectedPages||[]).filter(e=>e.instagramAccount).length,l={facebook:{connected:!!e.facebookProfile,tokenExpired:b,profile:e.facebookProfile||null,connectedAccountsCount:t+n+r+i,connectedPages:e.connectedPages||[],availablePages:e.availablePages||[],connectedAdAccounts:e.connectedAdAccounts||[]}};A(e=>{var t,n;let o=((null==(t=e.facebook.connectedAdAccounts)?void 0:t.length)||0)>0,a=((null==(n=l.facebook.connectedAdAccounts)?void 0:n.length)||0)>0,r=!!l.facebook.connected;return o&&!a&&r?(console.warn("IntegrationsContext: ignoring social-media payload that lost connectedAdAccounts (likely a degraded Graph API response). Keeping previous state until next fetch."),e):(p(g.socialMedia,l),l)})}if(401===f.status)return void k();if(403===f.status)I(u),p(g.pixel,u);else if(f.ok){let e=await f.json(),t=(null==(i=e.availablePixels)||null==(r=i.find(t=>t.id===e.pixelId))?void 0:r.name)||null,n={id:e.pixelId||null,name:t,availablePixels:e.availablePixels||[]};I(n),p(g.pixel,n)}if(h.ok){let e=await h.json(),t=e.cardState||"not_started",n={connected:"live"===t,phoneNumber:e.phoneNumber||null,tfvStatus:e.tfvStatus||null,cardState:t,onboardingStatus:e.onboardingStatus||"not_started",hasApprovedBrand:!!e.hasApprovedBrand,hasApprovedCampaign:!!e.hasApprovedCampaign,hasNumberOnMS:!!e.hasNumberOnMS};C(n),p(g.businessPhone,n)}if(m.ok){let e=await m.json();e.needsRefresh?fetch("/api/facebook/forms?refresh=true&includeArchived=true",{credentials:"include"}).then(e=>e.ok?e.json():null).then(e=>{var t;(null==e||null==(t=e.forms)?void 0:t.length)&&(N(e.forms),p(g.leadForms,e.forms))}).catch(()=>{}):(null==(l=e.forms)?void 0:l.length)&&(N(e.forms),p(g.leadForms,e.forms))}j(!0)}catch(e){console.error("IntegrationsContext: Error fetching integrations:",e),O(!0)}finally{L(!1)}},[v,null==b?void 0:b.email,k]),B=(0,a.useCallback)(async e=>{await Z(!0,e)},[Z]);(0,a.useEffect)(()=>{if(v&&b&&!(0,i.t)(n)){let e=h(g.calendar,c),t=h(g.socialMedia,d),n=h(g.pixel,u),o=h(g.businessPhone,f),a=h(g.leadForms,[]),r=S.google.connected||S.microsoft.connected||P.facebook.connected||!!E.id||w.connected||y.length>0;(e.google.connected||e.microsoft.connected)&&(x(e),r=!0),t.facebook.connected&&(A(t),r=!0),n.id&&(I(n),r=!0),o.connected&&(C(o),r=!0),a.length>
10&&(N(a),r=!0),Z(!r,r)}else if(!v&&navigator.onLine&&_){x(c),A(d),I(u),C(f),N([]);try{localStorage.removeItem(g.calendar),localStorage.removeItem(g.socialMedia),localStorage.removeItem(g.pixel),localStorage.removeItem(g.businessPhone),localStorage.removeItem(g.leadForms)}catch(e){console.error("Error clearing cache:",e)}j(!1)}else v||navigator.onLine},[v,null==b?void 0:b.id,Z,n]),(0,a.useEffect)(()=>{if(v&&!_&&!F&&!(0,i.t)(n)){let e=setTimeout(()=>{Z(!1,!0)},1e3);return()=>clearTimeout(e)}},[v,_,F,Z,n]),(0,a.useEffect)(()=>{let e=(0,l.ZD)(g.calendar,e=>{if(e)try{let t=JSON.parse(e);x(t)}catch(e){console.error("IntegrationsContext: Error parsing cross-tab calendar data",e)}}),t=(0,l.ZD)(g.socialMedia,e=>{if(e)try{let t=JSON.parse(e);A(t)}catch(e){console.error("IntegrationsContext: Error parsing cross-tab social media data",e)}}),n=(0,l.ZD)(g.pixel,e=>{if(e)try{let t=JSON.parse(e);I(t)}catch(e){console.error("IntegrationsContext: Error parsing cross-tab pixel data",e)}}),o=(0,l.ZD)(g.businessPhone,e=>{if(e)try{let t=JSON.parse(e);C(t)}catch(e){console.error("IntegrationsContext: Error parsing cross-tab phone data",e)}}),a=(0,l.ZD)(g.leadForms,e=>{if(e)try{let t=JSON.parse(e);N(t)}catch(e){console.error("IntegrationsContext: Error parsing cross-tab lead forms data",e)}});return()=>{e(),t(),n(),o(),a()}},[]),(0,s.I)({onFocus:()=>{Z(!0,!0)},minHiddenTime:3e4,enabled:v&&!(0,i.t)(n)});let T=(0,a.useCallback)(async()=>{try{let e=await fetch("/api/facebook/forms?refresh=true&includeArchived=true",{credentials:"include"});if(e.ok){let t=await e.json();t.forms&&(N(t.forms),p(g.leadForms,t.forms))}}catch(e){console.error("IntegrationsContext: Error refreshing lead forms:",e)}},[]),H=S.google.connected||S.microsoft.connected||P.facebook.connected||!!E.id||w.connected;return(0,o.jsx)(m.Provider,{value:{calendar:S,socialMedia:P,pixel:E,businessPhone:w,leadForms:y,isLoading:F&&!H,isOffline:M,refreshIntegrations:B,refreshLeadForms:T,updateCalendarIntegrations:e=>{x(e),p(g.calendar,e)},updateSocialMediaIntegrations:e=>{A(e),p(g.socialMedia,e)},updatePixel:e=>{I(e),p(g.pixel,e)},updateBusinessPhone:e=>{C(e),p(g.businessPhone,e)},updateLeadForms:e=>{N(e),p(g.leadForms,e)}},children:t})}function b(){let e=(0,a.useContext)(m);if(void 0===e)throw Error("useIntegrations must be used within an IntegrationsProvider");return e}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.