import{resolveCache as v}from"../server/esbuild/plugins/themes-resolver.js";import{getBilledPagesCount as m,logger as n,reporter as c}from"../server/utils/index.js";import{createClientCompiler as g,createServerCompiler as w}from"../server/esbuild/esbuild.js";import{initPlugins as C,runPlugins as p,writeClientEntries as h}from"../server/plugins/lifecycle.js";import{startDevServer as S}from"../server/web-server/dev-server.js";import{sendCliCommandExecution as y}from"./telemetry/index.js";import{validateAllMarkdowns as f}from"../server/plugins/markdown/compiler.js";import{green as R}from"../server/utils/reporter/colors.js";import{reportAllErrors as b}from"../server/utils/report-all-errors.js";async function B(i,e){n.patchConsole(),n.info("Starting project preview mode."),h(e),n.startTiming("initial-js"),await E(e),n.infoTime("initial-js","JavaScript compiled");const{pluginInstances:l,lifecycleContext:t}=await C({contentDir:e.contentDir,outdir:e.outdir,serverOutDir:void 0,devLogin:i["dev-login"]!==!1,setGlobalConfig:e.setGlobalConfig});e.lifecycleContext=t,await p(l,e,t,{failFast:!0}),y(i,t,e);const d=async()=>{const r=performance.now();c.clearErrors(),await p(l,e,t),await f(e,t),P().then(async()=>{await b(t);const a=m(e.getAllRoutes()),o=c.summary(`  \u{1F440} Last change processed in ${R(Math.round(performance.now()-r)+"ms")}`,a);e.runListeners("errors-updated",void 0,o),e.runListeners("pages-updated",void 0,a),e.runListeners("routes-updated")})};t.fs.watch(async()=>{v.clear(),await d()}),await f(e,t),await b(t);const s=m(e.getAllRoutes());c.summary("  \u{1F440} Watching for changes...",s),e.runListeners("pages-updated",void 0,s),n.verbose("Starting development server."),await S(e,t,{port:Number(i.port||4e3)}),c.listenStdin()}async function E(i){let e=!0,l=!0;const t=r=>{r==="client"?e=!0:l=!0,i.runListeners("build-started"),e!=l&&n.startTiming("rebuild")},d=async r=>{r==="client"?e=!1:l=!1,!(e||l)&&(i.buildRevision++,n.infoTime("rebuild","JavaScript re-compiled"),await i.reloadMarkdocOptions(),i.markUserCodeReady(),i.runListeners("build-updated"),i.lifecycleContext&&i.buildRevision>1&&i.lifecycleContext.fs.emitCodeUpdated())},s=[new Promise((r,a)=>{const o={label:"client",buildStart(){n.verbose("Start compiling client code."),n.startTiming(this.label)},buildEnd(){n.verboseTime(this.label,"Client compiled"),r(!0)},rebuildStart:()=>t("client"),rebuildEnd:()=>d("client")};g(i,i.outdir,"development",o).then(u=>u.watch()).catch(a)}),new Promise((r,a)=>{const o={label:"server",buildStart(){n.verbose("Start compiling server code."),n.startTiming(this.label)},buildEnd(){n.verboseTime(this.label,"Server compiled"),r(!0)},rebuildStart:()=>t("server"),rebuildEnd:()=>d("server")};w(i,i.serverOutDir,"development",o).then(u=>u.watch()).catch(a)})];await Promise.all(s),await i.reloadMarkdocOptions()}function P(){return new Promise(i=>{setTimeout(i,0)})}export{B as develop};
