"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchHubUser = exports.convertUserToHubUser = void 0;
exports.enrichUserSearchResult = enrichUserSearchResult;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const enrichments_1 = require("./_internal/enrichments");
const utils_1 = require("../search/utils");
const parseInclude_1 = require("../search/_internal/parseInclude");
const util_1 = require("../util");
const computeProps_1 = require("./_internal/computeProps");
const get_prop_1 = require("../objects/get-prop");
const getUserHomeUrl_1 = require("../urls/getUserHomeUrl");
const map_by_1 = require("../utils/map-by");
/**
 * Converts a IUser object into an IHubUser.
 *
 * @param user
 * @returns IHubUser
 */
const convertUserToHubUser = (user) => {
    // A private user will not have a description prop at all
    // thus we set it to undefined to differentiate from a empty description which would be null
    // eslint-disable-next-line no-prototype-builtins
    const description = Object.prototype.hasOwnProperty.call(user, "description")
        ? user.description
        : undefined;
    return {
        access: user.access,
        id: user.username,
        name: user.fullName,
        description,
        summary: description,
        createdDate: new Date(user.created),
        createdDateSource: "user.created",
        orgId: user.orgId,
        owner: user.username,
        updatedDate: new Date(user.modified),
        updatedDateSource: "user.modified",
        tags: user.tags,
        thumbnail: user.thumbnail,
        type: "User",
        typeKeywords: [],
        links: {
            // TODO: implement these
            self: "not-implemented",
            siteRelative: "not-implemented",
            thumbnail: null,
        },
    };
};
exports.convertUserToHubUser = convertUserToHubUser;
/**
 * Enriches an IUser object search result.
 * @param user
 * @param includes
 * @param requestOptions
 * @returns
 */
async function enrichUserSearchResult(user, include, requestOptions) {
    var _a;
    // Create the basic structure
    const hubUser = (0, exports.convertUserToHubUser)(user);
    const result = Object.assign(Object.assign({}, hubUser), { family: "people", rawResult: user });
    // Group Memberships need these additional properties
    if (user.memberType) {
        result.memberType = user.memberType;
        result.isGroupOwner = user.isGroupOwner;
    }
    // Parse the includes into a valid set of enrichments
    const specs = include.filter(util_1.unique).map(parseInclude_1.parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = (0, map_by_1.mapBy)("enrichment", specs).filter(util_1.unique);
    // fetch the enrichments
    let enriched = {};
    // Ignoring the else, because we currently have defaults, but want the guards
    // so if we remove that in the future, we don't call the fn
    /* istanbul ignore else */
    if (enrichments.length) {
        enriched = await (0, enrichments_1.fetchUserEnrichments)(user, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = (0, get_prop_1.getProp)(enriched, spec.path);
    });
    const token = (_a = requestOptions.authentication) === null || _a === void 0 ? void 0 : _a.token;
    // only construct thumbnail url if we have a thumbnail value
    // ui layer can decide how to handle a null thumbnail
    if (user.thumbnail) {
        result.links.thumbnail = (0, utils_1.getUserThumbnailUrl)(requestOptions.portal, user, token);
    }
    result.links.self = (0, getUserHomeUrl_1.getUserHomeUrl)(result.id, requestOptions);
    result.links.siteRelative = requestOptions.isPortal
        ? (0, getUserHomeUrl_1.getUserHomeUrl)(result.id, requestOptions)
        : `/people/${result.id}`;
    return result;
}
/**
 * Fetches a hub user by username
 * @param username - hub username. can also be "self"
 * @param context
 * @returns
 */
const fetchHubUser = async (username, context) => {
    // grab the user
    const user = username === "self"
        ? context.currentUser
        : await (0, arcgis_rest_portal_1.getUser)(Object.assign(Object.assign({}, context.hubRequestOptions), { username }));
    // convert to a hubUser
    let hubUser = (0, exports.convertUserToHubUser)(user);
    hubUser = await (0, computeProps_1.computeProps)(user, hubUser, context);
    return hubUser;
};
exports.fetchHubUser = fetchHubUser;
//# sourceMappingURL=HubUsers.js.map