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