import { TextDecoder } from "node:util";
const DEFAULT_WINDOWS_SHELL_OUTPUT_ENCODING = "gb18030";
const DEFAULT_SHELL_OUTPUT_ENCODING = "utf-8";
export function resolveShellOutputEncoding(platform = process.platform, env = process.env) {
    const configuredEncoding = env.ACODE_COMMAND_OUTPUT_ENCODING?.trim() ||
        env.AWS_TERMINAL_OUTPUT_ENCODING?.trim();
    if (configuredEncoding) {
        return configuredEncoding;
    }
    return platform === "win32"
        ? DEFAULT_WINDOWS_SHELL_OUTPUT_ENCODING
        : DEFAULT_SHELL_OUTPUT_ENCODING;
}
export function createShellOutputDecoder(encoding = resolveShellOutputEncoding()) {
    try {
        return new TextDecoder(encoding, { fatal: false });
    }
    catch {
        return new TextDecoder(DEFAULT_SHELL_OUTPUT_ENCODING, { fatal: false });
    }
}
export function decodeShellOutputChunk(decoder, chunk, end = false) {
    if (end) {
        return decoder.decode();
    }
    if (!chunk || chunk.length === 0) {
        return "";
    }
    return decoder.decode(chunk, { stream: true });
}
