import { WebSocketMessageParam } from "@/src/types";
import sendData from "../(utils)/send-data";
import sendError from "../(utils)/send-error";
import grabTtydInfo from "@/src/functions/ttyd/grab-ttyd-info";

export default async function socketClientRelayShell({
    ws,
    data,
}: WebSocketMessageParam) {
    try {
        const user = ws.data.user;
        const cmd = data?.cmd;

        if (!cmd) {
            throw new Error(`No Command Passed`);
        }

        const ttyd = await grabTtydInfo({
            cmd,
            user,
            ttyd_options: {
                cwd: user.super_admin ? "/root" : `/home/${user.username}`,
            },
        });

        sendData(ws, {
            event: "server:relay-shell",
            ttyd,
        });
    } catch (error: any) {
        sendError(ws, "Relay Shell Error! " + error.message);
    }
}
