import { getTypeFromEntity } from "../core/getTypeFromEntity";
import { buildCatalog } from "../search/_internal/buildCatalog";
import { getEntityTypeFromType } from "../search/_internal/getEntityTypeFromType";
import { getAssociatedEntitiesQuery } from "./getAssociatedEntitiesQuery";
import { getPendingEntitiesQuery } from "./getPendingEntitiesQuery";
import { getAvailableToRequestEntitiesQuery } from "./getAvailableToRequestEntitiesQuery";
import { getRequestingEntitiesQuery } from "./getRequestingEntitiesQuery";
import { isAssociationSupported } from "./internal/isAssociationSupported";
import { dotifyString, getWellKnownCatalog, getWellknownCollection, } from "../search/wellKnownCatalog";
/**
 * There are two primary UI workflows when we consider associations:
 * 1. Viewing associations
 * 2. Forming associations
 *
 * Because associations involve a 2-way agreement between parent
 * and child, when viewing associations, there are 3 gallery states
 * that can be viewed: "associated", "pending", and "requesting"
 * entities. Additionally, when forming associations, we need a
 * picker experience filtered to entities that can still be
 * requested for association.
 *
 * These define the "well-known" association catalogs that this
 * util can return. In turn, these can be passed into the catalog
 * and/or gallery-picker components to render the appropriate
 * UI experience.
 *
 * @param i18nScope - translation scope to be interpolated into the catalog
 * @param catalogName - name of the well-known catalog requested
 * @param entity - primary entity the catalog is being built for
 * @param associationType - type of entity the primary entity wants to view associations for
 * @param context - contextual auth and portal information
 * @returns {IHubCatalog}
 */
export async function getWellKnownAssociationsCatalog(i18nScope, catalogName, entity, associationType, context) {
    const entityType = getTypeFromEntity(entity);
    const isSupported = isAssociationSupported(entityType, associationType);
    if (!isSupported) {
        throw new Error(`getWellKnownAssociationsCatalog: Association between ${entityType} and ${associationType} is not supported.`);
    }
    i18nScope = dotifyString(i18nScope);
    const targetEntity = getEntityTypeFromType(entity.type);
    /** 1. build a collection based on the provided associationType */
    const collections = [
        getWellknownCollection(i18nScope, targetEntity, associationType),
    ];
    /** 2. build a query based on the provided catalogName */
    let query;
    switch (catalogName) {
        case "associated":
            query = await getAssociatedEntitiesQuery(entity, associationType, context);
            break;
        case "pending":
            query = await getPendingEntitiesQuery(entity, associationType, context);
            break;
        case "requesting":
            query = await getRequestingEntitiesQuery(entity, associationType, context);
            break;
        case "availableToRequest":
            query = getAvailableToRequestEntitiesQuery(entity, associationType);
            break;
    }
    /** 3. build the well-known catalog */
    // if query filters are undefined (e.g. query = null), we assume
    // an empty state, and we need to construct a default query
    // filter that will return no results
    const filters = (query === null || query === void 0 ? void 0 : query.filters)
        ? query.filters
        : [{ predicates: [{ type: ["Code Attachment"] }] }];
    return buildCatalog(i18nScope, catalogName, filters, collections, targetEntity);
}
/**
 * Specific util for building well-known (My content, Favorites,
 * Organization, and World) association catalogs to populate
 * a gallery picker experience for requesting association.
 *
 * In addition to the normal filters that define these well-known
 * catalogs, we also need to further filter the results to only
 * include entities that can still be requested for association.
 *
 * @param i18nScope - translation scope to be interpolated into the catalog
 * @param entity - primary entity the catalog is being built for
 * @param associationType - type of entity the primary entity wants to view associations for
 * @param context - contextual auth and portal information
 * @param catalogs - optional list of well-known catalogs to include
 * @returns {IHubCatalog[]}
 */
export const getAvailableToRequestAssociationCatalogs = (i18nScope, entity, associationType, context, catalogs) => {
    var _a;
    const entityType = getTypeFromEntity(entity);
    const isSupported = isAssociationSupported(entityType, associationType);
    if (!isSupported) {
        throw new Error(`getAvailableToRequestAssociationCatalogs: Association between ${entityType} and ${associationType} is not supported.`);
    }
    i18nScope = dotifyString(i18nScope);
    const filters = (_a = getAvailableToRequestEntitiesQuery(entity, associationType)) === null || _a === void 0 ? void 0 : _a.filters;
    // Default catalogs to include
    const catalogNames = catalogs || [
        "myContent",
        "favorites",
        "organization",
        "world",
    ];
    return catalogNames
        .map((name) => {
        const options = {
            user: context.currentUser,
            filters,
            collectionNames: [associationType],
            context,
        };
        return getWellKnownCatalog(i18nScope, name, "item", options);
    })
        .filter(Boolean);
};
//# sourceMappingURL=wellKnownAssociationCatalogs.js.map