"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateExternalAssetPackTasks = validateExternalAssetPackTasks;
const dnsPromises = __importStar(require("node:dns/promises"));
const node_net_1 = require("node:net");
function isExternalAssetPackTask(task) {
    return task.kind === 'asset-pack' && task.hostingMode === 'EXTERNAL';
}
function isPrivateNetworkIpAddress(address) {
    const normalized = address.trim().toLowerCase();
    if (!normalized) {
        return true;
    }
    if (normalized === '::1') {
        return true;
    }
    if (normalized.startsWith('fe80:') || normalized.startsWith('fc') || normalized.startsWith('fd')) {
        return true;
    }
    if (normalized.startsWith('::ffff:')) {
        return isPrivateNetworkIpAddress(normalized.slice('::ffff:'.length));
    }
    if ((0, node_net_1.isIP)(normalized) !== 4) {
        return false;
    }
    const octets = normalized.split('.').map((part) => Number.parseInt(part, 10));
    if (octets.length !== 4 || octets.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
        return true;
    }
    const [first, second] = octets;
    if (first === 10 || first === 127) {
        return true;
    }
    if (first === 169 && second === 254) {
        return true;
    }
    if (first === 172 && second >= 16 && second <= 31) {
        return true;
    }
    if (first === 192 && second === 168) {
        return true;
    }
    return false;
}
async function validateExternalAssetPackTasks(tasks) {
    const errors = [];
    for (const task of tasks) {
        if (!isExternalAssetPackTask(task)) {
            continue;
        }
        const downloadUrl = typeof task.downloadUrl === 'string' ? task.downloadUrl.trim() : '';
        if (!downloadUrl) {
            continue;
        }
        let hostname = '';
        try {
            hostname = new URL(downloadUrl).hostname;
        }
        catch {
            continue;
        }
        let resolvedAddresses;
        try {
            resolvedAddresses = await dnsPromises.lookup(hostname, { all: true, verbatim: true });
        }
        catch {
            errors.push(`[${task.cataloguePath}] Asset pack "${task.name}" downloadUrl hostname could not be resolved.`);
            continue;
        }
        if (!Array.isArray(resolvedAddresses) || resolvedAddresses.length === 0) {
            errors.push(`[${task.cataloguePath}] Asset pack "${task.name}" downloadUrl hostname did not resolve to a public address.`);
            continue;
        }
        if (resolvedAddresses.some((entry) => {
            const address = typeof entry?.address === 'string' ? entry.address.trim() : '';
            return !address || isPrivateNetworkIpAddress(address);
        })) {
            errors.push(`[${task.cataloguePath}] Asset pack "${task.name}" downloadUrl must resolve only to public IP addresses.`);
        }
    }
    return errors;
}
