import OneUptimeDate from "../../Types/Date";
import JSONFunctions from "../../Types/JSONFunctions";
import Typeof from "../../Types/Typeof";
import UniversalCookies from "universal-cookie";
import CookieName from "../../Types/CookieName";
export default class Cookie {
    static clearAllCookies() {
        const cookies = new UniversalCookies();
        // Remove all cookies defined in CookieName enum
        Object.values(CookieName).forEach((cookieName) => {
            cookies.remove(cookieName, { path: "/" });
        });
    }
    static setItem(key, value, options) {
        if (typeof value === Typeof.Object) {
            // if of type jsonobject.
            value = JSON.stringify(JSONFunctions.serializeValue(value));
        }
        const cookies = new UniversalCookies();
        const cookieOptions = {
            httpOnly: (options === null || options === void 0 ? void 0 : options.httpOnly) || false,
            path: (options === null || options === void 0 ? void 0 : options.path) ? options.path.toString() : "/",
        };
        if (options === null || options === void 0 ? void 0 : options.maxAgeInDays) {
            cookieOptions.maxAge = OneUptimeDate.getMillisecondsInDays(options.maxAgeInDays);
        }
        cookies.set(key, value, cookieOptions);
    }
    static getItem(cookieName) {
        const cookies = new UniversalCookies();
        const value = cookies.get(cookieName);
        try {
            if (value) {
                return JSONFunctions.deserializeValue(JSONFunctions.parse(value === null || value === void 0 ? void 0 : value.toString()));
            }
            return value;
        }
        catch (_a) {
            return value;
        }
    }
    static removeItem(key) {
        const cookies = new UniversalCookies();
        cookies.remove(key);
    }
    // check if cookie exists
    static exists(key) {
        const cookies = new UniversalCookies();
        return Boolean(cookies.get(key));
    }
}
//# sourceMappingURL=Cookie.js.map