import{t as e}from"./apiBase-SqS1USHn.js";import{a as t,o as n,p as r,r as i,s as a}from"./session-DfDMfr_C.js";function o(e){return e.toLowerCase()}var s=class{root;constructor(e){this.root={children:new Map,output:[]};let t=new Map;for(let n of e){let e=n.trim();if(!e)continue;let r=o(e);t.has(r)||t.set(r,e)}for(let e of t.values())this.insert(o(e),e);this.buildFailureLinks()}insert(e,t){let n=this.root;for(let t of e){let e=n.children.get(t);e||(e={children:new Map,output:[]},n.children.set(t,e)),n=e}n.output.push(t)}buildFailureLinks(){let e=[];for(let t of this.root.children.values())t.fail=this.root,e.push(t);for(;e.length;){let t=e.shift();for(let[n,r]of t.children){e.push(r);let i=t.fail;for(;i&&!i.children.has(n);)i=i.fail;r.fail=i?.children.get(n)??this.root,r.output=[...r.output,...r.fail.output]}}}findAll(e){if(!e)return[];let t=o(e),n=[],r=new Set,i=this.root;for(let e=0;e<t.length;e++){let a=t[e];for(;i!==this.root&&!i.children.has(a);)i=i.fail??this.root;if(i=i.children.get(a)??this.root,i.output.length)for(let t of i.output){let i=e-t.length+1,a=e+1;if(i<0)continue;let o=`${i}:${a}:${t}`;r.has(o)||(r.add(o),n.push({word:t,start:i,end:a}))}}return n.sort((e,t)=>e.start-t.start||e.end-t.end)}hasMatch(e){return this.findAll(e).length>0}filterTextDetailed(e){let t=this.findAll(e);if(!t.length)return{filtered:e,removed:[],hadMatches:!1};let n=``,r=0;for(let i of t)n+=e.slice(r,i.start),r=i.end;return n+=e.slice(r),{filtered:n.replace(/[ \t]+\n/g,`
`).replace(/\n{3,}/g,`

`).replace(/[ \t]{2,}/g,` `).trim(),removed:u(t),hadMatches:!0}}filterText(e){return this.filterTextDetailed(e).filtered}};function c(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`)}function l(e,t){let n=t.findAll(e);if(!n.length)return c(e);let r=``,i=0;for(let t of n)r+=c(e.slice(i,t.start)),r+=`<mark class="sw-hit">${c(e.slice(t.start,t.end))}</mark>`,i=t.end;return r+=c(e.slice(i)),r}function u(e){let t=new Set,n=[];for(let r of e)t.has(r.word)||(t.add(r.word),n.push(r.word));return n}var d=`xyxp_sensitive_words_cloud`,f=`xyxp_sensitive_words_custom`,p=`xyxp_sensitive_words_last_sync_at`;function m(e){if(!Array.isArray(e))return[];let t=new Set,n=[];for(let r of e){let e=String(r??``).trim();!e||t.has(e)||(t.add(e),n.push(e))}return n}function h(){return{words:[]}}function g(e){return!e||typeof e!=`object`?h():{words:m(e.words)}}function _(e,t){let n=new Set,r=[];for(let i of[...e,...t]){let e=i.trim();!e||n.has(e)||(n.add(e),r.push(e))}return r}var v=null,y=``;function b(e,t){return`${e.version}|${t.join(``)}`}async function x(){let e=await chrome.storage.local.get([d,f,p]),t=e[d],n={version:typeof t?.version==`string`?t.version:`0`,updatedAt:typeof t?.updatedAt==`string`?t.updatedAt:``,words:Array.isArray(t?.words)?t.words.map(String):[]},r=g(e[f]),i=typeof e.xyxp_sensitive_words_last_sync_at==`string`?e[p]:``;return{cloud:n,custom:r.words,lastSyncAt:i}}async function S(e=!1){let{cloud:t,custom:n}=await x(),r=b(t,n);return!e&&v&&y===r?v:(v=new s(_(t.words,n)),y=r,v)}function C(){v=null,y=``}async function w(e,n,r){let a=await i(),o=()=>({...n,Authorization:`Bearer ${a.cloudAccessToken.trim()}`}),s=await fetch(`${e}/api/v1/sensitive-words/sync`,{headers:o()});if(s.status===401&&r)try{a=await t(a),s=await fetch(`${e}/api/v1/sensitive-words/sync`,{headers:o()})}catch(e){throw e instanceof Error&&e.message===`SESSION_EXPIRED`?Error(`登录已失效，请重新登录`):e}return s}async function T(t){let i=e();if(!i)return{ok:!1,error:`未配置 VITE_API_BASE_URL`};if(!n(await a()))return{ok:!1,error:`请先登录账号`};let{cloud:o,lastSyncAt:s}=await x();if(!t?.force&&s){let e=Date.now()-Date.parse(s);if(Number.isFinite(e)&&e>=0&&e<864e5)return{ok:!0,updated:!1,version:o.version,wordCount:o.words.length}}try{let e={Accept:`application/json`};o.version&&o.version!==`0`&&(e[`If-None-Match`]=o.version);let t=await w(i,e,!0);if(t.status===304)return await chrome.storage.local.set({[p]:new Date().toISOString()}),{ok:!0,updated:!1,version:o.version,wordCount:o.words.length};if(!t.ok){let e=await t.text().catch(()=>``);return{ok:!1,error:r(e)??e??`HTTP ${t.status}`}}let n=await t.json(),a=n.data;if(!a||!Array.isArray(a.words))return{ok:!1,error:n.msg??`响应格式无效`};let s={version:a.version??t.headers.get(`ETag`)??String(Date.now()),updatedAt:a.updated_at??new Date().toISOString(),words:a.words.map(e=>String(e).trim()).filter(Boolean)};return await chrome.storage.local.set({[d]:s,[p]:new Date().toISOString()}),C(),{ok:!0,updated:!0,version:s.version,wordCount:s.words.length}}catch(e){return{ok:!1,error:e instanceof Error?e.message:`同步失败`}}}async function E(){if(!n(await a()))return;let e=await T();e.ok||console.warn(`[xyxp] sensitive words sync skipped:`,e.error)}async function D(e){return(await S()).filterTextDetailed(e)}export{T as a,h as c,u as d,C as i,g as l,D as n,f as o,S as r,p as s,E as t,l as u};