const fs=require('fs'),vm=require('vm');
function E(id=''){let h='';return{id,textContent:'',children:[],onclick:null,onchange:null,value:'',files:[],set innerHTML(v){h=v;if(v==='')this.children=[]},get innerHTML(){return h},appendChild(x){this.children.push(x);return x},prepend(x){this.children.unshift(x);return x},remove(){},lastChild:null}}
const els={},store={},ctx={console,Math,Date,Blob:function(){},URL:{createObjectURL(){return'x'}},FileReader:function(){},localStorage:{setItem(k,v){store[k]=v},getItem(k){return store[k]??null}},document:{getElementById(id){return els[id]||(els[id]=E(id))},createElement(){return E()}},window:{}};ctx.window=ctx;vm.createContext(ctx);['actions','screen','log','status','saveBtn','loadBtn','newBtn','exportBtn','importBtn','importFile'].forEach(id=>els[id]=E(id));
for(const f of ['data.js','tournaments.js','engine.js','game.js'])vm.runInContext(fs.readFileSync(__dirname+'/'+f,'utf8'),ctx);vm.runInContext('globalThis.D=FOOTBALL_DATA;globalThis.E=Engine;',ctx);
const Q=ctx.__FIFA_CUP_QA,D=ctx.D,E2=ctx.E;function A(c,m){if(!c)throw Error(m)}
const bad=ctx.FOOTBALL_TOURNAMENTS.find(t=>t.id==='ucl').teams.find(id=>E2.squad(id).length<11);A(bad,'no unavailable UCL opponent in current data');
const user=ctx.FOOTBALL_TOURNAMENTS.find(t=>t.id==='ucl').teams.find(id=>E2.squad(id).length>=18);A(user,'user');Q.startTournament('ucl',user);let s=Q.getState();s.tournament.phase='league';s.tournament.round=0;s.tournament.calendar=[[[user,bad]]];s.tournament.table={[user]:{p:0,w:0,d:0,l:0,gf:0,ga:0,pts:0},[bad]:{p:0,w:0,d:0,l:0,gf:0,ga:0,pts:0}};Q.setState(s);Q.startMatch(false);s=Q.getState();A(s.match===null,'auto unavailable match should finish');A(s.tournament.history.length===1,'history');console.log(JSON.stringify({unavailableOpponent:bad,autoFinished:true}));
