import { a as registerMarketingAdapter, n as MarketingProviderError, s as sendCampaignThroughCompliance } from "./src-BekHnJEo.js";
import { createMail } from "@platform-modules/mail";
//#region src/self-managed.ts
function campaignSendKey(campaignId, email) {
	return `marketing:${encodeURIComponent(campaignId)}:${encodeURIComponent(email)}`;
}
function makeSelfManagedAdapter(creds) {
	const { from, mail, store, complianceStore, replyTo } = creds;
	const sender = createMail({ send: (msg) => mail.send(msg) });
	return {
		name: "self-managed",
		supports: {
			segments: true,
			scheduling: true,
			automation: false
		},
		async upsertContact(contact, listId) {
			await store.upsertContact(contact, listId);
		},
		async removeContact(email, listId) {
			await store.removeContact(email, listId);
		},
		async tagContact(email, tag) {
			await store.tagContact(email, tag);
		},
		async createList(name) {
			return store.createList(name);
		},
		async lists() {
			return store.lists();
		},
		async createCampaign(campaign) {
			const created = {
				...campaign,
				id: `camp_${campaign.name.toLowerCase().replace(/\s+/g, "-")}`
			};
			await store.saveCampaign(created);
			return created;
		},
		async sendCampaign(campaignId, opts) {
			const campaign = await store.getCampaign(campaignId);
			if (!campaign) throw new MarketingProviderError(`campaign not found: ${campaignId}`, "self-managed", false);
			const recipients = await store.getListRecipients(campaign.listIds);
			return sendCampaignThroughCompliance(complianceStore, {
				campaignId,
				campaign,
				legal: opts.legal,
				recipients,
				trackingBaseUrl: opts.trackingBaseUrl
			}, async (message) => {
				await sender.send({
					from,
					to: message.email,
					replyTo,
					subject: message.subject,
					html: message.html,
					text: message.text,
					headers: message.headers,
					idempotencyKey: campaignSendKey(campaignId, message.email)
				});
			});
		},
		async campaignStats(campaignId) {
			return store.getStats(campaignId);
		},
		async syncSuppression() {
			return complianceStore.listSuppressed();
		},
		createSegment: store.createSegment.bind(store),
		listSegments: store.listSegments.bind(store),
		scheduleCampaign: store.scheduleCampaign.bind(store),
		recordOpen: store.recordOpen.bind(store),
		recordClick: store.recordClick.bind(store)
	};
}
registerMarketingAdapter("self-managed", (creds) => makeSelfManagedAdapter(creds));
//#endregion
export { makeSelfManagedAdapter };
