(function(){
'use strict';
/* ---- helpers ---- */
function g(s){return document.querySelector(s);}
function gid(s){return document.getElementById(s);}
function st(h){return(h||'').replace(/]*>/g,'').replace(/&[^;]+;/g,' ').trim();}
function esc(s){return String(s||'').replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"');}
function setH(id,url){var el=gid(id);if(el)el.href=url;}
/* ---- detect post ID ---- */
var postId=0;
document.body.classList.forEach(function(c){
var m=c.match(/^postid-(d+)$/);
if(m)postId=parseInt(m[1],10);
});
/* ---- API base ---- */
var BASE=(typeof wpApiSettings!=='undefined'&&wpApiSettings.root)
?wpApiSettings.root.replace(//$/,'')
:location.origin+'/wp-json';
/* ---- public copy helper ---- */
window.ECL={
copy:function(){
navigator.clipboard.writeText(location.href).then(function(){
var i=g('.ecl-sh-btn [data-icon="link"]')||document.querySelector('.ecl-sh-btn .fa-link');
if(i){
i.className=i.className.replace('fa-link','fa-check');
setTimeout(function(){i.className=i.className.replace('fa-check','fa-link');},2000);
}
});
}
};
/* ---- abort early if no post ID ---- */
if(!postId){
gid('ecl-loading').style.display='none';
return;
}
/* ---- fetch everything in parallel ---- */
Promise.all([
fetch(BASE+'/wp/v2/posts/'+postId+'?_embed=wp:featuredmedia,wp:term').then(function(r){return r.json();}),
fetch(BASE+'/wp/v2/posts/'+(postId-1)+'?_fields=id,title,link').then(function(r){return r.ok?r.json():null;}).catch(function(){return null;}),
fetch(BASE+'/wp/v2/posts/'+(postId+1)+'?_fields=id,title,link').then(function(r){return r.ok?r.json():null;}).catch(function(){return null;})
]).then(function(res){
var post=res[0],prev=res[1],next=res[2];
if(post&&!post.code){
render(post);
buildNav(prev,next);
loadRelated(post);
} else {
gid('ecl-loading').style.display='none';
}
}).catch(function(){
gid('ecl-loading').style.display='none';
});
/* ---- render post ---- */
function render(post){
var url=location.href;
var title=st(post.title.rendered);
var enc=encodeURIComponent(url);
var encT=encodeURIComponent(title);
/* category */
var cat='',catUrl='/blog',catId=0;
try{var t0=post._embedded['wp:term'][0][0];cat=t0.name;catUrl=t0.link;catId=t0.id;}catch(e){}
/* reading time */
var words=st(post.content.rendered).split(/s+/).length;
var mins=Math.max(2,Math.ceil(words/200));
/* tags */
var tags=[];
try{tags=post._embedded['wp:term'][1]||[];}catch(e){}
/* excerpt */
var exc=st(post.excerpt.rendered);
/* --- fill hero --- */
gid('ecl-bc-cat').textContent=cat||'مقال';
gid('ecl-cat-badge-text').textContent=cat||'مقال';
gid('ecl-cat-badge').href=catUrl;
gid('ecl-title').textContent=title;
gid('ecl-read-time').textContent=mins+' دقائق قراءة';
gid('ecl-cat-meta').textContent=cat||'—';
if(exc){
var excEl=gid('ecl-excerpt');
excEl.textContent=exc;
excEl.style.display='block';
}
/* --- article content --- */
gid('ecl-content').innerHTML=post.content.rendered;
/* --- tags --- */
if(tags.length){
var tel=gid('ecl-tags');
tel.style.display='flex';
tags.forEach(function(t){
var a=document.createElement('a');
a.href=t.link||'#';
a.textContent=t.name;
tel.appendChild(a);
});
}
/* --- share links --- */
var sharesAll=[
['ecl-sh-fb','ecl-sb-fb','https://www.facebook.com/sharer/sharer.php?u='+enc],
['ecl-sh-tw','ecl-sb-tw','https://x.com/share?url='+enc+'&text='+encT],
['ecl-sh-li','ecl-sb-li','https://www.linkedin.com/shareArticle?mini=true&url='+enc+'&title='+encT],
['ecl-sh-wa','ecl-sb-wa','https://wa.me/?text='+encT+'%20'+enc],
['ecl-sh-tg',null,'https://t.me/share/url?url='+enc+'&text='+encT]
];
sharesAll.forEach(function(s){
setH(s[0],s[2]);
if(s[1])setH(s[1],s[2]);
});
/* --- build TOC & reveal --- */
buildTOC();
gid('ecl-loading').style.display='none';
gid('ecl-main').style.display='block';
initScroll();
document.title=title+' — ECL Report';
}
/* ---- build table of contents ---- */
function buildTOC(){
var hs=document.querySelectorAll('#ecl-content h2,#ecl-content h3');
var list=gid('ecl-toc-list');
if(!hs.length){
list.innerHTML='
لا يوجد فهرس';
return;
}
var idx=0,html='';
hs.forEach(function(h){
var id=h.id||('ech'+(idx++));
h.id=id;
h.style.scrollMarginTop='80px';
var cls=h.tagName==='H3'?' class="tl3"':'';
html+='
'+h.textContent+'';
});
list.innerHTML=html;
var links=list.querySelectorAll('a');
var io=new IntersectionObserver(function(es){
es.forEach(function(e){
if(e.isIntersecting){
links.forEach(function(l){l.classList.remove('on');});
var a=list.querySelector('a[href="#'+e.target.id+'"]');
if(a)a.classList.add('on');
}
});
},{rootMargin:'-20% 0px -74% 0px'});
hs.forEach(function(h){io.observe(h);});
links.forEach(function(a){
a.addEventListener('click',function(e){
e.preventDefault();
var t=document.querySelector(a.getAttribute('href'));
if(t)t.scrollIntoView({behavior:'smooth',block:'start'});
});
});
}
/* ---- load related posts ---- */
function loadRelated(post){
var catId=0;
try{catId=post._embedded['wp:term'][0][0].id;}catch(e){}
if(!catId){gid('ecl-related').style.display='none';return;}
var FB=[
'https://images.unsplash.com/photo-1554224155-6726b3ff858f?w=640&q=75',
'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=640&q=75',
'https://images.unsplash.com/photo-1460925895917-afdab827c52f?w=640&q=75'
];
fetch(BASE+'/wp/v2/posts?_embed=wp:featuredmedia,wp:term&per_page=3&categories='+catId+'&exclude='+post.id)
.then(function(r){return r.json();})
.then(function(posts){
if(!posts||!posts.length){gid('ecl-related').style.display='none';return;}
gid('ecl-rel-grid').innerHTML=posts.map(function(p){
var img='',rcat='',rt=st(p.title.rendered);
try{img=p._embedded['wp:featuredmedia'][0].source_url;}catch(e){}
if(!img)img=FB[p.id%3];
try{rcat=p._embedded['wp:term'][0][0].name;}catch(e){}
return '
'
+''
+''
+'
'+esc(rcat)+'
'
+'
'+esc(rt)+'
'
+'
اقرأ المقال
'
+'
';
}).join('');
})
.catch(function(){gid('ecl-related').style.display='none';});
}
/* ---- prev / next navigation ---- */
function buildNav(prev,next){
if(prev&&!prev.code){
var el=gid('ecl-prev');
el.href=prev.link;
gid('ecl-prev-t').textContent=st(prev.title.rendered);
el.style.display='flex';
}
if(next&&!next.code){
var el2=gid('ecl-next');
el2.href=next.link;
gid('ecl-next-t').textContent=st(next.title.rendered);
el2.style.display='flex';
}
}
/* ---- scroll handler: progress bar + TOC ---- */
function initScroll(){
var prog=gid('ecl-prog');
var fill=gid('ecl-rp-fill');
var pct=gid('ecl-rp-pct');
var art=gid('ecl-content');
window.addEventListener('scroll',function(){
var top=art.getBoundingClientRect().top+scrollY;
var h=art.offsetHeight;
var sc=Math.max(0,scrollY-top);
var p=Math.min(100,Math.round(sc/h*100));
if(prog)prog.style.width=p+'%';
if(fill)fill.style.width=p+'%';
if(pct)pct.textContent=p+'%';
},{passive:true});
}
})();