/**
 * Shared babel-types helpers used by both the JSX usage extractor and the
 * Tailwind className walker. Kept tiny on purpose — anything heavier should
 * live closer to its caller.
 */

import * as t from "@babel/types";

import type { FactLocation } from "@fragments-sdk/core";

export function jsxAttributeName(attr: t.JSXAttribute): string | undefined {
  const name = attr.name;
  if (t.isJSXIdentifier(name)) return name.name;
  if (t.isJSXNamespacedName(name)) {
    return `${name.namespace.name}:${name.name.name}`;
  }
  return undefined;
}

export function locFromNode(
  node: t.Node,
  file: string,
): FactLocation | undefined {
  if (!node.loc) return undefined;
  return {
    file,
    line: node.loc.start.line,
    column: node.loc.start.column,
    endLine: node.loc.end.line,
    endColumn: node.loc.end.column,
  };
}
