"use strict";const t=require("../../../../common/vendor.js"),e=class e{constructor(){this.isUseCache=!0,this.translationCache=new Map}static getInstance(){return e.instance||(e.instance=new e),e.instance}async get(e){if(this.isUseCache){const t=this.translationCache.get(e.ID);if(void 0!==t)return t}const s=t.Jt.getMessageModel(e.ID);if(!s)return[];const{text:n}=s.getMessageContent()||{},a=[],i=await this.getNickList(s);for(let t=0;t<n.length;++t){const e=n[t];if("img"===e.name){a.push({type:"face",value:e.src});continue}const{transSplitingList:s,atNickList:r}=this.getSplitResult(e.text,i);for(let t=0;t<s.length;++t)a.push({type:"text",value:s[t]}),t<r.length&&a.push({type:"mention",value:r[t]})}const r=[],c=a.filter(((t,e)=>"text"===t.type&&""!==t.value.trim()&&(r.push(e),!0))).map((t=>t.value));if(0===c.length)return this.translationCache.set(s.ID,a),a;return(await this.getTranslationStandard(c)).forEach(((t,e)=>{a[r[e]].value=t})),this.translationCache.set(s.ID,a),a}clear(){this.translationCache.clear()}disableCache(){this.isUseCache=!1}enableCache(){this.isUseCache=!0}getTranslationStandard(e){return new Promise(((s,n)=>{t.Qt.translateText({sourceTextList:e,sourceLanguage:"auto"}).then((t=>{const{data:{translatedTextList:e}}=t;s(e)})).catch((t=>{n(t)}))}))}async getNickList(e){const s=[],{atUserList:n=[]}=e,a=t.qt.TYPES.MSG_AT_ALL;if(n.includes(a)&&s.push(`@${t.Wt.t("TUIChat.所有人")}`),n.length>0){const{data:e}=await t.Zt.getUserProfile({userIDList:n});e.forEach((t=>{const e=`@${t.nick||t.userID}`;s.push(e)}))}return[...new Set(s)]}getSplitResult(t,e){let s=0;const n=[],a=[];for(;s<t.length;){const i=t.indexOf("@",s);if(-1===i){n.push(t.substring(s));break}let r=!1;for(let c=0;c<e.length;++c){const h=t.indexOf(e[c],i);if(-1!==h&&h===i){n.push(t.substring(s,h)),a.push(e[c]),s=h+e[c].length,r=!0;break}}if(!r){n.push(t.substring(s));break}}return{transSplitingList:n,atNickList:a}}};e.instance=void 0;const s=e.getInstance();exports.translator=s;
