"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.getOneSSO = exports.getSSO = void 0;
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const get_strategies_1 = require("@cocalc/server/auth/sso/get-strategies");
const auth_1 = require("@cocalc/util/auth");
const lodash_1 = require("lodash");
const SQL_SELECT = `SELECT strategy, info,
  COALESCE(info -> 'icon',              conf -> 'icon')              as icon,
  COALESCE(info -> 'display',           conf -> 'display')           as display,
  COALESCE(info -> 'exclusive_domains', conf -> 'exclusive_domains') as exclusive_domains`;
function parseRow(row) {
    const { strategy, exclusive_domains, display, info, icon } = row;
    return {
        id: strategy,
        display: (0, auth_1.ssoDispayedName)({ display, name: strategy }),
        domains: exclusive_domains ?? [],
        descr: info?.description ?? null,
        backgroundColor: get_strategies_1.COLORS[strategy] ?? "",
        icon,
    };
}
async function getSSO() {
    const pool = (0, pool_1.default)("long");
    const { rows } = await pool.query(`
    ${SQL_SELECT}
    FROM passport_settings
    WHERE coalesce(info -> 'public', conf -> 'public', 'true'::JSONB)::BOOL IS FALSE`);
    const data = rows.map((row) => parseRow(row));
    return (0, lodash_1.sortBy)(data, (sso) => sso.display);
}
exports.getSSO = getSSO;
async function getOneSSO(id) {
    const pool = (0, pool_1.default)("long");
    const { rows } = await pool.query(`${SQL_SELECT}
     FROM passport_settings
     WHERE strategy=$1`, [id]);
    if (rows.length === 0)
        return;
    return parseRow(rows[0]);
}
exports.getOneSSO = getOneSSO;
//# sourceMappingURL=sso.js.map