1import{p as a,r as u,B as y,v as b,bc as B,N as n}from"./index-C9IH3k-2.js";import{s as E,u as I,f as h,W as S,b as j}from"./WhiteLabelFramedContent-BsGtRGOu.js";import{A as v}from"./Add3rdPartyScript-CynHdFNV.js";import{b as x}from"./HelperMethods-C-ewwU_x.js";import{W as A}from"./WhiteLabelFrame-BkraNf1a.js";const i="rl_catalogConnectRes",D=({onSuccess:l})=>E()?a.jsx(A,{path:"catalogConnect",messageType:i,onSuccess:l,title:"Catalog connect"}):a.jsx(L,{onSuccess:l}),L=({onSuccess:l})=>{const{isIFrame:c,api:p,reportToParent:F,reportErrorToParent:f}=I(),[g,d]=u.useState(!1),r=u.useRef(null),w=u.useRef([]);u.useEffect(()=>{v("https://connect.facebook.net/en_US/sdk.js").then(()=>{if(window.FB&&typeof window.FB.init=="function")try{window.FB.init(h)}catch(o){console.warn("FB.init failed (already initialised?)",o)}else window.fbAsyncInit=function(){window.FB.init(h)}});const e=o=>{if(!o.origin.endsWith("facebook.com"))return;let s;try{s=typeof o.data=="string"?JSON.parse(o.data):o.data}catch{console.log("Catalog FB raw event (unparsed):",o.data,"Flow ID:",r.current);return}console.log("Catalog FB message event:",s,"Flow ID:",r.current),r.current&&w.current.push(s)};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[]);const m=e=>{if(c){!e?.success&&Array.isArray(e?.errors)&&e.errors.length?f(i,new Error(e.errors.join(" "))):F(i,e);return}e?.success&&e?.catalogs?.length?n.success(`Connected ${e.catalogs.length} catalog${e.catalogs.length>1?"s":""}`):Array.isArray(e?.errors)&&e.errors.length?n.error(e.errors.join(" ")):n.warning("No catalog was granted during the Facebook flow."),typeof l=="function"&&l(e)},C=()=>{if(g){console.log("Catalog connect: flow already active, ignoring");return}if(!window.FB||typeof window.FB.login!="function"){const o=new Error("Facebook SDK not loaded yet. Try again in a moment.");c?f(i,o):n.error(o.message);return}const e=x();r.current=e,w.current=[],d(!0),window.FB.login(function(o){const s=w.current.slice();if(console.log("Catalog flow complete. Flow ID:",e),console.log(" FB.login response:",o),console.log(` Collected ${s.length} FB postMessage event(s):`,s),!o||!o.authResponse||!o.authResponse.code){c||n.warning("Catalog connection cancelled or failed."),r.current=null,d(!1);return}p.post("/api/v1/onboard/catalogConnect",{authResponse:o.authResponse,status:o.status,flowSessionId:e}).then(t=>{console.log(" Backend /catalogConnect response:",t?.data),t&&(t.status===200||t.status===207)?m(t.data):c||n.error("Server error while connecting catalog.")}).catch(t=>{console.error("Catalog connect POST error",t),c?f(i,t):n.error("Could not connect catalog. Please try again.")}).finally(()=>{r.current=null,d(!1)})},j)};return a.jsx(S,{children:a.jsx(y,{type:"primary",onClick:C,disabled:g,loading:g,style:{height:40},children:a.jsxs(b,{children:[a.jsx(B,{}),g?"Connecting...":"Connect Catalog"]})})})};export{D as default};
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.