"use strict";
/* Get projects that belongs to the authenticated user.
   If the user has no projects, creates one.
   If they have projects, returns the most recently active one.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOneProject = void 0;
const get_account_1 = __importDefault(require("lib/account/get-account"));
const create_1 = __importDefault(require("@cocalc/server/projects/create"));
const get_1 = __importDefault(require("@cocalc/server/projects/get"));
const misc_1 = require("@cocalc/util/misc");
async function handle(req, res) {
    const account_id = await (0, get_account_1.default)(req);
    try {
        res.json(await getOneProject(account_id));
    }
    catch (err) {
        res.json({ error: err.message });
    }
}
exports.default = handle;
// This is also used by the latex api endpoint.
async function getOneProject(account_id) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("user must be authenticated");
    }
    const projects = await (0, get_1.default)({ account_id, limit: 1 });
    if (projects.length >= 1) {
        return projects[0];
    }
    const title = "Untitled Project";
    return { project_id: await (0, create_1.default)({ account_id, title }), title };
}
exports.getOneProject = getOneProject;
//# sourceMappingURL=get-one.js.map