import{PopStateHandlers as v,setMultipleURLParams as _}from"/static/js/utils/utils.js?v=c676bb123f83b0ed";import{jsx as w}from"/static/js/utils/utils.js?v=c676bb123f83b0ed";// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
var y="_",u=/_+/g,p=/[#?]/g,h=/\s+/g,M=/(.)(?=.*\1)/g;function b(){return document.getElementById("hangman-solver-form")}var c=e=>e.value.toLowerCase().replace(h,"");function m(){return{input:[document.querySelector("input[name='input']"),e=>c(e).replace(p,y)],invalid:[document.querySelector("input[name='invalid']"),e=>{let t=c(e).replace(p,"").replace(u,""),r=[...new Set(t)];return r.sort(),r.join("")}],max_words:[document.querySelector("input[name='max_words']"),e=>Number.parseInt(e.value,10)],crossword_mode:[document.querySelector("input[name='crossword_mode']"),e=>e.checked],lang:[document.querySelector("select[name='lang']"),c]}}function C(){let e=m();return{input:e.input[1](e.input[0]),invalid:e.invalid[1](e.invalid[0]),max_words:e.max_words[1](e.max_words[0]),crossword_mode:e.crossword_mode[1](e.crossword_mode[0]),lang:e.lang[1](e.lang[0])}}function x(){return{letterOutput:document.getElementById("letter-frequency-information"),wordOutput:document.getElementById("matching-words")}}function H(e){return((e.crossword_mode?"":e.input.replace(u,""))+e.invalid).toLowerCase().replace(h,"").replace(M,"")}function L(e){let t="[^"+H(e)+"]",r=e.input.replace(u,n=>t+(n.length>1?"{".concat(n.length,"}"):""));return new RegExp("^"+r+"$","u")}async function T(e){let t=[...e.input].length;try{return await I(e.lang,t)}catch(r){return console.error("error loading words",r),[]}}var g=new Map;async function I(e,t){let r=g.get(e);if(r){let o=r.get(t);if(o)return o}else r=new Map,g.set(e,r);let n=await fetch("/hangman-loeser/worte/".concat(e.toLowerCase(),"/").concat(t,".txt"),{method:"GET",headers:{Accept:"text/plain"}});n.status!==200&&n.status!==404&&console.error("error loading words",n);let s=n.ok?(await n.text()).split("\n"):[];return r.set(t,s),s}function f(e,t,r,n){var o,a,l;if(n){for(let i of r)e.set(i,((o=e.get(i))!=null?o:0)-1);for(let i of t)e.set(i,((a=e.get(i))!=null?a:0)+1);return}let s=[...r];for(let i of t)s.includes(i)||(e.set(i,((l=e.get(i))!=null?l:0)+1),s.push(i))}async function O(e){let t=await T(e),r=e.invalid,n=e.input.replace(u,""),s=r.length===0&&n.length===0;if(n.length===e.input.length){let o=n.toLowerCase();return R(t,o)?[[o],new Map]:[[],new Map]}else if(s){let o=new Map;return t.forEach(a=>{f(o,a,"",e.crossword_mode)}),[t,o]}else{let o=new Map,a=L(e);return[t.filter(l=>a.test(l)?(f(o,l,n,e.crossword_mode),!0):!1),o]}}async function S(e){let[t,r]=await O(e),n=x();if(t.length>0){n.wordOutput.replaceChildren(Math.min(e.max_words,t.length).toString(),"/",t.length.toString()," passenden Wörter:",w("ul",{children:t.slice(0,e.max_words).map(o=>w("li",{children:[o]}))}));let s=[...r.entries()].filter(([o,a])=>a>0);s.sort((o,a)=>a[1]-o[1]),n.letterOutput.replaceChildren("Mögliche Buchstaben: ",s.map(o=>o.join(": ")).join(", "))}else n.letterOutput.replaceChildren("Nichts gefunden."),n.wordOutput.replaceChildren("")}function R(e,t){let r=0,n=e.length-1;for(;r<=n;){let s=Math.floor((r+n)/2);if(e[s]===t)return!0;e[s]<t?r=s+1:n=s-1}return!1}var E="HangmanSolverState";function d(e=void 0){let t=C();return _(Object.entries(t).map(([r,n])=>[r,n.toString()]),t,E),e==null||e.preventDefault(),S(t)}function W(e){let t=m();t.input[0].value=e.input,t.crossword_mode[0].checked=e.crossword_mode,t.invalid[0].value=e.invalid,t.max_words[0].value=e.max_words.toString(),t.lang[0].value=e.lang}function j(e){let t=e.state;return W(t),e.preventDefault(),S(t)}function q(){v[E]=j,b().addEventListener("submit",d);let e=m();e.lang[0].addEventListener("change",d),e.crossword_mode[0].addEventListener("change",d)}q();export{I as _loadWords};
// @license-end
//# sourceMappingURL=hangman_solver.js.map
