"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPublicPathNames = void 0;
/* Get basic information about a user or organization
from the database.  This should be enough to render
a nice "homepage" for that user or organization.
*/
var pool_1 = __importDefault(require("@cocalc/database/pool"));
var project_1 = __importDefault(require("./project"));
var owner_1 = require("lib/names/owner");
var get_public_path_1 = __importStar(require("lib/share/proxy/get-public-path"));
var path_1 = require("path");
function getPublicPathId(owner, project, public_path // this is the entire actual path
) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function () {
        var id, project_id, pool, result;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    if (!(0, get_public_path_1.shouldUseProxy)(owner)) return [3 /*break*/, 2];
                    return [4 /*yield*/, (0, get_public_path_1.default)({
                            url: path_1.join.apply(void 0, __spreadArray([owner, project], __read(public_path), false)),
                        })];
                case 1:
                    id = (_c.sent()).id;
                    return [2 /*return*/, id];
                case 2: return [4 /*yield*/, (0, project_1.default)(owner, project)];
                case 3:
                    project_id = _c.sent();
                    pool = (0, pool_1.default)("long");
                    return [4 /*yield*/, pool.query("SELECT id FROM public_paths WHERE LOWER(name)=$1 AND project_id=$2", [(_b = (_a = public_path[0]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : "", project_id])];
                case 4:
                    result = _c.sent();
                    if (result.rows.length > 0) {
                        return [2 /*return*/, result.rows[0].id];
                    }
                    throw Error("no public_path ".concat(owner, "/").concat(project, "/").concat(public_path[0]));
            }
        });
    });
}
exports.default = getPublicPathId;
// Given the id of a public path, returns owner name, project name, and public_path name
// if they are all defined and nonempty. Otherwise returns undefined.
function getPublicPathNames(id) {
    return __awaiter(this, void 0, void 0, function () {
        var pool, result, _a, project_id, public_path, _b, project, users, owner_id, account_id, owner;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    pool = (0, pool_1.default)("medium");
                    return [4 /*yield*/, pool.query("SELECT project_id, name FROM public_paths WHERE id=$1", [id])];
                case 1:
                    result = _c.sent();
                    if (result.rows.length == 0)
                        return [2 /*return*/];
                    _a = result.rows[0], project_id = _a.project_id, public_path = _a.name;
                    if (!public_path)
                        return [2 /*return*/];
                    return [4 /*yield*/, pool.query("SELECT name, users FROM projects WHERE project_id=$1 AND name IS NOT NULL AND name != ''", [project_id])];
                case 2:
                    // Having to get users is pretty stupid -- see comment in lib/project/get-owner.ts
                    result = _c.sent();
                    if (result.rows.length == 0)
                        return [2 /*return*/];
                    _b = result.rows[0], project = _b.name, users = _b.users;
                    owner_id = "";
                    for (account_id in users) {
                        if (users[account_id].group == "owner") {
                            owner_id = account_id;
                            break;
                        }
                    }
                    if (!owner_id)
                        return [2 /*return*/]; // shouldn't be possible
                    return [4 /*yield*/, (0, owner_1.getOwnerName)(owner_id)];
                case 3:
                    owner = _c.sent();
                    if (!owner)
                        return [2 /*return*/];
                    return [2 /*return*/, { owner: owner, project: project, public_path: public_path }];
            }
        });
    });
}
exports.getPublicPathNames = getPublicPathNames;
//# sourceMappingURL=public-path.js.map