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", {}])
  }).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();
  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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvY29uZmlnLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBRUEsS0FBSyxJQUFJLENBQUM7QUFFSCxNQUFNLG9CQUFvQixFQUFFLE9BQU87QUFBQSxFQUN4QyxhQUFhLEVBQ1YsU0FDQSxXQUNBLFFBQVEsS0FDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM5QixLQUFLLEVBQ0YsU0FDQSxXQUNBLFFBQVEsU0FDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM5QixPQUFPLEVBQ0osU0FDQSxXQUNBLFFBQVEsZUFDUixVQUFVLENBQUMsUUFBUSxJQUFJLElBQUk7QUFBQSxFQUM5QixTQUFTLEVBQ04sU0FDQSxXQUNBLFFBQVEsaUJBQ1IsVUFBVSxDQUFDLFFB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