(()=>{"use strict";var e,t,o,n,r,a;!function(e){e.Button="button",e.Auto="auto",e.Ctrl="Control",e.Shift="Shift",e.Alt="Alt"}(e||(e={})),function(e){e.None="none",e.Background="background",e.Underline="underline"}(t||(t={})),function(e){e.Us="us",e.Uk="uk"}(o||(o={})),function(e){e.System="system",e.Light="light",e.Dark="dark"}(n||(n={})),function(e){e.YoudaoDictionary="youdaoDictionary",e.YoudaoTranslate="youdaoTranslate",e.Imt="imt",e.Microsoft="microsoft",e.Caiyun="caiyun",e.Google="google"}(r||(r={})),function(e){e.Collect="collect",e.Toast="toast",e.PlayVoice="playVoice",e.playVoiceState="playVoiceState",e.FetchData="fetchData",e.WordDetail="wordDetail"}(a||(a={})),e.Button,n.System,t.None;const s="user_words_cache",c="user_books_cache";r.Microsoft,r.Google,r.YoudaoTranslate,r.Imt,r.Caiyun;const i=new class{constructor(){this.isProduction=!0}log(...e){this.isProduction||console.log("Logger----\x3e",...e)}warn(...e){this.isProduction||console.warn("Logger----\x3e",...e)}error(...e){this.isProduction||console.error("Logger----\x3e",...e)}};class l{static getAll(){return new Promise((e=>{chrome.storage.local.get([s],(t=>{const o=t[s];e(o||[])}))}))}static setAll(e){return new Promise((t=>{chrome.storage.local.set({[s]:e},(()=>{t()}))}))}static setItem(e){return new Promise((t=>{l.getAll().then((o=>{const n=o.find((t=>t.word===e.word));if(n)n.bookId=e.bookId,l.setAll(o);else{const t=[{word:e.word},...o];l.setAll(t)}t(!0)})).catch((()=>{t(!1)}))}))}static removeItem(e){return new Promise((t=>{l.getAll().then((o=>{const n=o.filter((t=>t.word!==e));l.setAll(n),t(!0)})).catch((()=>{t(!1)}))}))}static hasItem(e){return new Promise((t=>{l.getAll().then((o=>{const n=o.find((t=>t.word===e));t(!!n)})).catch((()=>{t(!1)}))}))}static findItem(e){return new Promise((t=>{l.getAll().then((o=>{const n=o.find((t=>t.word===e));t(n)})).catch((()=>{t(null)}))}))}static clearAll(){return new Promise((e=>{chrome.storage.local.remove([s],(()=>{e()}))}))}}l.init=()=>d({apiModule:"WordApi",apiName:"getAllWords",apiParams:{}}).then((e=>l.setAll(e))).then((()=>(i.warn("Cache all collected words successfully"),l.getAll()))).then((e=>(i.warn(e),e))).catch((()=>{}));class u{static getAll(){return new Promise((e=>{chrome.storage.local.get([c],(t=>{const o=t[c];e(o||[])}))}))}static setAll(e){return new Promise((t=>{chrome.storage.local.set({[c]:e},(()=>{t()}))}))}static clearAll(){return new Promise((e=>{chrome.storage.local.remove([c],(()=>{e()}))}))}}function d(e){return new Promise(((t,o)=>{chrome.runtime.sendMessage({type:a.FetchData,content:e},(e=>{"success"===e.status?t(e.content):o(e.content)}))}))}u.init=()=>d({apiModule:"BooksApi",apiName:"getBookList",apiParams:{}}).then((e=>u.setAll(e))).then((()=>(i.warn("Cache all books successfully"),u.getAll()))).then((e=>(i.warn(e),e))).catch((()=>{})),chrome.runtime.onMessage.addListener((e=>{const{type:t,content:o,source:n}=e,{state:r,word:s,playType:c}=o;if("background"!==n)return;if(t!==a.PlayVoice)return;if(!o)return;const i=function(e){const t=new Audio;var o;return o=e.word,/^[a-zA-Z]+$/.test(o)?t.src=function(e,t){return`https://dict.youdao.com/dictvoice?audio=${e.replace(/\s/g,"+")}&type=${t}`}(e.word,e.voiceType):t.src=function(e,t){const o=window.btoa(e);return`https://api.frdic.com/api/v2/speech/speakweb?langid=en&voicename=${{1:"en_uk_female",2:"en_us_male"}[t]}&txt=QYN${o}`}(e.word,e.voiceType),t.volume=1,t.play(),t}({word:s,voiceType:c});"play"===r&&i.play(),i.addEventListener("playing",(()=>{chrome.runtime.sendMessage({type:a.playVoiceState,content:{state:"playing",playType:c,word:s}})})),i.addEventListener("error",(()=>{chrome.runtime.sendMessage({type:a.playVoiceState,content:{state:"error",playType:c,word:s}})})),i.addEventListener("ended",(()=>{chrome.runtime.sendMessage({type:a.playVoiceState,content:{state:"ended",playType:c,word:s}})}))}))})();