import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import { EJSON } from "@/client-exports";
import checkQueue from "./events/client-requests/check-queue";
import webSocketSendDummyQueue from "./events/client-requests/send-dummy-queue";
import webSocketDeleteQueue from "./events/client-requests/delete-queue";

type Param = {
    ws: ServerWebSocket<WebSocketData>;
    message: string | Buffer;
};

export type WebSocketMessageParam = {
    ws: ServerWebSocket<WebSocketData>;
    message?: string | Buffer;
    data?: WebSocketDataType;
};

export default async function socketMessage({ ws, message }: Param) {
    const user = ws.data.user;
    const data = EJSON.parse(message.toString()) as
        | WebSocketDataType
        | undefined;

    const websocketMessageParams: WebSocketMessageParam = {
        ws,
        data,
        message,
    };

    switch (data?.event) {
        /**
         * Check Queue
         */
        case "client:check-queue":
            checkQueue({ ws });
            break;
        /**
         * Send Dummy Queue
         */
        case "client:dev:queue":
            webSocketSendDummyQueue({ ws });
            break;
        /**
         * Delete Queue
         */
        case "client:delete-queue":
            webSocketDeleteQueue({ ws, data });
            break;

        default:
            break;
    }
}
