1import{s as A}from"./index-BQSuW3M2.js";const E=async(r,n,t)=>{try{console.log("ð Starting hero image detection for:",r),console.log("ð HTML content length:",n.length);const l=new DOMParser().parseFromString(n,"text/html"),h=Array.from(l.querySelectorAll("img"));if(console.log("ð¼ï¸ Found images:",h.length),h.length===0)return console.log("â No images found in chapter"),{url:null,alt:null,confidence:"low"};h.forEach((s,p)=>{console.log(`Image ${p+1}: src="${s.getAttribute("src")}", alt="${s.getAttribute("alt")}"`)});const a=Array.from(l.querySelectorAll("h1, h2, h3, h4, h5, h6"))[0],w=Array.from(l.querySelectorAll("p")),f=await Promise.all(h.map(async s=>{const p=s.getAttribute("src")||"",y=s.getAttribute("alt")||null,b=$(s,a,l.body),u=P(s,l.body),d=I(s),m=S(s,b,d,u,w.length,l.body);return{element:s,src:p,alt:y,score:m,dimensions:d,position:b,distanceFromChapterStart:u}}));f.sort((s,p)=>p.score-s.score);const c=f[0];if(!c||c.score<30)return{url:null,alt:null,candidates:f,confidence:"low"};const i=await k(r,c.src,c.alt,t),g=c.score>70?"high":c.score>50?"medium":"low";return{url:i.url,alt:i.alt,candidates:f,confidence:g}}catch(o){return console.error("ð¥ Error in hero image detection:",o),console.error("Error details:",o.message,o.stack),{url:null,alt:null,confidence:"low"}}};function $(r,n,t){if(!n)return"standalone";const o=v(r,t),l=v(n,t);return o<l?"before-heading":o<l+5?"after-heading":o<10||Array.from(t.querySelectorAll("p")).slice(0,3).some(a=>a.contains(r))?"early-content":"standalone"}function v(r,n){const t=document.createTreeWalker(n,NodeFilter.SHOW_ELEMENT,null);let o=0,l=t.nextNode();for(;l;){if(l===r)return o;o++,l=t.nextNode()}return o}function P(r,n){return v(r,n)}function I(r){const n=r.getAttribute("width")?parseInt(r.getAttribute("width")):r.naturalWidth||0,t=r.getAttribute("height")?parseInt(r.getAttribute("height")):r.naturalHeight||0;return n===0&&t===0?{width:400,height:300}:{width:n,height:t}}function x(r,n,t){const{width:o,height:l}=n;if(o*l>5e5||o>800&&l>600)return!0;const e=t.clientWidth||800;if(o>e*.8)return!0;const a=window.getComputedStyle(r);return a.width==="100%"||a.width==="100vw"||a.maxWidth==="100%"}function C(r){const{width:n,height:t}=r;return n>800||n/t>1.5}function S(r,n,t,o,l,h){let e=0;switch(n){case"before-heading":e+=50;break;case"after-heading":e+=40;break;case"early-content":e+=30;break;case"standalone":e+=20;break}o<=3?e+=20:o<=7?e+=15:o<=15?e+=10:e-=5;const a=t.width*t.height,w=x(r,t,h),f=C(t);w?e+=30:f&&a>3e5?e+=25:a>3e5?e+=20:a>15e4?e+=18:a>5e4?e+=15:a>2e4?e+=10:a>5e3?e+=5:e-=15;const c=t.width/t.height;c>=1.2&&c<=2.5?e+=20:c>=1&&c<1.2?e+=15:c>2.5?e+=10:e-=10;const i=r.getAttribute("alt")||"";i.length>20?e+=8:i.length>10?e+=5:i.length>0&&(e+=2);const g=r.getAttribute("class")||"";/hero|banner|header|cover|featured|main|primary/i.test(g)&&(e+=20),/full|page|wide|large/i.test(g)&&(e+=15),/thumbnail|icon|small|mini|decorative/i.test(g)&&(e-=15);const s=r.getAttribute("src")||"";return/hero|banner|header|cover|featured|main|primary/i.test(s)&&(e+=15),/full|page|wide|large/i.test(s)&&(e+=10),/thumb|icon|small|mini|decorative/i.test(s)&&(e-=10),o<=3?(e+=25,f&&(e+=15)):o<=10&&(e+=15),(w||f||/hero|banner|header|cover|featured|main|primary/i.test(g)||/hero|banner|header|cover|featured|main|primary/i.test(s)||i.length>15&&/hero|banner|cover|featured/i.test(i.toLowerCase()))&&(e+=20),(a<1e4||/icon|thumb|decorative/i.test(g))&&(e-=25),Math.max(0,e)}async function k(r,n,t,o){try{const h=((b,u)=>{if(u.startsWith("http")||u.startsWith("data:"))return u;try{return new URL(u,`https://example.com/${b}`).pathname.replace(/^\//,"")}catch{return u}})(r,n);console.log(`ð§ Resolving image: "${n}" -> "${h}"`);const e=o.resources?.get?.(h)||o.resources?.get?.(n);if(!e)return console.log(`â Could not find resource for: ${h} or ${n}`),console.log("ð Available resources:",Object.keys(o.resources?.resources||{})),{url:null,alt:t};console.log("â
Found resource for image:",h);let a=null;try{e.getBlob&&(a=await e.getBlob())}catch{}if(!a)try{e.blob&&(a=await e.blob())}catch{}
1if(!a)try{if(e.getBase64){const b=await e.getBase64(),u=atob(b),d=new Uint8Array(u.length);for(let m=0;m<u.length;m++)d[m]=u.charCodeAt(m);a=new Blob([d],{type:"image/*"})}}catch{}if(!a)return console.log("â Failed to extract blob from resource"),{url:null,alt:t};console.log("â
Successfully extracted blob, size:",a.size);const w=Date.now(),f=Math.random().toString(36).slice(2,10),c=(h.split(".").pop()||"jpg").toLowerCase();let i;switch(c){case"png":i="image/png";break;case"webp":i="image/webp";break;case"gif":i="image/gif";break;case"svg":i="image/svg+xml";break;case"bmp":i="image/bmp";break;default:i="image/jpeg";break}const g=`enhanced/epub/${w}_${f}.${c}`;console.log(`ð Uploading to path: ${g} with content type: ${i}`);const{error:s}=await A.storage.from("chapter-hero-images").upload(g,a,{contentType:i,upsert:!1,cacheControl:"3600"});if(s){if(console.error("â Storage upload error:",s),s.message?.includes("duplicate")){console.log("ð Duplicate file detected, trying with new random ID");const b=Math.random().toString(36).slice(2,10),u=`enhanced/epub/${w}_${b}.${c}`,{error:d}=await A.storage.from("chapter-hero-images").upload(u,a,{contentType:i,upsert:!1,cacheControl:"3600"});if(d)return console.error("â Retry upload failed:",d),{url:null,alt:t};const{data:m}=A.storage.from("chapter-hero-images").getPublicUrl(u);return{url:m.publicUrl||null,alt:t}}return{url:null,alt:t}}const{data:p}=A.storage.from("chapter-hero-images").getPublicUrl(g),y=p.publicUrl||null;return y?console.log(`â
Successfully uploaded and made public: ${y}`):console.warn("â ï¸ Upload successful but public URL not available"),{url:y,alt:t}}catch(l){return console.error("ð¥ Error extracting image:",l),{url:null,alt:null}}}export{E as detectHeroImages};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.