const UG_EXAMS=window.SB_PAGE_DATA.ugExams;
function UgPapersGrid(){
const {Badge,Button,Tag}=window.ScorebuddyDesignSystem_ee75f2;
const TABS=Object.keys(UG_EXAMS);
const [tab,setTab]=React.useState(TABS[0]);
const cur=UG_EXAMS[tab];
// Admin placements: attach a test to a paper, rename or hide it, or add papers (same design).
const LS=window.SBListings,L=LS?LS.use('ug-papers'):[];
// Admin extras per row: the paper PDF (o.pdf) and a test per section chip (o.sections).
const extras=(o)=>({pdf:o.pdf?o.pdf.url:null,pdfName:o.pdf?o.pdf.name:null,secLinks:Object.fromEntries((o.sections||[]).map(x=>[String(x.label).toUpperCase(),x.href]))});
let years=cur.years.map(r=>{const o=LS&&LS.forSlot(L,tab,r.y);if(!o)return r;if(o.hidden)return null;const t=o.test;
return Object.assign({},r,{title:o.title,href:t?t.href:null,q:t?t.questions:r.q,t:t?t.durationMin+' mins':r.t,secs:o.subtitle||r.secs},extras(o));}).filter(Boolean);
/* An added paper (e.g. "IPMAT Indore 2026") looks like the built-in ones: "<exam> <year> — Question Paper + Key"
   and, when its question count matches, the section tiles (questions and minutes) of the latest built-in paper. */
const tpl=cur.years[0]||{};
const tplQs=String(tpl.secs||'').split(' · ').reduce((n,s)=>n+(+((s.match(/\s(\d+)$/)||[])[1])||0),0);
const addedRow=o=>{const y=(String(o.title||'').match(/\b(?:19|20)\d{2}\b/)||[])[0],same=tplQs&&o.test.questions===tplQs;
const plain=y&&new RegExp('^\\s*('+tab.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')+'\\s*)?'+y+'\\s*$','i').test(o.title);
return Object.assign({position:o.position,y:y||o.title,title:plain?null:o.title,href:o.test.href,q:o.test.questions,t:o.test.durationMin+' mins',
st:same?tpl.st:LS.TYPE[o.test.testType]||'—',secs:o.subtitle||(same?tpl.secs:(o.test.subjects||[]).join(' · '))},extras(o));};
/* An added row for a year that already has a built-in row with no test of its own (same question count) IS that paper:
   one card, in the built-in row's format (its section tiles and badge), with the added row's title, test, PDF and section tests. */
if(LS){const extra=[];LS.added(L,tab).forEach(o=>{const row=addedRow(o),i=years.findIndex(r=>String(r.y)===String(row.y)&&!r.href&&+r.q===+row.q);
if(i<0){extra.push(row);return;}
const r=years[i];years=years.slice();years[i]=Object.assign({},r,{title:row.title||r.title,href:row.href,q:row.q,t:row.t,secs:o.subtitle||r.secs,pdf:row.pdf||r.pdf,pdfName:row.pdfName||r.pdfName,secLinks:Object.assign({},r.secLinks,row.secLinks)});});
years=LS.insert(years,extra);}
const rows=years.map((r,ri)=>{
/* "QA (SA) 15 · QA (MCQ) 30 · Verbal Ability 45" + "40 + 40 + 40 mins" → section tiles. The sectional
   test for a tile is the one attached in Admin → Tests & Mocks → Website pages (by the section's label). */
const labels=String(r.secs||'').split(' · ').filter(Boolean);
const mins=String(r.st||'').replace(/mins?/i,'').split('+').map(x=>parseInt(x,10)).filter(x=>x>0);
const sections=labels.map((s,i)=>{const m=s.match(/^(.*?)\s+(\d+)$/);
return {name:m?m[1]:s,qs:m?+m[2]:null,mins:mins.length===labels.length?mins[i]:null,href:(r.secLinks&&r.secLinks[s.toUpperCase()])||null};});
return React.createElement(React.Fragment,{key:ri},window.SBPaperCard({
badge:r.isNew?tab+' · New':tab,isNew:r.isNew,title:r.title||tab+' '+r.y+' — Question Paper + Key',
meta:[{icon:'⏱',text:r.t},{icon:'📝',text:r.q?r.q+' questions':''},{icon:'🧩',text:sections.length?sections.length+' section'+(sections.length===1?'':'s'):''}],
sections,fullHref:r.href||null,pdf:r.pdf||null,pdfName:r.pdfName}));
});
return React.createElement('section',{style:{padding:'64px 48px',maxWidth:900,margin:'0 auto'}},
React.createElement('h2',{style:{fontFamily:'var(--font-display)',fontWeight:800,fontSize:28,marginBottom:10}},'Previous Year ',React.createElement('span',{style:{color:'var(--brand-accent)'}},'Question Papers')),
React.createElement('div',{style:{display:'flex',gap:8,flexWrap:'wrap',margin:'0 0 8px'}},
TABS.map(t=>React.createElement(Tag,{key:t,active:tab===t,onClick:()=>setTab(t)},t))),
React.createElement('p',{style:{fontFamily:'var(--font-body)',fontSize:13.5,color:'var(--text-muted)',margin:'0 0 20px'}},cur.note),
React.createElement('div',{style:{display:'flex',flexDirection:'column',gap:16}},rows),
/* the same papers as free reading pages (question + solution, no sign-up) — also how search engines find them */
React.createElement('p',{style:{fontFamily:'var(--font-body)',fontSize:14.5,color:'var(--text-muted)',margin:'20px 0 0',textAlign:'center'}},'Prefer reading? ',React.createElement('a',{href:'/pyq#ipm',style:{fontWeight:700}},'Every IPMAT, JIPMAT & IIM B question with its solution, free →')),
React.createElement('p',{style:{fontFamily:'var(--font-body)',fontSize:14.5,color:'var(--text-muted)',margin:'8px 0 0',textAlign:'center'}},'IIM Bangalore B.Sc. aspirant? ',React.createElement('a',{href:'iimb-ug-sample-paper.html',style:{fontWeight:700}},'IIMB UG sample paper'),', admit card and exam pattern →'));
}
window.UgPapersGrid=UgPapersGrid;
