import { summarizeError } from '../utils/error-display.js';
import { CloudflareControlPlaneError } from './types.js';
import { clean, collectAsync } from './utils.js';
export async function discoverZones(client, input) {
    if (!client.zones)
        return [];
    return await tryDiscover('zones', input.warnings, async () => {
        const query = {
            ...(input.accountId ? { account: { id: input.accountId } } : {}),
            ...(clean(input.zoneName) ? { name: clean(input.zoneName) } : {}),
        };
        return await collectAsync(client.zones.list(query));
    }) ?? [];
}
export async function selectDiscoveredZone(client, zones, input) {
    const zoneId = clean(input.zoneId) || input.configuredZoneId;
    if (zoneId && client.zones) {
        try {
            return await client.zones.get({ zone_id: zoneId });
        }
        catch (error) {
            input.warnings.push(`Could not load Cloudflare zone ${zoneId}: ${summarizeError(error)}`);
        }
    }
    const zoneName = clean(input.zoneName) || input.configuredZoneName;
    if (zoneName)
        return zones.find((zone) => zone.name === zoneName);
    return zones.length === 1 ? zones[0] : undefined;
}
export async function resolveZone(client, input) {
    if (!client.zones) {
        if (input.required) {
            throw new CloudflareControlPlaneError('The Cloudflare client does not expose Zones APIs required for the requested operation.', 'CLOUDFLARE_ZONES_API_UNAVAILABLE', 500);
        }
        return undefined;
    }
    const zoneId = clean(input.zoneId) || input.configuredZoneId;
    if (zoneId)
        return await client.zones.get({ zone_id: zoneId });
    const zoneName = clean(input.zoneName) || input.configuredZoneName;
    const zones = await collectAsync(client.zones.list({
        ...(input.accountId ? { account: { id: input.accountId } } : {}),
        ...(zoneName ? { name: zoneName } : {}),
    }));
    if (zoneName)
        return zones.find((zone) => zone.name === zoneName);
    return zones.length === 1 ? zones[0] : undefined;
}
export async function tryDiscover(label, warnings, run) {
    try {
        return await run();
    }
    catch (error) {
        warnings.push(`Could not discover Cloudflare ${label}: ${summarizeError(error)}`);
        return undefined;
    }
}
