import { applicationConfig } from "@/constants/application-config.js";

const cookieNames = {
    FUNNEL_SESSION_INFO: `FunnelSessionInfo-${applicationConfig.CURRENT_ENVIRONMENT}`,

    FUNNEL_SESSION_KEY: `FunnelSessionKey-${applicationConfig.CURRENT_ENVIRONMENT}`,
    FUNNEL_USER_ID: `FunnelUserId-${applicationConfig.CURRENT_ENVIRONMENT}`,

    // Existing Safelite.com cookies
    DXDEV: "dxdev",
    SESSION_ID: "sid",
    SESSION_KEY: "skey",

    //Commission Junction Cookie
    CJE: "cje",
};

const cookieExpirations = {
    SESSION_ID: convertToSeconds({ minutes: applicationConfig.SESSION_TIMEOUT_MINUTES }),
    DXDEV: convertToSeconds({ years: 1 }),
    FUNNEL_USER_ID: convertToSeconds({ weeks: 1 }),
    FUNNEL_SESSION_KEY: convertToSeconds({ minutes: 30 }),
    CJE: convertToSeconds({ days: 395 }),
    NON_CJ: convertToSeconds({ days: 100000 }),
};

export { cookieNames, cookieExpirations };

function convertToSeconds({ years, months, weeks, days, hours, minutes, seconds }) {
    let total = seconds ?? 0;
    total += (minutes ?? 0) * 60;
    total += (hours ?? 0) * 60 * 60;
    total += (days ?? 0) * 24 * 60 * 60;
    total += (weeks ?? 0) * 7 * 24 * 60 * 60;
    total += (months ?? 0) * 30 * 24 * 60 * 60;
    total += (years ?? 0) * 365 * 24 * 60 * 60;

    return total;
}
