import * as colors from "kleur/colors";
import path from "path";
import { pathToFileURL, fileURLToPath } from "url";
import { z } from "zod";
import load from "@proload/core";
import loadTypeScript from "@proload/plugin-tsm";
load.use([loadTypeScript]);
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", {}])
  }).strict().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"),
    experimentalStaticBuild: z.boolean().optional().default(false)
  }).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();
  let userConfig = {};
  let userConfigPath;
  if (options.filename) {
    userConfigPath = /^\.*\//.test(options.filename) ? options.filename : `./${options.filename}`;
    userConfigPath = fileURLToPath(new URL(userConfigPath, `file://${root}/`));
  }
  const config = await load("astro", { mustExist: false, cwd: root, filePath: userConfigPath });
  if (config) {
    userConfig = config.value;
  }
  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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvY29uZmlnLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBRUEsS0FBSyxJQUFJLENBQUM7QUFFSCxNQUFNLG9CQUFvQixFQUFFLE9BQU87QUFBQSxFQUN6QyxhQUFhLEVBQ1gsU0FDQSxXQUNBLFFBQVEsS0FDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM3QixLQUFLLEVBQ0gsU0FDQSxXQUNBLFFBQVEsU0FDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM3QixPQUFPLEVBQ0wsU0FDQSxXQUNBLFFBQVEsZUFDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM3QixTQUFTLEVBQ1AsU0FDQSxXQUNBL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