import {STYLE_EXTENSIONS} from "../core/ssr/css.js";
import {getViteTransform} from "../vite-plugin-astro/styles.js";
import * as path from "path";
import esbuild from "esbuild";
const PLUGIN_NAME = "@astrojs/rollup-plugin-build-css";
const ASTRO_STYLE_PREFIX = "@astro-inline-style";
const ASTRO_PAGE_STYLE_PREFIX = "@astro-page-all-styles";
const isCSSRequest = (request) => STYLE_EXTENSIONS.has(path.extname(request));
function getAstroPageStyleId(pathname) {
  let styleId = ASTRO_PAGE_STYLE_PREFIX + pathname;
  if (styleId.endsWith("/")) {
    styleId += "index";
  }
  styleId += ".js";
  return styleId;
}
function getAstroStyleId(pathname) {
  let styleId = ASTRO_STYLE_PREFIX + pathname;
  if (styleId.endsWith("/")) {
    styleId += "index";
  }
  styleId += ".css";
  return styleId;
}
function getAstroStylePathFromId(id) {
  return id.substr(ASTRO_STYLE_PREFIX.length + 1);
}
function isStyleVirtualModule(id) {
  return id.startsWith(ASTRO_STYLE_PREFIX);
}
function isPageStyleVirtualModule(id) {
  return id.startsWith(ASTRO_PAGE_STYLE_PREFIX);
}
function rollupPluginAstroBuildCSS(options) {
  const {astroPageStyleMap, astroStyleMap, chunkToReferenceIdMap, pureCSSChunks} = options;
  const styleSourceMap = new Map();
  let viteTransform;
  return {
    name: PLUGIN_NAME,
    enforce: "pre",
    configResolved(resolvedConfig) {
      viteTransform = getViteTransform(resolvedConfig);
      const viteCSSPost = resolvedConfig.plugins.find((p) => p.name === "vite:css-post");
      if (viteCSSPost) {
        delete viteCSSPost.renderChunk;
        delete viteCSSPost.generateBundle;
      }
    },
    async resolveId(id) {
      if (isPageStyleVirtualModule(id)) {
        return id;
      }
      if (isStyleVirtualModule(id)) {
        return id;
      }
      return void 0;
    },
    async load(id) {
      if (isPageStyleVirtualModule(id)) {
        const source = astroPageStyleMap.get(id);
        return source;
      }
      if (isStyleVirtualModule(id)) {
        return astroStyleMap.get(id);
      }
      return null;
    },
    async transform(value, id) {
      if (isStyleVirtualModule(id)) {
        styleSourceMap.set(id, value);
        return null;
      }
      if (isCSSRequest(id)) {
        let result = await viteTransform(value, id);
        if (result) {
          styleSourceMap.set(id, result.code);
        } else {
          styleSourceMap.set(id, value);
        }
        return result;
      }
      return null;
    },
    async renderChunk(_code, chunk) {
      let chunkCSS = "";
      let isPureCSS = true;
      for (const [id] of Object.entries(chunk.modules)) {
        if (!isCSSRequest(id) && !isPageStyleVirtualModule(id)) {
          isPureCSS = false;
        }
        if (styleSourceMap.has(id)) {
          chunkCSS += styleSourceMap.get(id);
        }
      }
      if (isPureCSS) {
        const {code: minifiedCSS} = await esbuild.transform(chunkCSS, {
          loader: "css",
          minify: true
        });
        const referenceId = this.emitFile({
          name: chunk.name + ".css",
          type: "asset",
          source: minifiedCSS
        });
        pureCSSChunks.add(chunk);
        chunkToReferenceIdMap.set(chunk.fileName, referenceId);
      }
      return null;
    },
    generateBundle(_options, bundle) {
      for (const [chunkId, chunk] of Object.entries(bundle)) {
        if (chunk.type === "chunk" && pureCSSChunks.has(chunk)) {
          delete bundle[chunkId];
        }
      }
    }
  };
}
export {
  getAstroPageStyleId,
  getAstroStyleId,
  getAstroStylePathFromId,
  rollupPluginAstroBuildCSS
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL3ZpdGUtcGx1Z2luLWJ1aWxkLWNzcy9pbmRleC50cyJdLAogICJtYXBwaW5ncyI6ICJBQUdBO0FBQ0E7QUFDQTtBQUNBO0FBRUEsTUFBTSxjQUFjO0FBR3BCLE1BQU0scUJBQXFCO0FBRTNCLE1BQU0sMEJBQTBCO0FBRWhDLE1BQU0sZUFBZSxDQUFDLFlBQW9CLGlCQUFpQixJQUFJLEtBQUssUUFBUTtBQUVyRSw2QkFBNkIsVUFBa0I7QUFDcEQsTUFBSSxVQUFVLDBCQUEwQjtBQUN4QyxNQUFJLFFBQVEsU0FBUyxNQUFNO0FBQ3pCLGVBQVc7QUFBQTtBQUViLGFBQVc7QUF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