"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPendingEntitiesQuery = void 0;
const getTypeFromEntity_1 = require("../core/getTypeFromEntity");
const getAssociationHierarchy_1 = require("./internal/getAssociationHierarchy");
const getReferencesDoesNotIncludeQuery_1 = require("./internal/getReferencesDoesNotIncludeQuery");
const getIncludesDoesNotReferenceQuery_1 = require("./internal/getIncludesDoesNotReferenceQuery");
const isAssociationSupported_1 = require("./internal/isAssociationSupported");
/**
 * Pending entities represent "outgoing" requests that are
 * awaiting "approval". They imply a one-way "connection"
 * between parent/child.
 *
 * From the parent's perspective:
 * parent: "includes" the child in its association query
 * child: does NOT "reference" the parent via a typeKeyword
 *
 * From the child's perspective:
 * parent: does NOT "include" the child in its association query
 * child: "references" the parent via a typeKeyword of the
 * form ref|<parentType>|<parentID>
 *
 * The following returns a query to view an entity's outgoing
 * requests for association with another entity type
 *
 * @param entity - Hub entity
 * @param associationType - entity type to query for
 * @param context - contextual auth and portal information
 * @returns {IQuery}
 */
const getPendingEntitiesQuery = async (entity, associationType, context) => {
    const entityType = (0, getTypeFromEntity_1.getTypeFromEntity)(entity);
    const isSupported = (0, isAssociationSupported_1.isAssociationSupported)(entityType, associationType);
    if (!isSupported) {
        throw new Error(`getPendingEntitiesQuery: Association between ${entityType} and ${associationType} is not supported.`);
    }
    const associationHierarchy = (0, getAssociationHierarchy_1.getAssociationHierarchy)(entityType);
    const isParent = associationHierarchy.children.includes(associationType);
    const query = isParent
        ? await (0, getIncludesDoesNotReferenceQuery_1.getIncludesDoesNotReferenceQuery)(entity, associationType, isParent, context)
        : await (0, getReferencesDoesNotIncludeQuery_1.getReferencesDoesNotIncludeQuery)(entity, associationType, isParent, context);
    return query;
};
exports.getPendingEntitiesQuery = getPendingEntitiesQuery;
//# sourceMappingURL=getPendingEntitiesQuery.js.map