import CJS_COMPAT_NODE_URL_dn9c4nlir7q from 'node:url';
import CJS_COMPAT_NODE_PATH_dn9c4nlir7q from 'node:path';
import CJS_COMPAT_NODE_MODULE_dn9c4nlir7q from "node:module";

var __filename = CJS_COMPAT_NODE_URL_dn9c4nlir7q.fileURLToPath(import.meta.url);
var __dirname = CJS_COMPAT_NODE_PATH_dn9c4nlir7q.dirname(__filename);
var require = CJS_COMPAT_NODE_MODULE_dn9c4nlir7q.createRequire(import.meta.url);

// ------------------------------------------------------------
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
// ------------------------------------------------------------
import "../../_node-chunks/chunk-ODCVTVYF.js";
import {
  doesStoryFileExist,
  generateStoryFile,
  getStoryMetadata,
  parseStaticDir,
  sendTelemetryError,
  throttle
} from "../../_node-chunks/chunk-XHX7XOUU.js";
import "../../_node-chunks/chunk-CSUHWWNG.js";
import "../../_node-chunks/chunk-6E33442Y.js";
import "../../_node-chunks/chunk-N63RIGIA.js";
import {
  globalSettings
} from "../../_node-chunks/chunk-DTPTTARU.js";
import {
  invariant
} from "../../_node-chunks/chunk-RJ4IGHNG.js";
import "../../_node-chunks/chunk-5DD3DRVV.js";
import {
  resolvePackageDir
} from "../../_node-chunks/chunk-FMQUYF6U.js";
import {
  isAbsolute,
  join
} from "../../_node-chunks/chunk-HUH3TXDQ.js";
import {
  glob
} from "../../_node-chunks/chunk-NR6FWHWG.js";
import "../../_node-chunks/chunk-XW3KSH44.js";
import {
  require_dist
} from "../../_node-chunks/chunk-4HLCCLOI.js";
import "../../_node-chunks/chunk-HXSGRVUD.js";
import "../../_node-chunks/chunk-PSHLFUWS.js";
import {
  require_picocolors
} from "../../_node-chunks/chunk-MKIOE2PF.js";
import {
  __commonJS,
  __require,
  __toESM
} from "../../_node-chunks/chunk-TRB37YZA.js";

// ../../node_modules/shell-quote/quote.js
var require_quote = __commonJS({
  "../../node_modules/shell-quote/quote.js"(exports, module) {
    "use strict";
    module.exports = function(xs) {
      return xs.map(function(s) {
        return s === "" ? "''" : s && typeof s == "object" ? s.op.replace(/(.)/g, "\\$1") : /["\s\\]/.test(s) && !/'/.test(s) ? "'" + s.replace(/(['])/g, "\\$1") + "'" : /["'\s]/.test(s) ? '"' + s.replace(/(["\\$`!])/g, "\\$1") + '"' : String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, "$1\\$2");
      }).join(" ");
    };
  }
});

// ../../node_modules/shell-quote/parse.js
var require_parse = __commonJS({
  "../../node_modules/shell-quote/parse.js"(exports, module) {
    "use strict";
    var CONTROL = "(?:" + [
      "\\|\\|",
      "\\&\\&",
      ";;",
      "\\|\\&",
      "\\<\\(",
      "\\<\\<\\<",
      ">>",
      ">\\&",
      "<\\&",
      "[&;()|<>]"
    ].join("|") + ")", controlRE = new RegExp("^" + CONTROL + "$"), META = "|&;()<> \\t", SINGLE_QUOTE = '"((\\\\"|[^"])*?)"', DOUBLE_QUOTE = "'((\\\\'|[^'])*?)'", hash = /^#$/, SQ = "'", DQ = '"', DS = "$", TOKEN = "", mult = 4294967296;
    for (i = 0; i < 4; i++)
      TOKEN += (mult * Math.random()).toString(16);
    var i, startsWithToken = new RegExp("^" + TOKEN);
    function matchAll(s, r) {
      for (var origIndex = r.lastIndex, matches = [], matchObj; matchObj = r.exec(s); )
        matches.push(matchObj), r.lastIndex === matchObj.index && (r.lastIndex += 1);
      return r.lastIndex = origIndex, matches;
    }
    function getVar(env2, pre, key) {
      var r = typeof env2 == "function" ? env2(key) : env2[key];
      return typeof r > "u" && key != "" ? r = "" : typeof r > "u" && (r = "$"), typeof r == "object" ? pre + TOKEN + JSON.stringify(r) + TOKEN : pre + r;
    }
    function parseInternal(string, env2, opts) {
      opts || (opts = {});
      var BS = opts.escape || "\\", BAREWORD = "(\\" + BS + `['"` + META + `]|[^\\s'"` + META + "])+", chunker = new RegExp([
        "(" + CONTROL + ")",
        // control chars
        "(" + BAREWORD + "|" + SINGLE_QUOTE + "|" + DOUBLE_QUOTE + ")+"
      ].join("|"), "g"), matches = matchAll(string, chunker);
      if (matches.length === 0)
        return [];
      env2 || (env2 = {});
      var commented = !1;
      return matches.map(function(match) {
        var s = match[0];
        if (!s || commented)
          return;
        if (controlRE.test(s))
          return { op: s };
        var quote = !1, esc = !1, out = "", isGlob = !1, i2;
        function parseEnvVar() {
          i2 += 1;
          var varend, varname, char = s.charAt(i2);
          if (char === "{") {
            if (i2 += 1, s.charAt(i2) === "}")
              throw new Error("Bad substitution: " + s.slice(i2 - 2, i2 + 1));
            if (varend = s.indexOf("}", i2), varend < 0)
              throw new Error("Bad substitution: " + s.slice(i2));
            varname = s.slice(i2, varend), i2 = varend;
          } else if (/[*@#?$!_-]/.test(char))
            varname = char, i2 += 1;
          else {
            var slicedFromI = s.slice(i2);
            varend = slicedFromI.match(/[^\w\d_]/), varend ? (varname = slicedFromI.slice(0, varend.index), i2 += varend.index - 1) : (varname = slicedFromI, i2 = s.length);
          }
          return getVar(env2, "", varname);
        }
        for (i2 = 0; i2 < s.length; i2++) {
          var c = s.charAt(i2);
          if (isGlob = isGlob || !quote && (c === "*" || c === "?"), esc)
            out += c, esc = !1;
          else if (quote)
            c === quote ? quote = !1 : quote == SQ ? out += c : c === BS ? (i2 += 1, c = s.charAt(i2), c === DQ || c === BS || c === DS ? out += c : out += BS + c) : c === DS ? out += parseEnvVar() : out += c;
          else if (c === DQ || c === SQ)
            quote = c;
          else {
            if (controlRE.test(c))
              return { op: s };
            if (hash.test(c)) {
              commented = !0;
              var commentObj = { comment: string.slice(match.index + i2 + 1) };
              return out.length ? [out, commentObj] : [commentObj];
            } else c === BS ? esc = !0 : c === DS ? out += parseEnvVar() : out += c;
          }
        }
        return isGlob ? { op: "glob", pattern: out } : out;
      }).reduce(function(prev, arg) {
        return typeof arg > "u" ? prev : prev.concat(arg);
      }, []);
    }
    module.exports = function(s, env2, opts) {
      var mapped = parseInternal(s, env2, opts);
      return typeof env2 != "function" ? mapped : mapped.reduce(function(acc, s2) {
        if (typeof s2 == "object")
          return acc.concat(s2);
        var xs = s2.split(RegExp("(" + TOKEN + ".*?" + TOKEN + ")", "g"));
        return xs.length === 1 ? acc.concat(xs[0]) : acc.concat(xs.filter(Boolean).map(function(x) {
          return startsWithToken.test(x) ? JSON.parse(x.split(TOKEN)[1]) : x;
        }));
      }, []);
    };
  }
});

// ../../node_modules/shell-quote/index.js
var require_shell_quote = __commonJS({
  "../../node_modules/shell-quote/index.js"(exports) {
    "use strict";
    exports.quote = require_quote();
    exports.parse = require_parse();
  }
});

// ../../node_modules/launch-editor/editor-info/macos.js
var require_macos = __commonJS({
  "../../node_modules/launch-editor/editor-info/macos.js"(exports, module) {
    module.exports = {
      "/Applications/Atom.app/Contents/MacOS/Atom": "atom",
      "/Applications/Atom Beta.app/Contents/MacOS/Atom Beta": "/Applications/Atom Beta.app/Contents/MacOS/Atom Beta",
      "/Applications/Brackets.app/Contents/MacOS/Brackets": "brackets",
      "/Applications/Sublime Text.app/Contents/MacOS/Sublime Text": "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl",
      "/Applications/Sublime Text.app/Contents/MacOS/sublime_text": "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl",
      "/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2": "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl",
      "/Applications/Sublime Text Dev.app/Contents/MacOS/Sublime Text": "/Applications/Sublime Text Dev.app/Contents/SharedSupport/bin/subl",
      "/Applications/Visual Studio Code.app/Contents/MacOS/Electron": "code",
      "/Applications/Visual Studio Code - Insiders.app/Contents/MacOS/Electron": "code-insiders",
      "/Applications/VSCodium.app/Contents/MacOS/Electron": "codium",
      "/Applications/Cursor.app/Contents/MacOS/Cursor": "cursor",
      "/Applications/Trae.app/Contents/MacOS/Electron": "trae",
      "/Applications/AppCode.app/Contents/MacOS/appcode": "/Applications/AppCode.app/Contents/MacOS/appcode",
      "/Applications/CLion.app/Contents/MacOS/clion": "/Applications/CLion.app/Contents/MacOS/clion",
      "/Applications/IntelliJ IDEA.app/Contents/MacOS/idea": "/Applications/IntelliJ IDEA.app/Contents/MacOS/idea",
      "/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea": "/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea",
      "/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea": "/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea",
      "/Applications/PhpStorm.app/Contents/MacOS/phpstorm": "/Applications/PhpStorm.app/Contents/MacOS/phpstorm",
      "/Applications/PyCharm.app/Contents/MacOS/pycharm": "/Applications/PyCharm.app/Contents/MacOS/pycharm",
      "/Applications/PyCharm CE.app/Contents/MacOS/pycharm": "/Applications/PyCharm CE.app/Contents/MacOS/pycharm",
      "/Applications/RubyMine.app/Contents/MacOS/rubymine": "/Applications/RubyMine.app/Contents/MacOS/rubymine",
      "/Applications/WebStorm.app/Contents/MacOS/webstorm": "/Applications/WebStorm.app/Contents/MacOS/webstorm",
      "/Applications/MacVim.app/Contents/MacOS/MacVim": "mvim",
      "/Applications/GoLand.app/Contents/MacOS/goland": "/Applications/GoLand.app/Contents/MacOS/goland",
      "/Applications/Rider.app/Contents/MacOS/rider": "/Applications/Rider.app/Contents/MacOS/rider",
      "/Applications/Zed.app/Contents/MacOS/zed": "zed"
    };
  }
});

// ../../node_modules/launch-editor/editor-info/linux.js
var require_linux = __commonJS({
  "../../node_modules/launch-editor/editor-info/linux.js"(exports, module) {
    module.exports = {
      atom: "atom",
      Brackets: "brackets",
      "code-insiders": "code-insiders",
      code: "code",
      vscodium: "vscodium",
      codium: "codium",
      cursor: "cursor",
      trae: "trae",
      emacs: "emacs",
      gvim: "gvim",
      idea: "idea",
      "idea.sh": "idea",
      phpstorm: "phpstorm",
      "phpstorm.sh": "phpstorm",
      pycharm: "pycharm",
      "pycharm.sh": "pycharm",
      rubymine: "rubymine",
      "rubymine.sh": "rubymine",
      sublime_text: "subl",
      vim: "vim",
      webstorm: "webstorm",
      "webstorm.sh": "webstorm",
      goland: "goland",
      "goland.sh": "goland",
      rider: "rider",
      "rider.sh": "rider",
      zed: "zed"
    };
  }
});

// ../../node_modules/launch-editor/editor-info/windows.js
var require_windows = __commonJS({
  "../../node_modules/launch-editor/editor-info/windows.js"(exports, module) {
    module.exports = [
      "Brackets.exe",
      "Code.exe",
      "Code - Insiders.exe",
      "VSCodium.exe",
      "Cursor.exe",
      "atom.exe",
      "sublime_text.exe",
      "notepad++.exe",
      "clion.exe",
      "clion64.exe",
      "idea.exe",
      "idea64.exe",
      "phpstorm.exe",
      "phpstorm64.exe",
      "pycharm.exe",
      "pycharm64.exe",
      "rubymine.exe",
      "rubymine64.exe",
      "webstorm.exe",
      "webstorm64.exe",
      "goland.exe",
      "goland64.exe",
      "rider.exe",
      "rider64.exe",
      "trae.exe"
    ];
  }
});

// ../../node_modules/launch-editor/guess.js
var require_guess = __commonJS({
  "../../node_modules/launch-editor/guess.js"(exports, module) {
    var path = __require("path"), shellQuote = require_shell_quote(), childProcess = __require("child_process"), COMMON_EDITORS_MACOS = require_macos(), COMMON_EDITORS_LINUX = require_linux(), COMMON_EDITORS_WIN = require_windows();
    module.exports = function(specifiedEditor) {
      if (specifiedEditor)
        return shellQuote.parse(specifiedEditor);
      if (process.env.LAUNCH_EDITOR)
        return [process.env.LAUNCH_EDITOR];
      if (process.versions.webcontainer)
        return [process.env.EDITOR || "code"];
      try {
        if (process.platform === "darwin") {
          let output = childProcess.execSync("ps x -o comm=", {
            stdio: ["pipe", "pipe", "ignore"]
          }).toString(), processNames = Object.keys(COMMON_EDITORS_MACOS), processList = output.split(`
`);
          for (let i = 0; i < processNames.length; i++) {
            let processName = processNames[i];
            if (processList.includes(processName))
              return [COMMON_EDITORS_MACOS[processName]];
            let processNameWithoutApplications = processName.replace("/Applications", "");
            if (output.indexOf(processNameWithoutApplications) !== -1) {
              if (processName !== COMMON_EDITORS_MACOS[processName])
                return [COMMON_EDITORS_MACOS[processName]];
              let runningProcess = processList.find((procName) => procName.endsWith(processNameWithoutApplications));
              if (runningProcess !== void 0)
                return [runningProcess];
            }
          }
        } else if (process.platform === "win32") {
          let runningProcesses = childProcess.execSync(
            'powershell -NoProfile -Command "[Console]::OutputEncoding=[Text.Encoding]::UTF8;Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"',
            {
              stdio: ["pipe", "pipe", "ignore"]
            }
          ).toString().split(`\r
`);
          for (let i = 0; i < runningProcesses.length; i++) {
            let fullProcessPath = runningProcesses[i].trim(), shortProcessName = path.basename(fullProcessPath);
            if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1)
              return [fullProcessPath];
          }
        } else if (process.platform === "linux") {
          let output = childProcess.execSync("ps x --no-heading -o comm --sort=comm", {
            stdio: ["pipe", "pipe", "ignore"]
          }).toString(), processNames = Object.keys(COMMON_EDITORS_LINUX);
          for (let i = 0; i < processNames.length; i++) {
            let processName = processNames[i];
            if (output.indexOf(processName) !== -1)
              return [COMMON_EDITORS_LINUX[processName]];
          }
        }
      } catch {
      }
      return process.env.VISUAL ? [process.env.VISUAL] : process.env.EDITOR ? [process.env.EDITOR] : [null];
    };
  }
});

// ../../node_modules/launch-editor/get-args.js
var require_get_args = __commonJS({
  "../../node_modules/launch-editor/get-args.js"(exports, module) {
    var path = __require("path");
    module.exports = function(editor, fileName, lineNumber, columnNumber = 1) {
      switch (path.basename(editor).replace(/\.(exe|cmd|bat)$/i, "")) {
        case "atom":
        case "Atom":
        case "Atom Beta":
        case "subl":
        case "sublime":
        case "sublime_text":
        case "wstorm":
        case "charm":
        case "zed":
          return [`${fileName}:${lineNumber}:${columnNumber}`];
        case "notepad++":
          return ["-n" + lineNumber, "-c" + columnNumber, fileName];
        case "vim":
        case "mvim":
          return [`+call cursor(${lineNumber}, ${columnNumber})`, fileName];
        case "joe":
        case "gvim":
          return [`+${lineNumber}`, fileName];
        case "emacs":
        case "emacsclient":
          return [`+${lineNumber}:${columnNumber}`, fileName];
        case "rmate":
        case "mate":
        case "mine":
          return ["--line", lineNumber, fileName];
        case "code":
        case "Code":
        case "code-insiders":
        case "Code - Insiders":
        case "codium":
        case "trae":
        case "cursor":
        case "vscodium":
        case "VSCodium":
          return ["-r", "-g", `${fileName}:${lineNumber}:${columnNumber}`];
        case "appcode":
        case "clion":
        case "clion64":
        case "idea":
        case "idea64":
        case "phpstorm":
        case "phpstorm64":
        case "pycharm":
        case "pycharm64":
        case "rubymine":
        case "rubymine64":
        case "webstorm":
        case "webstorm64":
        case "goland":
        case "goland64":
        case "rider":
        case "rider64":
          return ["--line", lineNumber, "--column", columnNumber, fileName];
      }
      return process.env.LAUNCH_EDITOR ? [fileName, lineNumber, columnNumber] : [fileName];
    };
  }
});

// ../../node_modules/launch-editor/index.js
var require_launch_editor = __commonJS({
  "../../node_modules/launch-editor/index.js"(exports, module) {
    var fs = __require("fs"), os = __require("os"), path = __require("path"), colors = require_picocolors(), childProcess = __require("child_process"), guessEditor = require_guess(), getArgumentsForPosition = require_get_args();
    function wrapErrorCallback(cb) {
      return (fileName, errorMessage) => {
        console.log(), console.log(
          colors.red("Could not open " + path.basename(fileName) + " in the editor.")
        ), errorMessage && (errorMessage[errorMessage.length - 1] !== "." && (errorMessage += "."), console.log(
          colors.red("The editor process exited with an error: " + errorMessage)
        )), console.log(), cb && cb(fileName, errorMessage);
      };
    }
    function isTerminalEditor(editor) {
      switch (editor) {
        case "vim":
        case "emacs":
        case "nano":
          return !0;
      }
      return !1;
    }
    var positionRE = /:(\d+)(:(\d+))?$/;
    function parseFile(file) {
      file.startsWith("file://") && (file = __require("url").fileURLToPath(file));
      let fileName = file.replace(positionRE, ""), match = file.match(positionRE), lineNumber = match && match[1], columnNumber = match && match[3];
      return {
        fileName,
        lineNumber,
        columnNumber
      };
    }
    var _childProcess = null;
    function launchEditor(file, specifiedEditor, onErrorCallback) {
      let parsed = parseFile(file), { fileName } = parsed, { lineNumber, columnNumber } = parsed;
      if (!fs.existsSync(fileName))
        return;
      typeof specifiedEditor == "function" && (onErrorCallback = specifiedEditor, specifiedEditor = void 0), onErrorCallback = wrapErrorCallback(onErrorCallback);
      let [editor, ...args] = guessEditor(specifiedEditor);
      if (!editor) {
        onErrorCallback(fileName, null);
        return;
      }
      if (process.platform === "linux" && fileName.startsWith("/mnt/") && /Microsoft/i.test(os.release()) && (fileName = path.relative("", fileName)), lineNumber) {
        let extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber);
        args.push.apply(args, extraArgs);
      } else
        args.push(fileName);
      if (_childProcess && isTerminalEditor(editor) && _childProcess.kill("SIGKILL"), process.platform === "win32") {
        let escapeCmdArgs2 = function(cmdArgs) {
          return cmdArgs.replace(/([&|<>,;=^])/g, "^$1");
        }, doubleQuoteIfNeeded2 = function(str) {
          return str.includes("^") ? `^"${str}^"` : str.includes(" ") ? `"${str}"` : str;
        };
        var escapeCmdArgs = escapeCmdArgs2, doubleQuoteIfNeeded = doubleQuoteIfNeeded2;
        let launchCommand = [editor, ...args.map(escapeCmdArgs2)].map(doubleQuoteIfNeeded2).join(" ");
        _childProcess = childProcess.exec(launchCommand, {
          stdio: "inherit",
          shell: !0
        });
      } else
        _childProcess = childProcess.spawn(editor, args, { stdio: "inherit" });
      _childProcess.on("exit", function(errorCode) {
        _childProcess = null, errorCode && onErrorCallback(fileName, "(code " + errorCode + ")");
      }), _childProcess.on("error", function(error) {
        let { code, message } = error;
        code === "ENOENT" && (message = `${message} ('${editor}' command does not exist in 'PATH')`), onErrorCallback(fileName, message);
      });
    }
    module.exports = launchEditor;
  }
});

