const D=new Set(["SCRIPT","STYLE","NOSCRIPT","TEXTAREA","IFRAME"]),A=n=>n.replace(/\s+/g," ").trim(),m=n=>n.toLowerCase().split("-")[0],c=new Map,w=new Map;let d=!1,f=null,p=!1,v="",b;const y=(n,e)=>x(n,e).race;function x(n,e){let t,r=!1,a;const o=new Promise(i=>{a=i,t=setTimeout(()=>i("timeout"),e)}),s=()=>{r||(clearTimeout(t),t=setTimeout(()=>a("timeout"),e))},l=()=>{r=!0,clearTimeout(t)};return n.then(l,l),{race:Promise.race([n,o]).then(i=>(l(),i)),reset:s}}function W(n){const{loaded:e,total:t}=n;return typeof e!="number"||!Number.isFinite(e)||e<0?null:typeof t=="number"&&Number.isFinite(t)&&t>1&&e<=t?Math.round(e/t*100):e<=1?Math.round(e*100):null}function M(n){const e=n.closest("[translate]");return e&&(e.getAttribute("translate")??"").toLowerCase()==="no"?!0:n.closest(".notranslate, code, pre, kbd, samp, var")!==null}function C(){const n=[];if(!document.body)return n;const e=document.createTreeWalker(document.body,NodeFilter.SHOW_TEXT,{acceptNode:t=>t.parentElement&&!D.has(t.parentElement.tagName.toUpperCase())&&!M(t.parentElement)&&A(t.data)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT});for(let t=e.nextNode();t;t=e.nextNode())n.push(t);return n}function X(n,e){const t=new Set([document.documentElement]);for(const r of e){if(!r.isConnected)continue;const a=r.parentElement?.closest("[lang]");a&&t.add(a)}for(const r of t)!r.isConnected||M(r)||(w.has(r)||w.set(r,r.getAttribute("lang")),r.setAttribute("lang",n))}function F(){for(const[n,e]of w)n.isConnected&&(e===null?n.removeAttribute("lang"):n.setAttribute("lang",e));w.clear(),d=!1}async function I(){const n=document.documentElement.lang;if(n)return n;const e=self.LanguageDetector;if(!e)return null;const t=A(C().map(r=>r.data).join(" ")).slice(0,1e3);if(!t)return null;if(t===v)return b??null;try{const r=await y(e.create(),5e3);if(r==="timeout")return null;const a=await y(r.detect(t),5e3);if(a==="timeout")return null;const o=a[0];v=t,b=o&&typeof o.detectedLanguage=="string"&&(o.confidence??0)>=.5?o.detectedLanguage:null}catch{v=t,b=null}return b??null}async function R(n,e){const t=self.Translator;if(!t)return"unusable";try{const r=await y(t.availability({sourceLanguage:n,targetLanguage:e}),2500);return r==="timeout"?"timeout":r==="available"||r==="downloadable"||r==="downloading"?"usable":"unusable"}catch{return"unusable"}}function Z(n,e){const t=a=>parent.postMessage({__ft_status:a},n),r=async a=>{const o=typeof a.target=="string"&&a.target.length<=35&&/^[a-zA-Z]{2,8}(-[a-zA-Z0-9]{1,8})*$/.test(a.target)?a.target:"en";if(a.action==="probe"){if(p){t({state:"translating",lang:f,usable:!0});return}const s=d?f:await I(),l=c.size>0||(s!==null&&m(s)!==m(o)&&C().length>0?await R(m(s),o)==="usable":!1);t({state:c.size?"translated":"idle",lang:s,usable:l});return}if(a.action==="revert"){if(p)return;for(const[l,i]of c)l.isConnected&&(l.data=i);c.clear();const s=f??b??null;F(),f=null,t({state:"reverted",lang:s});return}if(!(a.action!=="translate"||p)){p=!0;try{const s=d?f:await I(),l=s?m(s):null;if(!l||l===m(o)){t({state:"error",error:"no-source-lang"});return}d||(f=s);const i=self.Translator,N=i?await R(l,o):"unusable";if(!i||N!=="usable"){d||(f=null),t({state:"error",error:N==="timeout"?"engine-timeout":"engine-unavailable"});return}let S=!1,P;const{race:z,reset:j}=x(i.create({sourceLanguage:l,targetLanguage:o,monitor:u=>{const g=O=>{if(S){u.removeEventListener("downloadprogress",g);return}j();const h=W(O);h!==P&&(P=h,t({state:"downloading",lang:s,...h===null?{}:{progress:{done:h,total:100}}}))};u.addEventListener("downloadprogress",g)}}),6e4),_=await z.finally(()=>{S=!0});if(_==="timeout"){d||(f=null),t({state:"error",error:"engine-timeout"});return}d=!0;const T=C(),k=c.size===0;t({state:"translating",lang:s,progress:{done:0,total:T.length}});let L=0;const E=[];for(const u of T){c.has(u)||c.set(u,u.data);const g=await y(_.translate(c.get(u)),1e4);g!=="timeout"&&typeof g=="string"&&A(g)&&(u.data=g,E.push(u)),L+=1,(L%4===0||L===T.length)&&t({state:"translating",lang:s,progress:{done:L,total:T.length}})}if(!E.some(u=>u.isConnected)){k&&(c.clear(),w.clear(),d=!1,f=null),t({state:"error",error:"no-output"});return}X(o,E),t({state:"translated",lang:o})}catch(s){for(const[l,i]of c)l.isConnected&&(l.data=i);c.clear(),F(),f=null,t({state:"error",error:s instanceof Error?s.name:"unknown"})}finally{p=!1}}};addEventListener("message",a=>{if(a.source!==parent||a.origin!==n)return;const o=a.data?.__ft_cmd;o&&typeof o=="object"&&r(o)}),e&&typeof e=="object"&&r(e)}export{Z as install};
