"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicenseFileService = void 0;
const logger_1 = require("../utils/logger");
const path_1 = require("../utils/path");
const fs_1 = require("fs");
const path_2 = require("path");
const license_service_1 = require("./license.service");
const config_service_1 = require("./config/config.service");
const logger = logger_1.Logger.getInstance("license-file.service");
/**
 * LicenseFileService handles copying the license key file to the application binary directory.
 *
 * This service:
 * - Checks if license copying is enabled in config (default: true)
 * - Gets the license key from LicenseService (either from environment variable or config/iocd.license.key)
 * - Writes it to the io.Connect Desktop directory as "license.key"
 *
 * The license file needs to be present in the iocd directory for the application to run properly.
 */
class LicenseFileService {
    /**
     * Copy the license key to the iocd directory if it exists and copying is enabled
     */
    static async copyLicenseFile() {
        // Check if license copying is enabled (default: true)
        const copyEnabled = config_service_1.ConfigManager.config.license?.copyLicenseToOutput ?? true;
        if (!copyEnabled) {
            logger.debug("License copying is disabled in config - skipping license file copy");
            return;
        }
        const licenseService = new license_service_1.LicenseService();
        const licenseKey = licenseService.getLicenseKey();
        if (!licenseKey) {
            logger.debug("No license key found - skipping license file copy");
            return;
        }
        logger.debug("Found license key, will write to iocd directory");
        try {
            // Get the iocd directory
            const iocdDir = path_1.PathUtils.getIOCDResourcesDir();
            const targetLicensePath = (0, path_2.join)(iocdDir, "license.key");
            logger.debug(`Writing license file to ${targetLicensePath}`);
            (0, fs_1.writeFileSync)(targetLicensePath, licenseKey, 'utf-8');
            logger.info(`License file written successfully to ${targetLicensePath}`);
        }
        catch (error) {
            logger.error(`Failed to write license file: ${error}`);
            throw error;
        }
    }
}
exports.LicenseFileService = LicenseFileService;
//# sourceMappingURL=license-file.service.js.map