// src/core-server/presets/common-preset.ts
import { existsSync as existsSync3 } from "node:fs";
import { readFile as readFile5 } from "node:fs/promises";
import { normalizeStories, optionalEnvToBoolean } from "storybook/internal/common";
import {
  JsPackageManagerFactory as JsPackageManagerFactory2,
  getDirectoryFromWorkingDir,
  getPreviewBodyTemplate,
  getPreviewHeadTemplate,
  loadEnvs,
  removeAddon as removeAddonBase
} from "storybook/internal/common";
import { StoryIndexGenerator } from "storybook/internal/core-server";
import { readCsf as readCsf2 } from "storybook/internal/csf-tools";
import { logger as logger5 } from "storybook/internal/node-logger";
import { telemetry as telemetry9 } from "storybook/internal/telemetry";
var import_ts_dedent = __toESM(require_dist(), 1);

// src/core-server/server-channel/create-new-story-channel.ts
import {
  CREATE_NEW_STORYFILE_REQUEST,
  CREATE_NEW_STORYFILE_RESPONSE
} from "storybook/internal/core-events";
import { telemetry } from "storybook/internal/telemetry";
function initCreateNewStoryChannel(channel, options, coreOptions) {
  return channel.on(
    CREATE_NEW_STORYFILE_REQUEST,
    async (data) => {
      let result = await generateStoryFile(data.payload, options);
      result.success ? (channel.emit(CREATE_NEW_STORYFILE_RESPONSE, {
        success: !0,
        id: data.id,
        payload: {
          storyId: result.storyId,
          storyFilePath: result.storyFilePath,
          exportedStoryName: result.exportedStoryName,
          alreadyContainsArgs: result.alreadyContainsArgs
        },
        error: null
      }), coreOptions.disableTelemetry || telemetry("create-new-story-file", {
        success: !0
      })) : (channel.emit(CREATE_NEW_STORYFILE_RESPONSE, {
        success: !1,
        id: data.id,
        payload: result.errorType === "STORY_FILE_EXISTS" ? {
          type: "STORY_FILE_EXISTS",
          kind: result.kind
        } : void 0,
        error: result.error || "Unknown error occurred"
      }), coreOptions.disableTelemetry || await telemetry("create-new-story-file", {
        success: !1,
        error: result.errorType || result.error
      }));
    }
  ), channel;
}

// src/core-server/server-channel/file-search-channel.ts
import { readFile } from "node:fs/promises";
import { dirname, join as join2 } from "node:path";
import { extractRenderer, getFrameworkName, getProjectRoot } from "storybook/internal/common";
import {
  FILE_COMPONENT_SEARCH_REQUEST,
  FILE_COMPONENT_SEARCH_RESPONSE
} from "storybook/internal/core-events";
import { telemetry as telemetry2 } from "storybook/internal/telemetry";

