"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startDevMode = startDevMode;
const modifications_service_1 = require("./modifications/modifications.service");
const child_process_1 = require("child_process");
const logger_1 = require("../utils/logger");
const child_process_2 = require("child_process");
const path_1 = require("../utils/path");
const fs_1 = require("fs");
const ensure_iocd_exists_1 = require("../utils/ensure.iocd.exists");
const app_service_1 = __importDefault(require("./app.service"));
const license_file_service_1 = require("./license-file.service");
const logger = logger_1.Logger.getInstance("dev.service");
async function startDevMode() {
    logger.info('Will start io.cd in dev mode....');
    (0, ensure_iocd_exists_1.ensureIOCDComponentExists)();
    // some applications have base modifications
    await app_service_1.default.copyBaseModifications();
    // copy modifications over components
    await (0, modifications_service_1.applyModifications)("dev");
    // copy license file to binary directory if it exists
    await license_file_service_1.LicenseFileService.copyLicenseFile();
    // sign ioconnect desktop if MacOS
    await signIocdAppIfMacOs();
    await launchIocdComponent();
}
async function signIocdAppIfMacOs() {
    // Code signing is only needed on macOS
    if (process.platform !== 'darwin') {
        logger.debug('Skipping code signing - not on macOS');
        return;
    }
    try {
        logger.debug('Running on mac - will sign iocd component...');
        const appBundlePath = path_1.PathUtils.getIOCDAppBundlePath();
        logger.debug('Executable path:', appBundlePath);
        await signAppPackage(appBundlePath);
        logger.debug('Code signing completed');
    }
    catch (error) {
        logger.error(`Code signing failed: ${error}`);
        throw error;
    }
}
async function launchIocdComponent() {
    logger.info('Launching io.Connect Desktop...');
    try {
        let executablePath = path_1.PathUtils.getIOCDExePath();
        if (!(0, fs_1.existsSync)(executablePath)) {
            throw new Error(`Executable not found at path: ${executablePath}`);
        }
        logger.debug(`starting executable ${executablePath}`);
        (0, child_process_2.spawnSync)(executablePath, {
            stdio: 'inherit',
            cwd: path_1.PathUtils.getComponentDir("iocd")
        });
        logger.printLogLocation();
    }
    catch (error) {
        logger.error('Failed to launch io.Connect Desktop component:', error);
        throw error;
    }
}
/**
 * Sign a single app package using codesign
 */
async function signAppPackage(appPath) {
    try {
        logger.debug(`Signing ${appPath}...`);
        // Use codesign with ad-hoc signing (no developer certificate required)
        // --deep: Sign nested code recursively
        // --force: Replace any existing signature
        // --sign -: Use ad-hoc signing (no certificate)
        const command = `codesign --deep --force --sign - "${appPath}"`;
        logger.debug(`Executing: ${command}`);
        (0, child_process_1.execSync)(command, { stdio: 'pipe' });
        logger.debug(`Successfully signed ${appPath}`);
    }
    catch (error) {
        logger.error(`Failed to sign ${appPath}: ${error}`);
        throw error;
    }
}
//# sourceMappingURL=dev.service.js.map