import { getPortalUrl, getSelf } from "@esri/arcgis-rest-portal";
import { request } from "@esri/arcgis-rest-request";
import { failSafe } from "../../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
 */
export 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 = failSafe(getPortalSignInSettings, {});
    const signinSettings = await fsGetSignInSettings(context);
    const fsGetSelf = failSafe(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
 */
export function getPortalSignInSettings(context) {
    const url = `${getPortalUrl(context.requestOptions)}/portals/self/signinSettings`;
    const requestOpts = Object.assign(Object.assign({}, context.requestOptions), { httpMethod: "GET" });
    return request(url, requestOpts);
}
//# sourceMappingURL=computeProps.js.map