import{a as r}from"./chunk-IV62XIZ7.js";import{k as f}from"./chunk-V2L5DJQO.js";import{f as i,g as b,n as h}from"./chunk-7DWBZSFS.js";h();b();var e=f("TabManager"),o=class l{static instance;currentTab=null;constructor(){this.init()}static getInstance(){return l.instance||(l.instance=new l),l.instance}async init(){await this.updateCurrentTab(),i.tabs.onActivated.addListener(this.handleTabActivation),i.tabs.onUpdated.addListener(this.handleTabUpdate),e.info("🚀 TabManager initialized, performing initial content availability check..."),setTimeout(async()=>{try{let a=await r.getState().checkContentAvailability();e.info("📋 Initial content availability check result:",{hasContent:a,currentUrl:this.currentTab?.url,reason:"sidepanel_initialization"})}catch(a){e.warn("❌ Failed initial content availability check:",a)}},1e3)}handleTabActivation=a=>{this.updateCurrentTab(a.tabId)};handleSummarizationRequest=async()=>{try{if(r.getState().currentUrlState?.hasSummary){e.info("🤖 [TABMANAGER] Summary already exists, skipping");return}let n=async()=>{let c=Date.now();for(;Date.now()-c<1e4;){let u=r.getState(),d=u.currentUrlState;if(e.info("🤖 [TABMANAGER] Checking content availability...",{hasContent:d?.hasContent,waitTime:Date.now()-c}),d?.hasContent)return!0;await u.checkContentAvailability(),await new Promise(v=>setTimeout(v,500))}return!1};e.info("🤖 [TABMANAGER] Waiting for content to be available..."),await n()?(e.info("🤖 [TABMANAGER] Content is available, triggering summarization..."),window.dispatchEvent(new CustomEvent("speechify-start-summarization"))):e.warn("🤖 [TABMANAGER] Timeout waiting for content, cannot summarize")}catch(a){e.error("🤖 [TABMANAGER] Error handling summarization request:",a)}};handleTabUpdate=async(a,t)=>{let n=await i.tabs.get(a);if(n.active){if(t.url){this.updateCurrentTab(a);return}if((t.title||t.favIconUrl)&&n.url===this.currentTab?.url){this.currentTab&&(t.title!==void 0&&(this.currentTab.title=n.title),t.favIconUrl!==void 0&&(this.currentTab.favIconUrl=n.favIconUrl));let s={};t.title!==void 0&&(s.title=n.title),t.favIconUrl!==void 0&&(s.favIconUrl=n.favIconUrl),r.getState().updateCurrentUrlMetadata(s)}}};async updateCurrentTab(a){let t;if(a?t=await i.tabs.get(a):t=(await i.tabs.query({active:!0,currentWindow:!0}))[0],t&&t.id&&t.url)if(t.url!==this.currentTab?.url){e.info("🔄 Tab URL changed:",{oldUrl:this.currentTab?.url,newUrl:t.url,tabId:t.id,title:t.title}),this.currentTab={id:t.id,url:t.url,title:t.title,favIconUrl:t.favIconUrl},await r.getState().setCurrentUrl(this.currentTab.url,{title:this.currentTab.title,favIconUrl:this.currentTab.favIconUrl,tabId:this.currentTab.id}),e.debug("Resetting content availability state for new URL"),await r.getState().updateCurrentUrlState({hasContent:!1,contentCount:0}),e.info("🔍 Checking if content is already available for new URL...");try{let n=await r.getState().checkContentAvailability();e.info("📋 Direct content availability check result:",{url:t.url,hasContent:n,reason:"initial_check_after_url_change"})}catch(n){e.warn("❌ Failed to check content availability on URL change:",n)}}else e.debug("Tab update but URL unchanged:",t.url);else e.debug("Invalid tab data received:",{tab:t,hasId:!!t?.id,hasUrl:!!t?.url})}getCurrentTab(){return this.currentTab}cleanup(){i.tabs.onActivated.removeListener(this.handleTabActivation),i.tabs.onUpdated.removeListener(this.handleTabUpdate)}},A=o;export{A as a};
//# sourceMappingURL=chunk-IDIA6O63.js.map
