var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import logger from "../Utils/Logger";
import DomainCommon from "../../Types/Domain";
import dns from "dns";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import BadDataException from "../../Types/Exception/BadDataException";
export default class Domain extends DomainCommon {
    static getCnameRecords(data) {
        return new Promise((resolve, reject) => {
            dns.resolveCname(data.domain, (err, addresses) => {
                const domainLogAttributes = {
                    domain: data.domain,
                };
                if (err) {
                    logger.debug(`DNS CNAME lookup failed for domain ${data.domain}: ${err.message}`, domainLogAttributes);
                    // Handle specific DNS error types with user-friendly messages
                    if (err.message.includes("ENODATA") ||
                        err.message.includes("queryCname ENODATA")) {
                        reject(new BadDataException(`No CNAME records found for domain "${data.domain}". Please ensure you have added the CNAME record and wait for DNS propagation (up to 72 hours).`));
                        return;
                    }
                    if (err.message.includes("ENOTFOUND") ||
                        err.message.includes("queryCname ENOTFOUND")) {
                        reject(new BadDataException(`Domain "${data.domain}" not found. Please check if the domain is correct and accessible.`));
                        return;
                    }
                    if (err.message.includes("ETIMEDOUT") ||
                        err.message.includes("queryCname ETIMEDOUT")) {
                        reject(new BadDataException(`DNS lookup timeout for domain "${data.domain}". Please try again later.`));
                        return;
                    }
                    // Generic DNS error fallback
                    reject(new BadDataException(`Unable to verify CNAME record for domain "${data.domain}". DNS Error: ${err.message}. Please check your DNS configuration and try again.`));
                }
                else if (addresses.length > 0) {
                    resolve(addresses);
                }
                else {
                    reject(new BadDataException("No CNAME record found for domain: " + data.domain));
                }
            });
        });
    }
    static verifyTxtRecord(domain, verificationText) {
        return new Promise((resolve, reject) => {
            dns.resolveTxt(domain.toString(), (err, data) => {
                const domainLogAttributes = {
                    domain: domain.toString(),
                };
                if (err) {
                    logger.debug(`DNS TXT lookup failed for domain ${domain.toString()}: ${err.message}`, domainLogAttributes);
                    // Handle specific DNS error types with user-friendly messages
                    if (err.message.includes("ENODATA") ||
                        err.message.includes("queryTxt ENODATA")) {
                        return reject(new BadDataException(`No TXT records found for domain "${domain.toString()}". Please ensure you have added the TXT record and wait for DNS propagation (up to 72 hours).`));
                    }
                    if (err.message.includes("ENOTFOUND") ||
                        err.message.includes("queryTxt ENOTFOUND")) {
                        return reject(new BadDataException(`Domain "${domain.toString()}" not found. Please check if the domain is correct and accessible.`));
                    }
                    if (err.message.includes("ETIMEDOUT") ||
                        err.message.includes("queryTxt ETIMEDOUT")) {
                        return reject(new BadDataException(`DNS lookup timeout for domain "${domain.toString()}". Please try again later.`));
                    }
                    // Generic DNS error fallback
                    return reject(new BadDataException(`Unable to verify TXT record for domain "${domain.toString()}". DNS Error: ${err.message}. Please check your DNS configuration and try again.`));
                }
                logger.debug("Verify TXT Record", domainLogAttributes);
                logger.debug("Domain " + domain.toString(), domainLogAttributes);
                logger.debug("Data: ", domainLogAttributes);
                logger.debug(data, domainLogAttributes);
                let isVerified = false;
                for (const item of data) {
                    let txt = item;
                    if (Array.isArray(txt)) {
                        txt = txt.join("");
                    }
                    if (txt === verificationText) {
                        isVerified = true;
                        break;
                    }
                }
                resolve(isVerified);
            });
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Domain, "getCnameRecords", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], Domain, "verifyTxtRecord", null);
//# sourceMappingURL=Domain.js.map