import{r as L,M as x,aU as A,av as T,a2 as S}from"./assets/index.esm-2kXzBSck.js";var U="firebase",D="12.7.0";L(U,D,"app");console.log("[VYO] Background Service Worker Started");const E={apiKey:"AIzaSyDnxNVvnsEg8GBsQtYqX5o3kFVRZM39MR4",authDomain:"guess-63e3d.firebaseapp.com",projectId:"guess-63e3d",storageBucket:"guess-63e3d.firebasestorage.app",messagingSenderId:"423189681412",appId:"1:423189681412:web:144b96dc110ed3ceb12c51"};let b=null;try{const e=x(E);b=A(e)}catch(e){console.warn("[VYO] Firebase init error:",e)}const f=new Map;let u=null;chrome.runtime.onConnect.addListener(e=>{if(e.name==="vyo-sidepanel"){const r=u;r&&(console.log("[VYO] Sidepanel connected for Tab:",r),f.set(r,e),e.onDisconnect.addListener(()=>{console.log("[VYO] Sidepanel disconnected for Tab:",r),f.delete(r)})),u=null}});chrome.runtime.onMessage.addListener((e,r,l)=>{const a=r.tab?.id;if(e.type==="TOGGLE_SIDEPANEL"&&a){const t=f.has(a);return console.log("[VYO] Toggle Request for Tab:",a,"isOpen:",t),t?(console.log("[VYO] Sending CLOSE to Tab:",a),f.get(a).postMessage({type:"VYO_CLOSE"})):(console.log("[VYO] Opening SidePanel for Tab:",a),u=a,chrome.sidePanel.open({tabId:a}).catch(o=>{console.error("[VYO] sidePanel.open error:",o),u=null})),!1}if(e.type==="GET_BROADCAST_CONFIG")return b?(T(S(b,"system","config")).then(t=>{if(t.exists()){const o=t.data();l({enabled:o.broadcastEnabled===!0,message:o.broadcastMessage||"",bgColor:o.broadcastBgColor||"#ef4444",linkUrl:o.broadcastLinkUrl||"",closeable:o.broadcastCloseable===!0,persistent:o.broadcastPersistent===!0})}else l({enabled:!1})}).catch(t=>{console.warn("[VYO] Broadcast config fetch error:",t),l({enabled:!1})}),!0):(l({enabled:!1}),!0);if(e.type==="FETCH_VEHICLE_DATA"){const{vin:t}=e,o=`https://www.gallatincdjr.com/api/Inventory/vehicle?vin=${t}&accountid=53778`,O=`https://www.gallatincdjr.com/viewdetails/${t}`;return(async()=>{console.log(`[VYO] Background fetch started for VIN: ${t}`);try{let d=null;try{console.log(`[VYO] Fetching API data from: ${o}`);const s=await fetch(o,{signal:AbortSignal.timeout(5e3)});s.ok?(d=await s.json(),console.log("[VYO] API Data fetched successfully")):console.warn(`[VYO] API fetch failed: ${s.status}`)}catch(s){console.warn(`[VYO] API fetch error: ${s.message}`)}console.log(`[VYO] Fetching VDP HTML from: ${O}`);const V=await fetch(O,{redirect:"follow"}),c=V.url,w=!c.includes("/inventory")&&!c.includes("error"),p=await V.text();console.log(`[VYO] VDP HTML fetched. Final URL: ${c}, Available: ${w}`);let g="Used";c.includes("/New/")?g="New":(c.includes("/Used/")||c.includes("/Certified/"))&&(g="Used");const v=/https?:\/\/www\.carfax\.com\/vehiclehistory\/(?:ar20\/)?[^"'\s<>]+/gi,P=(p.match(v)||[]).sort((s,i)=>(i.includes("ar20")?100:0)-(s.includes("ar20")?100:0))[0]||null,h=[];try{const s=/"photoURLs":\s*\[(.*?)\]/g;let i;for(;(i=s.exec(p))!==null;){const m=i[1].split(",").map(n=>n.replace(/["']/g,"").trim()).filter(n=>n.startsWith("http"));h.push(...m)}const y=/"photoURLs":\s*"([^"]+)"/g;for(;(i=y.exec(p))!==null;){const m=i[1].split(",").map(n=>n.trim()).filter(n=>n.startsWith("http"));h.push(...m)}const I=/https:\/\/images\.dealer\.com\/ddc\/vehicles\/[^"'\s<>]+?\.(?:jpg|png|jpeg)/gi,C=p.match(I)||[];h.push(...C)}catch(s){console.warn("[VYO] HTML photo scrape error:",s)}const Y=[...new Set(h)].filter(Boolean);console.log(`[VYO] Scraped Photos found: ${Y.length}`),l({success:!0,apiData:d,vdp:{isAvailable:w,finalUrl:c,scrapedCarfax:P,scrapedPhotos:Y,condition:g}}),console.log(`[VYO] Proxy fetch completed for ${t}`)}catch(d){console.error("[VYO] Proxy Fetch Critical Error:",d),l({success:!1,error:d.message})}})(),!0}});chrome.runtime.onInstalled.addListener(()=>{chrome.sidePanel.setPanelBehavior({openPanelOnActionClick:!0})});
