#include sockchat/utils.js

const SockChatS2CAuthSuccess = (ctx, userId, userName, userColour, userPerms, chanName, maxLength) => {
    ctx.userId = userId;
    ctx.channelName = chanName;

    const statusInfo = SockChatParseStatusInfo(userName);

    const info = {
        wasConnected: ctx.wasConnected,
        session: { success: true },
        ctx: {
            maxMsgLength: parseInt(maxLength),
        },
        user: {
            id: ctx.userId,
            self: true,
            name: statusInfo.name,
            status: statusInfo.status,
            colour: SockChatParseUserColour(userColour),
            perms: SockChatParseUserPerms(userPerms),
        },
        channel: {
            name: ctx.channelName,
        },
    };

    ctx.wasConnected = true;
    ctx.keepAlive.start();

    ctx.dispatch('session:start', info);
    ctx.authPromise?.resolve(info);
};

const SockChatS2CAuthFail = (ctx, reason, expiresTime) => {
    ctx.wasKicked = true;

    const info = {
        session: {
            success: false,
            needsAuth: reason === 'authfail',
            outOfConnections: reason === 'sockfail',
        },
    };
    if(expiresTime !== undefined)
        info.baka = {
            type: 'join',
            perma: expiresTime === '-1',
            until: expiresTime === '-1' ? undefined : new Date(parseInt(expiresTime) * 1000),
        };

    ctx.dispatch('session:fail', info);
    ctx.authPromise?.reject(info);
};
