(function(){"use strict";function b(e,t){const n=e.replace(/^BOSS_/i,"");return t.get(n.toLowerCase())??n}function X(e,t){let n=0;for(let r=0;r<t.length;r++)e.includes(t[r])&&(n|=1<<r);return n}function G(e){return`${e.species}\0${e.gender}\0${e.passiveMask}`}function I(e){return e.source?{palbox:4,base:3,party:2,unknown:1}[e.source.location]*1e6+(e.source.talentHp??0)+(e.source.talentShot??0)+(e.source.talentDefense??0):0}function A(e,t){return e.captureCount-t.captureCount||e.generation-t.generation||e.breedCount-t.breedCount||I(t)-I(e)}const L=new WeakMap;function $(e){const t=L.get(e);if(t)return t;let n;return e.source?n=`source:${e.species}:${e.passiveMask}`:e.requiredCapture?n=`capture:${e.species}`:e.parents?n=`${e.species}:${e.gender}:${e.passiveMask}(${$(e.parents[0])}+${$(e.parents[1])})`:n=`${e.species}:${e.gender}:${e.passiveMask}`,L.set(e,n),n}function R(e,t){return e==="*"||t==="*"||e===t}function B(e,t,n,r){if(!R(e.gender,t)||!R(n.gender,r)||e.source&&n.source&&e.source.instanceId===n.source.instanceId)return!1;const M=t==="*"?e.gender:t,f=r==="*"?n.gender:r;return M==="*"||f==="*"||M!==f}function F(e,t,n,r,M,f){const l=new Map;for(const[i,,c,,a]of e.recipes)l.set(i.toLowerCase(),i),l.set(c.toLowerCase(),c),l.set(a.toLowerCase(),a);const w=b(n,l),m=(1<<r.length)-1,E=i=>{const c=new Map,a=p=>{var g;p.requiredCapture&&c.set(`${p.species}\0${p.gender}`,p),(g=p.parents)==null||g.forEach(a)};return a(i),{target:i,requiredCaptures:[...c.values()]}},O=i=>{const c=new Map,a=[],p=new Set;let g=0;const T=(s,u=c)=>{const S=G(s),C=u.get(S)??[],o=$(s),h=C.filter(_=>$(_)!==o);u.set(S,[...h,s].sort(A).slice(0,5)),f&&s.species===w&&s.passiveMask===m&&s.parents&&!p.has(o)&&(p.add(o),a.push(s),a.sort(A),g<5&&a.length>g&&(g+=1,f(a.slice(0,g).map(E))))};for(const s of t)s.gender&&T({species:b(s.characterId,l),gender:s.gender==="male"?"m":"f",passiveMask:X(s.passives,r),generation:0,breedCount:0,captureCount:0,source:s});if(i){for(const s of new Set(l.values()))if(s!==w)for(const u of["m","f"])T({species:s,gender:u,passiveMask:0,generation:0,breedCount:0,captureCount:1,requiredCapture:!0})}for(let s=1;s<=Math.max(0,M);s++){const u=new Map;for(const C of c.values())for(const o of C){const h=u.get(o.species)??[];h.push(o),u.set(o.species,h)}const S=new Map;for(const[C,o,h,_,q]of e.recipes){const K=u.get(C),U=u.get(h);if(!(!K||!U))for(const v of K)for(const k of U){if(!B(v,o,k,_))continue;const V=Math.max(v.generation,k.generation)+1;V===s&&T({species:q,gender:"*",passiveMask:v.passiveMask|k.passiveMask,generation:V,breedCount:v.breedCount+k.breedCount+1,captureCount:v.captureCount+k.captureCount,parents:[v,k]},S)}}for(const C of S.values())for(const o of C)T(o)}const x=[...c.values()].flat().filter(s=>s.species===w&&s.passiveMask===m&&!!s.parents);return x.sort(A),{targets:x.slice(0,5),states:c}},y=O(!1),N=(y.targets.length>0?y.targets:O(!0).targets).map(E),d=N[0];return{target:(d==null?void 0:d.target)??null,routes:N,reachableSpecies:new Set([...y.states.values()].flat().map(i=>i.species)).size,requiredCaptures:(d==null?void 0:d.requiredCaptures)??[]}}self.onmessage=e=>{try{const{data:t,owned:n,targetId:r,desiredPassives:M,maxGenerations:f}=e.data,w={type:"complete",solution:F(t,n,r,M,f,m=>{const E={type:"progress",routes:m};self.postMessage(E)})};self.postMessage(w)}catch(t){const n={type:"error",error:t instanceof Error?t.message:String(t)};self.postMessage(n)}}})();
