import { IDENTITY_URL } from "../Config";
import { AppApiRoute } from "../../ServiceRoute";
import LocalStorage from "./LocalStorage";
import URL from "../../Types/API/URL";
import Route from "../../Types/API/Route";
import Email from "../../Types/Email";
import BadDataException from "../../Types/Exception/BadDataException";
import Name from "../../Types/Name";
import ObjectID from "../../Types/ObjectID";
import API from "../Utils/API/API";
import Cookie from "./Cookie";
import CookieName from "../../Types/CookieName";
import SessionStorage from "./SessionStorage";
import { Logger } from "./Logger";
export default class UserUtil {
    static setProfilePicId(id) {
        if (!id) {
            LocalStorage.removeItem("profile_pic_id");
            return;
        }
        LocalStorage.setItem("profile_pic_id", id.toString());
    }
    static getProfilePicId() {
        // check cookie first.
        const profilePicIdCookie = Cookie.getItem(CookieName.ProfilePicID);
        if (profilePicIdCookie) {
            return new ObjectID(profilePicIdCookie);
        }
        if (!LocalStorage.getItem("profile_pic_id")) {
            return null;
        }
        return new ObjectID(LocalStorage.getItem("profile_pic_id") || "");
    }
    static getProfilePictureRoute(userId) {
        return Route.fromString(AppApiRoute.toString())
            .addRoute("/user/profile-picture")
            .addRoute(`/${userId.toString()}`);
    }
    static isCardRegistered() {
        return Boolean(LocalStorage.getItem("cardRegistered"));
    }
    static getSavedUserTimezone() {
        return LocalStorage.getItem("user_timezone");
    }
    static setSavedUserTimezone(timezone) {
        LocalStorage.setItem("user_timezone", timezone);
    }
    static getDismissedTimezonePrompt() {
        const value = LocalStorage.getItem("user_timezone_dismissed");
        if (!value) {
            return null;
        }
        return value;
    }
    static setDismissedTimezonePrompt(timezone) {
        LocalStorage.setItem("user_timezone_dismissed", timezone);
    }
    static clearDismissedTimezonePrompt() {
        LocalStorage.removeItem("user_timezone_dismissed");
    }
    static setCardRegistered(value) {
        LocalStorage.setItem("cardRegistered", value.toString());
    }
    static setUserId(id) {
        LocalStorage.setItem("user_id", id.toString());
    }
    static getUserId() {
        // check cookie first.
        const userIdCookie = Cookie.getItem(CookieName.UserID);
        if (userIdCookie) {
            return new ObjectID(userIdCookie);
        }
        return new ObjectID(LocalStorage.getItem("user_id") || "");
    }
    static getName() {
        // check cookie first.
        const userNameCookie = Cookie.getItem(CookieName.Name);
        if (userNameCookie) {
            return new Name(userNameCookie);
        }
        return new Name(LocalStorage.getItem("user_name") || "");
    }
    static setName(name) {
        LocalStorage.setItem("user_name", name.toString());
    }
    static getEmail() {
        // check cookie first.
        const userEmailCookie = Cookie.getItem(CookieName.Email);
        if (userEmailCookie) {
            return new Email(userEmailCookie);
        }
        if (!LocalStorage.getItem("user_email")) {
            return null;
        }
        return new Email(LocalStorage.getItem("user_email"));
    }
    static setEmail(email) {
        LocalStorage.setItem("user_email", email);
    }
    static initialUrl() {
        if (LocalStorage.getItem("initialUrl")) {
            return URL.fromString(LocalStorage.getItem("initialUrl"));
        }
        throw new BadDataException("Initial URL not found");
    }
    static setInitialUrl(url) {
        LocalStorage.setItem("initialUrl", url);
    }
    // TODO: Fix project type
    static setProject(project) {
        LocalStorage.setItem("project", project);
    }
    static getProject() {
        return LocalStorage.getItem("project");
    }
    static clear() {
        LocalStorage.clear();
    }
    static removeUserId() {
        LocalStorage.removeItem("user_id");
    }
    static removeAccessToken() {
        LocalStorage.removeItem("token");
    }
    static removeInitialUrl() {
        return sessionStorage.removeItem("initialUrl");
    }
    static isMasterAdmin() {
        // check cookie first.
        const isMasterAdminCookie = Cookie.getItem(CookieName.IsMasterAdmin);
        if (isMasterAdminCookie) {
            return isMasterAdminCookie === "true" || isMasterAdminCookie === true;
        }
        return LocalStorage.getItem("is_master_admin");
    }
    static setIsMasterAdmin(isMasterAdmin) {
        LocalStorage.setItem("is_master_admin", isMasterAdmin);
    }
    static isLoggedIn() {
        return Boolean(this.getEmail());
    }
    static logout() {
        LocalStorage.clear();
        SessionStorage.clear();
        Cookie.clearAllCookies();
        API.post({
            url: URL.fromString(IDENTITY_URL.toString()).addRoute("/logout"),
        }).catch((err) => {
            Logger.error("Error during logout:");
            Logger.error(err.message);
        });
    }
    static getUtmParams() {
        const localStorageItems = LocalStorage.getAllItems();
        const result = {};
        for (const key in localStorageItems) {
            if (!localStorageItems[key]) {
                continue;
            }
            if (key.startsWith("utm")) {
                result[key] = localStorageItems[key];
            }
        }
        return result;
    }
}
//# sourceMappingURL=User.js.map