import { getUser } from "@esri/arcgis-rest-portal";
import { fetchUserEnrichments } from "./_internal/enrichments";
import { getUserThumbnailUrl } from "../search/utils";
import { parseInclude } from "../search/_internal/parseInclude";
import { unique } from "../util";
import { computeProps } from "./_internal/computeProps";
import { getProp } from "../objects/get-prop";
import { getUserHomeUrl } from "../urls/getUserHomeUrl";
import { mapBy } from "../utils/map-by";
/**
 * Converts a IUser object into an IHubUser.
 *
 * @param user
 * @returns IHubUser
 */
export 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,
        },
    };
};
/**
 * Enriches an IUser object search result.
 * @param user
 * @param includes
 * @param requestOptions
 * @returns
 */
export async function enrichUserSearchResult(user, include, requestOptions) {
    var _a;
    // Create the basic structure
    const hubUser = 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(unique).map(parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = mapBy("enrichment", specs).filter(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 fetchUserEnrichments(user, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = 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 = getUserThumbnailUrl(requestOptions.portal, user, token);
    }
    result.links.self = getUserHomeUrl(result.id, requestOptions);
    result.links.siteRelative = requestOptions.isPortal
        ? 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
 */
export const fetchHubUser = async (username, context) => {
    // grab the user
    const user = username === "self"
        ? context.currentUser
        : await getUser(Object.assign(Object.assign({}, context.hubRequestOptions), { username }));
    // convert to a hubUser
    let hubUser = convertUserToHubUser(user);
    hubUser = await computeProps(user, hubUser, context);
    return hubUser;
};
//# sourceMappingURL=HubUsers.js.map