import path from 'node:path';
import { ensureDir, nowIso, writeTextAtomic } from '../fsx.js';
// Single source of truth for the Zellij clipboard pipeline used by `sks --mad`
// (and any other SKS-launched Zellij session). By default Zellij copies via the
// OSC 52 escape sequence, which several macOS terminals (Terminal.app always,
// some iTerm2 configs) silently drop. Setting copy_command="pbcopy" keeps
// Zellij's copy action wired to the macOS clipboard.
//
// Keep Zellij mouse_mode on by default so trackpad/wheel gestures are handled by
// Zellij pane scrollback instead of being translated into prompt-history input
// inside the focused Codex TUI. Copy remains wired through copy_command/pbcopy
// plus copy_on_select; users who need terminal-native selection can hold Shift
// or opt out with SKS_ZELLIJ_MOUSE_MODE=0.
export const ZELLIJ_CLIPBOARD_CONFIG_SCHEMA = 'sks.zellij-clipboard-config.v1';
/** Pick the platform-correct clipboard command. macOS is the primary target. */
export function resolveCopyCommand(platform = process.platform, env = process.env) {
    if (platform === 'darwin')
        return 'pbcopy';
    // Wayland sessions prefer wl-copy; fall back to xclip on X11. pbcopy is darwin-only.
    if (env.WAYLAND_DISPLAY)
        return 'wl-copy';
    return 'xclip -selection clipboard';
}
function kdlString(value) {
    return JSON.stringify(String(value || ''));
}
/** Render a minimal, valid Zellij config.kdl that enables clipboard copy. */
export function buildZellijClipboardKdl(cfg) {
    return [
        '// Generated by Sneakoscope (sks) so launched Zellij sessions copy to the OS clipboard.',
        `mouse_mode ${cfg.mouse_mode ? 'true' : 'false'}`,
        `copy_command ${kdlString(cfg.copy_command)}`,
        `copy_clipboard ${kdlString(cfg.copy_clipboard)}`,
        `copy_on_select ${cfg.copy_on_select ? 'true' : 'false'}`,
        ''
    ].join('\n');
}
export async function writeZellijClipboardConfig(root, missionId, platform = process.platform) {
    const copy_command = resolveCopyCommand(platform);
    const copy_clipboard = 'system';
    const copy_on_select = true;
    const mouse_mode = resolveMouseMode(process.env);
    const dir = path.join(root, '.sneakoscope', 'missions', missionId);
    await ensureDir(dir);
    const config_path = path.join(dir, 'zellij-clipboard.kdl');
    await writeTextAtomic(config_path, buildZellijClipboardKdl({ copy_command, copy_clipboard, copy_on_select, mouse_mode }));
    return {
        copy_command,
        copy_clipboard,
        copy_on_select,
        mouse_mode,
        // Appended AFTER `--default-layout <path>` in the create command. Zellij treats
        // --copy-command as mutually exclusive with --copy-clipboard (OSC52 target), so
        // only pass the command and copy-on-select flags on the CLI. The generated KDL
        // still records copy_clipboard for config-file consumers that use OSC52.
        optionFlags: ['--copy-command', copy_command, '--copy-on-select', String(copy_on_select), '--mouse-mode', String(mouse_mode)],
        config_path,
        generated_at: nowIso()
    };
}
export function resolveMouseMode(env = process.env) {
    const raw = String(env.SKS_ZELLIJ_MOUSE_MODE || '').trim();
    if (!raw)
        return true;
    if (/^(0|false|no|off)$/i.test(raw))
        return false;
    if (/^(1|true|yes|on)$/i.test(raw))
        return true;
    return true;
}
//# sourceMappingURL=zellij-clipboard-config.js.map