import fs from 'node:fs';
import path from 'node:path';
import { builtinModules } from 'node:module';

// src/features/gen-html/infrastructure/local-server/dependencies/generated-output.ts
var LOCAL_SERVER_SASS_VERSION = "^1.86.3";
var LOCAL_SERVER_REACT_QUERY_VERSION = "^5.81.2";
var LOCAL_SERVER_TYPES_NODE_VERSION = "^25.0.10";
var LOCAL_SERVER_TYPES_REACT_VERSION = "19.1.0";
var LOCAL_SERVER_TYPES_REACT_DOM_VERSION = "19.1.1";
var LOCAL_SERVER_TYPESCRIPT_VERSION = "5.8.2";
var LOCAL_SERVER_VITE_VERSION = "^6.3.1";
var STORYBOOK_VERSION = "8.6.14";
var IMPORT_SPECIFIER_PATTERN = /\b(?:import|export)\s+(?:type\s+)?(?:[^'"`]*?\s+from\s*)?['"]([^'"`]+)['"]|\bimport\s*\(\s*['"]([^'"`]+)['"]\s*\)/g;
var SOURCE_FILE_EXTENSION_PATTERN = /\.[cm]?[jt]sx?$/i;
var NODE_BUILTIN_MODULE_NAMES = new Set(
  builtinModules.flatMap((moduleName) => [moduleName, moduleName.replace(/^node:/, ""), `node:${moduleName.replace(/^node:/, "")}`])
);
var LOCAL_SERVER_DEFAULT_PACKAGE_JSON = {
  dependencies: {
    "@types/node": LOCAL_SERVER_TYPES_NODE_VERSION,
    "@types/react": LOCAL_SERVER_TYPES_REACT_VERSION,
    "@types/react-dom": LOCAL_SERVER_TYPES_REACT_DOM_VERSION,
    "@tanstack/react-query": LOCAL_SERVER_REACT_QUERY_VERSION,
    "@storybook/addon-essentials": STORYBOOK_VERSION,
    "@storybook/react": STORYBOOK_VERSION,
    "@storybook/react-vite": STORYBOOK_VERSION,
    react: "^19.1.0",
    "react-dom": "^19.1.0",
    sass: LOCAL_SERVER_SASS_VERSION,
    storybook: STORYBOOK_VERSION,
    typescript: LOCAL_SERVER_TYPESCRIPT_VERSION,
    vite: LOCAL_SERVER_VITE_VERSION
  }};

// src/features/gen-html/infrastructure/local-server/dependencies/generated-output.ts
function isBareImportSpecifier(specifier) {
  return Boolean(specifier) && !specifier.startsWith(".") && !specifier.startsWith("/") && !specifier.includes("://") && !specifier.startsWith("\0");
}
function getPackageNameFromImportSpecifier(specifier) {
  if (!isBareImportSpecifier(specifier) || NODE_BUILTIN_MODULE_NAMES.has(specifier)) {
    return "";
  }
  if (specifier.startsWith("@")) {
    const [scope, name] = specifier.split("/");
    return scope && name ? `${scope}/${name}` : "";
  }
  return specifier.split("/")[0] || "";
}
function listSourceFiles(directoryPath) {
  if (!fs.existsSync(directoryPath) || !fs.statSync(directoryPath).isDirectory()) {
    return [];
  }
  const collected = [];
  const queue = [directoryPath];
  while (queue.length > 0) {
    const currentDirectoryPath = queue.pop();
    if (!currentDirectoryPath) {
      continue;
    }
    for (const entry of fs.readdirSync(currentDirectoryPath, { withFileTypes: true })) {
      const entryPath = path.join(currentDirectoryPath, entry.name);
      if (entry.isDirectory()) {
        queue.push(entryPath);
        continue;
      }
      if (entry.isFile() && SOURCE_FILE_EXTENSION_PATTERN.test(entry.name)) {
        collected.push(entryPath);
      }
    }
  }
  return collected.sort((left, right) => left.localeCompare(right));
}
function isBundledLocalServerDependency(packageName) {
  return Object.prototype.hasOwnProperty.call(LOCAL_SERVER_DEFAULT_PACKAGE_JSON.dependencies, packageName);
}
function collectOutputDependencyNames(outputDirectory) {
  const packageNames = /* @__PURE__ */ new Set();
  for (const filePath of listSourceFiles(outputDirectory)) {
    const source = fs.readFileSync(filePath, "utf8");
    let match = IMPORT_SPECIFIER_PATTERN.exec(source);
    while (match) {
      const packageName = getPackageNameFromImportSpecifier(match[1] || match[2] || "");
      if (packageName && !isBundledLocalServerDependency(packageName)) {
        packageNames.add(packageName);
      }
      match = IMPORT_SPECIFIER_PATTERN.exec(source);
    }
    IMPORT_SPECIFIER_PATTERN.lastIndex = 0;
  }
  return [...packageNames].sort((left, right) => left.localeCompare(right));
}

export { collectOutputDependencyNames, getPackageNameFromImportSpecifier, isBareImportSpecifier, listSourceFiles };
