import{basename}from"node:path";import{isEveProject}from"#setup/scaffold/index.js";import{headlessAsker,interactiveAsker}from"#setup/ask.js";import{addChannels}from"#setup/boxes/add-channels.js";import{deployProject}from"#setup/boxes/deploy-project.js";import{selectChannels}from"#setup/boxes/select-channels.js";import{detectDeployment,projectResolutionFromDeployment}from"#setup/project-resolution.js";import{createPrompter}from"#setup/prompter.js";import{runHeadless,runInteractive}from"#setup/runner.js";import{createDefaultSetupState,snapshotSetupState}from"#setup/state.js";import{whimsyFor}from"#setup/cli/index.js";import{composeOnboardingBoxes}from"#setup/onboarding.js";const defaultDependencies={createPrompter,detectDeployment,isEveProject,hasInteractiveTerminal:()=>!!(process.stdin.isTTY&&process.stdout.isTTY)};async function runSetupCommand(t,n,r,i=defaultDependencies){let a=!await i.isEveProject(n),o=r.evePackageVersion??`beta`,s=r.presets??{},c=r.headless!==!0;if(c&&!i.hasInteractiveTerminal()){t.error("eve setup is interactive and needs a terminal. For automation, use `create-eve --headless` to scaffold a new agent or `eve channels add` inside an existing project."),process.exitCode=1;return}let l=i.createPrompter(),u={write:e=>l.log.message(e)};l.intro(a?`Create an 𝐞𝐯𝐞 agent`:`Set-up your 𝐞𝐯𝐞 agent`);let d=a?`Agent created.`:`Setup complete.`,f,p;a?(f=createDefaultSetupState(),p=composeOnboardingBoxes({prompter:l,presetName:s.presetName,presetMode:s.presetMode,presetModel:s.presetModel,presetChannels:s.presetChannels,presetConnections:s.presetConnections,presetCreateSlackbot:s.presetCreateSlackbot,provisioning:r.provisioning,presetChatPreference:s.presetChatPreference??`skip`,targetDirectory:n,inPlace:!c,overwriteExisting:s.overwriteExisting,presetNoDeploy:s.presetNoDeploy,headless:!c,evePackageVersion:o})):(l.log.message(whimsyFor(`project-detect`)),f={...createDefaultSetupState(),project:projectResolutionFromDeployment(await i.detectDeployment(n)),agentName:basename(n),projectPath:{kind:`resolved`,inPlace:!0,path:n}},p=[selectChannels({asker:c?interactiveAsker(l):headlessAsker(),presetChannels:s.presetChannels,variant:`in-project`}),addChannels({asker:c?interactiveAsker(l):headlessAsker(),prompter:l,evePackageVersion:o,presetCreateSlackbot:s.presetCreateSlackbot,headless:!c,slackbotFailure:`warn-and-continue`}),deployProject({prompter:l,skip:s.presetNoDeploy,headless:!c})]);try{if(c){let e=await runInteractive(p,f,u,{snapshot:snapshotSetupState});l.outro(e.kind===`cancelled`?`Cancelled.`:d);return}await runHeadless(p,f,u,{snapshot:snapshotSetupState}),l.outro(d)}catch(e){t.error(e instanceof Error?e.message:String(e)),process.exitCode=1}}export{runSetupCommand};