const SockChatUnfuckText = (text, isAction) => {
    // P7.1 doesn't wrap in <i>, likely a bug in SharpChat
    // check if this is the case with the PHPChat impl
    if(isAction && text.startsWith('<i>'))
        text = text.slice(3, -4);

    return text.replace(/ <br\/> /g, "\n")
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>');
};

const SockChatParseUserColour = str => {
    // todo
    return str;
};

const SockChatParseUserPerms = str => {
    const parts = str.split(str.includes("\f") ? "\f" : ' ');
    return {
        rank: parseInt(parts[0] ?? '0'),
        kick: parts[1] !== undefined && parts[1] !== '0',
        nick: parts[3] !== undefined && parts[3] !== '0',
        chan: parts[4] !== undefined && parts[4] !== '0',
    };
};

const SockChatParseMsgFlags = str => {
    return {
        nameBold: str[0] !== '0',
        nameItalics: str[1] !== '0',
        nameUnderline: str[2] !== '0',
        showColon: str[3] !== '0',
        isPM: str[4] !== '0',
        isAction: str[1] !== '0' && str[3] === '0',
    };
};

const SockChatParseStatusInfo = name => {
    const isAway = name.startsWith('&lt;');
    let message;

    if(isAway) {
        const index = name.indexOf('&gt;_');
        message = name.substring(4, index);
        name = name.substring(index + 5);
    }

    return {
        name: name,
        status: {
            isAway: isAway,
            message: message,
        },
    };
};
