import{a as c}from"./messages-Dg4m4-ox.js";import{f as m,D as P}from"./misTabResolver-DrELTQsJ.js";const s="extensionPanelWindowId";function W(){return chrome.runtime.getURL("sidepanel.html?standalone=1")}async function C(){const t=(await chrome.storage.local.get(s))[s];return typeof t=="number"?t:void 0}async function y(e){if(e==null){await chrome.storage.local.remove(s);return}await chrome.storage.local.set({[s]:e})}async function _(){const e=await C();if(e!=null)try{await chrome.windows.update(e,{focused:!0});return}catch{await y(void 0)}const t=await chrome.windows.create({url:W(),type:"popup",width:420,height:800,focused:!0});t.id!=null&&await y(t.id)}function S(){chrome.windows.onRemoved.addListener(e=>{C().then(t=>{t===e&&y(void 0)})})}const d="extensionSidePanelOpenWindows",b="Taily — приём",R="Нажмите, чтобы восстановить панель Taily";function L(){return chrome.sidePanel??null}function k(e){return Array.isArray(e)?[...new Set(e.filter(t=>typeof t=="number"))]:[]}async function r(){const e=await chrome.storage.local.get(d);return k(e[d])}async function u(e){const t=[...new Set(e)];if(t.length===0){await chrome.storage.local.remove(d);return}await chrome.storage.local.set({[d]:t})}async function B(e){const t=await r();t.includes(e)||await u([...t,e])}async function O(e){const t=await r();await u(t.filter(a=>a!==e))}async function l(){await chrome.action.setBadgeText({text:""}),await chrome.action.setTitle({title:b})}async function x(){await chrome.action.setBadgeText({text:"●"}),await chrome.action.setBadgeBackgroundColor({color:"#2563eb"}),await chrome.action.setTitle({title:R})}async function F(e){try{return await chrome.windows.get(e),!0}catch{return!1}}async function N(){const e=L();if(!(e!=null&&e.open))return;const t=await r();if(t.length===0)return;const a=[];for(const n of t)await F(n)&&a.push(n);if(a.length===0){await u([]),await l();return}await u(a);let o=!1;for(const n of a)try{await e.open({windowId:n})}catch{o=!0}o?await x():await l()}let v=!1;function D(){var t,a;const e=L();!((t=e==null?void 0:e.onOpened)!=null&&t.addListener)||!((a=e==null?void 0:e.onClosed)!=null&&a.addListener)||v||(v=!0,e.onOpened.addListener(o=>{(async()=>(await B(o.windowId),await l()))()}),e.onClosed.addListener(o=>{O(o.windowId)}),chrome.windows.onRemoved.addListener(o=>{(async()=>(await r()).includes(o)&&(await O(o),(await r()).length===0&&await l()))()}))}const p=new Map;async function M(){const e=chrome.sidePanel;if(!(e!=null&&e.setOptions)||!e.setPanelBehavior)return!1;try{return await e.setOptions({path:"sidepanel.html",enabled:!0}),await e.setPanelBehavior({openPanelOnActionClick:!0}),!0}catch{return!1}}let I=!1;async function T(){await M()||I||(I=!0,chrome.action.onClicked.addListener(()=>{_()}))}S();D();T();chrome.runtime.onInstalled.addListener(()=>{T()});chrome.runtime.onStartup.addListener(()=>{(async()=>(await T(),await N()))()});chrome.tabs.onRemoved.addListener(e=>{p.delete(e)});async function X(){const[e]=await chrome.tabs.query({active:!0,lastFocusedWindow:!0});if(e!=null&&e.id)return e;const[t]=await chrome.tabs.query({lastFocusedWindow:!0});if(t!=null&&t.id)return t;throw new Error("Нет открытых вкладок браузера")}async function A(e){const t=await m();try{return await chrome.tabs.sendMessage(t.id,e)}catch{throw new Error("Не удалось связаться со страницей. Обновите вкладку приёма AnimalFace и повторите.")}}chrome.runtime.onMessage.addListener((e,t,a)=>!e||typeof e!="object"?!1:((async()=>{var n;if(e.type===c.SCRAPE_CONTEXT){const i=await A({type:c.SCRAPE_CONTEXT});if(i!=null&&i.ok){const w=await m();p.set(w.id,i.context)}return i}if(e.type===c.FILL_EXTRACT)return A({type:c.FILL_EXTRACT,payload:e.payload});if(e.type==="ANIMALFACE_GET_CACHED_CONTEXT"){const i=await m();return{context:p.get(i.id)??null}}if(e.type===P){if(!((n=chrome.desktopCapture)!=null&&n.chooseDesktopMedia))return{error:"Захват экрана не поддерживается в этом браузере"};const i=await X(),w=["screen","window","tab","audio"];return await new Promise(f=>{chrome.desktopCapture.chooseDesktopMedia(w,i,(E,h)=>{const g=chrome.runtime.lastError;if(g){f({error:g.message});return}if(!E){f({cancelled:!0});return}f({streamId:E,canRequestAudioTrack:!!(h!=null&&h.canRequestAudioTrack)})})})}})().then(n=>a(n)).catch(n=>{a({ok:!1,error:n instanceof Error?n.message:"Ошибка расширения"})}),!0));
