import{logger as e}from"../../logger/index.mjs";import{launchBrowser as t}from"../../utils/puppeteer/launchBrowser.mjs";import{checkLinguisticStructure as n}from"./checkers/linguisticChecker.mjs";import{checkMetadata as r}from"./checkers/metadataChecker.mjs";import{checkHtmlAttributes as i,extractPageMetadata as a}from"./checkers/pageChecker.mjs";import{checkRobots as o}from"./checkers/robotsChecker.mjs";import{checkSitemap as s}from"./checkers/sitemapChecker.mjs";import{checkUrlStructure as c}from"./checkers/urlChecker.mjs";import{load as l}from"cheerio";const u=async(t,n,r=3)=>{let i;for(let a=1;a<=r;a++)try{let r=await t.goto(n,{waitUntil:`domcontentloaded`,timeout:45e3});if(!r)throw Error(`Failed to get a response from ${n}`);let i=r.status();if(e.info(`[gotoWithRetries] Status: ${i} for ${n}`),i>=400)throw Error(`HTTP ${i} on ${n}`);return await t.waitForSelector(`body`,{timeout:1e4}),await t.waitForNetworkIdle({idleTime:1e3,timeout:1e4}).catch(()=>{}),r}catch(e){if(i=e,a<r){await new Promise(e=>setTimeout(e,500*a));continue}throw i}},d=async(d,f)=>{let p,m=[],h=e=>{m.push(e),f(e)};try{let f=new URL(d).origin,g=new Set;h({progress:10,message:`Checking domain and parsing root page...`}),p=await t();let _=await p.newPage();await _.setUserAgent({userAgent:`Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119 Safari/537.36`,platform:`Linux`,userAgentMetadata:{brands:[{brand:`Google Chrome`,version:`119`}],platform:`Linux`,mobile:!1,platformVersion:`119`,architecture:`x86`,model:`Linux`}}),await _.setExtraHTTPHeaders({"Accept-Language":`en-US,en;q=0.9`}),await _.setRequestInterception(!0),_.on(`request`,e=>{let t=e.resourceType();t===`image`||t===`media`||t===`font`||t===`websocket`?e.abort():e.continue()}),await _.setViewport({width:1280,height:800}),_.on(`requestfailed`,t=>e.warn(`[requestfailed] ${t.url()} ${t.failure()?.errorText}`)),_.on(`console`,t=>e.info(`[console] ${t.type()} ${t.text()}`)),_.on(`pageerror`,t=>e.error(`[pageerror] ${t}`)),await u(_,d);let v=await _.content();e.info(`[runSingleAudit] Page loaded. Content length: ${v.length}`);let y=l(v);e.info(`[runSingleAudit] Cheerio loaded. Body found: ${y(`body`).length>0}`),await a(y,d,h),h({progress:20}),await i(y,d,h),h({progress:30,message:`Analyzing linguistic structure...`}),await n(y,d,g,h),h({progress:50,message:`Checking metadata structure...`}),await r(y,d,h),h({progress:60,message:`Analyzing URL structure...`});let b=await c(_,f,d,h);return h({progress:70,message:`Checking robots.txt...`}),await o(f,g,h),h({progress:80,message:`Checking sitemap.xml...`}),await s(f,g,h),h({progress:100,message:`Audit completed`}),{events:m,internalUrls:b}}catch(e){throw h({globalError:e.message}),e}finally{p&&await p.close()}};export{d as runSingleAudit};
//# sourceMappingURL=seoAudit.service.mjs.map