import{logger as e}from"../logger/index.mjs";import{AuditModel as t}from"../models/audit.model.mjs";import{mutateScore as n}from"../services/audit/analysis/calculateScore.mjs";import{runSingleAudit as r}from"../services/audit/seoAudit.service.mjs";const i=(e,t)=>{e.raw.write(`data: ${JSON.stringify(t)}\n\n`)},a=async(a,o)=>{o.hijack();let s=o.getHeaders();for(let[e,t]of Object.entries(s))t!==void 0&&o.raw.setHeader(e,t);o.raw.setHeader(`Content-Type`,`text/event-stream; charset=utf-8`),o.raw.setHeader(`Cache-Control`,`no-cache, no-transform`),o.raw.setHeader(`Connection`,`keep-alive`),o.raw.setHeader(`X-Accel-Buffering`,`no`),o.raw.flushHeaders&&o.raw.flushHeaders(),o.raw.write(`: connected

`);let{url:c}=a.query,l={score:0,totalScore:0},u=0;if(!c){i(o,{status:`error`,globalError:`Missing URL parameter`}),o.raw.end();return}try{new URL(c)}catch{i(o,{status:`error`,globalError:`Invalid URL format`}),o.raw.end();return}let d=!1;a.raw.on(`close`,()=>{e.info(`Client connection closed`),d=!0});try{await r(c,e=>{l=n(l,e),e.progress!==void 0&&(u=e.progress),d||i(o,{...e,progress:u,score:Math.round(l.totalScore>0?l.score/l.totalScore*100:0)})});try{let n=new URL(c).hostname,r=Math.round(l.totalScore>0?l.score/l.totalScore*100:0);await new t({domain:n,score:r}).save(),e.info(`Audit saved for domain: ${n} with score: ${r}`)}catch(t){e.error(`Failed to save audit to database:`,t)}o.raw.end()}catch(t){e.error(`Audit GET error:`,t),d||(i(o,{globalError:t instanceof Error?t.message:`Internal server error`}),o.raw.end())}};export{a as auditGetHandler};
//# sourceMappingURL=audit.controller.mjs.map