const PARHELIA_LOG_PREFIX = "[Parhelia]";
const HIDDEN_CONSOLE_CHANNEL_PREFIXES = ["[AgentTerminal]"];
const INSTALL_MARKER = "__parheliaConsolePrefixInstalled";
const consoleMethods = [
    "debug",
    "error",
    "groupCollapsed",
    "info",
    "log",
    "warn",
];
export function prefixConsoleArgs(args) {
    if (args.length === 0) {
        return [PARHELIA_LOG_PREFIX];
    }
    const [first, ...rest] = args;
    if (typeof first !== "string") {
        return [PARHELIA_LOG_PREFIX, first, ...rest];
    }
    return [applyPrefix(first), ...rest];
}
export function installParheliaConsolePrefix(target = console) {
    const prefixedConsole = target;
    if (prefixedConsole[INSTALL_MARKER]) {
        return;
    }
    prefixedConsole[INSTALL_MARKER] = true;
    const writableConsole = target;
    for (const method of consoleMethods) {
        const original = writableConsole[method];
        if (typeof original !== "function") {
            continue;
        }
        const boundOriginal = original.bind(target);
        writableConsole[method] = (...args) => {
            if (shouldHideConsoleArgs(args)) {
                return;
            }
            boundOriginal(...prefixConsoleArgs(args));
        };
    }
}
function shouldHideConsoleArgs(args) {
    const [first] = args;
    if (typeof first !== "string") {
        return false;
    }
    const trimmed = first.trimStart();
    return HIDDEN_CONSOLE_CHANNEL_PREFIXES.some((channelPrefix) => trimmed.startsWith(channelPrefix) ||
        trimmed.startsWith(`${PARHELIA_LOG_PREFIX} ${channelPrefix}`));
}
function applyPrefix(message) {
    if (message.length === 0) {
        return PARHELIA_LOG_PREFIX;
    }
    const trimmed = message.trimStart();
    if (trimmed.startsWith(PARHELIA_LOG_PREFIX)) {
        return message;
    }
    return `${PARHELIA_LOG_PREFIX} ${message}`;
}
//# sourceMappingURL=consolePrefix.js.map