"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchGroupEnrichments = fetchGroupEnrichments;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const _enrichments_1 = require("../../items/_enrichments");
const get_prop_1 = require("../../objects/get-prop");
const OperationStack_1 = require("../../OperationStack");
const create_operation_pipeline_1 = require("../../utils/create-operation-pipeline");
/**
 * Fetch the count of items shared to the group.
 * This is done by searching for content in the group
 * and using the returned `total` value
 * @param input
 * @returns
 */
const enrichGroupContentCount = (input) => {
    const { data, stack, requestOptions } = input;
    const opId = stack.start("enrichGroupContentCount");
    // w/o the : any here, I get a compile error about
    // .authentication being incompatible w/ ArcGISIdentityManager
    const options = Object.assign({ groupId: data.group.id, num: 1 }, requestOptions);
    return (0, arcgis_rest_portal_1.searchGroupContent)(options)
        .then((results) => {
        stack.finish(opId);
        return {
            data: Object.assign(Object.assign({}, data), { contentCount: results.total }),
            stack,
            requestOptions,
        };
    })
        .catch((error) => handleEnrichmentError(error, input, opId));
};
/**
 * Create a summary of the Group membership by searching for members,
 * limiting to three for a sample, and using the `total`.
 * @param input
 * @returns
 */
const enrichGroupMembershipSummary = (input) => {
    const { data, stack, requestOptions } = input;
    const opId = stack.start("enrichGroupMembershipSummary");
    // w/o the `: any` here, I get a compile error about
    // .authentication being incompatible w/ ArcGISIdentityManager
    const options = Object.assign({ num: 3 }, requestOptions);
    return (0, arcgis_rest_portal_1.searchGroupUsers)(data.group.id, options)
        .then((results) => {
        stack.finish(opId);
        return {
            data: Object.assign(Object.assign({}, data), {
                membershipSummary: { total: results.total, users: results.users },
            }),
            stack,
            requestOptions,
        };
    })
        .catch((error) => handleEnrichmentError(error, input, opId));
};
/**
 * Get the requesting user's membership in the target group, as well
 * as the membership access requirements for the target group
 * @param input
 * @returns
 */
const enrichGroupUserMembership = (input) => {
    const { data, stack, requestOptions } = input;
    const opId = stack.start("enrichGroupUserMembership");
    return (0, arcgis_rest_portal_1.getGroup)(data.group.id, requestOptions)
        .then((result) => {
        stack.finish(opId);
        return {
            data: Object.assign(Object.assign({}, data), {
                membershipAccess: result.membershipAccess,
                userMembership: (0, get_prop_1.getProp)(result, "userMembership.memberType"),
            }),
            stack,
            requestOptions,
        };
    })
        .catch((error) => handleEnrichmentError(error, input, opId));
};
// add the error to the content.errors,
// log current stack operation as finished with an error
// and return output that can be piped into the next operation
const handleEnrichmentError = (error, input, opId) => {
    const { data, stack, requestOptions } = input;
    stack.finish(opId, { error });
    return {
        data: Object.assign(Object.assign({}, data), { errors: (0, _enrichments_1.getEnrichmentErrors)(error, data.errors) }),
        stack,
        requestOptions,
    };
};
/**
 * Available enrichments for Groups
 */
const groupEnrichmentOperations = {
    membershipSummary: enrichGroupMembershipSummary,
    contentCount: enrichGroupContentCount,
    userMembership: enrichGroupUserMembership,
};
/**
 * Fetch enrichments for Groups
 * @param group
 * @param enrichments
 * @param requestOptions
 * @returns
 */
function fetchGroupEnrichments(group, enrichments, requestOptions) {
    // create a pipeline of enrichment operations
    const operations = enrichments.reduce((ops, enrichment) => {
        const operation = groupEnrichmentOperations[enrichment];
        // only include the enrichments that we know how to fetch
        // if you are updating this file, remove this comment and address the lint error
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        operation && ops.push(operation);
        return ops;
    }, []);
    const pipeline = (0, create_operation_pipeline_1.createOperationPipeline)(operations);
    // execute pipeline and return the item and enrichments
    return pipeline({
        data: { group },
        stack: new OperationStack_1.default(),
        requestOptions,
    }).then((output) => {
        // TODO: send telemetry so we have info on what enrichments are requested and possible errors
        return output.data;
    });
}
//# sourceMappingURL=enrichments.js.map