import CJS_COMPAT_NODE_URL_fkaa0x0zihe from 'node:url';
import CJS_COMPAT_NODE_PATH_fkaa0x0zihe from 'node:path';
import CJS_COMPAT_NODE_MODULE_fkaa0x0zihe from "node:module";

var __filename = CJS_COMPAT_NODE_URL_fkaa0x0zihe.fileURLToPath(import.meta.url);
var __dirname = CJS_COMPAT_NODE_PATH_fkaa0x0zihe.dirname(__filename);
var require = CJS_COMPAT_NODE_MODULE_fkaa0x0zihe.createRequire(import.meta.url);

// ------------------------------------------------------------
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
// ------------------------------------------------------------
import {
  resolvePackageDir
} from "./chunk-IZCZHPXK.js";
import "./chunk-QUBI53SI.js";
import {
  require_dist
} from "./chunk-OMWEUIQF.js";
import {
  __toESM
} from "./chunk-4ILNNMLG.js";

// src/core-server/presets/vitePlugins/vite-inject-mocker/plugin.ts
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";

// ../node_modules/@rolldown/pluginutils/dist/index.js
function exactRegex(str, flags) {
  return new RegExp(`^${escapeRegex(str)}$`, flags);
}
var escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
function escapeRegex(str) {
  return str.replace(escapeRegexRE, "\\$&");
}

// src/core-server/presets/vitePlugins/vite-inject-mocker/plugin.ts
var import_ts_dedent = __toESM(require_dist(), 1);
var entryPath = "/vite-inject-mocker-entry.js", entryCode = import_ts_dedent.dedent`
    <script type="module" src=".${entryPath}"></script>
  `, server, viteInjectMockerRuntime = (options) => {
  let viteConfig;
  return {
    name: "vite:storybook-inject-mocker-runtime",
    buildStart() {
      viteConfig.command === "build" && this.emitFile({
        type: "chunk",
        id: join(
          resolvePackageDir("storybook"),
          "assets",
          "server",
          "mocker-runtime.template.js"
        ),
        fileName: entryPath.slice(1)
      });
    },
    config() {
      return {
        optimizeDeps: {
          include: ["@vitest/mocker", "@vitest/mocker/browser"]
        },
        resolve: {
          // Aliasing necessary for package managers like pnpm, since resolving modules from a virtual module
          // leads to errors, if the imported module is not a dependency of the project.
          // By resolving the module to the real path, we can avoid this issue.
          alias: {
            "@vitest/mocker/browser": fileURLToPath(import.meta.resolve("@vitest/mocker/browser")),
            "@vitest/mocker": fileURLToPath(import.meta.resolve("@vitest/mocker"))
          }
        }
      };
    },
    configResolved(config) {
      viteConfig = config;
    },
    configureServer(server_) {
      server = server_, options.previewConfigPath && server.watcher.on("change", (file) => {
        file === options.previewConfigPath && server.ws.send({
          type: "custom",
          event: "invalidate-mocker"
        });
      });
    },
    resolveId: {
      filter: {
        id: [exactRegex(entryPath)]
      },
      handler(id) {
        return exactRegex(id).test(entryPath) ? id : null;
      }
    },
    async load(id) {
      return exactRegex(id).test(entryPath) ? readFileSync(
        join(resolvePackageDir("storybook"), "assets", "server", "mocker-runtime.template.js"),
        "utf-8"
      ) : null;
    },
    transformIndexHtml(html) {
      let headTag = html.match(/<head[^>]*>/);
      if (headTag) {
        let headTagIndex = html.indexOf(headTag[0]);
        return html.slice(0, headTagIndex + headTag[0].length) + entryCode + html.slice(headTagIndex + headTag[0].length);
      }
    }
  };
};
export {
  viteInjectMockerRuntime
};
