"use strict";
/*
Add item to the shopping cart for signed in user.

request body:

- product and description: to add something new to the cart
or
- id: to move something back into the cart that was removed
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const add_1 = __importStar(require("@cocalc/server/shopping/cart/add"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const get_params_1 = __importDefault(require("lib/api/get-params"));
async function handle(req, res) {
    try {
        res.json(await add(req));
    }
    catch (err) {
        res.json({ error: `${err.message}` });
        return;
    }
}
exports.default = handle;
async function add(req) {
    const account_id = await (0, get_account_1.default)(req);
    if (account_id == null) {
        throw Error("must be signed in to use shopping cart");
    }
    const { product, description, id, purchased, project_id } = (0, get_params_1.default)(req);
    if (id != null) {
        if (purchased) {
            // put copy of it in the cart
            return await (0, add_1.buyItAgain)(account_id, id);
        }
        else {
            // adding something back to cart that was saved for later
            return await (0, add_1.putBackInCart)(account_id, id);
        }
    }
    if (!product) {
        throw Error("if id isn't specified then the product must be set");
    }
    return await (0, add_1.default)(account_id, product, description, project_id);
}
//# sourceMappingURL=add.js.map