// src/core-server/utils/parser/generic-parser.ts
import { parser, types as t } from "storybook/internal/babel";
var GenericParser = class {
  /**
   * Parse the content of a file and return the exports
   *
   * @param content The content of the file
   * @returns The exports of the file
   */
  async parse(content) {
    let ast = parser.parse(content, {
      allowImportExportEverywhere: !0,
      allowAwaitOutsideFunction: !0,
      allowNewTargetOutsideFunction: !0,
      allowReturnOutsideFunction: !0,
      allowUndeclaredExports: !0,
      plugins: [
        // Language features
        "typescript",
        "jsx",
        // Latest ECMAScript features
        "asyncGenerators",
        "bigInt",
        "classProperties",
        "classPrivateProperties",
        "classPrivateMethods",
        "classStaticBlock",
        "dynamicImport",
        "exportNamespaceFrom",
        "logicalAssignment",
        "moduleStringNames",
        "nullishCoalescingOperator",
        "numericSeparator",
        "objectRestSpread",
        "optionalCatchBinding",
        "optionalChaining",
        "privateIn",
        "regexpUnicodeSets",
        "topLevelAwait",
        // ECMAScript proposals
        "asyncDoExpressions",
        "decimal",
        "decorators",
        "decoratorAutoAccessors",
        "deferredImportEvaluation",
        "destructuringPrivate",
        "doExpressions",
        "explicitResourceManagement",
        "exportDefaultFrom",
        "functionBind",
        "functionSent",
        "importAttributes",
        "importReflection",
        "moduleBlocks",
        "partialApplication",
        "recordAndTuple",
        "sourcePhaseImports",
        "throwExpressions"
      ]
    }), exports = [];
    return ast.program.body.forEach(function(node) {
      t.isExportNamedDeclaration(node) ? (t.isFunctionDeclaration(node.declaration) && t.isIdentifier(node.declaration.id) && exports.push({
        name: node.declaration.id.name,
        default: !1
      }), t.isClassDeclaration(node.declaration) && t.isIdentifier(node.declaration.id) && exports.push({
        name: node.declaration.id.name,
        default: !1
      }), node.declaration === null && node.specifiers.length > 0 && node.specifiers.forEach((specifier) => {
        t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported) && exports.push({
          name: specifier.exported.name,
          default: !1
        });
      }), t.isVariableDeclaration(node.declaration) && node.declaration.declarations.forEach((declaration) => {
        t.isVariableDeclarator(declaration) && t.isIdentifier(declaration.id) && exports.push({
          name: declaration.id.name,
          default: !1
        });
      })) : t.isExportDefaultDeclaration(node) && exports.push({
        name: "default",
        default: !0
      });
    }), { exports };
  }
};

// src/core-server/utils/parser/index.ts
function getParser(renderer) {
  return new GenericParser();
}

// src/core-server/utils/search-files.ts
var FILE_EXTENSIONS = ["js", "mjs", "cjs", "jsx", "mts", "ts", "tsx", "cts"], IGNORED_FILES = [
  "**/node_modules/**",
  "**/*.spec.*",
  "**/*.test.*",
  "**/*.stories.*",
  "**/storybook-static/**"
];
async function searchFiles({
  searchQuery,
  cwd,
  ignoredFiles = IGNORED_FILES,
  fileExtensions = FILE_EXTENSIONS
}) {
  let { globby, isDynamicPattern } = await import("../../_node-chunks/globby-DIXVHIEZ.js"), hasSearchSpecialGlobChars = isDynamicPattern(searchQuery, { cwd }), searchQueryHasExtension = /(\.[a-z]+)$/i.test(searchQuery), fileExtensionsPattern = `{${fileExtensions.join(",")}}`, globbedSearchQuery = hasSearchSpecialGlobChars ? searchQuery : searchQueryHasExtension ? [`**/*${searchQuery}*`, `**/*${searchQuery}*/**`] : [
    `**/*${searchQuery}*.${fileExtensionsPattern}`,
    `**/*${searchQuery}*/**/*.${fileExtensionsPattern}`
  ];
  return (await globby(globbedSearchQuery, {
    ignore: ignoredFiles,
    gitignore: !0,
    caseSensitiveMatch: !1,
    cwd,
    objectMode: !0
  })).map((entry) => entry.path).filter((entry) => fileExtensions.some((ext) => entry.endsWith(`.${ext}`)));
}

// src/core-server/server-channel/file-search-channel.ts
async function initFileSearchChannel(channel, options, coreOptions) {
  return channel.on(
    FILE_COMPONENT_SEARCH_REQUEST,
    async (data) => {
      let searchQuery = data.id;
      try {
        if (!searchQuery)
          return;
        let frameworkName = await getFrameworkName(options), rendererName = await extractRenderer(frameworkName), entries = (await searchFiles({
          searchQuery,
          cwd: getProjectRoot()
        })).map(async (file) => {
          let parser3 = getParser(rendererName);
          try {
            let content = await readFile(join2(getProjectRoot(), file), "utf-8"), { storyFileName } = getStoryMetadata(join2(getProjectRoot(), file)), dir = dirname(file), storyFileExists = doesStoryFileExist(join2(getProjectRoot(), dir), storyFileName), info = await parser3.parse(content);
            return {
              filepath: file,
              exportedComponents: info.exports,
              storyFileExists
            };
          } catch (e) {
            return coreOptions.disableTelemetry || telemetry2("create-new-story-file-search", {
              success: !1,
              error: `Could not parse file: ${e}`
            }), {
              filepath: file,
              storyFileExists: !1,
              exportedComponents: null
            };
          }
        });
        coreOptions.disableTelemetry || telemetry2("create-new-story-file-search", {
          success: !0,
          payload: {
            fileCount: entries.length
          }
        }), channel.emit(FILE_COMPONENT_SEARCH_RESPONSE, {
          success: !0,
          id: searchQuery,
          payload: {
            files: await Promise.all(entries)
          },
          error: null
        });
      } catch (e) {
        channel.emit(FILE_COMPONENT_SEARCH_RESPONSE, {
          success: !1,
          id: searchQuery ?? "",
          error: `An error occurred while searching for components in the project.
${e?.message}`
        }), coreOptions.disableTelemetry || telemetry2("create-new-story-file-search", {
          success: !1,
          error: `An error occured while searching for components: ${e}`
        });
      }
    }
  ), channel;
}

// src/core-server/server-channel/open-in-editor-channel.ts
var import_launch_editor = __toESM(require_launch_editor(), 1);
import { OPEN_IN_EDITOR_REQUEST, OPEN_IN_EDITOR_RESPONSE } from "storybook/internal/core-events";
import { telemetry as telemetry3 } from "storybook/internal/telemetry";
async function initOpenInEditorChannel(channel, _options, coreOptions) {
  return channel.on(OPEN_IN_EDITOR_REQUEST, async (payload) => {
    let sendTelemetry = (data) => {
      coreOptions.disableTelemetry || telemetry3("open-in-editor", data);
    };
    try {
      let { file: targetFile, line, column } = payload;
      if (!targetFile)
        throw new Error("No file was provided to open");
      let location = typeof line == "number" ? `${targetFile}:${line}${typeof column == "number" ? `:${column}` : ""}` : targetFile;
      await new Promise((resolve, reject) => {
        (0, import_launch_editor.default)(location, void 0, (_fileName, errorMessage) => {
          errorMessage ? reject(new Error(errorMessage)) : resolve();
        });
      }), channel.emit(OPEN_IN_EDITOR_RESPONSE, {
        file: targetFile,
        line,
        column,
        error: null
      }), sendTelemetry({ success: !0 });
    } catch (e) {
      let error = e?.message || "Failed to open in editor";
      channel.emit(OPEN_IN_EDITOR_RESPONSE, {
        error,
        ...payload
      }), sendTelemetry({ success: !1, error });
    }
  }), channel;
}

// src/core-server/server-channel/preview-initialized-channel.ts
import { PREVIEW_INITIALIZED } from "storybook/internal/core-events";
import { telemetry as telemetry4 } from "storybook/internal/telemetry";
import { getLastEvents } from "storybook/internal/telemetry";
import { getSessionId } from "storybook/internal/telemetry";
var makePayload = (userAgent, lastInit, sessionId) => {
  let payload = {
    userAgent,
    isNewUser: !1,
    timeSinceInit: void 0
  };
  return sessionId && lastInit?.body?.sessionId === sessionId && (payload.timeSinceInit = Date.now() - lastInit.timestamp, payload.isNewUser = !!lastInit.body.payload.newUser), payload;
};
function initPreviewInitializedChannel(channel, options, _coreConfig) {
  channel.on(PREVIEW_INITIALIZED, async ({ userAgent }) => {
    if (!options.disableTelemetry)
      try {
        let sessionId = await getSessionId(), lastEvents = await getLastEvents(), lastInit = lastEvents.init;
        if (!lastEvents["preview-first-load"]) {
          let payload = makePayload(userAgent, lastInit, sessionId);
          telemetry4("preview-first-load", payload);
        }
      } catch {
      }
  });
}

// src/core-server/server-channel/story-discovery-channel.ts
import { existsSync as existsSync2 } from "node:fs";
import { mkdir, readFile as readFile4, rm } from "node:fs/promises";
import { join as join4 } from "node:path";
import {
  cache,
  executeCommand,
  getProjectRoot as getProjectRoot2,
  resolvePathInStorybookCache
} from "storybook/internal/common";
import {
  STORY_DISCOVERY_PROGRESS,
  STORY_DISCOVERY_REQUEST,
  STORY_DISCOVERY_RESPONSE
} from "storybook/internal/core-events";
import { getStorybookMetadata, telemetry as telemetry5 } from "storybook/internal/telemetry";

// src/core-server/utils/story-generation.ts
import { existsSync } from "node:fs";
import { readFile as readFile3 } from "node:fs/promises";
import { dirname as dirname3, join as join3 } from "node:path";
import { JsPackageManagerFactory } from "storybook/internal/common";
import { experimental_loadStorybook, generateStoryFile as generateStoryFile2 } from "storybook/internal/core-server";
import { logger } from "storybook/internal/node-logger";

