"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2021 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 });
const customize_1 = __importDefault(require("@cocalc/server/settings/customize"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const get_account_info_1 = require("lib/share/get-account-info");
const is_collaborator_1 = __importDefault(require("@cocalc/server/projects/is-collaborator"));
const site_defaults_1 = require("@cocalc/util/db-schema/site-defaults");
const software_envs_1 = require("@cocalc/server/software-envs");
const db_schema_1 = require("@cocalc/util/db-schema");
const revalidate = 30;
async function withCustomize(obj, options = {}) {
    let customize;
    try {
        // NOTE: important to make a (shallow) copy, since customize gets mutated below.
        customize = { ...(await (0, customize_1.default)()) };
    }
    catch (_err) {
        // fallback to be empty; during static build
        // this happens.
        customize = {};
    }
    if (obj.context?.req != null) {
        const account_id = await (0, get_account_1.default)(obj.context.req);
        customize.isAuthenticated = !!account_id;
        if (account_id) {
            customize.account = {
                account_id,
                ...(options.name ? await (0, get_account_info_1.getName)(account_id) : undefined),
            };
            // Also, if a project id is in the props and account_id is set, it's very
            // useful to know if the user is a collaborator on the project, since that
            // can impact a lot about how we display things.  This is typically used
            // for the share pages.
            const project_id = obj.props?.project_id;
            if (project_id) {
                customize.isCollaborator = await (0, is_collaborator_1.default)({
                    account_id,
                    project_id,
                });
            }
        }
    }
    else {
        customize.isAuthenticated = false;
    }
    customize.onCoCalcCom = customize.kucalc === site_defaults_1.KUCALC_COCALC_COM;
    customize.noindex = obj.props?.unlisted ?? false;
    customize.imprintOrPolicies =
        (customize.imprint ?? "" + customize.policies ?? "") != "";
    customize.serverTime = Date.now();
    // this is used for creating new projects from a share
    const softwareEnvs = await (0, software_envs_1.getSoftwareEnvironments)("server");
    customize.defaultComputeImage =
        softwareEnvs?.default ?? db_schema_1.DEFAULT_COMPUTE_IMAGE;
    if (obj == null) {
        return { props: { customize } };
    }
    if (obj.revalidate != null) {
        obj.revalidate = Math.min(revalidate, obj.revalidate);
    }
    if (obj.props == null) {
        obj.props = { customize };
    }
    else {
        obj.props.customize = customize;
    }
    delete obj.context;
    return obj;
}
exports.default = withCustomize;
//# sourceMappingURL=with-customize.js.map