export async function getCurrentLocation(): Promise<locationObj> {
    console.debug("Fetching location");

    if (typeof window === "undefined") {
        console.error("Location fetch has run serverside - invalid method");
    }

    if (!navigator.geolocation) {
        console.error("Geolocation is not supported");
        throw new Error("Geolocation is not supported");
    }

    return new Promise((resolve, reject) => {
        const options = {
            timeout: 10000,
            maximumAge: 300,
        };

        navigator.geolocation.getCurrentPosition(
            (position) => {
                console.debug(`Position obtained: ${position.coords}`);
                resolve({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                });
            },
            (error) => {
                console.error("Error fetching location: ", error);
                reject(error);
            }
        );
        options;
    });
}

export interface locationObj {
    latitude: number;
    longitude: number;
}
