import { ssgBuild } from "./build/ssg.js";
import { joinPaths, prependForwardSlash, propsToFilename } from "./utils/paths.js";
import { createPlugin } from "./vite-plugin-astro-image.js";
import { copyWasmFiles } from "./vendor/squoosh/copy-wasm.js";
import { getImage } from "./lib/get-image.js";
import { getPicture } from "./lib/get-picture.js";
const PKG_NAME = "@astrojs/image";
const ROUTE_PATTERN = "/_image";
function integration(options = {}) {
  const resolvedOptions = {
    serviceEntryPoint: "@astrojs/image/squoosh",
    logLevel: "info",
    ...options
  };
  let _config;
  let _buildConfig;
  const staticImages = /* @__PURE__ */ new Map();
  function getViteConfiguration() {
    return {
      plugins: [createPlugin(_config, resolvedOptions)],
      optimizeDeps: {
        include: [
          "image-size"
        ].filter(Boolean)
      },
      build: {
        rollupOptions: {
          external: ["sharp"]
        }
      },
      ssr: {
        noExternal: ["@astrojs/image", resolvedOptions.serviceEntryPoint]
      },
      assetsInclude: ["**/*.wasm"]
    };
  }
  return {
    name: PKG_NAME,
    hooks: {
      "astro:config:setup": async ({ command, config, updateConfig, injectRoute }) => {
        _config = config;
        updateConfig({ vite: getViteConfiguration() });
        if (command === "dev" || config.output === "server") {
          injectRoute({
            pattern: ROUTE_PATTERN,
            entryPoint: "@astrojs/image/endpoint"
          });
        }
        const { default: defaultLoader } = await (resolvedOptions.serviceEntryPoint === "@astrojs/image/sharp" ? import("./loaders/sharp.js") : import("./loaders/squoosh.js"));
        globalThis.astroImage = {
          defaultLoader
        };
      },
      "astro:build:start": async ({ buildConfig }) => {
        _buildConfig = buildConfig;
      },
      "astro:build:setup": async () => {
        function addStaticImage(transform) {
          const srcTranforms = staticImages.has(transform.src) ? staticImages.get(transform.src) : /* @__PURE__ */ new Map();
          const filename = propsToFilename(transform);
          srcTranforms.set(filename, transform);
          staticImages.set(transform.src, srcTranforms);
          return prependForwardSlash(joinPaths(_config.base, "assets", filename));
        }
        if (_config.output === "static") {
          globalThis.astroImage.addStaticImage = addStaticImage;
        }
      },
      "astro:build:generated": async ({ dir }) => {
        var _a;
        if (_config.output === "static") {
          const loader = (_a = globalThis == null ? void 0 : globalThis.astroImage) == null ? void 0 : _a.loader;
          if (loader && "transform" in loader && staticImages.size > 0) {
            await ssgBuild({
              loader,
              staticImages,
              config: _config,
              outDir: dir,
              logLevel: resolvedOptions.logLevel
            });
          }
        }
      },
      "astro:build:done": async ({ dir }) => {
        if (resolvedOptions.serviceEntryPoint === "@astrojs/image/squoosh") {
          await copyWasmFiles(_config.output === "static" ? dir : _buildConfig.server);
        }
      }
    }
  };
}
export {
  integration as default,
  getImage,
  getPicture
};
