"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePrerequisites = validatePrerequisites;
const logger_1 = require("../../utils/logger");
const license_service_1 = __importDefault(require("../license.service"));
const pacakge_json_helper_1 = require("../../utils/pacakge.json.helper");
const ensure_iocd_exists_1 = require("../../utils/ensure.iocd.exists");
const logger = logger_1.Logger.getInstance("installer.prerequisites");
async function validatePrerequisites(packageNames) {
    // Check if the required package is available
    logger.info('Validating prerequisites...');
    for (const packageName of packageNames) {
        logger.debug(`Checking required package: ${packageName}`);
        try {
            await import(packageName);
            logger.debug(`${packageName} found`);
            // Check if it's properly installed as devDependency
            await validatePackageInstallation(packageName);
        }
        catch (error) {
            throw new Error(`${packageName} is required but not found. Please install it as a dev dependency:\n` +
                `npm install --save-dev ${packageName}\n` +
                'or\n' +
                `yarn add --dev ${packageName}\n\n` +
                `Note: ${packageName} should be installed as devDependency to avoid being bundled in the final package.`);
        }
    }
    (0, ensure_iocd_exists_1.ensureIOCDComponentExists)();
    // Validate license
    const licenseInfo = license_service_1.default.validateLicense();
    if (licenseInfo.status !== "Active") {
        throw new Error(`Valid license required for packaging: ${licenseInfo.errorMessage || 'Unknown issue'}`);
    }
    logger.info('Prerequisites validated successfully!');
}
async function validatePackageInstallation(packageName) {
    try {
        // Read the current package.json to check dependency installation
        const packageJson = await pacakge_json_helper_1.PackageJSONHelper.getPackageJson();
        if (!packageJson) {
            logger.debug('No package.json found in the current directory, skipping package installation validation.');
            return;
        }
        // Check if package is in dependencies (bad for build tools)
        if (packageJson.dependencies && packageJson.dependencies[packageName]) {
            logger.warn(`${packageName} is installed as a regular dependency. ` +
                'It should be installed as devDependency to avoid being bundled in the final package.\n' +
                'Please move it to devDependencies:\n' +
                `1. Remove: npm uninstall ${packageName}\n` +
                `2. Reinstall: npm install --save-dev ${packageName}`);
        }
        // Check if package is in devDependencies (good) or peerDependencies (also acceptable)
        const hasInDevDeps = packageJson.devDependencies && packageJson.devDependencies[packageName];
        const hasInPeerDeps = packageJson.peerDependencies && packageJson.peerDependencies[packageName];
        if (hasInDevDeps) {
            logger.debug(`${packageName} correctly installed as devDependency`);
        }
        else if (hasInPeerDeps) {
            logger.debug(`${packageName} specified as peerDependency (user must install separately)`);
        }
        else {
            // Package is available (import worked) but not in package.json
            // This could happen if it's installed globally or in a parent node_modules
            logger.warn(`${packageName} is available but not found in package.json dependencies. ` +
                'For best practices, install it as devDependency:\n' +
                `npm install --save-dev ${packageName}`);
        }
    }
    catch (error) {
        // If we can't read package.json, just log a debug message
        logger.debug(`Could not validate ${packageName} installation: ${error instanceof Error ? error.message : String(error)}`);
    }
}
//# sourceMappingURL=prerequisites.js.map