import { Resource } from "../resource.js";
import { logger } from "../util/logger.js";
import { DEFAULT_RECORD_TYPES } from "./record.js";
/**
 * Map numeric DNS record type to string type
 */
function mapDnsRecordType(type) {
    const typeMap = {
        1: "A",
        2: "NS",
        5: "CNAME",
        6: "SOA",
        15: "MX",
        16: "TXT",
        28: "AAAA",
        33: "SRV",
        12: "PTR",
    };
    return typeMap[type] || "A";
}
/**
 * Import DNS records for a domain using Cloudflare's DNS-over-HTTPS API.
 * This resource allows you to fetch DNS records for a domain and store them
 * in a structured format.
 *
 * @example
 * // Import all default record types
 * const allRecords = await ImportDnsRecords("example.com", {
 *   domain: "example.com"
 * });
 *
 * @example
 * // Import only specific record types
 * const specificRecords = await ImportDnsRecords("example.com", {
 *   domain: "example.com",
 *   recordTypes: ["A", "MX"]
 * });
 *
 * @example
 * // Import DNS records and transfer them to a Cloudflare zone
 * const dnsRecords = await ImportDnsRecords("dns-records", {
 *   domain: "example.com",
 * });
 *
 * const zone = await Zone("example.com", {
 *   name: "example.com",
 *   type: "full",
 * });
 *
 * // Records are directly compatible with DnsRecords function
 * await DnsRecords("transfer-dns-records", {
 *   zoneId: zone.id,
 *   records: dnsRecords.records,
 * });
 */
export const ImportDnsRecords = Resource("dns::ImportDnsRecords", async function (_id, props) {
    // For delete phase, just return destroyed state since this is a read-only resource
    if (this.phase === "delete") {
        return this.destroy();
    }
    const recordTypes = props.recordTypes || DEFAULT_RECORD_TYPES;
    const allRecords = [];
    for (const type of recordTypes) {
        try {
            const res = await fetch(`https://cloudflare-dns.com/dns-query?name=${props.domain}&type=${type}`, {
                headers: {
                    accept: "application/dns-json",
                },
            });
            if (!res.ok) {
                throw new Error(`Failed to fetch ${type} records: ${res.statusText}`);
            }
            const data = (await res.json());
            if (data.Answer) {
                // Transform records to be compatible with DnsRecords function
                const compatRecords = data.Answer.map((record) => {
                    const recordType = mapDnsRecordType(record.type);
                    const result = {
                        name: record.name,
                        type: recordType,
                        TTL: record.TTL,
                        data: record.data,
                        content: record.data, // Default mapping
                        ttl: record.TTL, // Normalized lowercase ttl
                    };
                    // Special handling for MX records to split priority and content
                    if (recordType === "MX") {
                        const parts = record.data.split(" ");
                        if (parts.length >= 2) {
                            const priority = Number.parseInt(parts[0], 10);
                            // Join the rest as hostname in case there are spaces
                            const hostname = parts.slice(1).join(" ");
                            if (!Number.isNaN(priority)) {
                                result.priority = priority;
                                result.content = hostname;
                            }
                        }
                    }
                    return result;
                });
                allRecords.push(...compatRecords);
            }
        }
        catch (error) {
            logger.warn(`Failed to fetch ${type} records for ${props.domain}:`, error);
        }
    }
    // Return the resource with fetched records as a flat array
    return {
        domain: props.domain,
        recordTypes: [...recordTypes],
        records: allRecords,
        importedAt: Date.now(),
    };
});
//# sourceMappingURL=import-dns.js.map