import { useTranslation } from "react-i18next";
import { Context } from "../context/context";
import { ConnectionState } from "../rtmt/rtmt";
import Snackbar from "react-native-snackbar";

export default class Util {
    public static range(size: number) {
        var ans: number[] = [];
        for (let i = 0; i < size; i++) {
            ans.push(i);
        }
        return ans;
    }

    public static checkConnectionAndMaybeAlert(context: Context, message: string): boolean {
        if (context.rtmt.connectionState !== "connected") {
            Snackbar.show({text: message});
            return true;
        }
        return false;
    }

    public static getTextByConnectionState(context: Context, connectionState: ConnectionState): string {
        const { t } = useTranslation();
        const map: { [key: string]: string } = {
            connecting: t("Connecting"),
            connected: t("Connected"),
            error: t("CannotConnect"),
            closed: t("ConnectingAgain"),
            reconnecting: t("ConnectingAgain"),
        };
        return map[connectionState];
    }
}
