"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hubSearch = hubSearch;
const tslib_1 = require("tslib");
const HubError_1 = require("../HubError");
const get_prop_1 = require("../objects/get-prop");
const util_1 = require("../util");
const getApi_1 = require("./_internal/commonHelpers/getApi");
const portalSearchGroupMembers_1 = require("./_internal/portalSearchGroupMembers");
const portalSearchItems_1 = require("./_internal/portalSearchItems");
const portalSearchGroups_1 = require("./_internal/portalSearchGroups");
const portalSearchUsers_1 = require("./_internal/portalSearchUsers");
const hubSearchItems_1 = require("./_internal/hubSearchItems");
const hubSearchChannels_1 = require("./_internal/hubSearchChannels");
const hubSearchEvents_1 = require("./_internal/hubSearchEvents");
const hubSearchEventAttendees_1 = require("./_internal/hubSearchEventAttendees");
const portalFetchOrgs_1 = require("./_internal/portalFetchOrgs");
const hubSearchPosts_1 = require("./_internal/hubSearchPosts");
/**
 * Main entrypoint for searching via Hub
 *
 * Default's to search ArcGIS Portal but can delegate
 * to Hub API when it's available.
 * @param query
 * @param options
 * @returns
 */
async function hubSearch(query, options) {
    // Validate inputs
    if (!query) {
        throw new HubError_1.default("hubSearch", "Query is required.");
    }
    if (!Array.isArray(query.filters)) {
        throw new HubError_1.default("hubSearch", "Query must have a filters array.");
    }
    if (!query.filters.length) {
        throw new HubError_1.default("hubSearch", "Query must contain at least one Filter.");
    }
    if (!options.requestOptions) {
        throw new HubError_1.default("hubSearch", "requestOptions: IHubRequestOptions is required.");
    }
    // Ensure includes is an array
    if (!options.include) {
        options.include = [];
    }
    // Get the type of the first filterGroup
    const filterType = query.targetEntity;
    // NOTE: We want to clone the `options` object to do some expansion operations,
    // But if we clone `options.requestOptions`, the underlying `ArcGISIdentityManager` will
    // lose some fundamental functions like `getToken`. As a workaround, we just
    // clone everything else on the `options` object.
    const { requestOptions } = options, remainder = tslib_1.__rest(options, ["requestOptions"]);
    const formattedOptions = (0, util_1.cloneObject)(remainder);
    formattedOptions.requestOptions = requestOptions;
    formattedOptions.api = (0, getApi_1.getApi)(filterType, formattedOptions);
    const fnHash = {
        portal: {
            item: portalSearchItems_1.portalSearchItems,
            group: portalSearchGroups_1.portalSearchGroups,
            user: portalSearchUsers_1.searchPortalUsersLegacy,
            portalUser: portalSearchUsers_1.searchPortalUsers,
            communityUser: portalSearchUsers_1.searchCommunityUsers,
            groupMember: portalSearchGroupMembers_1.portalSearchGroupMembers,
            organization: portalFetchOrgs_1.portalFetchOrgs,
        },
        hub: {
            item: hubSearchItems_1.hubSearchItems,
            channel: hubSearchChannels_1.hubSearchChannels,
            discussionPost: hubSearchItems_1.hubSearchItems,
            event: hubSearchEvents_1.hubSearchEvents,
            eventAttendee: hubSearchEventAttendees_1.hubSearchEventAttendees,
            post: hubSearchPosts_1.hubSearchPosts,
        },
    };
    const fn = (0, get_prop_1.getProp)(fnHash, `${formattedOptions.api}.${filterType}`);
    if (!fn) {
        throw new HubError_1.default(`hubSearch`, `Search via "${filterType}" filter against "${formattedOptions.api}" api is not implemented. Please ensure "targetEntity" is defined on the query.`);
    }
    return fn((0, util_1.cloneObject)(query), formattedOptions);
}
//# sourceMappingURL=hubSearch.js.map