import { parseImportProtectionAst } from "./ast.js";
import * as t from "@babel/types";
import babel from "@babel/core";
//#region src/import-protection-plugin/postCompileUsage.ts
/**
* Given transformed code, returns the first "meaningful" usage position for an
* import from `source` that survives compilation.
*
* "Preferred" positions (call, new, member-access) take priority over bare
* identifier references. The returned column is 0-based (Babel loc semantics).
*/
function findPostCompileUsagePos(code, source) {
	return findPostCompileUsagePosFromAst(parseImportProtectionAst(code), source);
}
function findPostCompileUsagePosFromAst(ast, source) {
	const imported = /* @__PURE__ */ new Set();
	for (const node of ast.program.body) if (t.isImportDeclaration(node) && node.source.value === source) {
		if (node.importKind === "type") continue;
		for (const s of node.specifiers) {
			if (t.isImportSpecifier(s) && s.importKind === "type") continue;
			imported.add(s.local.name);
		}
	}
	if (imported.size === 0) return void 0;
	let preferred;
	let anyUsage;
	try {
		babel.traverse(ast, {
			ImportDeclaration(path) {
				path.skip();
			},
			Identifier(path) {
				if (preferred && anyUsage) {
					path.stop();
					return;
				}
				const { node, parent, scope } = path;
				if (!imported.has(node.name)) return;
				if (path.isBindingIdentifier()) return;
				if (t.isObjectProperty(parent) && parent.key === node && !parent.computed && !parent.shorthand) return;
				if (t.isObjectMethod(parent) && parent.key === node && !parent.computed) return;
				if (t.isExportSpecifier(parent) && parent.exported === node) return;
				const binding = scope.getBinding(node.name);
				if (binding && binding.kind !== "module") return;
				const loc = node.loc?.start;
				if (!loc) return;
				const pos = {
					line: loc.line,
					column0: loc.column
				};
				if (t.isCallExpression(parent) && parent.callee === node || t.isNewExpression(parent) && parent.callee === node || t.isMemberExpression(parent) && parent.object === node) preferred ||= pos;
				else anyUsage ||= pos;
			}
		});
	} catch {
		return;
	}
	return preferred ?? anyUsage;
}
//#endregion
export { findPostCompileUsagePos };

//# sourceMappingURL=postCompileUsage.js.map