/**
 * Import information extraction
 */
import * as ts from "typescript";
import { resolveImport } from "../../resolver.js";
/**
 * Extract import information from an import declaration
 */
export const extractImport = (node, sourceFile, program) => {
    if (!ts.isStringLiteral(node.moduleSpecifier)) {
        return null;
    }
    const specifier = node.moduleSpecifier.text;
    const result = resolveImport(specifier, sourceFile.fileName, program.options.sourceRoot, {
        clrResolver: program.clrResolver,
        bindings: program.bindings,
        projectRoot: program.options.projectRoot,
        surface: program.options.surface,
        authoritativeTsonicPackageRoots: program.authoritativeTsonicPackageRoots,
        declarationModuleAliases: program.declarationModuleAliases,
    });
    const importedNames = [];
    if (node.importClause) {
        // Default import
        if (node.importClause.name) {
            importedNames.push({
                name: "default",
                alias: node.importClause.name.text,
            });
        }
        // Named imports
        if (node.importClause.namedBindings) {
            if (ts.isNamespaceImport(node.importClause.namedBindings)) {
                // import * as ns from "..."
                importedNames.push({
                    name: "*",
                    alias: node.importClause.namedBindings.name.text,
                });
            }
            else if (ts.isNamedImports(node.importClause.namedBindings)) {
                // import { a, b as c } from "..."
                node.importClause.namedBindings.elements.forEach((spec) => {
                    importedNames.push({
                        name: (spec.propertyName ?? spec.name).text,
                        alias: spec.propertyName ? spec.name.text : undefined,
                    });
                });
            }
        }
    }
    if (result.ok) {
        return {
            kind: result.value.isLocal
                ? result.value.isSourcePackage
                    ? "source_package"
                    : "local"
                : result.value.isClr
                    ? "clr"
                    : "node_module",
            specifier,
            resolvedPath: result.value.resolvedPath || undefined,
            namespace: result.value.resolvedNamespace,
            importedNames,
        };
    }
    return {
        kind: "local",
        specifier,
        importedNames,
    };
};
//# sourceMappingURL=imports.js.map