import {
  createProject
} from "./chunk-XAMAYRDA.js";
import "./chunk-FBGWG4N6.js";
import "./chunk-K44MW7JJ.js";

// src/server/utils/project-creator.ts
import { execSync } from "child_process";
import { join } from "path";
function validateProjectName(name) {
  if (!name || name.trim() === "") {
    return { valid: false, error: "Project name cannot be empty" };
  }
  if (!/^[a-zA-Z0-9._ -]+$/.test(name)) {
    return {
      valid: false,
      error: "Project name can only contain letters, numbers, hyphens, underscores, dots, and spaces"
    };
  }
  if (name.includes("/") || name.includes("\\") || name.includes("..")) {
    return { valid: false, error: "Project name cannot contain path separators" };
  }
  return { valid: true };
}
async function directoryExists(path) {
  try {
    const { access } = await import("fs/promises");
    const { constants } = await import("fs");
    await access(path, constants.F_OK);
    return true;
  } catch {
    return false;
  }
}
async function createDirectoryWithGit(projectName, workdir) {
  const validation = validateProjectName(projectName);
  if (!validation.valid) {
    throw new Error(validation.error);
  }
  const fullPath = workdir.replace(/\/+$/, "");
  const { stat, mkdir, rm, access, constants } = await import("fs/promises");
  const dirAlreadyExisted = await access(fullPath, constants.F_OK).then(() => true).catch(() => false);
  try {
    const stats = await stat(fullPath);
    if (!stats.isDirectory()) {
      throw new Error(`A file named '${projectName}' already exists at ${fullPath}`);
    }
  } catch (err) {
    if (err instanceof Error && "code" in err && err.code === "ENOENT") {
      try {
        await mkdir(fullPath, { recursive: true });
      } catch (mkdirErr) {
        if (mkdirErr instanceof Error && "code" in mkdirErr && mkdirErr.code === "EACCES") {
          const eaccError = mkdirErr;
          const permError = new Error(`Permission denied: cannot create directory at ${fullPath}`, {
            cause: eaccError
          });
          permError.code = "EACCES";
          throw permError;
        }
        throw mkdirErr;
      }
    } else if (err instanceof Error && "code" in err && err.code === "EACCES") {
      const eaccError = err;
      const permError = new Error(`Permission denied: cannot access directory at ${fullPath}`, {
        cause: eaccError
      });
      permError.code = "EACCES";
      throw permError;
    } else {
      throw err;
    }
  }
  if (!await directoryExists(join(fullPath, ".git"))) {
    try {
      execSync("git init", { cwd: fullPath, stdio: "pipe" });
    } catch (gitErr) {
      const errMsg = gitErr instanceof Error ? gitErr.message : "Unknown";
      const exitCode = gitErr.status ?? gitErr.exitCode;
      const isPermission = errMsg.includes("Permission denied") || exitCode === 128;
      let sudoSuccess = false;
      if (isPermission) {
        try {
          const currentUser = execSync("id -un", { encoding: "utf-8" }).trim();
          execSync(`sudo -u ${currentUser} git init`, { cwd: fullPath, stdio: "pipe" });
          sudoSuccess = true;
        } catch {
        }
      }
      if (!sudoSuccess) {
        const permError = new Error(`Failed to initialize git: ${errMsg}`);
        if (isPermission) {
          permError.code = "EACCES";
        }
        if (!dirAlreadyExisted) {
          try {
            await rm(fullPath, { recursive: true, force: true });
          } catch {
          }
        }
        throw permError;
      }
    }
  }
  return createProject(projectName, fullPath);
}
export {
  createDirectoryWithGit,
  validateProjectName
};
//# sourceMappingURL=project-creator-6DU7E75S.js.map