import { a as readMainFacts, i as listCustomerSlugs } from "./customer-dir-CkMMXhb0.js";
//#region src/core/identity.ts
/**
* Identity resolution (CDP v1, N4-3): deterministic deduplication of customers
* by a canonical key (normalized domain, falling back to email domain). Reports
* clusters of likely-duplicate customers so they can be merged.
*/
function normalizeDomain(value) {
	let v = value.trim().toLowerCase();
	if (v.includes("@")) v = v.split("@").pop() ?? v;
	v = v.replace(/^https?:\/\//, "").replace(/^www\./, "");
	v = v.replace(/\/.*$/, "");
	return v;
}
/** Canonical key for a customer: normalized domain, else email domain, else "". */
async function canonicalKey(dataDir, slug) {
	const facts = await readMainFacts(dataDir, slug).catch(() => null);
	if (!facts) return "";
	if (facts.domain) return normalizeDomain(facts.domain);
	if (facts.email) return normalizeDomain(facts.email);
	return "";
}
/** Group customers by canonical key; clusters with ≥2 members are duplicates. */
async function findDuplicateClusters(dataDir) {
	const byKey = /* @__PURE__ */ new Map();
	for (const slug of listCustomerSlugs(dataDir)) {
		const key = await canonicalKey(dataDir, slug);
		if (!key) continue;
		byKey.set(key, [...byKey.get(key) ?? [], slug]);
	}
	const clusters = [];
	for (const [key, slugs] of byKey) if (slugs.length >= 2) clusters.push({
		key,
		slugs
	});
	return clusters;
}
//#endregion
export { findDuplicateClusters as n, normalizeDomain as r, canonicalKey as t };

//# sourceMappingURL=identity-_uZ3Lbr2.js.map