"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.getPlatformCommand = getPlatformCommand;
exports.runCommand = runCommand;
exports.runCommandQuiet = runCommandQuiet;
exports.spawnCommand = spawnCommand;
const child_process_1 = require("child_process");
const os = __importStar(require("os"));
function getPlatformCommand(command) {
    const isWindows = os.platform() === 'win32';
    if (isWindows && (command === 'npm' || command === 'openclaw')) {
        return `${command}.cmd`;
    }
    return command;
}
function runCommand(command, options = {}) {
    try {
        (0, child_process_1.execSync)(command, { stdio: 'inherit', cwd: options.cwd });
    }
    catch (error) {
        throw new Error(`Command failed: ${command}`);
    }
}
function runCommandQuiet(command, options = {}) {
    try {
        return (0, child_process_1.execSync)(command, { encoding: 'utf-8', cwd: options.cwd }).trim();
    }
    catch (error) {
        throw new Error(`Command failed: ${command}`);
    }
}
/**
 * Spawns a command and inherits stdio. This is useful for interactive commands or long-running processes
 * like logs tailing.
 * This function returns a Promise that resolves when the command exits.
 */
function spawnCommand(command, args, options = {}) {
    return new Promise((resolve, reject) => {
        // Use 'inherit' to directly use parent stdio. This avoids pipe issues and TTY detection problems.
        const child = (0, child_process_1.spawn)(command, args, {
            stdio: 'inherit',
            cwd: options.cwd,
            shell: true // Use shell to ensure command resolution works similar to execSync
        });
        child.on('error', (error) => {
            reject(error);
        });
        child.on('close', (code) => {
            if (code === 0) {
                resolve();
            }
            else {
                // Some commands (like tail -f) might be killed with signals which results in non-zero or null code.
                // But for openclaw logs, usually user interrupts it.
                // If code is null (signal killed), treat as success/resolve? 
                // Or if user Ctrl+C, node process also gets signal.
                // Let's resolve on non-zero too if it's just logs command? 
                // But to be safe, reject on error code, caller can catch.
                if (code === null) {
                    resolve(); // Signal killed
                }
                else {
                    reject(new Error(`Command failed with exit code ${code}`));
                }
            }
        });
    });
}
//# sourceMappingURL=system.js.map