import { Resource, ResourceKind } from "../resource.js";
import { handleApiError } from "./api-error.js";
import { createCloudflareApi } from "./api.js";
import { resolveEmailZoneId } from "./email-common.js";
export function isEmailRouting(resource) {
    return resource?.[ResourceKind] === "cloudflare::EmailRouting";
}
/**
 * Configures email routing for a Cloudflare zone, allowing emails sent to the zone's domain
 * to be routed according to defined rules.
 *
 * @example
 * ## Enable email routing for a zone
 *
 * Enable email routing for your domain to start receiving and routing emails.
 *
 * ```ts
 * const emailRouting = await EmailRouting("example.com-routing", {
 *   zone: "example.com",
 *   enabled: true
 * });
 * ```
 *
 * @example
 * ## Use with existing Zone resource
 *
 * Configure email routing using an existing Zone resource reference.
 *
 * ```ts
 * const zone = await Zone("my-zone", {
 *   name: "example.com"
 * });
 *
 * const emailRouting = await EmailRouting("example.com-routing", {
 *   zone: zone,
 *   enabled: true,
 *   skipWizard: true
 * });
 * ```
 */
export const EmailRouting = Resource("cloudflare::EmailRouting", async function (_id, props) {
    const api = await createCloudflareApi(props);
    const zoneId = await resolveEmailZoneId(api, props.zone);
    if (this.phase === "delete") {
        // Disable email routing DNS
        const response = await api.delete(`/zones/${zoneId}/email/routing/dns`);
        if (!response.ok && response.status !== 404) {
            await handleApiError(response, "disabling", "email routing DNS");
        }
        return this.destroy();
    }
    if (this.phase === "update" && this.output) {
        // Check if we need to update the enabled state
        const currentEnabled = this.output.enabled;
        const desiredEnabled = props.enabled ?? true;
        if (currentEnabled !== desiredEnabled) {
            if (desiredEnabled) {
                // Enable email routing DNS
                const enablePayload = {
                    enabled: true,
                    ...(props.skipWizard && { skip_wizard: props.skipWizard }),
                };
                const response = await api.post(`/zones/${zoneId}/email/routing/dns`, enablePayload);
                if (!response.ok) {
                    await handleApiError(response, "enabling", "email routing DNS");
                }
            }
            else {
                // Disable email routing DNS
                const response = await api.delete(`/zones/${zoneId}/email/routing/dns`);
                if (!response.ok) {
                    await handleApiError(response, "disabling", "email routing DNS");
                }
            }
        }
        // Get current state
        const getResponse = await api.get(`/zones/${zoneId}/email/routing/dns`);
        if (!getResponse.ok) {
            if (getResponse.status === 404) {
                throw new Error("Email routing DNS is not configured for this zone");
            }
            await handleApiError(getResponse, "getting", "email routing DNS settings");
        }
        const result = (await getResponse.json());
        return {
            zoneId,
            enabled: result.result.enabled,
            name: result.result.name,
            created: result.result.created,
            modified: result.result.modified,
            tag: result.result.tag,
        };
    }
    // Create/Enable email routing
    const enabled = props.enabled ?? true;
    if (enabled) {
        const enablePayload = {
            enabled: true,
            ...(props.skipWizard && { skip_wizard: props.skipWizard }),
        };
        const response = await api.post(`/zones/${zoneId}/email/routing/dns`, enablePayload);
        if (!response.ok) {
            await handleApiError(response, "enabling", "email routing DNS");
        }
    }
    // Get the current state
    const getResponse = await api.get(`/zones/${zoneId}/email/routing/dns`);
    if (!getResponse.ok) {
        if (getResponse.status === 404) {
            throw new Error("Email routing DNS is not configured. Set enabled: true to configure it.");
        }
        await handleApiError(getResponse, "getting", "email routing DNS settings");
    }
    const result = (await getResponse.json());
    return {
        zoneId,
        enabled: result.result.enabled,
        name: result.result.name,
        created: result.result.created,
        modified: result.result.modified,
        tag: result.result.tag,
    };
});
//# sourceMappingURL=email-routing.js.map