"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.packageJsonHasDependencies = packageJsonHasDependencies;
exports.zipEntryPathsIncludeNodeModules = zipEntryPathsIncludeNodeModules;
exports.validateJavaScriptBundleDependencies = validateJavaScriptBundleDependencies;
const validateCustomAIBundle_1 = require("./validateCustomAIBundle");
function packageJsonHasDependencies(packageJsonContent) {
    try {
        const pkg = JSON.parse(packageJsonContent);
        const depCount = Object.keys(pkg.dependencies ?? {}).length;
        const devDepCount = Object.keys(pkg.devDependencies ?? {}).length;
        return depCount + devDepCount > 0;
    }
    catch {
        return false;
    }
}
function zipEntryPathsIncludeNodeModules(entryPaths) {
    return entryPaths.some((entry) => {
        const normalized = (0, validateCustomAIBundle_1.normalizeZipEntryPath)(entry);
        if (normalized === 'node_modules' || normalized.startsWith('node_modules/')) {
            return true;
        }
        const slash = normalized.indexOf('/');
        if (slash > 0) {
            const rest = normalized.slice(slash + 1);
            return rest === 'node_modules' || rest.startsWith('node_modules/');
        }
        return false;
    });
}
function validateJavaScriptBundleDependencies(entryPaths, entries) {
    const packageEntry = entries.find((entry) => {
        const name = (0, validateCustomAIBundle_1.normalizeZipEntryPath)(entry.entryName);
        return name === 'package.json' || name.endsWith('/package.json');
    });
    if (!packageEntry) {
        return null;
    }
    const content = packageEntry.getData().toString('utf8');
    if (!packageJsonHasDependencies(content)) {
        return null;
    }
    if (!zipEntryPathsIncludeNodeModules(entryPaths)) {
        return ('JavaScript bundle has dependencies in package.json but no node_modules/ folder in the zip. ' +
            'Run npm install locally and include node_modules at the zip root, then re-upload.');
    }
    return null;
}
//# sourceMappingURL=validateJsBundleDependencies.js.map