import { getGroupUsers } from "@esri/arcgis-rest-portal";
import { CANNOT_DISCUSS } from "./constants";
import { unique } from "../util";
import { AclCategory } from "./api/enums/aclCategory";
import { SharingAccess } from "./api/enums/sharingAccess";
import { AclSubCategory } from "./api/enums/aclSubCategory";
/**
 * Utility to determine if a given IGroup, IItem, IHubContent, or IHubItemEntity
 * is discussable.
 * @param {IGroup|IItem|IHubContent|IHubItemEntity} subject
 * @return {boolean}
 */
export function isDiscussable(subject) {
    let result = false;
    if (subject) {
        const typeKeywords = (subject.typeKeywords || []);
        result = !typeKeywords.includes(CANNOT_DISCUSS);
    }
    return result;
}
/**
 * Adds or removes CANNOT_DISCUSS type keyword and returns the updated list
 * @param {IGroup|IHubContent|IHubItemEntity} subject
 * @param {boolean} discussable
 * @returns {string[]} updated list of type keywords
 */
export function setDiscussableKeyword(typeKeywords, discussable) {
    const updatedTypeKeywords = (typeKeywords || []).filter((typeKeyword) => typeKeyword !== CANNOT_DISCUSS);
    if (!discussable) {
        updatedTypeKeywords.push(CANNOT_DISCUSS);
    }
    return updatedTypeKeywords;
}
/**
 * Determines if the given channel is considered to be a `public` channel, supporting the V2 ACL model.
 * @param channel An IChannel record
 * @returns true if the channel is considered `public`
 */
export function isPublicChannel(channel) {
    return channel.channelAcl.some(({ category }) => [AclCategory.AUTHENTICATED_USER, AclCategory.ANONYMOUS_USER].includes(category));
}
/**
 * Determines if the given channel is considered to be an `org` channel, supporting the V2 ACL model.
 * @param channel An IChannel record
 * @returns true if the channel is considered `org`
 */
export function isOrgChannel(channel) {
    return (!isPublicChannel(channel) &&
        channel.channelAcl.some(({ category, subCategory }) => category === AclCategory.ORG && subCategory === AclSubCategory.MEMBER));
}
/**
 * Determines if the given channel is considered to be a `private` channel, supporting both
 * legacy permissions and V2 ACL model.
 * @param channel An IChannel record
 * @returns true if the channel is considered `private`
 */
export function isPrivateChannel(channel) {
    return !isPublicChannel(channel) && !isOrgChannel(channel);
}
/**
 * Determines the given channel's access, supporting the V2 ACL model.
 * model.
 * @param channel An IChannel record
 * @returns `public`, `org` or `private`
 */
export function getChannelAccess(channel) {
    let access = SharingAccess.PRIVATE;
    if (isPublicChannel(channel)) {
        access = SharingAccess.PUBLIC;
    }
    else if (isOrgChannel(channel)) {
        access = SharingAccess.ORG;
    }
    return access;
}
/**
 * Returns an array of org ids configured for the channel, supporting the V2 ACL model.
 * @param channel An IChannel record
 * @returns an array of org ids for the given channel
 */
export function getChannelOrgIds(channel) {
    return channel.channelAcl.reduce((acc, permission) => permission.category === AclCategory.ORG &&
        [AclSubCategory.MEMBER, AclSubCategory.ADMIN].includes(permission.subCategory) &&
        !acc.includes(permission.key)
        ? [...acc, permission.key]
        : acc, []);
}
/**
 * Returns an array of group ids configured for the channel, supporting the V2 ACL model.
 * @param channel An IChannel record
 * @returns an array of group ids for the given channel
 */
export function getChannelGroupIds(channel) {
    return channel.channelAcl.reduce((acc, permission) => permission.category === AclCategory.GROUP &&
        [AclSubCategory.MEMBER, AclSubCategory.ADMIN].includes(permission.subCategory) &&
        !acc.includes(permission.key)
        ? [...acc, permission.key]
        : acc, []);
}
/**
 * A utility method used to build an IQuery to search for users that are permitted to be at-mentioned for the given channel.
 * @param terms An array of strings to search for. Each string is mapped to `username` and `fullname`, filters as an OR condition
 * @param channel An IChannel record
 * @param currentUsername The currently authenticated user's username
 * @param requestOptions An IRequestOptions object
 * @returns a promise that resolves an IQuery
 */
