var w=Object.defineProperty;var h=(r,t,e)=>t in r?w(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var u=(r,t,e)=>h(r,typeof t!="symbol"?t+"":t,e);import{S as l}from"./index-CTnVPfHU.js";const f=new Set(["en","fr","de","es"]);class C extends Error{constructor(e,s){super(s);u(this,"status");this.status=e,this.name="HttpError"}}class y{constructor(t="YOUR_TOKEN_PLACEHOLDER",e="https://api.frdic.com"){u(this,"baseURL");u(this,"token");this.baseURL=e.replace(/\/$/,""),this.token=t}static normalizeLanguage(t){const e=String(t||"").trim().toLowerCase();if(f.has(e))return e;throw new Error(`Unsupported language for Eudic: ${t}. Allowed: en/fr/de/es`)}buildHeaders(){return{Authorization:`${this.token}`,"Content-Type":"application/json"}}async request(t,e){const s=await fetch(`${this.baseURL}${t}`,{...e,headers:{...this.buildHeaders(),...(e==null?void 0:e.headers)||{}}});if(s.status===204)return;let o=null;const a=await s.text();try{o=a?JSON.parse(a):{}}catch{o={raw:a}}if(!s.ok){const c=(o==null?void 0:o.message)||s.statusText||"Request failed";throw new C(s.status,c)}return o}async listCategories(t){return(await this.request(`/api/open/v1/studylist/category?language=${encodeURIComponent(t)}`)).data||[]}async createCategory(t,e){const s={language:t,name:e},o=await this.request("/api/open/v1/studylist/category",{method:"POST",body:JSON.stringify(s)});if(!o.data)throw new Error("Failed to create category");return o.data}async ensureCategory(t,e){const o=(await this.listCategories(t)).find(c=>c.name===e);return o?o.id:(await this.createCategory(t,e)).id}async addWord(t){return(await this.request("/api/open/v1/studylist/word",{method:"POST",body:JSON.stringify(t)})).message}}class E{constructor(t={}){u(this,"name","Eudic");u(this,"client");u(this,"defaultCategoryName");this.client=new y(t.token),this.defaultCategoryName=t.categoryName||"DeepAcquire"}async sync(t){const e=y.normalizeLanguage(t.learnLanguage),s=t.categoryName||this.defaultCategoryName;if(t.dryRun){const n=await this.client.listCategories(e);return{total:t.words.length,successCount:0,failureCount:0,report:[],metadata:{dryRun:!0,language:e,categoryName:s,categoryCount:Array.isArray(n)?n.length:0}}}const o=await this.client.ensureCategory(e,s),a=[];let c=0,d=0;for(const n of t.words){const g=Array.isArray(n.contexts)&&n.contexts.length>0?n.contexts[0]:void 0;try{await this.client.addWord({language:e,word:n.word,context_line:g,category_ids:[o]}),c+=1,a.push({word:n.word,ok:!0})}catch(i){d+=1,a.push({word:n.word,ok:!1,errorMessage:(i==null?void 0:i.message)||String(i)})}}return{total:t.words.length,successCount:c,failureCount:d,report:a,metadata:{categoryId:o,categoryName:s,language:e}}}}function N(r){return Object.values(r||{})}async function x(r){var d,n,g,i;const t=await l.getSettings(),e=(r==null?void 0:r.token)??((n=(d=t.integrations)==null?void 0:d.eudic)==null?void 0:n.token)??"";if(!e)throw new Error("Eudic token is not set. Please set it in Options → Integrations.");const s=(r==null?void 0:r.categoryName)??((i=(g=t.integrations)==null?void 0:g.eudic)==null?void 0:i.categoryName),o=new E({token:e,categoryName:s}),a=Array.isArray(r==null?void 0:r.words)?r.words:N(await l.getVocabulary()),c={learnLanguage:t.general.learnLanguage,words:a,categoryName:s,dryRun:r==null?void 0:r.dryRun};return await o.sync(c)}export{x as s};
