"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectName = getProjectName;
exports.execBuffered = execBuffered;
exports.readFileString = readFileString;
exports.writeFileString = writeFileString;
const path_1 = __importDefault(require("path"));
/**
 * Convenience helpers for working with streaming Runtime APIs.
 * These provide simple string-based APIs on top of the low-level streaming primitives.
 */
/**
 * Extract project name from a project path
 * Works for both local paths and remote paths
 */
function getProjectName(projectPath) {
    // For local paths, use path.basename
    // For remote paths (containing /), use the last segment
    return path_1.default.basename(projectPath);
}
/**
 * Execute a command and buffer all output into strings
 */
async function execBuffered(runtime, command, options) {
    const stream = await runtime.exec(command, options);
    // Write stdin if provided
    if (options.stdin !== undefined) {
        const writer = stream.stdin.getWriter();
        try {
            await writer.write(new TextEncoder().encode(options.stdin));
            await writer.close();
        }
        catch (err) {
            writer.releaseLock();
            throw err;
        }
    }
    else {
        // Close stdin immediately if no input
        await stream.stdin.close();
    }
    // Read stdout and stderr concurrently
    const [stdout, stderr, exitCode, duration] = await Promise.all([
        streamToString(stream.stdout),
        streamToString(stream.stderr),
        stream.exitCode,
        stream.duration,
    ]);
    return { stdout, stderr, exitCode, duration };
}
/**
 * Read file contents as a UTF-8 string
 */
async function readFileString(runtime, path, abortSignal) {
    const stream = runtime.readFile(path, abortSignal);
    return streamToString(stream);
}
/**
 * Write string contents to a file atomically
 */
async function writeFileString(runtime, path, content, abortSignal) {
    const stream = runtime.writeFile(path, abortSignal);
    const writer = stream.getWriter();
    try {
        await writer.write(new TextEncoder().encode(content));
        await writer.close();
    }
    catch (err) {
        writer.releaseLock();
        throw err;
    }
}
/**
 * Convert a ReadableStream<Uint8Array> to a UTF-8 string
 */
async function streamToString(stream) {
    const reader = stream.getReader();
    const decoder = new TextDecoder("utf-8");
    let result = "";
    try {
        while (true) {
            const { done, value } = await reader.read();
            if (done)
                break;
            result += decoder.decode(value, { stream: true });
        }
        // Final flush
        result += decoder.decode();
        return result;
    }
    finally {
        reader.releaseLock();
    }
}
//# sourceMappingURL=helpers.js.map