"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAssociatedEntitiesQuery = void 0;
const getTypeFromEntity_1 = require("../core/getTypeFromEntity");
const getAssociationHierarchy_1 = require("./internal/getAssociationHierarchy");
const isAssociationSupported_1 = require("./internal/isAssociationSupported");
const getIncludesAndReferencesQuery_1 = require("./internal/getIncludesAndReferencesQuery");
/**
 * Associated entities are those which have mutually
 * "agreed" to be connected with one another. They
 * require a two-way "connection" between parent/child:
 *
 * parent: "includes" 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
 * associations 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 getAssociatedEntitiesQuery = async (entity, associationType, context) => {
    const entityType = (0, getTypeFromEntity_1.getTypeFromEntity)(entity);
    const isSupported = (0, isAssociationSupported_1.isAssociationSupported)(entityType, associationType);
    if (!isSupported) {
        throw new Error(`getAssociatedEntitiesQuery: Association between ${entityType} and ${associationType} is not supported.`);
    }
    const associationHierarchy = (0, getAssociationHierarchy_1.getAssociationHierarchy)(entityType);
    const isParent = associationHierarchy.children.includes(associationType);
    const query = await (0, getIncludesAndReferencesQuery_1.getIncludesAndReferencesQuery)(entity, associationType, isParent, context);
    return query;
};
exports.getAssociatedEntitiesQuery = getAssociatedEntitiesQuery;
//# sourceMappingURL=getAssociatedEntitiesQuery.js.map