"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLicenseProject = exports.LS_KEY_LICENSE_PROJECT = exports.useTimeFixer = exports.loadDateRange = exports.getType = void 0;
const local_storage_1 = require("@cocalc/frontend/misc/local-storage");
const billing_1 = require("@cocalc/util/consts/billing");
const misc_1 = require("@cocalc/util/misc");
const timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
const use_customize_1 = __importDefault(require("lib/use-customize"));
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = require("react");
// site license type in a form, we have 4 forms hence 4 types
// later, this will be mapped to just "LicenseType", where boost and regular
// are "quota" and item.boost = true|false.
function getType(item) {
    const descr = item.description;
    if (descr.dedicated_disk != null && descr.dedicated_disk !== false) {
        return "disk";
    }
    else if (descr.dedicated_vm != null && descr.dedicated_vm !== false) {
        return "vm";
    }
    else if (descr.boost === true) {
        return "boost";
    }
    else {
        return "regular";
    }
}
exports.getType = getType;
// when loading an item from the cart/saved for later, we have to fix the start/end dates to be at least "today" at the start of the day in the users time zone.
function loadDateRange(range) {
    if (range == null) {
        const now = new Date();
        return [(0, timecalcs_1.startOfDay)(now), (0, timecalcs_1.endOfDay)(now)];
    }
    for (const idx of [0, 1]) {
        const v = range[idx];
        if (typeof v === "string") {
            range[idx] = new Date(v);
        }
    }
    if (range[0] instanceof Date) {
        const today = (0, timecalcs_1.startOfDay)(new Date());
        const prevStart = range[0];
        if (range[0].getTime() < today.getTime()) {
            range[0] = today;
        }
        // we we have to shift the start, move the end forward as well and preserve the duration.
        if (range[1] instanceof Date) {
            if (range[0].getTime() > range[1].getTime()) {
                const days = (0, timecalcs_1.getDays)({ start: prevStart, end: range[1] });
                range[1] = (0, timecalcs_1.endOfDay)(new Date(Date.now() + billing_1.ONE_DAY_MS * days));
            }
        }
    }
    return range;
}
exports.loadDateRange = loadDateRange;
/**
 * use serverTime to fix the users exact time and return an object with these properties:
 * - offset: difference in milliseconds between server time and user time
 * - timezone: the user's timezone
 * - serverTime: the Date object of serverTime
 * - userTime: the Date object of userTime
 * - function toServerTime(date): converts a Date object from the user's time to the server time
 *
 * @param serverTime -- milliseconds since epoch from the server
 */
function useTimeFixer() {
    const { serverTime: customizeServerTime } = (0, use_customize_1.default)();
    return (0, react_1.useMemo)(() => {
        // server time is supposed to be always set, but just in case …
        if (customizeServerTime == null) {
            console.warn("WARNING: customize.serverTime is not set, using Date.now()");
        }
        const serverTime = customizeServerTime ?? Date.now();
        const localTime = Date.now();
        // important: useMemo, b/c we calculate the offset only *once* when the page loads, not each time the hook is called
        const offset = localTime - serverTime;
        function toTimestamp(date) {
            return (0, dayjs_1.default)(date).toDate().getTime();
        }
        function toServerTime(date) {
            return new Date(toTimestamp(date) - offset);
        }
        function fromServerTime(date) {
            return new Date(toTimestamp(date) + offset);
        }
        return {
            offset,
            timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
            serverTimeDate: new Date(serverTime),
            toServerTime,
            fromServerTime,
        };
    }, []);
}
exports.useTimeFixer = useTimeFixer;
exports.LS_KEY_LICENSE_PROJECT = "store_site_license_project_id";
const LS_KEY_LICENSE_ASSOCIATION = "store_site_license_association";
/**
 * We want to make it possible to purchase a license and applying it automatically to a project.
 * For that, we check if there is a query param "project_id" (and save it in local storage) or just check local storage.
 */
function useLicenseProject(router) {
    const [upgradeProjectId, setUpgradeProjectId] = (0, react_1.useState)();
    (0, react_1.useEffect)(() => {
        const { project_id } = router.query;
        const projectIdLS = (0, local_storage_1.get_local_storage)(exports.LS_KEY_LICENSE_PROJECT);
        if (typeof project_id === "string" && (0, misc_1.isValidUUID)(project_id)) {
            setUpgradeProjectId(project_id);
            (0, local_storage_1.set_local_storage)(exports.LS_KEY_LICENSE_PROJECT, project_id);
        }
        else if (typeof projectIdLS === "string" && (0, misc_1.isValidUUID)(projectIdLS)) {
            setUpgradeProjectId(projectIdLS);
        }
        else {
            if (project_id != null) {
                console.warn(`Invalid ?project_id=... query param: '${project_id}'`);
            }
        }
    }, []);
    // this removes the project_id from local storage and the query param
    function upgradeProjectDelete() {
        (0, local_storage_1.delete_local_storage)(exports.LS_KEY_LICENSE_PROJECT);
        setUpgradeProjectId(undefined);
        const { pathname } = router;
        const query = router.query;
        delete query.project_id;
        router.replace({ pathname, query }, undefined, { shallow: true });
    }
    // the ID created in the shopping cart, not the actual ID!
    // when this is called, we kind of "consume" the project_id
    // and remove it from the query param and local storage
    function storeLicenseProjectAssociation(id) {
        const project_id = (0, local_storage_1.get_local_storage)(exports.LS_KEY_LICENSE_PROJECT);
        if (typeof project_id !== "string" || !(0, misc_1.isValidUUID)(project_id)) {
            console.warn(`Invalid project_id in local storage: '${project_id}'`);
        }
        (0, local_storage_1.set_local_storage)(LS_KEY_LICENSE_ASSOCIATION, `${id}::${project_id}`);
        upgradeProjectDelete();
    }
    return {
        upgradeProjectId,
        upgradeProjectDelete,
        storeLicenseProjectAssociation,
    };
}
exports.useLicenseProject = useLicenseProject;
//# sourceMappingURL=util.js.map