import { a as readMainFacts, o as writeMainFacts } from "./customer-dir-CkMMXhb0.js";
import { t as getSecret } from "./vault-CfwZdNzC.js";
//#region src/core/enrichment.ts
const ENRICHABLE = [
	"domain",
	"email",
	"phone",
	"industry"
];
function isEmpty(v) {
	return v === void 0 || v === null || typeof v === "string" && v.trim() === "";
}
/** Merge additions into base, only filling fields that are currently empty. */
function mergeEnrichment(base, additions) {
	const out = { ...base };
	for (const key of ENRICHABLE) {
		const incoming = additions[key];
		if (isEmpty(out[key]) && incoming !== void 0 && incoming.trim() !== "") out[key] = incoming;
	}
	return out;
}
const DEFAULT_PROVIDERS = [{
	name: "domain-from-email",
	enrich(input) {
		const email = input.email ?? "";
		const at = email.indexOf("@");
		if (at > 0 && at < email.length - 1) {
			const domain = email.slice(at + 1).trim().toLowerCase();
			if (domain) return { domain };
		}
		return {};
	}
}];
/** Run providers in order, merging their output into the input (gaps only). */
async function runEnrichment(input, providers, ctx) {
	let data = {
		...input.domain !== void 0 ? { domain: input.domain } : {},
		...input.email !== void 0 ? { email: input.email } : {},
		...input.phone !== void 0 ? { phone: input.phone } : {},
		...input.industry !== void 0 ? { industry: input.industry } : {}
	};
	for (const provider of providers) {
		const additions = await provider.enrich({
			...data,
			name: input.name
		}, ctx);
		data = mergeEnrichment(data, additions);
	}
	return data;
}
function vaultContext(dataDir) {
	const key = process.env["DXCRM_VAULT_KEY"];
	if (!key) return { getSecret: (n) => process.env[n] };
	return { getSecret(name) {
		try {
			return getSecret(dataDir, key, name) ?? process.env[name];
		} catch {
			return process.env[name];
		}
	} };
}
/**
* Enrich one customer: read main_facts, run providers, and (optionally) write
* the newly-filled fields back. Existing human-entered facts are preserved.
*/
async function enrichCustomer(dataDir, slug, opts = {}) {
	const facts = await readMainFacts(dataDir, slug);
	const providers = opts.providers ?? DEFAULT_PROVIDERS;
	const ctx = opts.ctx ?? vaultContext(dataDir);
	const before = {
		...facts.domain !== void 0 ? { domain: facts.domain } : {},
		...facts.email !== void 0 ? { email: facts.email } : {},
		...facts.phone !== void 0 ? { phone: facts.phone } : {},
		...facts.industry !== void 0 ? { industry: facts.industry } : {}
	};
	const merged = await runEnrichment({
		name: facts.name,
		...before
	}, providers, ctx);
	const applied = {};
	for (const key of ENRICHABLE) {
		const value = merged[key];
		if (isEmpty(before[key]) && value !== void 0 && value.trim() !== "") applied[key] = value;
	}
	let written = false;
	if (opts.write && Object.keys(applied).length > 0) {
		await writeMainFacts(dataDir, slug, {
			...facts,
			...applied,
			updated: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)
		});
		written = true;
	}
	return {
		applied,
		merged,
		written
	};
}
//#endregion
export { enrichCustomer };

//# sourceMappingURL=enrichment-CDFdWmvD.js.map