
import { api } from "../../services/Api";
import { ILoginRequest, ILoginResult, IOauthLoginRequest } from "./types";

interface IOauthLoginResult extends ILoginResult {
    href: string | null;
}

export class LoginPageService {
    public static async login(request: ILoginRequest): Promise<ILoginResult> {

        try {
            const response = await api.post("auth/login", request);
            if (response.status === 200) return { loggedIn: true, isError: false };

            return { loggedIn: false, isError: false };
        }

        catch (error) {
            console.error("Error: ", error);
            return { loggedIn: false, isError: true };
        }
    }



    public static async loginOauth(request: IOauthLoginRequest): Promise<IOauthLoginResult> {
        try {
            const response = await api.post<string>("/oauth/auth", request);
            if (response.status === 200) return { loggedIn: true, isError: false, href: response.data };

            return { loggedIn: false, isError: false, href: null };
        }

        catch (error) {
            console.error("Error: ", error);
            return { loggedIn: false, isError: true, href: null };
        }
    }
}
