import{SkippedSignal,select,text}from"../ask.js";import{CURRENT_DIRECTORY_PROJECT_NAME}from"../state.js";import{pathExists}from"../path-exists.js";import{basename,join,resolve}from"node:path";import{isEveProject}from"#setup/scaffold/index.js";const PROJECT_NAME_REGEX=/^(?!.*---)[a-z0-9-_.]+$/,DEFAULT_PROJECT_NAME=`my-agent`;function isValidProjectName(e){return PROJECT_NAME_REGEX.test(e)&&e.length>0&&e.length<=100}function validateProjectName(e){let t=e.trim();if(t.length===0)return`Project name cannot be empty.`;if(t===`..`)return`Project name cannot be '..'.`;if(!isValidProjectName(t))return`Project name can only contain up to 100 lowercase letters, digits, and the characters '.', '_', '-'.`}function validatePresetName(e){let t=e.trim(),n=validateProjectName(t);if(n)throw Error(n);return t}async function askAgentName(e,t){return(await e.ask(text({key:`name`,message:`What's your agent's name?`,placeholder:DEFAULT_PROJECT_NAME,recommended:DEFAULT_PROJECT_NAME,validate:e=>validateProjectName(e.trim()||DEFAULT_PROJECT_NAME)??null,required:t}))).trim()||DEFAULT_PROJECT_NAME}function deriveInPlaceProjectName(e){let t=resolve(e??process.cwd()),n=basename(t);if(validateProjectName(n)!==void 0)throw Error(`Cannot infer a valid project name from "${t}". Pass --target-dir with a valid basename, or scaffold into a renamed directory.`);return n}function resolveTarget(n){let i=n.deps??{pathExists,isEveProject},parent=()=>resolve(n.targetDirectory??process.cwd()),inPlaceBasename=()=>basename(parent());async function assertNewDirectoryAvailable(e){if(e===CURRENT_DIRECTORY_PROJECT_NAME)return;let t=resolve(parent(),e);if(await i.pathExists(t)&&!(n.resumeExisting===!0&&await i.isEveProject(t)))throw Error(`Directory "${e}" already exists. Choose a different name.`)}async function looksLikeProject(e){let[t,n]=await Promise.all([i.pathExists(join(e,`package.json`)),i.pathExists(join(e,`.vercel`))]);return t||n}async function askFreeDirectoryName(e){let t=e;for(;;){if(t===CURRENT_DIRECTORY_PROJECT_NAME||!await i.pathExists(resolve(parent(),t)))return t;n.notify(`Directory "${t}" already exists. Choose a different name.`),t=await askAgentName(n.asker,!0)}}function inPlaceInput(){return{agentName:n.presetName===void 0?deriveInPlaceProjectName(n.targetDirectory):validatePresetName(n.presetName),inPlace:!0}}return{id:`resolve-target`,async gather(){if(n.inPlace){if(n.presetName===void 0&&validateProjectName(inPlaceBasename())!==void 0)try{return{agentName:await askAgentName(n.asker,!1),inPlace:!0}}catch(t){if(!(t instanceof SkippedSignal))throw t}return inPlaceInput()}if(n.presetName!==void 0){let e=validatePresetName(n.presetName);return await assertNewDirectoryAvailable(e),{agentName:e,inPlace:!1}}let r=await askAgentName(n.asker,!0);return await looksLikeProject(parent())&&await n.asker.ask(select({key:`target-directory`,message:`This directory already looks like a project. Where should the agent live?`,options:[{id:`current`,label:`Scaffold into this directory`,value:`current`},{id:`new`,label:`Create a new directory ./${r}`,value:`new`}],recommended:`new`,required:!0}))===`current`?{agentName:r,inPlace:!0}:{agentName:await askFreeDirectoryName(r),inPlace:!1}},async perform({input:e}){let t=e.inPlace?CURRENT_DIRECTORY_PROJECT_NAME:e.agentName;return{agentName:e.agentName,projectPath:{kind:`resolved`,inPlace:e.inPlace,path:resolve(parent(),t)}}},apply(e,t){return{...e,agentName:t.agentName,projectPath:t.projectPath}}}}export{resolveTarget};