import { searchGroupContent, searchGroupUsers, getGroup, } from "@esri/arcgis-rest-portal";
import { getEnrichmentErrors } from "../../items/_enrichments";
import { getProp } from "../../objects/get-prop";
import OperationStack from "../../OperationStack";
import { createOperationPipeline, } from "../../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 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 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 getGroup(data.group.id, requestOptions)
        .then((result) => {
        stack.finish(opId);
        return {
            data: Object.assign(Object.assign({}, data), {
                membershipAccess: result.membershipAccess,
                userMembership: 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: 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
 */
export 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 = createOperationPipeline(operations);
    // execute pipeline and return the item and enrichments
    return pipeline({
        data: { group },
        stack: new OperationStack(),
        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