/**
 * Pure path predicates for identifying Storybook story files from a flat list
 * of repo paths. Used by the Cloud scan, which has a file tree from the GitHub
 * API rather than a globbable local filesystem.
 */

/** Matches `Foo.stories.tsx` / `Foo.story.jsx` / `.mdx`, capturing the basename. */
const STORY_FILE_RE = /(?:^|[/\\])([^/\\]+)\.(?:stories|story)\.(?:tsx?|jsx?|mdx)$/;

/** Story files that are themselves tests — excluded from ingestion. */
const STORY_TEST_RE = /\.(?:test\.(?:stories|story)|(?:stories|story)\.test)\./;

const IGNORED_PATH_SEGMENTS = ["node_modules/", "/dist/", "/build/", "/.storybook/"];

/**
 * True when `path` is a Storybook story file we should parse (and not a test
 * variant living under an ignored directory).
 */
export function isStoryFilePath(path: string): boolean {
  const normalized = path.replace(/\\/g, "/");
  if (STORY_TEST_RE.test(normalized)) return false;
  if (IGNORED_PATH_SEGMENTS.some((seg) => normalized.includes(seg))) return false;
  return STORY_FILE_RE.test(normalized);
}

/** The component basename a story file describes (e.g. `Button.stories.tsx` -> `Button`). */
export function storyFileBasename(path: string): string | null {
  const match = path.replace(/\\/g, "/").match(STORY_FILE_RE);
  return match ? match[1] : null;
}
