//#region src/sync/calendly.ts
async function calendlyRequest(apiKey, path) {
	const { default: https } = await import("https");
	return new Promise((resolve, reject) => {
		const req = https.request(`https://api.calendly.com${path}`, { headers: {
			Authorization: `Bearer ${apiKey}`,
			"Content-Type": "application/json"
		} }, (res) => {
			let data = "";
			res.on("data", (chunk) => {
				data += chunk.toString();
			});
			res.on("end", () => {
				try {
					resolve(JSON.parse(data));
				} catch {
					reject(/* @__PURE__ */ new Error(`Invalid JSON from Calendly API: ${data.slice(0, 200)}`));
				}
			});
		});
		req.on("error", reject);
		req.end();
	});
}
async function getCurrentUserUri(apiKey) {
	return (await calendlyRequest(apiKey, "/users/me")).resource.uri;
}
async function listEventTypes(apiKey) {
	const userUri = await getCurrentUserUri(apiKey);
	return (await calendlyRequest(apiKey, `/event_types?user=${encodeURIComponent(userUri)}&active=true`)).collection.map((et) => ({
		uri: et.uri,
		slug: et.slug,
		name: et.name,
		duration: et.duration,
		schedulingUrl: et.scheduling_url,
		active: et.active
	}));
}
async function getSchedulingLink(apiKey, eventTypeSlug, prefill) {
	const eventType = (await listEventTypes(apiKey)).find((et) => et.slug === eventTypeSlug || et.name.toLowerCase().includes(eventTypeSlug.toLowerCase()));
	if (!eventType) throw new Error(`Event type '${eventTypeSlug}' not found in Calendly`);
	let url = eventType.schedulingUrl;
	const params = new URLSearchParams();
	if (prefill?.name) params.set("name", prefill.name);
	if (prefill?.email) params.set("email", prefill.email);
	if (params.toString()) url += `?${params.toString()}`;
	return url;
}
//#endregion
exports.getSchedulingLink = getSchedulingLink;
exports.listEventTypes = listEventTypes;

//# sourceMappingURL=calendly-D3coO92o.cjs.map