import{logger as e}from"../../logger/index.mjs";import{launchBrowser as t}from"../../utils/puppeteer/launchBrowser.mjs";import{ALL_LOCALES as n}from"@intlayer/types/allLocales";const r=/name\s*:\s*['"]([^'"]*intlayer[^'"]*)['"]\s*,\s*version\s*:\s*['"]([^'"]+)['"]/gi,i=/name\s*:\s*['"]Intlayer['"]\s*,\s*version\s*:\s*['"][^'"]+['"]\s*,\s*doc\s*:\s*[`'"]https:\/\/intlayer\.org\/docs[`'"]/i,a=e=>{let t={},n=new RegExp(r.source,`gi`);for(let r of e.matchAll(n)){let e=r[1];t[e]=r[2].replace(/^[^0-9]*/,``)||r[2]}return t},o=5*1024*1024,s=async r=>{e.info(`[scanShowcaseProject] Scanning ${r}...`);let s=await t({pipe:!1,dumpio:!1});try{let t=await s.newPage();await t.setViewport({width:1280,height:720}),await t.goto(r,{waitUntil:`domcontentloaded`,timeout:45e3}),await t.waitForSelector(`body`,{timeout:1e4}),await t.waitForNetworkIdle({idleTime:1e3,timeout:1e4}).catch(()=>{});let c=Object.values(n),l=await t.evaluate(e=>{let t=document.documentElement,n=t.getAttribute(`lang`)||``,r=t.getAttribute(`dir`)||`ltr`,i=Array.from(document.querySelectorAll(`link[rel="alternate"][hreflang]`)).map(e=>e.getAttribute(`hreflang`)||``),a=document.querySelector(`link[rel="canonical"]`)?.getAttribute(`href`)||``,o=Array.from(document.querySelectorAll(`a[href]`)).map(e=>e.getAttribute(`href`)||``),s=e.map(e=>`/${e}`),c=o.filter(e=>e.startsWith(`/`)||e.startsWith(window.location.origin)),l=c.filter(e=>s.some(t=>e.startsWith(`${t}/`)||e===t)),u=l.length>0,d=c.length>0&&l.length===c.length,f=Array.from(document.querySelectorAll(`script:not([src])`)).map(e=>e.textContent||``).filter(Boolean),p=Array.from(document.querySelectorAll(`script[src]`)).map(e=>e.src).filter(Boolean),m=performance.getEntriesByType(`resource`).filter(e=>e.initiatorType===`script`||e.name.match(/\.(js|mjs|cjs)(\?[^"'\s]*)?$/i)).map(e=>e.name);return{lang:n,dir:r,hreflangs:i,canonical:a,hasLocalizedLinks:u,allAnchorsLocalized:d,inlineScripts:f,externalScriptUrls:Array.from(new Set([...p,...m])),metaTitle:document.querySelector(`meta[property="og:title"]`)?.getAttribute(`content`)||document.title||``,metaDescription:document.querySelector(`meta[property="og:description"]`)?.getAttribute(`content`)||document.querySelector(`meta[name="description"]`)?.getAttribute(`content`)||``}},c),u=!1;try{u=(await fetch(new URL(`/robots.txt`,r).toString())).ok}catch{}let d=!1,f=0;try{let e=await fetch(new URL(`/sitemap.xml`,r).toString());e.ok&&(d=!0,f=((await e.text()).match(/<loc>/g)||[]).length)}catch{}let p={},m=!1;for(let e of l.inlineScripts)Object.assign(p,a(e));if(!p.intlayer&&!Object.keys(p).some(e=>e.includes(`intlayer`))){let e=l.externalScriptUrls.slice(0,50);await Promise.allSettled(e.map(async e=>{try{let t=new AbortController,n=setTimeout(()=>t.abort(),5e3),r=await fetch(e,{signal:t.signal}).finally(()=>clearTimeout(n));if(!r.ok||Number(r.headers.get(`content-length`)||0)>o)return;let s=await r.text();if(s.length>o)return;i.test(s)?(m=!0,Object.assign(p,a(s))):s.includes(`intlayer`)&&Object.assign(p,a(s))}catch{}}))}let h=Object.keys(p).some(e=>e.toLowerCase().includes(`intlayer`)&&!e.toLowerCase().includes(`@intlayer`))||l.inlineScripts.some(e=>i.test(e))||m,g=Object.keys(p),_=p.intlayer,v=0;l.lang&&(v+=10),l.canonical&&(v+=10),l.hreflangs.length>0&&(v+=20),l.hreflangs.includes(`x-default`)&&(v+=10),u&&(v+=10),d&&(v+=10),l.hasLocalizedLinks&&(v+=20),l.allAnchorsLocalized&&(v+=10);let y;return h&&(e.info(`[scanShowcaseProject] Taking screenshot of ${r}...`),y=await t.screenshot({type:`jpeg`,quality:30})),await t.close(),{hasIntlayer:h,intlayerVersion:_,packageDetails:p,libsUsed:g,screenshotBuffer:y,metaTitle:l.metaTitle,metaDescription:l.metaDescription,scanDetails:{score:v,langTag:l.lang,htmlDir:l.dir,hreflangs:l.hreflangs,hasXDefault:l.hreflangs.includes(`x-default`),hasCanonical:!!l.canonical,hasLocalizedLinks:l.hasLocalizedLinks,allAnchorsLocalized:l.allAnchorsLocalized,robotsTxt:{accessible:u,disallowWithoutLocaleAlternates:u},sitemapXml:{urlsDiscoveredCount:f,alternatesPresent:f>0,xDefaultPresent:d}}}}finally{await s.close().catch(()=>{})}};export{a as extractPackagesFromScript,s as scanShowcaseProject};
//# sourceMappingURL=showcaseScan.service.mjs.map