import fs from "fs";
import { fileURLToPath } from "url";
import { transform } from "@astrojs/compiler";
import { transformWithVite } from "./styles.js";
const configCache = new WeakMap();
function isSSR(options) {
  if (options === void 0) {
    return false;
  }
  if (typeof options === "boolean") {
    return options;
  }
  if (typeof options == "object") {
    return !!options.ssr;
  }
  return false;
}
async function compile(config, filename, source, viteTransform, opts) {
  const filenameURL = new URL(`file://${filename}`);
  const normalizedID = fileURLToPath(filenameURL);
  const isPage = normalizedID.startsWith(fileURLToPath(config.pages)) || normalizedID.startsWith(fileURLToPath(config.layouts));
  const pathname = filenameURL.pathname.substr(config.projectRoot.pathname.length - 1);
  let cssTransformError;
  const transformResult = await transform(source, {
    as: isPage ? "document" : "fragment",
    pathname,
    projectRoot: config.projectRoot.toString(),
    site: config.buildOptions.site,
    sourcefile: filename,
    sourcemap: "both",
    internalURL: "astro/internal",
    experimentalStaticExtraction: config.buildOptions.experimentalStaticBuild,
    preprocessStyle: async (value, attrs) => {
      const lang = `.${(attrs == null ? void 0 : attrs.lang) || "css"}`.toLowerCase();
      try {
        const result = await transformWithVite({
          value,
          lang,
          id: filename,
          transformHook: viteTransform,
          ssr: isSSR(opts)
        });
        let map;
        if (!result)
          return null;
        if (result.map) {
          if (typeof result.map === "string") {
            map = result.map;
          } else if (result.map.mappings) {
            map = result.map.toString();
          }
        }
        return { code: result.code, map };
      } catch (err) {
        cssTransformError = err;
        return null;
      }
    }
  });
  if (cssTransformError)
    throw cssTransformError;
  return transformResult;
}
function invalidateCompilation(config, filename) {
  if (configCache.has(config)) {
    const cache = configCache.get(config);
    cache.delete(filename);
  }
}
async function cachedCompilation(config, filename, source, viteTransform, opts) {
  let cache;
  if (!configCache.has(config)) {
    cache = new Map();
    configCache.set(config, cache);
  } else {
    cache = configCache.get(config);
  }
  if (cache.has(filename)) {
    return cache.get(filename);
  }
  if (source === null) {
    const fileUrl = new URL(`file://${filename}`);
    source = await fs.promises.readFile(fileUrl, "utf-8");
  }
  const transformResult = await compile(config, filename, source, viteTransform, opts);
  cache.set(filename, transformResult);
  return transformResult;
}
export {
  cachedCompilation,
  invalidateCompilation
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL3ZpdGUtcGx1Z2luLWFzdHJvL2NvbXBpbGUudHMiXSwKICAibWFwcGluZ3MiOiAiQUFLQTtBQUNBO0FBQ0E7QUFDQTtBQUlBLE1BQU0sY0FBYyxJQUFJO0FBR3hCLGVBQWUsU0FBMEQ7QUFDeEUsTUFBSSxZQUFZLFFBQVc7QUFDMUIsV0FBTztBQUFBO0FBRVIsTUFBSSxPQUFPLFlBQVksV0FBVztBQUNqQyxXQUFPO0FBQUE7QUFFUixNQUFJLE9BQU8sV0FBVyxVQUFVO0FBQy9CLFdBQU8sQ0FBQyxDQUFDLFFBQVE7QUFBQTtBQUVsQixTQUFPO0FBQUE7QUFHUix1QkFBdUIsUUFBcUIsVUFBa0IsUUFBZ0IsZUFBOEIsTUFBMkI7QUFHdEksUUFBTSxjQUFjLElBQUksSUFBSSxVQUFVO0FBQ3RDLFFBQU0sZUFBZSxjQUFjO0FBQ25DLFFBQU0sU0FBUyxhQUFhLFdBQVcsY0FBYyxPQUFPLFdBQVcsYUFBYSxXQUFXLGNBQWMsT0FBTztBQUNwSCxRQUFNLFdBQVcsWUFBWSxTQUFTLE9BQU8sT0FBTyxZQUFZLFNBQVMsU0FBUztBQUVsRixNQUFJO0FBS0osUUFBTSxrQkFBa0IsTUFBTSxVQUFVLFFBQVE7QUFBQSxJQUMvQyxJQUFJLFNBQVMsYUFBYTtBQUFBLElBQzFCO0FBQUEsSUFDQSxhQUFhLE9BQU8sWUFBWTtBQUFBLElBQ2hDLE1BQU0sT0FBTyxhQUFhO0FBQUEsSUFDMUIsWUFBWTtBQUFBLElBQ1osV0FBVztBQUFBLElBQ1gsYUFBYTtBQUFBLElBQ2IsOEJBQThCLE9BQU8sYUFBYTtBQUFBLElBRWxELGlCQUFpQixPQUFP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