class n{menuId="select-ai-ocr-image";screenshotMenuId="select-ai-screenshot-ocr";listenerRegistered=!1;constructor(){this.registerClickListener()}registerClickListener(){this.listenerRegistered||(this.listenerRegistered=!0,chrome.contextMenus.onClicked.addListener((e,t)=>{e.menuItemId===this.menuId?this.delegateToContentScript(t?.id,e.srcUrl):e.menuItemId===this.screenshotMenuId&&this.triggerScreenshot(t?.id)}))}async createMenus(){await this.removeAllMenus(),await this.createMenu({id:this.menuId,title:"AI 图片解释",contexts:["image"]}),await this.createMenu({id:this.screenshotMenuId,title:"截图识别",contexts:["all"]})}createMenu(e){return new Promise((t,r)=>{chrome.contextMenus.create(e,()=>{const o=chrome.runtime.lastError;if(o){r(new Error(o.message||"Failed to create context menu"));return}t()})})}removeAllMenus(){return new Promise(e=>{try{chrome.contextMenus.removeAll(()=>{const t=chrome.runtime.lastError;t&&console.warn("[ContextMenu] Failed to remove menus:",t.message),e()})}catch(t){console.warn("[ContextMenu] Failed to remove menus:",t),e()}})}delegateToContentScript(e,t){!e||!t||chrome.tabs.sendMessage(e,{action:"ocr-from-context-menu",imageUrl:t},r=>{chrome.runtime.lastError?(console.error("[ContextMenu] Failed to send to content script:",chrome.runtime.lastError.message),this.showNotification("操作失败，请刷新页面后重试")):r?.error&&(console.error("[ContextMenu] Content script error:",r.error),this.showNotification(r.error))})}triggerScreenshot(e){e&&chrome.tabs.sendMessage(e,{action:"start-screenshot"},t=>{chrome.runtime.lastError?(console.error("[ContextMenu] Failed to send screenshot message:",chrome.runtime.lastError.message),this.showNotification("操作失败，请刷新页面后重试")):t?.error&&(console.error("[ContextMenu] Content script error:",t.error),this.showNotification(t.error))})}showNotification(e){chrome.notifications.create({type:"basic",iconUrl:"app-icon.png",title:"Select AI",message:e})}destroy(){this.removeAllMenus()}}const i=new n;export{i as contextMenuService};
