import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  CliError,
  CliErrorCode,
  RepoType,
  generator,
  getEnvFilePath,
  getTestOverrides,
  gitClone,
  pathExists,
  require_is_git_url,
  require_lib,
  temporaryDirectory,
  updateEnvFile
} from "./chunk-SP6CBGLK.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-4EFJZ3GQ.js";

// ../create-new-packages/app-command/src/services/generator/index.ts
init_esm_shims();

// ../create-new-packages/app-command/src/services/generator/generator-service.ts
init_esm_shims();
var import_variant = __toESM(require_lib(), 1);
import { randomUUID } from "node:crypto";

// ../create-new-packages/app-command/src/services/generator/template-fetcher.ts
init_esm_shims();
import { join } from "node:path";
import { rm } from "node:fs/promises";
var import_is_git_url = __toESM(require_is_git_url(), 1);
async function downloadAndExtractRepo(url, branch) {
  const repoDir = temporaryDirectory({ prefix: "wix-create-app" });
  try {
    await gitClone(url, repoDir, branch);
    await rm(join(repoDir, ".git"), { recursive: true, force: true });
    return repoDir;
  } catch (e) {
    throw new CliError({
      code: CliErrorCode.FailedToCloneRepo(),
      cause: e
    });
  }
}
async function fetchTemplate(url, path = ".", branch) {
  if (!(0, import_is_git_url.default)(url)) {
    throw new CliError({
      code: CliErrorCode.InvalidGitUrl({ url }),
      info: { url },
      cause: null
    });
  }
  const repoDir = await downloadAndExtractRepo(url, branch);
  const templateDirInRepo = join(repoDir, path);
  if (!await pathExists(templateDirInRepo)) {
    throw new CliError({
      code: CliErrorCode.TemplatePathNotFound({ templatePath: path }),
      info: { templatePath: path },
      cause: null
    });
  }
  return templateDirInRepo;
}

// ../create-new-packages/app-command/src/services/generator/generator-service.ts
var { wixCliDependencyVersion, wixCliAppDependencyVersion } = getTestOverrides();
async function generateApp({
  packageName,
  packageFolder,
  appData,
  packageManager,
  repoType,
  template,
  templateParams,
  ideForMcp,
  environmentVariables
}) {
  const model = {
    packageName,
    devCenter: appData,
    deps: {
      "@wix/cli": wixCliDependencyVersion ?? "^1.0.0",
      "@wix/cli-app": wixCliAppDependencyVersion ?? "^1.0.0"
    },
    packageManager: {
      name: packageManager.name,
      installCmd: packageManager.getInstallCmd(),
      runCmd: packageManager.getRunCmd()
    },
    isMonorepo: (0, import_variant.isType)(repoType, RepoType.Monorepo),
    templateParams,
    generatePageID: () => randomUUID(),
    ideForMcpType: ideForMcp?.type
  };
  const templateFolder = await (0, import_variant.match)(template, {
    App: ({ payload: { cliCloneData } }) => fetchTemplate(
      cliCloneData.repositoryUrl,
      cliCloneData.pathInRepository,
      "astro-app-template"
    ),
    GitRepo: ({ url, path }) => fetchTemplate(url, path),
    Local: ({ path }) => path,
    Default: ({ path }) => path
  });
  await generator({
    template: templateFolder,
    cwd: packageFolder,
    data: model
  });
  await updateEnvFile(getEnvFilePath(packageFolder), environmentVariables);
}
export {
  generateApp
};
//# sourceMappingURL=generator-CUTI2BWL.js.map