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)),
  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(),
    remarkPlugins: z.array(z.any()).optional(),
    rehypePlugins: z.array(z.any()).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),
    tailwindConfig: z.string().optional(),
    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)),
    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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvY29uZmlnLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUVPLE1BQU0sb0JBQW9CLEVBQUUsT0FBTztBQUFBLEVBQ3hDLGFBQWEsRUFDVixTQUNBLFdBQ0EsUUFBUSxLQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLEtBQUssRUFDRixTQUNBLFdBQ0EsUUFBUSxTQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLE9BQU8sRUFDSixTQUNBLFdBQ0EsUUFBUSxlQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLFFBQVEsRUFDTCxTQUNBLFdBQ0EsUUFBUSxZQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLE1BQU0sRUFDSCxTQUNBLFdBQ0EsUUFBUSxVQUNSLFVBQVUsQ0FBQyxRQUFRLElBQUksSUFBSTtBQUFBLEVBQzlCLFdBQVcsRUFBRSxNQUFNLEVBQUUsVUFBVSxXQUFX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