/**
 * parse-dts-exports — pure function that extracts the public surface of a
 * single `.d.ts` file as a list of top-level exported binding names plus
 * any `export * from "./relative"` re-export specifiers.
 *
 * Implementation: delegates to `oxc-parser`'s `staticExports` view, which
 * already decomposes each export statement into entries with `importName` /
 * `exportName` / `moduleRequest` discriminators. We translate that view
 * into the two outputs the W0198 preflight cares about — bare names and
 * wildcard re-export specifiers — and drop default exports per the
 * design's "named-bindings only" rule.
 *
 * Why oxc-parser instead of regex: top-level `.d.ts` syntax has enough TS
 * surface area (declaration merging, conditional `exports` map types,
 * ambient namespace augmentation) that a real AST is cheaper to maintain
 * than a regex with the same coverage. Why oxc-parser instead of
 * `typescript`: typescript isn't in `@sanity/ailf`'s runtime install graph
 * and adding it adds ~50MB; oxc-parser is a few-MB native binary aligned
 * with our existing `oxlint` / `oxfmt` toolchain.
 */
import { parseSync } from "oxc-parser";
export function parseDtsExports(src) {
    // Filename hint drives the parser's grammar — `.d.ts` enables the
    // ambient-only forms we want and disables expression-context grammar
    // we'd otherwise have to ignore.
    const result = parseSync("input.d.ts", src, { lang: "dts" });
    const names = new Set();
    const reExports = [];
    for (const exportStmt of result.module.staticExports) {
        for (const entry of exportStmt.entries) {
            // Wildcard re-export: `export * from "./other"`. The namespace form
            // `export * as ns from "./other"` falls into the named-export branch
            // below because it does expose a binding (`ns`) at the top level.
            if (entry.importName.kind === "AllButDefault" &&
                entry.moduleRequest !== null) {
                reExports.push(entry.moduleRequest.value);
                continue;
            }
            // Anything that produces a stable named binding visible to consumers.
            // Covers own declarations, local re-exports (`export { x as y }`),
            // module re-exports (`export { x } from "./y"`), and namespace
            // re-exports (`export * as ns from "./y"`). `export default ...`
            // lands in `exportName.kind === "Default"` and is intentionally
            // skipped — the W0198 preflight only judges named bindings.
            if (entry.exportName.kind === "Name" && entry.exportName.name) {
                names.add(entry.exportName.name);
            }
        }
    }
    return {
        names: [...names].sort(),
        reExports,
    };
}