// ../../node_modules/@hipster/sb-utils/dist/component-analyzer-B7RUGTYG.mjs
import { basename, dirname as dirname2 } from "node:path";
import { readFile as readFile2 } from "node:fs/promises";
var MODEL_VERSION = "1.0.0", DEFAULT_MODEL_CONFIG = {
  LOC_DIVISOR: 60,
  LOC_MAX_SCORE: 20,
  IMPORT_DIVISOR: 1,
  IMPORT_MAX_SCORE: 15,
  HOOK_MULTIPLIER: 1.5,
  HOOKS_MAX_SCORE: 15,
  CUSTOM_HOOK_BONUS: 2,
  PATTERN_WEIGHTS: {
    AUTH: 5,
    DATA_FETCHING: 5,
    ROUTER: 3,
    ASYNC: 3,
    ERROR_HANDLING: 2,
    STATE_MANAGEMENT: 4,
    CONTEXT: 3,
    CSS_IN_JS: 4,
    HOOKS: 1
  },
  PATTERN_MAX_SCORE: 30,
  CLASSIFICATION_BASE: {
    DEFAULT: 8,
    DESIGN_SYSTEM: 5,
    PAGE: 15,
    FEATURE: 12,
    UTILITY: 6
  },
  LEVELS: {
    SIMPLE: 25,
    MEDIUM: 50,
    HIGH: 75
  }
}, DESIGN_SYSTEM_NAMES = [
  "button",
  "input",
  "textarea",
  "select",
  "checkbox",
  "radio",
  "switch",
  "toggle",
  "icon",
  "avatar",
  "badge",
  "tag",
  "label",
  "tooltip",
  "popover",
  "modal",
  "dialog",
  "dropdown",
  "menu",
  "navbar",
  "nav",
  "tab",
  "accordion",
  "card",
  "alert",
  "notification",
  "toast",
  "spinner",
  "loader",
  "progress",
  "slider",
  "table",
  "form",
  "field",
  "text",
  "heading",
  "title",
  "link",
  "anchor"
], PAGE_NAMES = [
  "page",
  "screen",
  "view",
  "dashboard",
  "profile",
  "settings",
  "login",
  "register",
  "signup",
  "home",
  "landing",
  "admin"
], FEATURE_INDICATORS = [
  "feature",
  "features",
  "module",
  "domain"
], PATTERNS = {
  HOOKS: [
    /\buse[A-Z]\w*(?:\s*<[^>]*>)?\s*\(/g,
    /\bReact\.use[A-Z]\w*(?:\s*<[^>]*>)?\s*\(/g,
    /\buse\w+\s*=\s*use[A-Z]\w*(?:\s*<[^>]*>)?\s*\(/g
  ],
  AUTH: [
    /\bauth\b/i,
    /\blogin\b/i,
    /\blogout\b/i,
    /\bsignin\b/i,
    /\bsignout\b/i,
    /\bauthentication\b/i,
    /\bauthorization\b/i,
    /\btoken\b/i,
    /\bsession\b/i,
    /\buser\b\s*\.\s*\w+/i,
    /\bpermission\b/i,
    /\brole\b/i,
    /\buseAuth\b/i,
    /\bAuthProvider\b/i
  ],
  DATA_FETCHING: [
    /\bfetch\s*\(/,
    /\baxios\s*\./,
    /\bapi\s*\//,
    /\bhttp\s*\//,
    /\bgraphql/i,
    /\bquery\s*\(/,
    /\bmutation\s*\(/,
    /\buseQuery\s*\(/,
    /\buseMutation\s*\(/,
    /\bswr\s*\(/,
    /\breact-query/i,
    /\bapollo/i
  ],
  ROUTER: [
    /\buseRouter\b/,
    /\buseNavigate\b/,
    /\buseParams\b/,
    /\buseLocation\b/,
    /\bLink\s+from\s+['"]react-router/i,
    /\bNavLink\s+from\s+['"]react-router/i,
    /\bBrowserRouter\b/,
    /\bRoutes\b/,
    /\bRoute\b/,
    /\bnext\/router/i,
    /\bnext\/navigation/i
  ],
  ASYNC: [
    /\basync\s+/,
    /\bawait\s+/,
    /\bPromise\s*\./,
    /\bthen\s*\(/,
    /\bcatch\s*\(/,
    /\buseAsync\b/i
  ],
  ERROR_HANDLING: [
    /\btry\s*\{/,
    /\bcatch\s*\(/,
    /\bError\s*\(/,
    /\bthrow\s+/,
    /\buseError\b/i,
    /\berror\s*boundary/i
  ],
  STATE_MANAGEMENT: [
    /\buseReducer\b/,
    /\buseContext\b/,
    /\buseStore\b/i,
    /\buseSelector\b/i,
    /\buseDispatch\b/i,
    /\bredux/i,
    /\bzustand/i,
    /\bjotai/i,
    /\brecoil/i
  ],
  CONTEXT: [
    /\bcreateContext\b/,
    /\buseContext\b/,
    /\bContext\.Provider\b/,
    /\bContext\.Consumer\b/
  ],
  CSS_IN_JS: [
    /\bimport.*styled.*from.*styled-components\b/,
    /\bstyled\.\w+\s*</,
    /\bstyled\.\w+\s*`/,
    /\bcss\s*`/,
    /\bkeyframes\s*\(/,
    /@emotion/,
    /styled-jsx/,
    /linaria/,
    /react-jss/,
    /vanilla-extract/
  ]
};
function braceDelta(line) {
  let delta = 0;
  for (let ch of line) ch === "{" ? delta += 1 : ch === "}" && (delta -= 1);
  return delta;
}
function countNonEmptyRuntimeLines(lines) {
  let TYPE_OR_INTERFACE_DECL_RE = /^\s*(export\s+)?(declare\s+)?(type|interface)\s+[A-Za-z_$][\w$]*/, TYPE_ONLY_EXPORT_RE = /^\s*export\s+type\s*\{/, nonEmptyRuntimeLines = 0, inTypeBlock = !1, typeBraceDepth = 0;
  for (let line of lines) {
    let trimmed = line.trim();
    if (trimmed.length !== 0 && !(!inTypeBlock && TYPE_ONLY_EXPORT_RE.test(trimmed))) {
      if (!inTypeBlock && TYPE_OR_INTERFACE_DECL_RE.test(trimmed)) {
        inTypeBlock = !0, typeBraceDepth += braceDelta(trimmed);
        let endsWithSemicolon = /;\s*$/.test(trimmed), endsWithClosingBrace = /}\s*;?\s*$/.test(trimmed), oneLineBraceBlock = trimmed.includes("{") && trimmed.includes("}");
        typeBraceDepth <= 0 && (endsWithSemicolon || endsWithClosingBrace || oneLineBraceBlock) && (inTypeBlock = !1, typeBraceDepth = 0);
        continue;
      }
      if (inTypeBlock) {
        typeBraceDepth += braceDelta(trimmed);
        let endsWithSemicolon = /;\s*$/.test(trimmed), endsWithClosingBrace = /}\s*;?\s*$/.test(trimmed);
        typeBraceDepth <= 0 && (endsWithSemicolon || endsWithClosingBrace) && (inTypeBlock = !1, typeBraceDepth = 0);
        continue;
      }
      nonEmptyRuntimeLines += 1;
    }
  }
  return nonEmptyRuntimeLines;
}
async function extractLowLevelFeatures(filePath, content) {
  let fileContent = content ?? await readFile2(filePath, "utf-8"), lines = fileContent.split(`
`), nonEmptyLines = lines.filter((line) => line.trim().length > 0).length, nonEmptyRuntimeLines = countNonEmptyRuntimeLines(lines), importLines = lines.filter((line) => line.trim().startsWith("import")), reactImports = [], externalImports = [], internalImports = [];
  for (let line of importLines) {
    let fromMatch = line.match(/from\s+['"]([^'"]+)['"]/), pathMatch = line.match(/import\s+['"]([^'"]+)['"]/), importPath = fromMatch?.[1] ?? pathMatch?.[1];
    importPath && (importPath === "react" || importPath.startsWith("react/") || importPath.startsWith("@types/react") ? reactImports.push(importPath) : importPath.startsWith(".") || importPath.startsWith("/") ? internalImports.push(importPath) : externalImports.push(importPath));
  }
  let hookNames = /* @__PURE__ */ new Set();
  for (let pattern of PATTERNS.HOOKS) {
    let matches = fileContent.match(pattern);
    if (matches) for (let match of matches) {
      let nameMatch = match.match(/\b(use[A-Z]\w*)/);
      nameMatch && hookNames.add(nameMatch[1]);
    }
  }
  let patternCounts = {};
  for (let key of Object.keys(PATTERNS)) {
    let count = 0;
    for (let pattern of PATTERNS[key]) {
      let matches = fileContent.match(pattern);
      matches && (count += matches.length);
    }
    patternCounts[key] = count;
  }
  return {
    meta: {
      filePath,
      fileName: basename(filePath),
      directory: dirname2(filePath)
    },
    metrics: {
      totalLines: lines.length,
      nonEmptyLines,
      nonEmptyRuntimeLines
    },
    imports: {
      total: reactImports.length + externalImports.length + internalImports.length,
      react: reactImports,
      external: externalImports,
      internal: internalImports
    },
    hooks: {
      count: hookNames.size,
      names: Array.from(hookNames)
    },
    patternCounts
  };
}
function synthesizeHighLevelFeatures(low) {
  let { fileName, directory } = low.meta, fileNameLower = fileName.toLowerCase(), pathLower = (directory + "/" + fileName).toLowerCase(), isDesignSystemCandidate = DESIGN_SYSTEM_NAMES.some((name) => fileNameLower.includes(name) && !fileNameLower.includes("page") && !fileNameLower.includes("screen")), isPageCandidate = PAGE_NAMES.some((name) => fileNameLower.includes(name)), isFeatureCandidate = FEATURE_INDICATORS.some((indicator) => pathLower.includes(indicator)), hasCustomHooks = low.hooks.names.some((name) => !name.startsWith("useState") && !name.startsWith("useEffect") && !name.startsWith("useCallback") && !name.startsWith("useMemo") && !name.startsWith("useRef")), size = Math.round(Math.min(10, low.metrics.nonEmptyRuntimeLines / 20)), coupling = Math.round(Math.min(10, low.imports.total / 1)), logic = Math.round(Math.min(10, low.hooks.count * 1.5 + (hasCustomHooks ? 1 : 0))), integrationRaw = 0;
  low.patternCounts.AUTH > 0 && (integrationRaw += 3), low.patternCounts.DATA_FETCHING > 0 && (integrationRaw += 3), low.patternCounts.ROUTER > 0 && (integrationRaw += 2), low.patternCounts.STATE_MANAGEMENT > 0 && (integrationRaw += 2);
  let integration = Math.min(10, integrationRaw);
  return {
    isDesignSystemCandidate,
    isPageCandidate,
    isFeatureCandidate,
    hasCustomHooks,
    hasComplexState: low.patternCounts.STATE_MANAGEMENT > 0,
    hasAuthIntegration: low.patternCounts.AUTH > 0,
    hasDataFetching: low.patternCounts.DATA_FETCHING > 0,
    hasRouting: low.patternCounts.ROUTER > 0,
    dimensions: {
      size,
      coupling,
      logic,
      integration
    }
  };
}
function classifyComplexityWithConfig(low, high, config) {
  let type = "unknown";
  high.isDesignSystemCandidate ? type = "design-system" : high.isPageCandidate ? type = "page" : high.isFeatureCandidate ? type = "feature" : low.meta.fileName.toLowerCase().includes("util") && (type = "utility");
  let locRaw = Math.floor(low.metrics.nonEmptyRuntimeLines / config.LOC_DIVISOR), locScore = Math.min(config.LOC_MAX_SCORE, locRaw), hooksScore = Math.min(config.HOOKS_MAX_SCORE, Math.floor(high.dimensions.logic * config.HOOK_MULTIPLIER) + (high.hasCustomHooks ? config.CUSTOM_HOOK_BONUS : 0)), dependenciesScore = Math.min(config.IMPORT_MAX_SCORE, Math.floor(low.imports.total / config.IMPORT_DIVISOR)), patternsScore = 0;
  for (let [patternKey, weight] of Object.entries(config.PATTERN_WEIGHTS)) {
    let upperKey = patternKey.toUpperCase();
    PATTERNS[upperKey], (low.patternCounts[upperKey] ?? 0) > 0 && (patternsScore += weight);
  }
  patternsScore = Math.min(config.PATTERN_MAX_SCORE, patternsScore);
  let classificationScore = config.CLASSIFICATION_BASE.DEFAULT;
  type === "design-system" ? classificationScore = config.CLASSIFICATION_BASE.DESIGN_SYSTEM : type === "page" ? classificationScore = config.CLASSIFICATION_BASE.PAGE : type === "feature" ? classificationScore = config.CLASSIFICATION_BASE.FEATURE : type === "utility" && (classificationScore = config.CLASSIFICATION_BASE.UTILITY);
  let rawScore = locScore + hooksScore + dependenciesScore + patternsScore + classificationScore, complexityScore = Math.min(100, Math.round(rawScore)), level;
  complexityScore <= config.LEVELS.SIMPLE ? level = "simple" : complexityScore <= config.LEVELS.MEDIUM ? level = "medium" : complexityScore <= config.LEVELS.HIGH ? level = "high" : level = "very-high";
  let factors = [];
  return low.metrics.nonEmptyRuntimeLines > config.LOC_DIVISOR * 5 && factors.push("Large component size"), low.hooks.count > 3 && factors.push("Multiple hooks usage"), low.imports.total > 5 && factors.push("High dependency count"), high.hasAuthIntegration && factors.push("Authentication logic"), high.hasDataFetching && factors.push("Data fetching operations"), low.patternCounts.STATE_MANAGEMENT > 0 && factors.push("State management"), low.patternCounts.CSS_IN_JS > 0 && factors.push("CSS-in-JS styling"), high.hasRouting && factors.push("Router integration"), low.patternCounts.ASYNC > 0 && factors.push("Async operations"), low.patternCounts.CONTEXT > 0 && factors.push("Context usage"), low.patternCounts.ERROR_HANDLING > 0 && factors.push("Error handling"), type === "page" ? factors.push("Page component") : type === "feature" ? factors.push("Feature component") : type === "design-system" && factors.push("Design system component"), {
    modelVersion: MODEL_VERSION,
    modelConfig: config,
    score: complexityScore,
    level,
    type,
    factors: factors.slice(0, 6),
    breakdown: {
      locScore,
      hooksScore,
      dependenciesScore,
      patternsScore,
      classificationScore
    }
  };
}
async function getComponentComplexity(filePath, config = DEFAULT_MODEL_CONFIG) {
  try {
    let lowLevel = await extractLowLevelFeatures(filePath), highLevel = synthesizeHighLevelFeatures(lowLevel);
    return {
      ...classifyComplexityWithConfig(lowLevel, highLevel, config),
      features: {
        low: lowLevel,
        high: highLevel
      }
    };
  } catch (error) {
    throw new Error(`Failed to analyze component complexity for ${filePath}: ${String(error)}`);
  }
}
async function runAnalyzeComponent({ filePath }) {
  try {
    console.log(`Analyzing component complexity for: ${filePath}
`);
    let complexity = await getComponentComplexity(filePath), { low, high } = complexity.features;
    console.log("\u{1F4CA} Component Complexity Analysis"), console.log("=".repeat(50)), console.log(`File: ${low.meta.fileName}`), console.log(`Directory: ${low.meta.directory}`), console.log(`
\u{1F3F7}\uFE0F  Classification: ${complexity.type}`), console.log(`
\u{1F4CF} Basic Metrics:`), console.log(`  Lines of code: ${low.metrics.totalLines}`), console.log(`  Non-empty lines: ${low.metrics.nonEmptyLines}`), console.log(`  Non-empty runtime lines (excluding types): ${low.metrics.nonEmptyRuntimeLines}`), low.hooks.count > 0 && (console.log(`
\u{1FA9D} Hooks:`), console.log(`  Count: ${low.hooks.count}`), console.log(`  Custom hooks: ${high.hasCustomHooks ? "Yes" : "No"}`), low.hooks.names.length > 0 && console.log(`  Hook names: ${low.hooks.names.join(", ")}`)), low.imports.total > 0 && (console.log(`
\u{1F4E6} Dependencies:`), console.log(`  Total: ${low.imports.total}`), console.log(`  External: ${low.imports.external.length}`), console.log(`  Internal: ${low.imports.internal.length}`), low.imports.react.length > 0 && console.log(`  React imports: ${low.imports.react.join(", ")}`), low.imports.external.length > 0 && console.log(`  Third-party: ${low.imports.external.join(", ")}`));
    let activePatterns = [];
    high.hasAuthIntegration && activePatterns.push("auth"), high.hasDataFetching && activePatterns.push("data-fetching"), high.hasRouting && activePatterns.push("router"), low.patternCounts.ASYNC > 0 && activePatterns.push("async-operations"), low.patternCounts.ERROR_HANDLING > 0 && activePatterns.push("error-handling"), low.patternCounts.STATE_MANAGEMENT > 0 && activePatterns.push("state-management"), low.patternCounts.CONTEXT > 0 && activePatterns.push("context-usage"), low.patternCounts.CSS_IN_JS > 0 && activePatterns.push("css-in-js"), activePatterns.length > 0 && (console.log(`
\u{1F50D} Patterns Detected:`), console.log(`  ${activePatterns.join(", ")}`)), console.log(`
\u{1F4C8} Complexity Score:`), console.log(`  Overall score: ${complexity.score}/100`), console.log(`
Score Breakdown:`), console.log(`  LOC score: ${complexity.breakdown.locScore}/20`), console.log(`  Hooks score: ${complexity.breakdown.hooksScore}/15`), console.log(`  Dependencies score: ${complexity.breakdown.dependenciesScore}/15`), console.log(`  Patterns score: ${complexity.breakdown.patternsScore}/30`), console.log(`  Classification score: ${complexity.breakdown.classificationScore}/20`);
    let complexityLevel = "Low";
    complexity.score >= 70 ? complexityLevel = "High" : complexity.score >= 40 && (complexityLevel = "Medium"), console.log(`
Complexity Level: ${complexityLevel} (${complexity.level})`), complexity.factors.length > 0 && console.log(`
Complexity Factors: ${complexity.factors.join(", ")}`);
  } catch (error) {
    console.error("Error analyzing component:", error), process.exit(1);
  }
}
import.meta.url === `file://${process.argv[1]}` && runAnalyzeComponent({ filePath: process.argv[2] });

// src/core-server/utils/story-generation.ts
async function findEasyToStorybookComponents(files, sampleComponents) {
  let candidates = [];
  for (let file of files)
    try {
      logger.debug(`Analyzing component complexity: ${file}`);
      let analysis = await getComponentComplexity(file), { low, high } = analysis.features;
      if (logger.debug(`Level: ${analysis.level}, Factors: ${analysis.factors}`), analysis.level === "simple") {
        candidates.push({
          file,
          score: analysis.score
        });
        continue;
      }
      if (high.hasAuthIntegration || high.hasDataFetching || high.hasRouting || high.hasComplexState || analysis.type === "page" || low.imports.internal.length > 10 || analysis.level === "very-high")
        continue;
      logger.debug(`Found easy to Storybook component: ${file}`), logger.debug(`Factors: ${analysis.factors.join(", ")}`), candidates.push({
        file,
        score: analysis.score
      });
    } catch (e) {
      logger.error(`Failed to analyze ${file}: ${e}`);
    }
  return candidates.sort((a, b) => a.score - b.score).slice(0, sampleComponents);
}
async function filterOutBarrelFiles(files) {
  let filteredFiles = [];
  for (let file of files)
    file.includes("index") && (await readFile3(file, "utf-8")).includes("export * from") || filteredFiles.push(file);
  return filteredFiles;
}
async function extractComponentsFromFiles(files) {
  let components = [];
  for (let file of files) {
    logger.debug(`Analyzing file: ${file}`);
    try {
      let componentInfo = await analyzeComponentFile(file);
      componentInfo && components.push(...componentInfo);
    } catch (error) {
      let errorMessage = error instanceof Error ? error.message : String(error);
      logger.debug(`Failed to analyze ${file}: ${errorMessage}`);
    }
  }
  return components;
}
async function analyzeComponentFile(filePath) {
  let { readFile: readFile6 } = await import("node:fs/promises"), { basename: basename3, extname } = await import("node:path");
  try {
    let content = await readFile6(filePath, "utf-8"), components = [], defaultExportMatch = content.match(
      /export\s+default\s+(?:function\s+|const\s+|class\s+)?([A-Z][a-zA-Z0-9]*)/
    );
    defaultExportMatch && components.push({
      filePath,
      exportName: defaultExportMatch[1],
      isDefaultExport: !0,
      exportCount: 1
      // We'll update this below
    });
    let namedExportMatches = content.matchAll(
      /export\s+(?:const|function|class)\s+([A-Z][a-zA-Z0-9]*)/g
    );
    for (let match of namedExportMatches) {
      let exportName = match[1];
      components.some((c) => c.exportName === exportName && c.isDefaultExport) || components.push({
        filePath,
        exportName,
        isDefaultExport: !1,
        exportCount: 1
        // We'll update this below
      });
    }
    let exportCount = (content.match(/export\s+(?:default|const|function|class|{|interface|type)/g) || []).length;
    if (components.forEach((comp) => {
      comp.exportCount = exportCount;
    }), components.length === 0) {
      let fileName = basename3(filePath, extname(filePath));
      /^[A-Z]/.test(fileName) && components.push({
        filePath,
        exportName: fileName,
        isDefaultExport: !0,
        exportCount: 1
      });
    }
    return components.length > 0 ? components : null;
  } catch (error) {
    return logger.debug(`Error reading file ${filePath}: ${error}`), null;
  }
}
function doesAnyStoryFileExist(componentDir, componentName) {
  let extensions = ["ts", "tsx", "js", "jsx"];
  for (let ext of extensions)
    if (existsSync(join3(componentDir, `${componentName}.stories.${ext}`)) || existsSync(join3(componentDir, `${componentName}.story.${ext}`)))
      return !0;
  return !1;
}
async function generateStoriesForComponents(components, options, force) {
  let generated = 0, skipped = 0, failed = 0, stories = [];
  for (let component of components) {
    logger.debug(`Generating story for ${component.filePath} - ${component.exportName}`);
    let componentDir = dirname3(component.filePath);
    if (doesAnyStoryFileExist(componentDir, component.exportName) && !force) {
      skipped++, logger.info(
        `\u23ED\uFE0F  Skipped (story already exists for ${component.exportName}): ${component.filePath}`
      );
      continue;
    }
    try {
      let result = await generateStoryFile2(
        {
          componentFilePath: component.filePath,
          componentExportName: component.exportName,
          componentIsDefaultExport: component.isDefaultExport,
          componentExportCount: component.exportCount,
          tags: ["auto-generated", "!dev"]
        },
        options,
        { checkFileExists: !0 }
      );
      result.success ? (generated++, logger.info(`\u2705 Generated story: ${result.storyFilePath}`), stories.push({
        storyId: result.storyId,
        storyFilePath: result.storyFilePath,
        componentName: component.exportName,
        componentFilePath: component.filePath
      })) : result.errorType === "STORY_FILE_EXISTS" ? (skipped++, logger.info(`\u23ED\uFE0F  Skipped (already exists): ${result.storyFilePath}`)) : (failed++, logger.error(`\u274C Failed to generate story for ${component.filePath}: ${result.error}`));
    } catch (error) {
      failed++;
      let errorMessage = error instanceof Error ? error.message : String(error);
      logger.error(`\u274C Failed to generate story for ${component.filePath}: ${errorMessage}`), logger.debug(`Full error: ${error}`);
    }
  }
  return { generated, skipped, failed, stories };
}
async function generateSampledStories({
  sampleSize = 20,
  globPattern = "**/*.{ts,tsx,js,jsx}",
  options
}) {
  logger.debug(`Starting story generation with glob: ${globPattern}`), logger.debug(`Sample size: ${sampleSize}`);
  let matchCount = 0;
  try {
    logger.debug("Loading Storybook configuration...");
    let storybookOptions = await experimental_loadStorybook({
      configDir: options.configDir,
      packageJson: JsPackageManagerFactory.getPackageManager({
        configDir: options.configDir
      }).primaryPackageJson
    });
    logger.debug("Storybook configuration loaded successfully");
    let files = [];
    if (logger.debug("Finding files matching glob pattern..."), files = await glob(globPattern, {
      cwd: process.cwd(),
      absolute: !0,
      ignore: [
        "**/node_modules/**",
        "**/.git/**",
        "**/dist/**",
        "**/build/**",
        "**/storybook-static/**",
        "**/*.stories.*",
        "**/*.test.*",
        "**/*.d.*",
        "**/*.config.*",
        "**/*.spec.*"
      ]
    }), logger.debug(`Found ${files.length} files matching glob pattern`), logger.debug(files.join(`
`)), matchCount = files.length, files = await filterOutBarrelFiles(files), files.length === 0)
      return logger.warn(`No files found matching glob pattern: ${globPattern}`), {
        success: !0,
        generatedStories: [],
        matchCount
      };
    sampleSize > 0 && (logger.debug("Filtering out easy to Storybook components..."), files = (await findEasyToStorybookComponents(files, sampleSize)).map((c) => c.file), logger.debug(`Found ${files.length} easy to Storybook components`)), logger.debug("Extracting component information from files...");
    let components = await extractComponentsFromFiles(files);
    if (components.length === 0)
      return logger.debug("No components found in the matched files"), {
        success: !0,
        generatedStories: [],
        matchCount
      };
    logger.debug(`Extracted ${components.length} components from files`), logger.debug("Generating stories for selected components...");
    let results = await generateStoriesForComponents(components, storybookOptions, !1), { generated, skipped, failed, stories } = results;
    return logger.info("Story generation completed:"), logger.info(`  \u2705 Generated: ${generated}`), logger.info(`  \u23ED\uFE0F  Skipped: ${skipped}`), logger.info(`  \u274C Failed: ${failed}`), {
      success: failed === 0,
      generatedStories: stories,
      matchCount
    };
  } catch (error) {
    let errorMessage = error instanceof Error ? error.message : String(error);
    return logger.error(`Failed to generate stories: ${errorMessage}`), logger.debug(`Full error: ${error}`), {
      success: !1,
      generatedStories: [],
      error: errorMessage,
      matchCount
    };
  }
}

// src/core-server/server-channel/story-discovery-channel.ts
var STORY_DISCOVERY_CACHE_KEY = "experimental/story-discovery/has-run";
function initStoryDiscoveryChannel(channel, options, coreOptions) {
  if (!coreOptions.disableTelemetry)
    return channel.on(STORY_DISCOVERY_REQUEST, async (data) => {
      let { sampleSize = 20, globPattern } = data.payload, generatedStoryFiles = [], matchCount = 0;
      try {
        if (await cache.get(STORY_DISCOVERY_CACHE_KEY)) {
          console.log("Skipping discovery run - already ran");
          return;
        }
        let metadata = await getStorybookMetadata(options.configDir), isReactStorybook = metadata?.renderer === "@storybook/react", hasVitestAddon = metadata?.addons && "@storybook/addon-vitest" in metadata.addons;
        if (!isReactStorybook || !hasVitestAddon) {
          console.log("Skipping discovery run - not react vitest", { metadata });
          return;
        }
        await cache.set(STORY_DISCOVERY_CACHE_KEY, { timestamp: Date.now() }), console.log("Generating stories...");
        let generationResult = await generateSampledStories({
          sampleSize,
          globPattern,
          options
        });
        if (matchCount = generationResult.matchCount, console.log("Stories generated:", generationResult), generationResult.error) {
          channel.emit(STORY_DISCOVERY_RESPONSE, {
            success: !1,
            id: data.id,
            error: generationResult.error || "Unknown error occurred"
          }), telemetry5("story-discovery", {
            success: !1,
            error: generationResult.error,
            sampleSize,
            matchCount
          });
          return;
        }
        if (generationResult.generatedStories.length === 0) {
          console.log("No stories generated"), telemetry5("story-discovery", {
            success: !1,
            error: "No stories generated",
            sampleSize,
            matchCount
          });
          return;
        }
        generatedStoryFiles = generationResult.generatedStories.map(
          (story) => story.storyFilePath
        ), channel.emit(STORY_DISCOVERY_PROGRESS, {
          phase: "generating",
          progress: {
            generatedCount: generationResult.generatedStories.length
          }
        });
        let storyIds = generationResult.generatedStories.map(
          (story) => story.storyId
        );
        console.log("Running tests on stories:", storyIds);
        let testRunResult = await runStoryTests(generatedStoryFiles), testSummaryWithPending = {
          ...testRunResult.testSummary,
          pending: 0
          // Tests are complete, so no pending tests
        };
        channel.emit(STORY_DISCOVERY_PROGRESS, {
          phase: "testing",
          progress: {
            testSummary: testSummaryWithPending
          }
        });
        let testResults = {
          results: testRunResult.testResults,
          summary: testSummaryWithPending
        }, allGeneratedStories = generationResult.generatedStories;
        console.log("Test results:", testResults), channel.emit(STORY_DISCOVERY_RESPONSE, {
          success: !0,
          id: data.id,
          payload: {
            success: testRunResult.success,
            generatedStories: allGeneratedStories,
            testResults: testResults.results,
            testSummary: testResults.summary
          },
          error: null
        }), telemetry5("story-discovery", {
          success: testRunResult.success,
          generatedCount: allGeneratedStories.length,
          testResults: testResults.summary,
          testSummary: testRunResult.testSummary,
          sampleSize,
          matchCount: generationResult.matchCount
        });
      } catch (error) {
        let errorMessage = error instanceof Error ? error.message : String(error);
        console.error("Error generating stories:", errorMessage), channel.emit(STORY_DISCOVERY_RESPONSE, {
          success: !1,
          id: data.id,
          error: errorMessage
        }), telemetry5("story-discovery", {
          success: !1,
          error: errorMessage,
          sampleSize,
          matchCount
        });
      } finally {
        generatedStoryFiles.length > 0 && (console.log("Cleaning up generated story files..."), await Promise.allSettled(
          generatedStoryFiles.map(async (filePath) => {
            try {
              await rm(filePath, { force: !0 }), console.log(`Deleted story file: ${filePath}`);
            } catch (cleanupError) {
              console.warn(`Failed to delete story file ${filePath}:`, cleanupError);
            }
          })
        ));
      }
    }), channel;
}
function extractUniqueErrors(testResults) {
  let errorSet = /* @__PURE__ */ new Set();
  for (let result of testResults)
    if (result.error) {
      let messageMatch = result.error.match(/Message:\s*([^\n-]+)/);
      if (messageMatch && messageMatch[1]) {
        let coreError = messageMatch[1].trim();
        coreError && errorSet.add(coreError);
      } else {
        let cleanedError = result.error.replace(/\u001b\[[0-9;]*m/g, "").replace(/\n+/g, " ").replace(/\s+/g, " ").trim();
        cleanedError && errorSet.add(cleanedError);
      }
    }
  return Array.from(errorSet);
}
async function runStoryTests(generatedStoryFiles) {
  try {
    let cacheDir = resolvePathInStorybookCache("story-discovery-tests");
    await mkdir(cacheDir, { recursive: !0 });
    let timestamp = Date.now(), outputFile = join4(cacheDir, `test-results-${timestamp}.json`), startTime = Date.now(), testFailureMessage;
    try {
      await executeCommand({
        command: "npx",
        args: [
          "vitest",
          "run",
          "--project=storybook",
          "--reporter=json",
          `--outputFile=${outputFile}`,
          ...generatedStoryFiles
        ],
        stdio: "ignore"
      });
    } catch (error) {
      testFailureMessage = error instanceof Error ? error.message : String(error);
    }
    let duration = Date.now() - startTime;
    if (!existsSync2(outputFile))
      return {
        success: !1,
        testResults: [],
        duration: 0,
        error: testFailureMessage,
        testSummary: {
          total: 0,
          passed: 0,
          failed: 0,
          failureRate: 0,
          successRate: 0,
          successRateWithoutEmptyRender: 0,
          uniqueErrors: [],
          uniqueErrorCount: 0,
          passingCount: 0,
          failingCount: 0,
          passedButEmptyRenderCount: 0,
          passedComponentPaths: []
        }
      };
    let resultsJson = await readFile4(outputFile, "utf8"), testResults = JSON.parse(resultsJson), storyTestResults = [], passingCount = 0, failingCount = 0, passedButEmptyRenderCount = 0, passedComponentPaths = [];
    for (let testSuite of testResults.testResults)
      for (let assertion of testSuite.assertionResults) {
        let storyId = assertion.meta?.storyId || assertion.fullName, status = assertion.status === "passed" ? "PASS" : assertion.status === "failed" ? "FAIL" : "PENDING", hasEmptyRender = assertion.meta?.reports?.some(
          (report) => report.type === "render-analysis" && report.result?.emptyRender === !0
        );
        if (status === "PASS") {
          if (passingCount++, hasEmptyRender && passedButEmptyRenderCount++, assertion.meta?.componentPath) {
            let root = getProjectRoot2(), storiesFilePath = testSuite.name, componentPath = assertion.meta.componentPath.replace("./", ""), fullComponentPath = storiesFilePath.substring(0, storiesFilePath.lastIndexOf("/") + 1) + componentPath, relativeComponentPath = fullComponentPath.startsWith(root) ? fullComponentPath.slice(root.length).replace(/^[\\/]/, "") : fullComponentPath;
            passedComponentPaths.push(relativeComponentPath);
          }
        } else status === "FAIL" && failingCount++;
        let error;
        assertion.failureMessages && assertion.failureMessages.length > 0 && (error = assertion.failureMessages[0].split(`
`)[0]), storyTestResults.push({
          storyId,
          status,
          error
        });
      }
    let total = testResults.numTotalTests, passed = testResults.numPassedTests, failed = testResults.numFailedTests, successRate = total > 0 ? parseFloat((passed / total).toFixed(2)) : 0, failureRate = total > 0 ? parseFloat((failed / total).toFixed(2)) : 0, successRateWithoutEmptyRender = total > 0 ? parseFloat(((passed - passedButEmptyRenderCount) / total).toFixed(2)) : 0, uniqueErrors = extractUniqueErrors(storyTestResults), uniqueErrorCount = uniqueErrors.length, uniquePassedComponentPaths = Array.from(new Set(passedComponentPaths)), testSummary = {
      total,
      passed,
      failed,
      // Additional metrics for Vitest
      failureRate,
      successRate,
      successRateWithoutEmptyRender,
      uniqueErrors,
      uniqueErrorCount,
      passingCount,
      failingCount,
      passedButEmptyRenderCount,
      passedComponentPaths: uniquePassedComponentPaths
    };
    return {
      success: testResults.success,
      testResults: storyTestResults,
      testSummary,
      duration
    };
  } catch (error) {
    return console.error("Error running story tests:", error), {
      success: !1,
      testResults: [],
      testSummary: {
        total: 0,
        passed: 0,
        failed: 0,
        failureRate: 0,
        successRate: 0,
        successRateWithoutEmptyRender: 0,
        uniqueErrors: [],
        uniqueErrorCount: 0,
        passingCount: 0,
        failingCount: 0,
        passedButEmptyRenderCount: 0,
        passedComponentPaths: []
      },
      error: error instanceof Error ? error.message : String(error),
      duration: 0
    };
  }
}

// src/core-server/utils/checklist.ts
import { createFileSystemCache, resolvePathInStorybookCache as resolvePathInStorybookCache2 } from "storybook/internal/common";
import { experimental_UniversalStore } from "storybook/internal/core-server";
import { logger as logger2 } from "storybook/internal/node-logger";
import { telemetry as telemetry6 } from "storybook/internal/telemetry";

// ../../node_modules/es-toolkit/dist/predicate/isPrimitive.mjs
function isPrimitive(value) {
  return value == null || typeof value != "object" && typeof value != "function";
}

// ../../node_modules/es-toolkit/dist/predicate/isTypedArray.mjs
function isTypedArray(x) {
  return ArrayBuffer.isView(x) && !(x instanceof DataView);
}

// ../../node_modules/es-toolkit/dist/object/clone.mjs
function clone(obj) {
  if (isPrimitive(obj))
    return obj;
  if (Array.isArray(obj) || isTypedArray(obj) || obj instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && obj instanceof SharedArrayBuffer)
    return obj.slice(0);
  let prototype = Object.getPrototypeOf(obj), Constructor = prototype.constructor;
  if (obj instanceof Date || obj instanceof Map || obj instanceof Set)
    return new Constructor(obj);
  if (obj instanceof RegExp) {
    let newRegExp = new Constructor(obj);
    return newRegExp.lastIndex = obj.lastIndex, newRegExp;
  }
  if (obj instanceof DataView)
    return new Constructor(obj.buffer.slice(0));
  if (obj instanceof Error) {
    let newError = new Constructor(obj.message);
    return newError.stack = obj.stack, newError.name = obj.name, newError.cause = obj.cause, newError;
  }
  if (typeof File < "u" && obj instanceof File)
    return new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified });
  if (typeof obj == "object") {
    let newObject = Object.create(prototype);
    return Object.assign(newObject, obj);
  }
  return obj;
}

// ../../node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
function isPlainObject(value) {
  if (!value || typeof value != "object")
    return !1;
  let proto = Object.getPrototypeOf(value);
  return proto === null || proto === Object.prototype || Object.getPrototypeOf(proto) === null ? Object.prototype.toString.call(value) === "[object Object]" : !1;
}

// ../../node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
function isUnsafeProperty(key) {
  return key === "__proto__";
}

// ../../node_modules/es-toolkit/dist/object/mergeWith.mjs
function mergeWith(target, source, merge2) {
  let sourceKeys = Object.keys(source);
  for (let i = 0; i < sourceKeys.length; i++) {
    let key = sourceKeys[i];
    if (isUnsafeProperty(key))
      continue;
    let sourceValue = source[key], targetValue = target[key], merged = merge2(targetValue, sourceValue, key, target, source);
    merged !== void 0 ? target[key] = merged : Array.isArray(sourceValue) ? Array.isArray(targetValue) ? target[key] = mergeWith(targetValue, sourceValue, merge2) : target[key] = mergeWith([], sourceValue, merge2) : isPlainObject(sourceValue) ? isPlainObject(targetValue) ? target[key] = mergeWith(targetValue, sourceValue, merge2) : target[key] = mergeWith({}, sourceValue, merge2) : (targetValue === void 0 || sourceValue !== void 0) && (target[key] = sourceValue);
  }
  return target;
}

// ../../node_modules/es-toolkit/dist/string/words.mjs
var CASE_SPLIT_PATTERN = new RegExp("\\p{Lu}?\\p{Ll}+|[0-9]+|\\p{Lu}+(?!\\p{Ll})|\\p{Emoji_Presentation}|\\p{Extended_Pictographic}|\\p{L}+", "gu");

// ../../node_modules/es-toolkit/dist/object/toMerged.mjs
function toMerged(target, source) {
  return mergeWith(clone(target), source, function mergeRecursively(targetValue, sourceValue) {
    if (Array.isArray(sourceValue))
      return Array.isArray(targetValue) ? mergeWith(clone(targetValue), sourceValue, mergeRecursively) : mergeWith([], sourceValue, mergeRecursively);
    if (isPlainObject(sourceValue))
      return isPlainObject(targetValue) ? mergeWith(clone(targetValue), sourceValue, mergeRecursively) : mergeWith({}, sourceValue, mergeRecursively);
  });
}

// src/shared/checklist-store/checklistData.state.ts
var initialState = {
  items: {
    accessibilityTests: { status: "open" },
    autodocs: { status: "open" },
    ciTests: { status: "open" },
    controls: { status: "open" },
    coverage: { status: "open" },
    guidedTour: { status: "open" },
    installA11y: { status: "open" },
    installChromatic: { status: "open" },
    installDocs: { status: "open" },
    installVitest: { status: "open" },
    mdxDocs: { status: "open" },
    moreComponents: { status: "open" },
    moreStories: { status: "open" },
    onboardingSurvey: { status: "open" },
    organizeStories: { status: "open" },
    publishStorybook: { status: "open" },
    renderComponent: { status: "open" },
    runTests: { status: "open" },
    viewports: { status: "open" },
    visualTests: { status: "open" },
    whatsNewStorybook10: { status: "open" },
    writeInteractions: { status: "open" }
  },
  widget: {}
};

// src/shared/checklist-store/index.ts
var UNIVERSAL_CHECKLIST_STORE_OPTIONS = {
  id: "storybook/checklist",
  initialState
};

// src/core-server/utils/checklist.ts
async function initializeChecklist() {
  try {
    let store = experimental_UniversalStore.create({
      ...UNIVERSAL_CHECKLIST_STORE_OPTIONS,
      leader: !0
    }), cache2 = createFileSystemCache({
      basePath: resolvePathInStorybookCache2("checklist"),
      ns: "storybook"
    }), [[userState, saveUserState], [projectState, saveProjectState]] = await Promise.all([
      globalSettings().then((settings) => {
        let save = throttle(() => settings.save(), 1e3), state = {
          items: settings.value.checklist?.items ?? {},
          widget: settings.value.checklist?.widget ?? {}
        };
        return [state, ({
          items = state.items,
          widget = state.widget
        }) => {
          settings.value.checklist = { items, widget }, save();
        }];
      }),
      cache2.get("state").then((cachedState) => [{ items: cachedState?.items ?? {} }, ({ items }) => cache2.set("state", { items })])
    ]);
    store.setState(
      (value) => ({
        ...toMerged(value, toMerged(userState, projectState)),
        loaded: !0
      })
    ), store.onStateChange((state, previousState) => {
      let entries = Object.entries(state.items), projectValues = {}, userValues = {};
      entries.forEach(([id, { status, mutedAt }]) => {
        status === "done" ? projectValues[id] = { status } : (status === "accepted" || status === "skipped") && (userValues[id] = { status }), mutedAt && (userValues[id] = {
          ...userValues[id],
          mutedAt
        });
      }), saveProjectState({ items: projectValues }), saveUserState({ items: userValues, widget: state.widget });
      let { mutedItems, statusItems } = entries.reduce(
        (acc, [item, { mutedAt, status }]) => {
          let prev = previousState.items[item];
          return mutedAt !== prev?.mutedAt && acc.mutedItems.push(item), status !== prev?.status && acc.statusItems.push(item), acc;
        },
        { mutedItems: [], statusItems: [] }
      );
      mutedItems.length > 0 && telemetry6("onboarding-checklist-muted", {
        items: mutedItems,
        completedItems: entries.reduce((acc, [id, { status }]) => status === "done" || status === "accepted" ? acc.concat([id]) : acc, []),
        skippedItems: entries.reduce((acc, [id, { status }]) => status === "skipped" ? acc.concat([id]) : acc, [])
      }), statusItems.forEach((item) => {
        let { status } = state.items[item];
        telemetry6("onboarding-checklist-status", { item, status });
      });
    });
  } catch (err) {
    logger2.error("Failed to initialize checklist"), logger2.error(err);
  }
}

// src/core-server/utils/constants.ts
var defaultStaticDirs = [
  {
    from: join(resolvePackageDir("storybook"), "assets/browser"),
    to: "/sb-common-assets"
  }
], defaultFavicon = join(resolvePackageDir("storybook"), "assets/browser/favicon.svg");

// src/core-server/utils/save-story/save-story.ts
import { writeFile } from "node:fs/promises";
import { basename as basename2, join as join5 } from "node:path";
import { formatFileContent } from "storybook/internal/common";
import {
  SAVE_STORY_REQUEST,
  SAVE_STORY_RESPONSE,
  STORY_RENDERED
} from "storybook/internal/core-events";
import { storyNameFromExport, toId } from "storybook/internal/csf";
import { printCsf, readCsf } from "storybook/internal/csf-tools";
import { logger as logger3 } from "storybook/internal/node-logger";
import { isExampleStoryId, telemetry as telemetry7 } from "storybook/internal/telemetry";

// src/core-server/utils/save-story/duplicate-story-with-new-name.ts
import { types as t2, traverse } from "storybook/internal/babel";

// src/core-server/utils/save-story/utils.ts
var SaveStoryError = class extends Error {
};

// src/core-server/utils/save-story/duplicate-story-with-new-name.ts
var duplicateStoryWithNewName = (csfFile, storyName, newStoryName) => {
  let node = csfFile._storyExports[storyName], cloned = t2.cloneNode(node);
  if (!cloned)
    throw new SaveStoryError("cannot clone Node");
  let found = !1;
  if (traverse(cloned, {
    Identifier(path) {
      found || path.node.name === storyName && (found = !0, path.node.name = newStoryName);
    },
    ObjectProperty(path) {
      let key = path.get("key");
      key.isIdentifier() && key.node.name === "args" && path.remove();
    },
    noScope: !0
  }), !(t2.isCallExpression(cloned.init) && t2.isMemberExpression(cloned.init.callee) && t2.isIdentifier(cloned.init.callee.property) && cloned.init.callee.property.name === "story") && (t2.isArrowFunctionExpression(cloned.init) || t2.isCallExpression(cloned.init)))
    throw new SaveStoryError("Creating a new story based on a CSF2 story is not supported");
  return traverse(csfFile._ast, {
    Program(path) {
      path.pushContainer(
        "body",
        t2.exportNamedDeclaration(t2.variableDeclaration("const", [cloned]))
      );
    }
  }), cloned;
};

// src/core-server/utils/save-story/update-args-in-csf-file.ts
import { types as t4, traverse as traverse2 } from "storybook/internal/babel";

// src/core-server/utils/save-story/valueToAST.ts
import { parser as parser2, types as t3 } from "storybook/internal/babel";
function valueToAST(literal) {
  if (literal === null)
    return t3.nullLiteral();
  switch (typeof literal) {
    case "function":
      return parser2.parse(literal.toString(), {
        allowReturnOutsideFunction: !0,
        allowSuperOutsideMethod: !0
      }).program.body[0]?.expression;
    case "number":
      return t3.numericLiteral(literal);
    case "string":
      return t3.stringLiteral(literal);
    case "boolean":
      return t3.booleanLiteral(literal);
    case "undefined":
      return t3.identifier("undefined");
    default:
      return Array.isArray(literal) ? t3.arrayExpression(literal.map(valueToAST)) : t3.objectExpression(
        Object.keys(literal).filter((k) => typeof literal[k] < "u").map((k) => {
          let value = literal[k];
          return t3.objectProperty(t3.stringLiteral(k), valueToAST(value));
        })
      );
  }
}

// src/core-server/utils/save-story/update-args-in-csf-file.ts
var updateArgsInCsfFile = async (node, input) => {
  let found = !1, args = Object.fromEntries(
    Object.entries(input).map(([k, v]) => [k, valueToAST(v)])
  );
  if (!(t4.isCallExpression(node) && t4.isMemberExpression(node.callee) && t4.isIdentifier(node.callee.property) && node.callee.property.name === "story") && (t4.isArrowFunctionExpression(node) || t4.isCallExpression(node)))
    throw new SaveStoryError("Updating a CSF2 story is not supported");
  if (t4.isObjectExpression(node)) {
    let properties = node.properties, argsProperty = properties.find((property) => {
      if (t4.isObjectProperty(property)) {
        let key = property.key;
        return t4.isIdentifier(key) && key.name === "args";
      }
      return !1;
    });
    if (argsProperty) {
      if (t4.isObjectProperty(argsProperty)) {
        let a = argsProperty.value;
        if (t4.isObjectExpression(a)) {
          a.properties.forEach((p) => {
            if (t4.isObjectProperty(p)) {
              let key = p.key;
              t4.isIdentifier(key) && key.name in args && (p.value = args[key.name], delete args[key.name]);
            }
          });
          let remainder = Object.entries(args);
          Object.keys(args).length && remainder.forEach(([key, value]) => {
            a.properties.push(t4.objectProperty(t4.identifier(key), value));
          });
        }
      }
    } else
      properties.unshift(
        t4.objectProperty(
          t4.identifier("args"),
          t4.objectExpression(
            Object.entries(args).map(([key, value]) => t4.objectProperty(t4.identifier(key), value))
          )
        )
      );
    return;
  }
  traverse2(node, {
    ObjectExpression(path) {
      if (found)
        return;
      found = !0;
      let argsProperty = path.get("properties").find((property) => {
        if (property.isObjectProperty()) {
          let key = property.get("key");
          return key.isIdentifier() && key.node.name === "args";
        }
        return !1;
      });
      if (argsProperty) {
        if (argsProperty.isObjectProperty()) {
          let a = argsProperty.get("value");
          if (a.isObjectExpression()) {
            a.traverse({
              ObjectProperty(p) {
                let key = p.get("key");
                key.isIdentifier() && key.node.name in args && (p.get("value").replaceWith(args[key.node.name]), delete args[key.node.name]);
              },
              noScope: !0
            });
            let remainder = Object.entries(args);
            Object.keys(args).length && remainder.forEach(([key, value]) => {
              a.pushContainer("properties", t4.objectProperty(t4.identifier(key), value));
            });
          }
        }
      } else
        path.unshiftContainer(
          "properties",
          t4.objectProperty(
            t4.identifier("args"),
            t4.objectExpression(
              Object.entries(args).map(([key, value]) => t4.objectProperty(t4.identifier(key), value))
            )
          )
        );
    },
    noScope: !0
  });
};

// src/core-server/utils/save-story/save-story.ts
var parseArgs = (args) => JSON.parse(args, (_, value) => value === "__sb_empty_function_arg__" ? () => {
} : value), removeExtraNewlines = (code, name) => {
  let anything = "([\\s\\S])", newline = "(\\r\\n|\\r|\\n)", closing = newline + "};" + newline, regex = new RegExp(
    // Looks for an export by the given name, considers the first closing brace on its own line
    // to be the end of the story definition.
    `^(?<before>${anything}*)(?<story>export const ${name} =${anything}+?${closing})(?<after>${anything}*)$`
  ), { before, story, after } = code.match(regex)?.groups || {};
  return story ? before + story.replaceAll(/(\r\n|\r|\n)(\r\n|\r|\n)([ \t]*[a-z0-9_]+): /gi, "$2$3:") + after : code;
};
function initializeSaveStory(channel, options, coreConfig) {
  channel.on(SAVE_STORY_REQUEST, async ({ id, payload }) => {
    let { csfId, importPath, args, name } = payload, newStoryId, newStoryName, sourceFileName, sourceFilePath, sourceStoryName;
    try {
      sourceFileName = basename2(importPath), sourceFilePath = join5(process.cwd(), importPath);
      let csf = await readCsf(sourceFilePath, {
        makeTitle: (userTitle) => userTitle || "myTitle"
      }), parsed = csf.parse(), stories = Object.entries(parsed._stories), [componentId, storyId] = csfId.split("--");
      newStoryName = name && storyNameFromExport(name), newStoryId = newStoryName && toId(componentId, newStoryName);
      let [storyName] = stories.find(([key, value]) => value.id.endsWith(`--${storyId}`)) || [];
      if (!storyName)
        throw new SaveStoryError("Source story not found.");
      if (name && csf.getStoryExport(name))
        throw new SaveStoryError("Story already exists.");
      sourceStoryName = storyNameFromExport(storyName), await updateArgsInCsfFile(
        name ? duplicateStoryWithNewName(parsed, storyName, name) : csf.getStoryExport(storyName),
        args ? parseArgs(args) : {}
      );
      let code = await formatFileContent(
        sourceFilePath,
        removeExtraNewlines(printCsf(csf).code, name || storyName)
      );
      await Promise.all([
        new Promise((resolve) => {
          channel.on(STORY_RENDERED, resolve), setTimeout(() => resolve(channel.off(STORY_RENDERED, resolve)), 3e3);
        }),
        writeFile(sourceFilePath, code)
      ]), channel.emit(SAVE_STORY_RESPONSE, {
        id,
        success: !0,
        payload: {
          csfId,
          newStoryId,
          newStoryName,
          newStoryExportName: name,
          sourceFileContent: code,
          sourceFileName,
          sourceStoryName,
          sourceStoryExportName: storyName
        },
        error: null
      });
      let isCLIExample = isExampleStoryId(newStoryId ?? csfId);
      !coreConfig.disableTelemetry && !isCLIExample && await telemetry7("save-story", {
        action: name ? "createStory" : "updateStory",
        success: !0
      });
    } catch (error) {
      channel.emit(SAVE_STORY_RESPONSE, {
        id,
        success: !1,
        error: error instanceof SaveStoryError ? error.message : "Unknown error"
      }), logger3.error(
        `Error writing to ${sourceFilePath}:
${error.stack || error.message || error.toString()}`
      ), !coreConfig.disableTelemetry && !(error instanceof SaveStoryError) && await telemetry7("save-story", {
        action: name ? "createStory" : "updateStory",
        success: !1,
        error
      });
    }
  });
}

// src/core-server/utils/whats-new.ts
import { writeFile as writeFile2 } from "node:fs/promises";
import { findConfigFile, loadMainConfig } from "storybook/internal/common";
import {
  REQUEST_WHATS_NEW_DATA,
  RESULT_WHATS_NEW_DATA,
  SET_WHATS_NEW_CACHE,
  TELEMETRY_ERROR,
  TOGGLE_WHATS_NEW_NOTIFICATIONS
} from "storybook/internal/core-events";
import { printConfig, readConfig } from "storybook/internal/csf-tools";
import { logger as logger4 } from "storybook/internal/node-logger";
import { telemetry as telemetry8 } from "storybook/internal/telemetry";
var WHATS_NEW_CACHE = "whats-new-cache", WHATS_NEW_URL = "https://storybook.js.org/whats-new/v1";
function initializeWhatsNew(channel, options, coreOptions) {
  channel.on(SET_WHATS_NEW_CACHE, async (data) => {
    let cache2 = await options.cache.get(WHATS_NEW_CACHE).catch((e) => (logger4.verbose(e), {}));
    await options.cache.set(WHATS_NEW_CACHE, { ...cache2, ...data });
  }), channel.on(REQUEST_WHATS_NEW_DATA, async () => {
    try {
      let post = await fetch(WHATS_NEW_URL).then(async (response) => {
        if (response.ok)
          return response.json();
        throw response;
      }), disableWhatsNewNotifications = (await loadMainConfig({ configDir: options.configDir })).core?.disableWhatsNewNotifications === !0, cache2 = await options.cache.get(WHATS_NEW_CACHE) ?? {}, data = {
        ...post,
        status: "SUCCESS",
        postIsRead: post.url === cache2.lastReadPost,
        showNotification: post.url !== cache2.lastDismissedPost && post.url !== cache2.lastReadPost,
        disableWhatsNewNotifications
      };
      channel.emit(RESULT_WHATS_NEW_DATA, { data });
    } catch (e) {
      logger4.verbose(e instanceof Error ? e.message : String(e)), channel.emit(RESULT_WHATS_NEW_DATA, {
        data: { status: "ERROR" }
      });
    }
  }), channel.on(
    TOGGLE_WHATS_NEW_NOTIFICATIONS,
    async ({ disableWhatsNewNotifications }) => {
      let isTelemetryEnabled = coreOptions.disableTelemetry !== !0;
      try {
        let mainPath = findConfigFile("main", options.configDir);
        invariant(mainPath, `unable to find Storybook main file in ${options.configDir}`);
        let main = await readConfig(mainPath);
        if (!main._exportsObject)
          throw new Error(
            "Unable to parse Storybook main file while trying to read 'core' property"
          );
        main.setFieldValue(["core", "disableWhatsNewNotifications"], disableWhatsNewNotifications), await writeFile2(mainPath, printConfig(main).code), isTelemetryEnabled && await telemetry8("core-config", { disableWhatsNewNotifications });
      } catch (error) {
        invariant(error instanceof Error), isTelemetryEnabled && await sendTelemetryError(error, "core-config", {
          cliOptions: options,
          presetOptions: { ...options, corePresets: [], overridePresets: [] },
          skipPrompt: !0
        });
      }
    }
  ), channel.on(TELEMETRY_ERROR, async (error) => {
    coreOptions.disableTelemetry !== !0 && await sendTelemetryError(error, "browser", {
      cliOptions: options,
      presetOptions: { ...options, corePresets: [], overridePresets: [] },
      skipPrompt: !0
    });
  });
}

// src/core-server/presets/common-preset.ts
var interpolate = (string, data = {}) => Object.entries(data).reduce((acc, [k, v]) => acc.replace(new RegExp(`%${k}%`, "g"), v), string), staticDirs = async (values = []) => [
  ...defaultStaticDirs,
  ...values
], favicon = async (value, options) => {
  if (value)
    return value;
  let staticDirsValue = await options.presets.apply("staticDirs"), faviconPaths = (staticDirsValue ? staticDirsValue.map((dir) => typeof dir == "string" ? dir : `${dir.from}:${dir.to}`) : []).map((dir) => {
    let results = [], normalizedDir = staticDirsValue && !isAbsolute(dir) ? getDirectoryFromWorkingDir({
      configDir: options.configDir,
      workingDir: process.cwd(),
      directory: dir
    }) : dir, { staticPath, targetEndpoint } = parseStaticDir(normalizedDir);
    return ["/favicon.svg", "/favicon.ico"].includes(targetEndpoint) && results.push(staticPath), targetEndpoint === "/" && (results.push(join(staticPath, "favicon.svg")), results.push(join(staticPath, "favicon.ico"))), results.filter((path) => existsSync3(path));
  }).reduce((l1, l2) => l1.concat(l2), []);
  return faviconPaths.length > 1 && logger5.warn(import_ts_dedent.dedent`
      Looks like multiple favicons were detected. Using the first one.

      ${faviconPaths.join(", ")}
    `), faviconPaths[0] || defaultFavicon;
}, babel = async (_, options) => {
  let { presets } = options, babelDefault = await presets.apply("babelDefault", {}, options) ?? {};
  return {
    ...babelDefault,
    // This override makes sure that we will never transpile babel further down then the browsers that storybook supports.
    // This is needed to support the mount property of the context described here:
    // https://storybook.js.org/docs/writing-tests/interaction-testing#run-code-before-each-story-in-a-file
    overrides: [
      ...babelDefault?.overrides ?? [],
      {
        include: /\.(story|stories)\.[cm]?[jt]sx?$/,
        presets: [
          [
            "@babel/preset-env",
            {
              bugfixes: !0,
              targets: {
                // This is the same browser supports that we use to bundle our manager and preview code.
                chrome: 100,
                safari: 15,
                firefox: 91
              }
            }
          ]
        ]
      }
    ]
  };
}, title = (previous, options) => previous || options.packageJson?.name || !1, logLevel = (previous, options) => previous || options.loglevel || "info", previewHead = async (base, { configDir, presets }) => {
  let interpolations = await presets.apply("env");
  return getPreviewHeadTemplate(configDir, interpolations);
}, env = async () => {
  let { raw } = await loadEnvs({ production: !0 });
  return raw;
}, previewBody = async (base, { configDir, presets }) => {
  let interpolations = await presets.apply("env");
  return getPreviewBodyTemplate(configDir, interpolations);
}, typescript = () => ({
  check: !1,
  // 'react-docgen' faster than `react-docgen-typescript` but produces lower quality results
  reactDocgen: "react-docgen",
  reactDocgenTypescriptOptions: {
    shouldExtractLiteralValuesFromEnum: !0,
    shouldRemoveUndefinedFromOptional: !0,
    propFilter: (prop) => prop.parent ? !/node_modules/.test(prop.parent.fileName) : !0,
    // NOTE: this default cannot be changed
    savePropValueAsString: !0
  }
}), experimental_serverAPI = (extension, options) => {
  let removeAddon = removeAddonBase, packageManager = JsPackageManagerFactory2.getPackageManager({
    configDir: options.configDir
  });
  return options.disableTelemetry || (removeAddon = async (id, opts) => (await telemetry9("remove", { addon: id, source: "api" }), removeAddonBase(id, { ...opts, packageManager }))), { ...extension, removeAddon };
}, core = async (existing, options) => ({
  ...existing,
  disableTelemetry: options.disableTelemetry === !0,
  enableCrashReports: options.enableCrashReports || optionalEnvToBoolean(process.env.STORYBOOK_ENABLE_CRASH_REPORTS)
}), features = async (existing) => ({
  ...existing,
  argTypeTargetsV7: !0,
  legacyDecoratorFileOrder: !1,
  disallowImplicitActionsInRenderV8: !0,
  viewport: !0,
  highlight: !0,
  controls: !0,
  interactions: !0,
  actions: !0,
  backgrounds: !0,
  outline: !0,
  measure: !0
}), csfIndexer = {
  test: /(stories|story)\.(m?js|ts)x?$/,
  createIndex: async (fileName, options) => (await readCsf2(fileName, options)).parse().indexInputs
}, experimental_indexers = (existingIndexers) => [csfIndexer].concat(existingIndexers || []), frameworkOptions = async (_, options) => {
  let config = await options.presets.apply("framework");
  return typeof config == "string" ? {} : typeof config > "u" ? null : config.options;
}, managerHead = async (_, options) => {
  let location = join(options.configDir, "manager-head.html");
  if (existsSync3(location)) {
    let contents = readFile5(location, { encoding: "utf8" }), interpolations = options.presets.apply("env");
    return interpolate(await contents, await interpolations);
  }
  return "";
}, experimental_serverChannel = async (channel, options) => {
  let coreOptions = await options.presets.apply("core");
  return initializeChecklist(), initializeWhatsNew(channel, options, coreOptions), initializeSaveStory(channel, options, coreOptions), initFileSearchChannel(channel, options, coreOptions), initCreateNewStoryChannel(channel, options, coreOptions), initStoryDiscoveryChannel(channel, options, coreOptions), initOpenInEditorChannel(channel, options, coreOptions), initPreviewInitializedChannel(channel, options, coreOptions), channel;
}, resolvedReact = async (existing) => {
  try {
    return {
      ...existing,
      react: resolvePackageDir("react"),
      reactDom: resolvePackageDir("react-dom")
    };
  } catch {
    return existing;
  }
}, managerEntries = async (existing) => [
  join(resolvePackageDir("storybook"), "dist/core-server/presets/common-manager.js"),
  ...existing || []
], storyIndexGeneratorPromise, storyIndexGenerator = async (_, options) => storyIndexGeneratorPromise || (storyIndexGeneratorPromise = (async () => {
  let workingDir = process.cwd(), configDir = options.configDir, stories = await options.presets.apply("stories"), normalizedStories = normalizeStories(stories, {
    configDir,
    workingDir
  }), [indexers, docs] = await Promise.all([
    options.presets.apply("experimental_indexers", []),
    options.presets.apply("docs")
  ]), generator = new StoryIndexGenerator(normalizedStories, {
    workingDir,
    configDir,
    indexers,
    docs
  });
  return await generator.initialize(), generator;
})(), storyIndexGeneratorPromise);
export {
  babel,
  core,
  csfIndexer,
  env,
  experimental_indexers,
  experimental_serverAPI,
  experimental_serverChannel,
  favicon,
  features,
  frameworkOptions,
  logLevel,
  managerEntries,
  managerHead,
  previewBody,
  previewHead,
  resolvedReact,
  staticDirs,
  storyIndexGenerator,
  title,
  typescript
};
