import { getLogger } from "./logger.js";

//#region src/utils/provider-detect.ts
/**
* Detect drift between declared and actually-patched LLM providers.
*
* If an application declares the providers it uses (e.g. `['openai']`)
* but `patch()` ends up instrumenting a different set, the SDK logs a
* one-time warning so drift between config and runtime is visible.
* Declared set is taken from the `expectedProviders` option on
* {@link patch}.
*
* Warn-only: never throws, never blocks `patch()`, never interferes
* with event emission. The warning fires once per unique
* (expected, patched) combination to avoid log spam.
*/
const CANONICAL_ALIASES = { "azure-openai": "openai" };
const warnedCombinations = /* @__PURE__ */ new Set();
function canonicalize(providers) {
	const out = /* @__PURE__ */ new Set();
	for (const p of providers) {
		if (!p) continue;
		out.add(CANONICAL_ALIASES[p] ?? p);
	}
	return out;
}
function setsEqual(a, b) {
	if (a.size !== b.size) return false;
	for (const v of a) if (!b.has(v)) return false;
	return true;
}
function sortedArray(s) {
	return [...s].sort();
}
function warnIfProviderMismatch(opts) {
	const expectedRaw = opts.expectedProviders;
	if (expectedRaw == null || expectedRaw.length === 0) return;
	const expected = canonicalize(expectedRaw.map(String));
	const patched = canonicalize(opts.patchedProviders.map(String));
	if (expected.size === 0) return;
	if (setsEqual(expected, patched)) return;
	const missing = [...expected].filter((p) => !patched.has(p));
	const unexpected = [...patched].filter((p) => !expected.has(p));
	if (missing.length === 0 && unexpected.length === 0) return;
	const dedupKey = [
		opts.appKey ?? "",
		sortedArray(expected).join(","),
		sortedArray(patched).join(",")
	].join("");
	if (warnedCombinations.has(dedupKey)) return;
	warnedCombinations.add(dedupKey);
	const parts = [];
	if (opts.appKey) parts.push(`application '${opts.appKey}'`);
	parts.push(`declared providers ${JSON.stringify(sortedArray(expected))} do not match providers patched at runtime ${JSON.stringify(sortedArray(patched))}`);
	if (missing.length > 0) parts.push(`missing: ${JSON.stringify(missing.sort())}`);
	if (unexpected.length > 0) parts.push(`unexpected: ${JSON.stringify(unexpected.sort())}`);
	const message = `amplitude-ai: ${parts.join("; ")}. Events will still be emitted; consider aligning your declared providers with what your code actually uses.`;
	getLogger().warn(message);
}
function _resetForTests() {
	warnedCombinations.clear();
}

//#endregion
export { _resetForTests, warnIfProviderMismatch };
//# sourceMappingURL=provider-detect.js.map