import datasquirel from "@moduletrace/datasquirel";
import { User } from "../types";
import userAuth from "../utils/user-auth";

type Param = {
    req: Request;
    debug?: boolean;
};

type Return = {
    user?: User | null;
};

export default async function socketInit({
    req,
    debug,
}: Param): Promise<Return> {
    const cookieString = req.headers.get("Cookie") || undefined;

    if (debug) {
        console.log("DEBUG:::socketInit:cookieString", cookieString);
    }

    if (!cookieString)
        return {
            user: null,
        };

    const { singleRes: user } = await userAuth({ bun_req: req });

    if (debug) {
        console.log("DEBUG:::socketInit:user", user);
    }

    return { user };
}
