import { spawnSync } from "node:child_process";
import { existsSync } from "node:fs";
import { unlink } from "node:fs/promises";
/**
 * Delete a session file, trying the `trash` CLI first, then falling back to unlink
 */
export async function deleteSessionFile(sessionPath) {
    // Try `trash` first (if installed)
    const trashArgs = sessionPath.startsWith("-") ? ["--", sessionPath] : [sessionPath];
    const trashResult = spawnSync("trash", trashArgs, { encoding: "utf-8" });
    const getTrashErrorHint = () => {
        const parts = [];
        if (trashResult.error) {
            parts.push(trashResult.error.message);
        }
        const stderr = trashResult.stderr?.trim();
        if (stderr) {
            parts.push(stderr.split("\n")[0] ?? stderr);
        }
        if (parts.length === 0)
            return null;
        return `trash: ${parts.join(" · ").slice(0, 200)}`;
    };
    // If trash reports success, or the file is gone afterwards, treat it as successful
    if (trashResult.status === 0 || !existsSync(sessionPath)) {
        return { ok: true, method: "trash" };
    }
    // Fallback to permanent deletion
    try {
        await unlink(sessionPath);
        return { ok: true, method: "unlink" };
    }
    catch (err) {
        const unlinkError = err instanceof Error ? err.message : String(err);
        const trashErrorHint = getTrashErrorHint();
        const error = trashErrorHint ? `${unlinkError} (${trashErrorHint})` : unlinkError;
        return { ok: false, method: "unlink", error };
    }
}
//# sourceMappingURL=session-selector-delete.js.map