"use strict";const e="https://koosync.com/api//client/api/v1",t="install_record",a="GET_CONFIG",o="CONTENT_REQUEST",r="WAVE_PATH_URL",n="UPDATED_TABS",s="UPDATED_TAB_COMPLETE";let c=[];function i(e){const t=new FormData;return Object.keys(e).forEach((a=>{const o=e[a];Array.isArray(o)?o.forEach(((e,o)=>{t.append(`${a}[${o}]`,e)})):t.append(a,e[a])})),t}async function l(e){const{url:t,method:a,headers:o={},data:r={},dataType:n}=e;let s={},c=t;try{if("get"===(null==a?void 0:a.toLowerCase())||"delete"===(null==a?void 0:a.toLowerCase()))c=function(e,t){if(!t||0===Object.keys(t).length)return e;const a=new URLSearchParams;for(const o in t)Object.prototype.hasOwnProperty.call(t,o)&&a.append(o,t[o]);return`${e}?${a.toString()}`}(t,r),s={method:a,headers:o};else{if("post"!==(null==a?void 0:a.toLowerCase()))throw new Error(`不支持的请求方法: ${a}`);{const e="FormData"===n;s={method:a,headers:e?o:{...o,"Content-Type":"application/json"},body:e?i(r):JSON.stringify(r)}}}const e=await fetch(c,s);if(!e.ok)throw new Error(`请求失败，状态码: ${e.status}`);return await e.json()}catch(l){return console.error("数据请求失败:",l),{code:500,message:l.message,data:null}}}function d(){chrome.runtime.onMessage.addListener(((t,n,s)=>{switch(t.what){case a:!async function(t,a){try{const o={url:`${e}/media-wave/config`,method:"get",data:{numbering:t,type:1}},r=await l(o),n={url:`${e}/media-wave/config`,method:"get",data:{numbering:t,type:2}},s=await l(n);let i=[];200===(null==r?void 0:r.code)&&(null==r?void 0:r.data)&&(i=Array.isArray(r.data)?r.data.map((e=>({value:e,type:"v1"}))):[]);let d=[];200===(null==s?void 0:s.code)&&(null==s?void 0:s.data)&&(d=Array.isArray(s.data)?s.data.map((e=>({value:e,type:"v2"}))):[]);const h=new Set(d.map((e=>e.value))),u=[...i.filter((e=>!h.has(e.value))),...d];c=u,"function"==typeof a&&a(u)}catch(o){console.error("更新视频波配置失败:",o),"function"==typeof a&&a([])}}(t.payload.site_id,s);break;case o:chrome.tabs.query({active:!0,currentWindow:!0},(e=>{const a=e[0];a&&n.tab.id===a.id?l(t).then(s):s({code:403,message:"请求不来自活动标签页"})}));break;case r:t.url;break;default:console.log("未处理的消息类型:",t.what)}return!0}))}chrome.storage.local.get([t]).then((e=>{const a=e[t],o=chrome.runtime.getManifest().version;a&&a.is_first_install&&a.last_install_version===o||(chrome.storage.local.set({[t]:{is_first_install:!0,last_install_version:o}}),console.log("扩展已安装/更新到版本:",o))})).catch((e=>{console.error("获取安装记录失败:",e)})),d(),chrome.tabs.onUpdated.addListener(((e,t,a)=>{t&&(chrome.tabs.sendMessage(e,{what:n,payload:{hostname:"update.neocamx.com"}}).catch((()=>{})),"complete"===(null==t?void 0:t.status)&&(chrome.tabs.sendMessage(e,{what:s}).catch((()=>{})),chrome.tabs.query({currentWindow:!0}).then((e=>{})).catch((e=>console.error("获取标签页列表失败:",e)))))})),chrome.tabs.onRemoved.addListener((e=>{chrome.tabs.query({currentWindow:!0}).then((e=>{})).catch((e=>console.error("获取标签页列表失败:",e)))}));
