const require_credentials = require('./credentials-C8DWtnMY.cjs');

//#region src/deploy/dns/hostinger-api.ts
/**
* Hostinger DNS API client
*
* API Documentation: https://developers.hostinger.com/
* Authentication: Bearer token from hpanel.hostinger.com/profile/api
*/
const HOSTINGER_API_BASE = "https://developers.hostinger.com";
/**
* Hostinger API error
*/
var HostingerApiError = class extends Error {
	constructor(message, status, statusText, errors) {
		super(message);
		this.status = status;
		this.statusText = statusText;
		this.errors = errors;
		this.name = "HostingerApiError";
	}
};
/**
* Hostinger DNS API client
*
* @example
* ```ts
* const api = new HostingerApi(token);
*
* // Get all records for a domain
* const records = await api.getRecords('traflabs.io');
*
* // Create/update records
* await api.upsertRecords('traflabs.io', [
*   { name: 'api.joemoer', type: 'A', ttl: 300, records: ['1.2.3.4'] }
* ]);
* ```
*/
var HostingerApi = class {
	token;
	constructor(token) {
		this.token = token;
	}
	/**
	* Make a request to the Hostinger API
	*/
	async request(method, endpoint, body) {
		const url = `${HOSTINGER_API_BASE}${endpoint}`;
		const response = await fetch(url, {
			method,
			headers: {
				"Content-Type": "application/json",
				Authorization: `Bearer ${this.token}`
			},
			body: body ? JSON.stringify(body) : void 0
		});
		if (!response.ok) {
			let errorMessage = `Hostinger API error: ${response.status} ${response.statusText}`;
			let errors;
			try {
				const errorBody = await response.json();
				if (errorBody.message) errorMessage = `Hostinger API error: ${errorBody.message}`;
				errors = errorBody.errors;
			} catch {}
			throw new HostingerApiError(errorMessage, response.status, response.statusText, errors);
		}
		const text = await response.text();
		if (!text || text.trim() === "") return void 0;
		return JSON.parse(text);
	}
	/**
	* Get all DNS records for a domain
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	*/
	async getRecords(domain) {
		const response = await this.request("GET", `/api/dns/v1/zones/${domain}`);
		return response.data || [];
	}
	/**
	* Create or update DNS records
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	* @param records - Records to create/update
	* @param overwrite - If true, replaces all existing records. If false, merges with existing.
	*/
	async upsertRecords(domain, records, overwrite = false) {
		await this.request("PUT", `/api/dns/v1/zones/${domain}`, {
			overwrite,
			zone: records
		});
	}
	/**
	* Validate DNS records before applying
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	* @param records - Records to validate
	* @returns true if valid, throws if invalid
	*/
	async validateRecords(domain, records) {
		await this.request("POST", `/api/dns/v1/zones/${domain}/validate`, {
			overwrite: false,
			zone: records
		});
		return true;
	}
	/**
	* Delete specific DNS records
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	* @param filters - Filters to match records for deletion
	*/
	async deleteRecords(domain, filters) {
		await this.request("DELETE", `/api/dns/v1/zones/${domain}`, { filters });
	}
	/**
	* Check if a specific record exists
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	* @param name - Subdomain name (e.g., 'api.joemoer')
	* @param type - Record type (e.g., 'A')
	*/
	async recordExists(domain, name, type = "A") {
		const records = await this.getRecords(domain);
		return records.some((r) => r.name === name && r.type === type);
	}
	/**
	* Create a single A record if it doesn't exist
	*
	* @param domain - Root domain (e.g., 'traflabs.io')
	* @param subdomain - Subdomain name (e.g., 'api.joemoer')
	* @param ip - IP address to point to
	* @param ttl - TTL in seconds (default: 300)
	* @returns true if created, false if already exists
	*/
	async createARecordIfNotExists(domain, subdomain, ip, ttl = 300) {
		const exists = await this.recordExists(domain, subdomain, "A");
		if (exists) return false;
		await this.upsertRecords(domain, [{
			name: subdomain,
			type: "A",
			ttl,
			records: [{ content: ip }]
		}]);
		return true;
	}
};

//#endregion
//#region src/deploy/dns/HostingerProvider.ts
/**
* Hostinger DNS provider implementation.
*/
var HostingerProvider = class {
	name = "hostinger";
	api = null;
	/**
	* Get or create the Hostinger API client.
	*/
	async getApi() {
		if (this.api) return this.api;
		const token = await require_credentials.getHostingerToken();
		if (!token) throw new Error("Hostinger API token not configured. Run `gkm login --service=hostinger` or get your token from https://hpanel.hostinger.com/profile/api");
		this.api = new HostingerApi(token);
		return this.api;
	}
	async getRecords(domain) {
		const api = await this.getApi();
		const records = await api.getRecords(domain);
		return records.map((r) => ({
			name: r.name,
			type: r.type,
			ttl: r.ttl,
			values: r.records.map((rec) => rec.content)
		}));
	}
	async upsertRecords(domain, records) {
		const api = await this.getApi();
		const results = [];
		const existingRecords = await api.getRecords(domain);
		for (const record of records) {
			const existing = existingRecords.find((r) => r.name === record.name && r.type === record.type);
			const existingValue = existing?.records?.[0]?.content;
			if (existing && existingValue === record.value) {
				results.push({
					record,
					created: false,
					unchanged: true
				});
				continue;
			}
			await api.upsertRecords(domain, [{
				name: record.name,
				type: record.type,
				ttl: record.ttl,
				records: [{ content: record.value }]
			}]);
			results.push({
				record,
				created: !existing,
				unchanged: false
			});
		}
		return results;
	}
	async deleteRecords(domain, records) {
		const api = await this.getApi();
		const results = [];
		const existingRecords = await api.getRecords(domain);
		const recordsToDelete = records.filter((record) => existingRecords.some((r) => r.name === record.name && r.type === record.type));
		if (recordsToDelete.length > 0) try {
			await api.deleteRecords(domain, recordsToDelete.map((r) => ({
				name: r.name,
				type: r.type
			})));
			for (const record of recordsToDelete) results.push({
				record,
				deleted: true,
				notFound: false
			});
		} catch (error) {
			for (const record of recordsToDelete) results.push({
				record,
				deleted: false,
				notFound: false,
				error: String(error)
			});
		}
		for (const record of records) {
			const existing = existingRecords.find((r) => r.name === record.name && r.type === record.type);
			if (!existing) results.push({
				record,
				deleted: false,
				notFound: true
			});
		}
		return results;
	}
};

//#endregion
exports.HostingerProvider = HostingerProvider;
//# sourceMappingURL=HostingerProvider-5KYmwoK2.cjs.map