import {existsSync} from "fs";
import * as colors from "kleur/colors";
import path from "path";
import {pathToFileURL} from "url";
import {z} from "zod";
const AstroConfigSchema = z.object({
  projectRoot: z.string().optional().default(".").transform((val) => new URL(val)),
  src: z.string().optional().default("./src").transform((val) => new URL(val)),
  pages: z.string().optional().default("./src/pages").transform((val) => new URL(val)),
  layouts: z.string().optional().default("./src/layouts").transform((val) => new URL(val)),
  public: z.string().optional().default("./public").transform((val) => new URL(val)),
  dist: z.string().optional().default("./dist").transform((val) => new URL(val)),
  renderers: z.array(z.string()).optional().default(["@astrojs/renderer-svelte", "@astrojs/renderer-vue", "@astrojs/renderer-react", "@astrojs/renderer-preact"]),
  markdownOptions: z.object({
    footnotes: z.boolean().optional(),
    gfm: z.boolean().optional(),
    render: z.any().optional().default(["@astrojs/markdown-remark", {}])
  }).optional().default({}),
  buildOptions: z.object({
    site: z.string().optional(),
    sitemap: z.boolean().optional().default(true),
    pageUrlFormat: z.union([z.literal("file"), z.literal("directory")]).optional().default("directory")
  }).optional().default({}),
  devOptions: z.object({
    hostname: z.string().optional().default("localhost"),
    port: z.number().optional().default(3e3),
    trailingSlash: z.union([z.literal("always"), z.literal("never"), z.literal("ignore")]).optional().default("ignore")
  }).optional().default({}),
  vite: z.any().optional().default({})
});
async function validateConfig(userConfig, root) {
  const fileProtocolRoot = pathToFileURL(root + path.sep);
  const AstroConfigRelativeSchema = AstroConfigSchema.extend({
    projectRoot: z.string().default(".").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
    src: z.string().default("./src").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
    pages: z.string().default("./src/pages").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
    layouts: z.string().default("./src/layouts").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
    public: z.string().default("./public").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
    dist: z.string().default("./dist").transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot))
  });
  return AstroConfigRelativeSchema.parseAsync(userConfig);
}
function addTrailingSlash(str) {
  return str.replace(/\/*$/, "/");
}
async function loadConfig(options) {
  const root = options.cwd ? path.resolve(options.cwd) : process.cwd();
  const astroConfigPath = new URL(`./${options.filename || "astro.config.mjs"}`, `file://${root}/`);
  let userConfig = {};
  if (existsSync(astroConfigPath)) {
    userConfig = (await import(astroConfigPath.href)).default;
  }
  return validateConfig(userConfig, root);
}
function formatConfigError(err) {
  const errorList = err.issues.map((issue) => `  ! ${colors.bold(issue.path.join("."))}  ${colors.red(issue.message + ".")}`);
  return `${colors.red("[config]")} Astro found issue(s) with your configuration:
${errorList.join("\n")}`;
}
export {
  AstroConfigSchema,
  formatConfigError,
  loadConfig,
  validateConfig
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvY29uZmlnLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUVPLE1BQU0sb0JBQW9CLEVBQUUsT0FBTztBQUFBLEVBQ3hDLGFBQWEsRUFDVixTQUNBLFdBQ0EsUUFBUSxLQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLEtBQUssRUFDRixTQUNBLFdBQ0EsUUFBUSxTQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLE9BQU8sRUFDSixTQUNBLFdBQ0EsUUFBUSxlQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLFNBQVMsRUFDTixTQUNBLFdBQ0EsUUFBUSxpQkFDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM5QixRQUFRLEVBQ0wsU0FDQSxXQUNBLFFBQVEsWUFDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM5QixNQUFNLEVBQ0gsU0FDQSxX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