import { S as SSRStaticReplace, u as unheadDevtools, C as CreateHeadTransform, T as TreeshakeServerComposables, U as UseSeoMetaTransform, M as MinifyTransform, c as createHeadTransformContext } from './shared/bundler.D9eYJXZg.mjs';
import 'node:fs';
import 'node:path';
import 'node:url';
import 'magic-string';
import 'oxc-walker';
import '@vitejs/devtools-kit';
import 'oxc-parser';
import 'ufo';
import 'unplugin';
import 'node:vm';
import 'unhead/utils';

function resolveCoreDefs(options) {
  const defs = [];
  const common = { filter: options.filter, sourcemap: options.sourcemap };
  if (options.treeshake !== false) {
    const treeshakeOpts = typeof options.treeshake === "object" ? options.treeshake : {};
    defs.push({ instance: TreeshakeServerComposables, options: { ...common, ...treeshakeOpts } });
  }
  if (options.transformSeoMeta !== false) {
    const seoMetaOpts = typeof options.transformSeoMeta === "object" ? options.transformSeoMeta : {};
    defs.push({ instance: UseSeoMetaTransform, options: { ...common, ...seoMetaOpts } });
  }
  if (options.minify !== false) {
    const minifyOpts = typeof options.minify === "object" ? options.minify : options.minify === true ? { js: true, css: true } : {};
    if (minifyOpts.js || minifyOpts.css) {
      defs.push({ instance: MinifyTransform, options: { ...common, ...minifyOpts } });
    }
  }
  return defs;
}
function dispatch(bundler, defs) {
  const out = [];
  for (const { instance, options } of defs) {
    const plugin = instance[bundler](options);
    if (Array.isArray(plugin))
      out.push(...plugin);
    else out.push(plugin);
  }
  return out;
}
function resolveStreamingOpts(streaming) {
  return streaming && typeof streaming === "object" ? streaming : void 0;
}
function pushPlugin(out, value) {
  if (Array.isArray(value))
    out.push(...value);
  else out.push(value);
}
function createFrameworkPlugin({ framework, streamingPlugin }) {
  return (options = {}) => {
    const { streaming, validate, devtools, ...coreOpts } = options;
    const defs = resolveCoreDefs(coreOpts);
    const streamOpts = resolveStreamingOpts(streaming);
    const wantStreaming = !!streaming;
    return {
      vite: () => {
        const plugins = dispatch("vite", defs);
        const ctx = createHeadTransformContext();
        if (validate !== false) {
          ctx.addRuntimePlugin({
            import: { name: "ValidatePlugin", source: `${framework}/plugins`, as: "__unhead_validate" },
            client: "_h.use(__unhead_validate({ root: __ROOT__ }))"
          });
        }
        if (devtools !== false) {
          const devtoolsOpts = typeof devtools === "object" ? devtools : {};
          plugins.push(unheadDevtools({ ...devtoolsOpts, _ctx: ctx }));
        }
        plugins.push(SSRStaticReplace.vite({}));
        plugins.push(CreateHeadTransform(ctx));
        if (wantStreaming)
          pushPlugin(plugins, streamingPlugin.vite(streamOpts));
        return plugins;
      },
      webpack: () => {
        const plugins = dispatch("webpack", defs);
        plugins.push(SSRStaticReplace.webpack({}));
        if (wantStreaming)
          pushPlugin(plugins, streamingPlugin.webpack(streamOpts));
        return plugins;
      },
      rspack: () => {
        const plugins = dispatch("rspack", defs);
        plugins.push(SSRStaticReplace.rspack({}));
        if (wantStreaming)
          pushPlugin(plugins, streamingPlugin.rspack(streamOpts));
        return plugins;
      },
      rollup: () => {
        const plugins = dispatch("rollup", defs);
        plugins.push(SSRStaticReplace.rollup({}));
        if (wantStreaming)
          pushPlugin(plugins, streamingPlugin.rollup(streamOpts));
        return plugins;
      }
    };
  };
}

export { createFrameworkPlugin };
