"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchAssociatedContent = searchAssociatedContent;
const getTypeFromEntity_1 = require("../core/getTypeFromEntity");
const HubEntityType_1 = require("../core/types/HubEntityType");
const HubError_1 = require("../HubError");
const getOgcCollectionUrl_1 = require("./_internal/hubSearchItemsHelpers/getOgcCollectionUrl");
const searchOgcItems_1 = require("./_internal/hubSearchItemsHelpers/searchOgcItems");
/**
 * Searches for content that is associated with a given entity.
 * Current associations supported are "related" and "connected".
 *
 * If unspecified, the number of results returned is 4.
 *
 * NOTE: only item based entities are supported for now.
 *
 * @returns Search results for the associated content
 */
async function searchAssociatedContent(opts) {
    const { scope, requestOptions, num = 4 } = opts;
    const searchOptions = {
        requestOptions,
        num,
    };
    // Validate that we received an item-based entity
    const entityType = (0, getTypeFromEntity_1.getTypeFromEntity)(opts.entity);
    if (!HubEntityType_1.HUB_ITEM_ENTITY_TYPES.includes(entityType)) {
        throw new HubError_1.default("searchAssociatedContent", `associated content is not supported for entity type "${entityType}"`);
    }
    // For now, we only support searching associated items, not other types of entities
    if (scope.targetEntity !== "item") {
        throw new HubError_1.default("searchAssociatedContent", `associated content scope does not support targetEntity "${scope.targetEntity}"`);
    }
    // The "connected" association requires a layerId
    if (!opts.layerId && opts.association === "connected") {
        throw new HubError_1.default("searchAssociatedContent", `"layerId" is required for searching "connected" content`);
    }
    // Calculate the hubId
    const { entity, layerId } = opts;
    const hubId = layerId ? `${entity.id}_${layerId}` : entity.id;
    // Construct the OGC API URL
    const { association } = opts;
    const collectionUrl = (0, getOgcCollectionUrl_1.getOgcCollectionUrl)(scope, searchOptions);
    const associationUrl = `${collectionUrl}/items/${hubId}/${association}`;
    return (0, searchOgcItems_1.searchOgcItems)(associationUrl, scope, searchOptions);
}
//# sourceMappingURL=searchAssociatedContent.js.map