export async function getChannelUsersQuery(terms, channel, currentUsername, requestOptions) {
    const getNonOwnerAclKeysByCategory = (category, channel) => channel.channelAcl.reduce((acc, channelAcl) => channelAcl.category === category &&
        !acc[channelAcl.subCategory].includes(channelAcl.key)
        ? Object.assign(Object.assign({}, acc), { [channelAcl.subCategory]: [
                ...acc[channelAcl.subCategory],
                channelAcl.key,
            ] }) : acc, { member: [], admin: [] });
    const filters = [];
    if (isPublicChannel(channel)) {
        // any authenticated user when channel access is public
        filters.push({
            operation: "AND",
            predicates: [{ orgid: { from: "0", to: "{" } }],
        });
    }
    else {
        // either org or private
        const filter = {
            operation: "OR",
            predicates: [],
        };
        // process orgs
        const orgIdsBySubCategory = getNonOwnerAclKeysByCategory(AclCategory.ORG, channel);
        // org members
        if (orgIdsBySubCategory.member.length) {
            filter.predicates.push({
                orgid: orgIdsBySubCategory.member,
            });
        }
        // org admins
        if (orgIdsBySubCategory.admin.length) {
            filter.predicates.push({
                orgid: orgIdsBySubCategory.admin,
                role: "org_admin",
            });
        }
        // process groups
        const groupIdsBySubCategory = getNonOwnerAclKeysByCategory(AclCategory.GROUP, channel);
        // group members
        if (groupIdsBySubCategory.member.length) {
            filter.predicates.push({ group: groupIdsBySubCategory.member });
        }
        // group admins
        if (groupIdsBySubCategory.admin.length) {
            // the /community/users endpoint supports searching for users that are members of specific groups via `group`, but there's no way
            // to further refine results to a specific type of membership within that group (e.g. memberType:admin). to do so, we need to fetch the
            // group members via /community/groups/:id/users for each group and then build the appropriate predicates. however, given that group
            // owners & admins will be returned in the results for the member predicate above, we only need to do this for groups whose ID is not
            // also present in the above member predicate
            const adminOnlyGroupIds = groupIdsBySubCategory.admin.filter((groupId) => !groupIdsBySubCategory.member.includes(groupId));
            if (adminOnlyGroupIds.length) {
                // fetch the member list for each channel group
                const groupMemberships = await Promise.all(adminOnlyGroupIds.map((groupId) => 
                // catch & resolve null when an error occurs. depending on the group's visibility settings, the
                // user may, or may not be able to access the group's user list.
                getGroupUsers(groupId, requestOptions).catch((_e) => null)));
                // build the appropriate predicate for each group
                groupMemberships.forEach((groupUserResult, idx) => {
                    // only if the user can access the group's user list
                    if (groupUserResult) {
                        filter.predicates.push({
                            group: adminOnlyGroupIds[idx],
                            // restrict results to the group owner and managers/admins
                            username: [
                                groupUserResult.owner,
                                ...groupUserResult.admins,
                            ].filter(unique),
                        });
                    }
                });
            }
        }
        filters.push(filter);
    }
    // if the user is authenticated, omit that user from the results
    if (currentUsername) {
        filters.push({
            operation: "AND",
            predicates: [{ username: { not: currentUsername } }],
        });
    }
    // conditionally add terms
    const inputFilter = terms.reduce((acc, term) => term
        ? Object.assign(Object.assign({}, acc), { predicates: [
                ...acc.predicates,
                { username: term },
                { fullname: term },
            ] }) : acc, {
        operation: "OR",
        predicates: [],
    });
    if (inputFilter.predicates.length) {
        filters.push(inputFilter);
    }
    return {
        targetEntity: "communityUser",
        filters,
    };
}
/**
 * Transforms a given channel and optional channel groups array into a IHubSearchResult
 * @param channel
 * @param groups
 * @returns
 */
export const channelToSearchResult = (channel, groups) => {
    return Object.assign(Object.assign({}, channel), { id: channel.id, name: channel.name, createdDate: new Date(channel.createdAt), createdDateSource: "channel", updatedDate: new Date(channel.updatedAt), updatedDateSource: "channel", type: "channel", access: getChannelAccess(channel), family: "channel", owner: channel.creator, links: {
            // TODO: add links?
            thumbnail: null,
            self: null,
            siteRelative: null,
        }, includes: { groups }, rawResult: channel });
};
/**
 * Constructs file name for exported csvs
 * @param entityTitle
 * @returns string
 */
export function getPostCSVFileName(entityTitle) {
    const et = entityTitle;
    const suffix = `_${new Date(Date.now())
        .toISOString()
        .replace(/[^a-z0-9]/gi, "-")}.csv`;
    const prefix = et
        // coerce to lower case
        .toLowerCase()
        // replace non-alpha-numeric chars with hyphens
        .replace(/[^a-z0-9]/g, "-")
        // replace consecutive hyphens with single hyphen
        .replace(/-{2,}/g, "-")
        // replace leading hyphens with empty string
        .replace(/^-/, "")
        // truncate filename so it doesn't exceed 250 chars to avoid OS filename length limitations
        .substring(0, 250 - suffix.length)
        // replace trailing hyphens
        .replace(/-$/, "");
    return [prefix, suffix].join("");
}
//# sourceMappingURL=utils.js.map