"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestAssociation = void 0;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const getTypeFromEntity_1 = require("../core/getTypeFromEntity");
const fetchHubEntity_1 = require("../core/fetchHubEntity");
const getAssociationHierarchy_1 = require("./internal/getAssociationHierarchy");
const isAssociationSupported_1 = require("./internal/isAssociationSupported");
const setAssociationKeyword_1 = require("./internal/setAssociationKeyword");
const get_prop_1 = require("../objects/get-prop");
const updateHubEntity_1 = require("../core/updateHubEntity");
/**
 * When an entity sends an "outgoing" association request
 * or accepts an "incoming" association request, half of
 * the association "connection" is made.
 *
 * from the parent's perspective: the parent "includes"
 * the child in its association group
 *
 * From the child's perspective: the child "references"
 * the parent via a typeKeyword of the form ref|<parentType>|<parentId>
 *
 * Note: we export this function under 2 names - requestAssociation
 * and acceptAssociation. These actions are functionally equivalent,
 * but we want to make the intent more clear to the consumer.
 *
 * @param entity - entity requesting association
 * @param type - type of the entity the requesting entity wants to associate with
 * @param id - id of the entity the requesting entity wants to associate with
 * @param context - contextual portal and auth information
 */
const requestAssociation = async (entity, associationType, id, context) => {
    const entityType = (0, getTypeFromEntity_1.getTypeFromEntity)(entity);
    const isSupported = (0, isAssociationSupported_1.isAssociationSupported)(entityType, associationType);
    if (!isSupported) {
        throw new Error(`requestAssociation: Association between ${entityType} and ${associationType} is not supported.`);
    }
    const associationHierarchy = (0, getAssociationHierarchy_1.getAssociationHierarchy)(entityType);
    const isParent = associationHierarchy.children.includes(associationType);
    if (isParent) {
        const associationGroupId = (0, get_prop_1.getProp)(entity, "associations.groupId");
        const { owner } = await (0, fetchHubEntity_1.fetchHubEntity)(associationType, id, context);
        try {
            await (0, arcgis_rest_portal_1.shareItemWithGroup)({
                id,
                owner,
                groupId: associationGroupId,
                authentication: context.session,
            });
        }
        catch (err) {
            const error = err;
            throw new Error(`requestAssociation: there was an error sharing ${id} to ${String(associationGroupId)}: ${error}`);
        }
    }
    else {
        entity.typeKeywords = (0, setAssociationKeyword_1.setAssociationKeyword)(entity.typeKeywords, associationType, id);
        await (0, updateHubEntity_1.updateHubEntity)(entityType, entity, context);
    }
};
exports.requestAssociation = requestAssociation;
//# sourceMappingURL=requestAssociation.js.map