import { parse } from "@babel/parser";
import { isPropertyInfo, splitJSPath } from "@superblocksteam/library-shared";
import { traverse } from "../traverse.js";
export const EXTERNAL_LIBRARY_OPTIONS = [
    {
        library: "sonner",
        imports: new Set(["toast"]),
    },
    {
        library: "@superblocksteam/library",
        imports: new Set([
            "copyToClipboard",
            "navigateTo",
            "setQueryParams",
            "download",
            "logoutIntegrations",
            "Global",
            "Theme",
            "Env",
            "Embed",
        ]),
    },
];
export const REQUIRED_IMPORTS_BY_PROPERTY_TYPE = {
    BINDING: [
        {
            importName: "SB",
            importPath: "@superblocksteam/library",
        },
    ],
    COMPUTED: [
        {
            importName: "computed",
            importPath: "@superblocksteam/library",
        },
    ],
    TEMPLATE: [
        {
            importName: "computed",
            importPath: "@superblocksteam/library",
        },
    ],
    DIMENSION: [
        {
            importName: "Dim",
            importPath: "@superblocksteam/library",
        },
    ],
};
export const EXPRESSION_IMPORTS_BY_IDENTIFIER = {
    Theme: {
        importName: "Theme",
        importPath: "@superblocksteam/library",
    },
    Dim: {
        importName: "Dim",
        importPath: "@superblocksteam/library",
    },
    Global: {
        importName: "Global",
        importPath: "@superblocksteam/library",
    },
};
const EXPRESSION_IDENTIFIERS = new Set(Object.keys(EXPRESSION_IMPORTS_BY_IDENTIFIER));
export function getRequiredImportsForProperty(property) {
    const allImports = new Set();
    function collectImports(prop) {
        if (isPropertyInfo(prop)) {
            if (prop.type === "EXPRESSION" && typeof prop.value === "string") {
                // this is good enough, we don't need to run a larger babel parse step for our common cases
                const splitPath = splitJSPath(prop.value);
                const key = splitPath?.[0];
                if (key && EXPRESSION_IDENTIFIERS.has(key)) {
                    allImports.add(EXPRESSION_IMPORTS_BY_IDENTIFIER[key]);
                    return;
                }
            }
            else if (prop.type in REQUIRED_IMPORTS_BY_PROPERTY_TYPE) {
                REQUIRED_IMPORTS_BY_PROPERTY_TYPE[prop.type]?.forEach((imp) => allImports.add(imp));
            }
        }
        if (typeof prop === "object" && prop !== null && !Array.isArray(prop)) {
            for (const key in prop) {
                collectImports(prop[key]);
            }
        }
    }
    collectImports(property);
    return Array.from(allImports);
}
export function getImports(code) {
    const importPaths = [];
    try {
        const ast = parse(code, {
            sourceType: "module",
            plugins: ["jsx", "typescript"],
        });
        traverse(ast, {
            ImportDeclaration(path) {
                importPaths.push(path.node.source.value);
            },
        });
    }
    catch {
        return importPaths;
    }
    return importPaths;
}
//# sourceMappingURL=imports.js.map