import CJS_COMPAT_NODE_URL_6jofinbrhmg from 'node:url';
import CJS_COMPAT_NODE_PATH_6jofinbrhmg from 'node:path';
import CJS_COMPAT_NODE_MODULE_6jofinbrhmg from "node:module";

var __filename = CJS_COMPAT_NODE_URL_6jofinbrhmg.fileURLToPath(import.meta.url);
var __dirname = CJS_COMPAT_NODE_PATH_6jofinbrhmg.dirname(__filename);
var require = CJS_COMPAT_NODE_MODULE_6jofinbrhmg.createRequire(import.meta.url);

// ------------------------------------------------------------
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
// ------------------------------------------------------------

// src/preview/virtual-module-mapping.ts
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import {
  getBuilderOptions,
  loadPreviewOrConfigFile,
  normalizeStories,
  readTemplate
} from "storybook/internal/common";
import { toImportFn } from "@storybook/core-webpack";

// ../../../node_modules/slash/index.js
function slash(path) {
  return path.startsWith("\\\\?\\") ? path : path.replace(/\\/g, "/");
}

// src/preview/virtual-module-mapping.ts
var getVirtualModules = async (options) => {
  let virtualModules = {}, builderOptions = await getBuilderOptions(options), workingDir = process.cwd(), isProd = options.configType === "PRODUCTION", nonNormalizedStories = await options.presets.apply("stories", []), entries = [], stories = normalizeStories(nonNormalizedStories, {
    configDir: options.configDir,
    workingDir
  }), previewAnnotations = [
    ...(await options.presets.apply("previewAnnotations", [], options)).map(
      (entry) => typeof entry == "object" ? entry.absolute : slash(entry)
    ),
    loadPreviewOrConfigFile(options)
  ].filter(Boolean), storiesFilename = "storybook-stories.js", storiesPath = resolve(join(workingDir, storiesFilename)), needPipelinedImport = !!builderOptions.lazyCompilation && !isProd;
  virtualModules[storiesPath] = toImportFn(stories, { needPipelinedImport });
  let configEntryPath = resolve(join(workingDir, "storybook-config-entry.js"));
  return virtualModules[configEntryPath] = (await readTemplate(
    fileURLToPath(
      import.meta.resolve("@storybook/builder-webpack5/templates/virtualModuleModernEntry.js")
    )
  )).replaceAll("'{{storiesFilename}}'", `'./${storiesFilename}'`).replaceAll(
    "'{{previewAnnotations}}'",
    previewAnnotations.filter(Boolean).map((entry) => `'${entry}'`).join(",")
  ).replaceAll(
    "'{{previewAnnotations_requires}}'",
    previewAnnotations.filter(Boolean).map((entry) => `require('${entry}')`).join(",")
  ).replace(/\\/g, "\\\\"), entries.push(configEntryPath), {
    virtualModules,
    entries
  };
};

export {
  getVirtualModules
};
