// src/actions/deplint/getStorybookConfigImports.ts
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";

// src/actions/deplint/getBasePackageName.ts
function getBasePackageName(importName) {
  const importNameScrubbed = importName.replaceAll('"', "").trim();
  if (importNameScrubbed.startsWith("@")) {
    const parts = importNameScrubbed.split("/");
    return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : importNameScrubbed;
  }
  return importNameScrubbed.split("/", 1)[0];
}

// src/actions/deplint/getStorybookConfigImports.ts
var storybookMainPattern = /(?:^|[\\/])\.storybook[\\/]main\.[cm]?[jt]sx?$/;
var npmPackageNamePattern = /^@?[a-z0-9][\w./-]*$/i;
function isStorybookMainFile(filePath) {
  return storybookMainPattern.test(filePath);
}
function getPropertyName(prop) {
  if (ts.isIdentifier(prop.name)) return prop.name.text;
  if (ts.isStringLiteral(prop.name) || ts.isNoSubstitutionTemplateLiteral(prop.name)) return prop.name.text;
  return void 0;
}
function pushIfPackage(text, collected) {
  const trimmed = text.trim();
  if (trimmed.length === 0) return;
  if (trimmed.startsWith(".") || trimmed.startsWith("/") || trimmed.startsWith("node:")) return;
  if (!npmPackageNamePattern.test(trimmed)) return;
  collected.push(trimmed);
}
function extractFromEntry(node, collected) {
  if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
    pushIfPackage(node.text, collected);
    return;
  }
  if (ts.isObjectLiteralExpression(node)) {
    for (const prop of node.properties) {
      if (ts.isPropertyAssignment(prop) && getPropertyName(prop) === "name") {
        extractFromEntry(prop.initializer, collected);
      }
    }
    return;
  }
  if (ts.isCallExpression(node)) {
    for (const arg of node.arguments) {
      if (ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg)) {
        pushIfPackage(arg.text, collected);
      }
    }
    return;
  }
  if (ts.isArrayLiteralExpression(node)) {
    for (const element of node.elements) extractFromEntry(element, collected);
  }
}
function getStorybookConfigImports(filePath, importPaths) {
  if (!isStorybookMainFile(filePath)) return [];
  let sourceCode;
  try {
    sourceCode = fs.readFileSync(filePath, "utf8");
  } catch {
    return [];
  }
  const sourceFile = ts.createSourceFile(
    path.basename(filePath),
    sourceCode,
    ts.ScriptTarget.Latest,
    true
  );
  const collected = [];
  function visit(node) {
    if (ts.isPropertyAssignment(node)) {
      const name = getPropertyName(node);
      if (name === "addons") {
        if (ts.isArrayLiteralExpression(node.initializer)) {
          for (const element of node.initializer.elements) extractFromEntry(element, collected);
        }
      } else if (name === "framework" || name === "builder") {
        extractFromEntry(node.initializer, collected);
      }
    }
    ts.forEachChild(node, visit);
  }
  visit(sourceFile);
  const cleaned = [...new Set(collected.map(getBasePackageName))];
  for (const imp of cleaned) {
    importPaths[imp] ??= [];
    if (!importPaths[imp].includes(filePath)) importPaths[imp].push(filePath);
  }
  return cleaned;
}
export {
  getStorybookConfigImports,
  isStorybookMainFile
};
//# sourceMappingURL=getStorybookConfigImports.mjs.map
