//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
	let target = {};
	for (var name in all) {
		__defProp(target, name, {
			get: all[name],
			enumerable: true
		});
	}
	if (!no_symbols) {
		__defProp(target, Symbol.toStringTag, { value: "Module" });
	}
	return target;
};

//#endregion
//#region extensions/wati/src/targets.ts
/**
* WATI phone number targeting and E.164 normalization.
*/
/**
* Normalize a phone number to E.164 format.
* Strips common prefixes (wati:, whatsapp:, +) and non-digit characters,
* then prepends "+".
*/
function normalizeE164(raw) {
	const digits = raw.trim().replace(/^(wati|whatsapp):/i, "").trim().replace(/[^\d+]/g, "");
	if (digits.startsWith("+")) return `+${digits.slice(1).replace(/\D/g, "")}`;
	if (!digits) return "";
	return `+${digits}`;
}
/**
* Check if a value looks like a WATI phone target (E.164 phone number).
*/
function looksLikeWatiTarget(raw) {
	const normalized = normalizeE164(raw);
	return /^\+\d{7,15}$/.test(normalized);
}
/**
* Normalize a WATI target. Returns null if invalid.
*/
function normalizeWatiTarget(raw) {
	const normalized = normalizeE164(raw);
	if (!normalized || !looksLikeWatiTarget(normalized)) return null;
	return normalized;
}
/**
* Check if a phone number is in the allowlist.
*/
function isPhoneAllowed(phone, allowFrom) {
	if (allowFrom.includes("*")) return true;
	const normalizedPhone = normalizeE164(phone);
	if (!normalizedPhone) return false;
	return allowFrom.some((entry) => {
		const entryStr = String(entry).trim();
		if (entryStr === "*") return true;
		return normalizeE164(entryStr) === normalizedPhone;
	});
}

//#endregion
//#region extensions/wati/src/api.ts
var api_exports = /* @__PURE__ */ __exportAll({
	probeWati: () => probeWati,
	sendWatiMedia: () => sendWatiMedia,
	sendWatiMessage: () => sendWatiMessage
});
async function sendWatiMessage(params) {
	const { account, to, text } = params;
	const apiUrl = account.apiUrl;
	const apiToken = account.apiToken;
	if (!apiUrl || !apiToken) throw new Error("WATI API URL or token not configured");
	const phone = normalizeE164(to).replace(/^\+/, "");
	if (!phone) throw new Error(`Invalid WATI target: ${to}`);
	const url = `${apiUrl.replace(/\/$/, "")}/api/v1/sendSessionMessage/${phone}`;
	const response = await fetch(url, {
		method: "POST",
		headers: {
			Authorization: `Bearer ${apiToken}`,
			"Content-Type": "application/json"
		},
		body: JSON.stringify({ messageText: text })
	});
	if (!response.ok) {
		const body = await response.text().catch(() => "");
		throw new Error(`WATI API error ${response.status}: ${body}`);
	}
	const result = await response.json();
	return {
		ok: result.result !== false,
		info: result.info
	};
}
async function sendWatiMedia(params) {
	const { account, to, mediaUrl, caption, fileName } = params;
	const apiUrl = account.apiUrl;
	const apiToken = account.apiToken;
	if (!apiUrl || !apiToken) throw new Error("WATI API URL or token not configured");
	const phone = normalizeE164(to).replace(/^\+/, "");
	if (!phone) throw new Error(`Invalid WATI target: ${to}`);
	const url = `${apiUrl.replace(/\/$/, "")}/api/v1/sendSessionFile/${phone}`;
	const body = new FormData();
	const mediaResponse = await fetch(mediaUrl);
	if (!mediaResponse.ok) throw new Error(`Failed to fetch media: ${mediaResponse.status}`);
	const blob = await mediaResponse.blob();
	body.append("file", blob, fileName ?? "attachment");
	if (caption) body.append("caption", caption);
	const response = await fetch(url, {
		method: "POST",
		headers: { Authorization: `Bearer ${apiToken}` },
		body
	});
	if (!response.ok) {
		const responseBody = await response.text().catch(() => "");
		throw new Error(`WATI API error ${response.status}: ${responseBody}`);
	}
	const result = await response.json();
	return {
		ok: result.result !== false,
		info: result.info
	};
}
async function probeWati(account) {
	const apiUrl = account.apiUrl;
	const apiToken = account.apiToken;
	if (!apiUrl || !apiToken) return {
		ok: false,
		error: "API URL or token not configured"
	};
	try {
		const url = `${apiUrl.replace(/\/$/, "")}/api/v1/getContacts`;
		const response = await fetch(url, {
			method: "GET",
			headers: { Authorization: `Bearer ${apiToken}` },
			signal: AbortSignal.timeout(1e4)
		});
		if (!response.ok) return {
			ok: false,
			error: `HTTP ${response.status}`
		};
		return { ok: true };
	} catch (err) {
		return {
			ok: false,
			error: String(err)
		};
	}
}

//#endregion
export { looksLikeWatiTarget as a, isPhoneAllowed as i, probeWati as n, normalizeE164 as o, sendWatiMessage as r, normalizeWatiTarget as s, api_exports as t };