/* CUET (UG) previous year papers (cuet-previous-year-papers.html): year tabs, then the subject papers of that year,
   grouped as Languages · General Test · Domain subjects (from the CUET catalog, GET /api/cuet).
   Papers are placed in Admin → CUET tests → PYQ papers (page 'cuet-pyq', groups "cuetpyq:<year>:<subject key>");
   a second paper of the same year and subject is Shift 2. Each open paper also has free answer & solution pages
   at /cuet/pyq/<year>/<subject>[-shift-N] (server/src/lib/pyq-pages.js). Plan right: CUET PYQs, or the subject's right.
   Styles: the Practice page's fpq-* classes. */
const h=React.createElement;
function cpMins(m){return window.SBListings?window.SBListings.mins(m):m+' mins';}
function cpSlug(s){return String(s||'').toLowerCase().replace(/&/g,' and ').replace(/[^a-z0-9]+/g,'-').replace(/^-+|-+$/g,'').slice(0,60);}

function CuetPyqPage(){
const LS=window.SBListings;
const P=LS?LS.use('cuet-pyq'):[];
const [cat,setCat]=React.useState(null);
const [tests,setTests]=React.useState([]);
const [year,setYear]=React.useState(0);
const [grp,setGrp]=React.useState('all');
React.useEffect(()=>{let on=true;
fetch('/api/cuet',{credentials:'same-origin'}).then(r=>r.ok?r.json():null).then(d=>{if(on)setCat(d||{groups:[],subjects:[]});}).catch(()=>{if(on)setCat({groups:[],subjects:[]});});
(window.SBAuth?window.SBAuth.ready.then(()=>window.SBAuth.api('GET','/mocks')):Promise.reject()).then(l=>{if(on)setTests(Array.isArray(l)?l:[]);},()=>{});
return ()=>{on=false;};},[]);
if(!cat)return h('div',{className:'fpq'},h('div',{className:'fpq-wrap fpq-main'},h('div',{className:'fpq-empty'},h('p',null,'Loading CUET papers…'))));
const subs={};(cat.subjects||[]).forEach(s=>{subs[s.key]=s;});
/* every placed paper: year, subject, shift (counted over open papers, as the solution pages are) */
const shiftN={};
const papers=(LS?LS.added(P):[]).map(o=>{const [,y,k]=String(o.grp||'').split(':');const s=subs[k];if(!/^\d{4}$/.test(y||'')||!s)return null;
const t=o.test,id=(/id=(\d+)/.exec(t.href||'')||[])[1],a=tests.find(x=>String(x.id)===id);
let shift=0;if(!t.comingSoon){const key=y+':'+k;shift=shiftN[key]=(shiftN[key]||0)+1;}
return {year:+y,subj:s,title:o.title||t.title,desc:o.subtitle||'',href:t.href,q:t.questions,dur:cpMins(t.durationMin),marks:t.maxMarks,soon:!!t.comingSoon,opensAt:t.opensAt||null,shift,
attempts:a?a.myAttempts||0:0,best:a?a.myBestPct:null,lastId:a?a.myLastAttemptId:null,tries:a?a.maxAttempts||null:null};}).filter(Boolean);
/* a subject with two papers that year: "Shift 1", "Shift 2" */
papers.forEach(p=>{p.multi=(shiftN[p.year+':'+p.subj.key]||0)>1;});
const years=[...new Set(papers.map(p=>p.year))].sort((a,b)=>b-a);
const curY=years.indexOf(year)>=0?year:years[0];
const inYear=papers.filter(p=>p.year===curY);
const groups=(cat.groups||[]).filter(g=>inYear.some(p=>p.subj.group===g.key));
const curG=grp==='all'||groups.some(g=>g.key===grp)?grp:'all';
const shown=inYear.filter(p=>curG==='all'||p.subj.group===curG);
function tabs(list,cur,set,cls,label){
return h('div',{className:'fpq-tabs '+(cls||''),role:'tablist','aria-label':label},list.map(t=>h('button',{key:t.key,type:'button',role:'tab','aria-selected':cur===t.key,className:'fpq-tab'+(cur===t.key?' on':''),onClick:()=>set(t.key)},
h('span',null,t.label),t.count!=null?h('small',null,t.count):null)));
}
/* Start / Retake with the attempts the admin allows (SBListings.tries), and the card's attempts note */
function cuStart(r,retake,start){
const T=window.SBListings?window.SBListings.tries(r):{suffix:''};
if(T.done)return h('span',{className:'fpq-btn ghost',style:{cursor:'default'},title:'You have used every attempt this test allows'},T.used);
return h('a',{className:'fpq-btn',href:r.href},(r.attempts?retake:start)+T.suffix+' →');
}
function cuTries(r){
const T=!r.soon&&window.SBListings?window.SBListings.tries(r):{};
return T.note?h('p',{className:'fpq-when',style:{margin:'10px 0 0'}},T.note):null;
}
function card(p,i){
const meta=[p.q?p.q+' Qs':null,p.dur||null,p.marks?p.marks+' marks':null].filter(Boolean);
const sol=!p.soon&&p.shift?'/cuet/pyq/'+p.year+'/'+cpSlug(p.subj.key)+(p.shift>1?'-shift-'+p.shift:''):null;
return h('div',{key:(p.href||'')+i,className:'fpq-card'+(p.soon?' soon':'')},
h('div',{className:'fpq-card-top'},p.soon?h('span',{className:'fpq-soon-tag'},'COMING SOON'):null,h('span',{className:'fpq-badge'},'CUET '+p.year),
p.multi&&p.shift?h('span',{className:'fpq-badge was'},'Shift '+p.shift):null,
p.attempts&&!(window.SBListings&&window.SBListings.tries(p).done&&p.best==null)?h('span',{className:'fpq-done'},window.SBListings&&window.SBListings.tries(p).done?'Best '+p.best+'%':'✓ Attempted'+(p.best!=null?' · best '+p.best+'%':'')):null),
h('h3',null,p.subj.name),h('p',null,p.desc||p.title),
h('div',{className:'fpq-meta'},meta.map(m=>h('span',{key:m},m))),
h('div',{className:'fpq-acts'},p.soon?h('span',{className:'fpq-btn ghost',style:{cursor:'default'}},'⏳ Coming soon'):cuStart(p,'Retake paper','Take the paper'),
p.lastId?h('a',{className:'fpq-btn ghost',href:'analysis.html?id='+p.lastId},'Last analysis'):sol?h('a',{className:'fpq-btn ghost',href:sol},'Answers & solutions'):null,
window.SBShare?window.SBShare.button(h,p.href,p.title):null),
cuTries(p));
}
return h('div',{className:'fpq'},
h('section',{className:'fpq-hero'},h('div',{className:'fpq-wrap'},
h('div',{className:'fpq-hero-top'},h('span',{className:'fpq-eyebrow'},'CUET (UG) PYQs')),
h('h1',null,'CUET Previous Year ',h('span',null,'Papers')),
h('p',null,'Real CUET (UG) papers'+(years.length?' from '+years[years.length-1]+(years.length>1?' to '+years[0]:''):'')+' — English, General Aptitude Test and domain subjects — timed and marked like the exam, with your score, rank and a detailed analysis. Every question also has a free answer and solution.'),
h('div',{className:'fpq-jump'},h('a',{href:'#papers'},'Year-wise papers'),h('a',{href:'/cuet/pyq'},'All questions with solutions →'),h('a',{href:'cuet.html'},'CUET mock tests →')))),
h('div',{className:'fpq-wrap fpq-main'},
h('section',{className:'fpq-sec',id:'papers'},
h('div',{className:'fpq-head'},h('div',null,h('h2',null,'Year-wise papers'),h('p',null,'Pick a year, then a subject paper.'))),
years.length?[
h(React.Fragment,{key:'y'},tabs(years.map(y=>({key:y,label:'CUET '+y,count:papers.filter(p=>p.year===y).length})),curY,setYear,'fpq-exam','Year')),
groups.length>1?h('div',{key:'g',style:{marginTop:12}},tabs([{key:'all',label:'All subjects',count:inYear.length}].concat(groups.map(g=>({key:g.key,label:g.label,count:inYear.filter(p=>p.subj.group===g.key).length}))),curG,setGrp,'fpq-under','Subject group')):null,
h('div',{key:'p',className:'fpq-panel',role:'tabpanel'},shown.length?h('div',{className:'fpq-grid'},shown.map(card)):null)]
:h('div',{className:'fpq-empty'},h('div',{className:'fpq-empty-ic','aria-hidden':true},'📜'),h('p',null,'CUET previous year papers are coming soon.'),h('a',{href:'cuet.html',className:'fpq-btn ghost'},'Practise CUET mock tests'))),
h('section',{className:'fpq-sec'},h('div',{className:'fpq-head'},h('div',null,h('h2',null,'Why solve CUET PYQs'),
h('p',null,'CUET repeats its patterns: the same question types, passage lengths and topics come back year after year. Solving the real papers under exam timing is the surest way to know where you stand — and what to work on next.')))))
);
}
window.CuetPyqPage=CuetPyqPage;
