"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeProps = computeProps;
exports.getPortalSignInSettings = getPortalSignInSettings;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
const fail_safe_1 = require("../../utils/fail-safe");
/**
 * Given a model and a user, sets various computed properties that can't be directly mapped.
 * @param model
 * @param user
 * @param context
 * @returns
 */
async function computeProps(model, user, context) {
    var _a, _b, _c;
    // 1. compute any props for user settings
    user.settings = context.userHubSettings;
    // 2. compute any props for user's org settings
    // TODO: only fetch this if the user has necessary privs (org admin)
    const fsGetSignInSettings = (0, fail_safe_1.failSafe)(getPortalSignInSettings, {});
    const signinSettings = await fsGetSignInSettings(context);
    const fsGetSelf = (0, fail_safe_1.failSafe)(arcgis_rest_portal_1.getSelf, {});
    const _portalself = await fsGetSelf(context.requestOptions);
    user.hubOrgSettings = {
        showInformationalBanner: !!((_c = (_b = (_a = _portalself.portalProperties) === null || _a === void 0 ? void 0 : _a.hub) === null || _b === void 0 ? void 0 : _b.settings) === null || _c === void 0 ? void 0 : _c.informationalBanner),
        enableTermsAndConditions: !!signinSettings.termsAndConditions,
        termsAndConditions: signinSettings.termsAndConditions,
        enableSignupText: !!signinSettings.signupText,
        signupText: signinSettings.signupText,
    };
    return user;
}
/**
 * Fetches the portal's signin settings by making a request to the
 * ${portalUrl}/portals/self/signinSettings endpoint.
 *
 * Returns a promise that resolves with the signin settings object.
 *
 * @param context
 */
function getPortalSignInSettings(context) {
    const url = `${(0, arcgis_rest_portal_1.getPortalUrl)(context.requestOptions)}/portals/self/signinSettings`;
    const requestOpts = Object.assign(Object.assign({}, context.requestOptions), { httpMethod: "GET" });
    return (0, arcgis_rest_request_1.request)(url, requestOpts);
}
//# sourceMappingURL=computeProps.js.map