"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchEntityCatalog = fetchEntityCatalog;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const fetch_1 = require("../events/fetch");
const HubError_1 = require("../HubError");
const HubSites_1 = require("../sites/HubSites");
const lookup_domain_1 = require("../sites/domains/lookup-domain");
const getHubTypeFromItemType_1 = require("../core/getHubTypeFromItemType");
const fetchHubEntity_1 = require("../core/fetchHubEntity");
const get_prop_1 = require("../objects/get-prop");
const strip_protocol_1 = require("../urls/strip-protocol");
const is_cuid_1 = require("../utils/is-cuid");
const is_guid_1 = require("../utils/is-guid");
/**
 * Fetch a Catalog for an entity.
 * This function fetches the entity and returns the `.catalog` property
 * However, other properties can be fetched by passing the name of the property
 * in the `prop` option.
 * Additionally, passing the `hubEntityType` option provide a performance boost
 * by skipping the fetch of the backing Item, then fetching the entity.
 * @param identifier
 * @param context
 * @param options { hubEntityType?: string; prop: string }
 * @returns
 */
async function fetchEntityCatalog(identifier, context, options) {
    let catalog;
    // identifier can be a guid or a url
    if (identifier.indexOf("http") === 0) {
        // Handle Sites via Url
        let url = identifier;
        // get down the the hostname
        url = (0, strip_protocol_1.stripProtocol)(url);
        // if url does not include a hash (i.e. it's not portal)
        // then we want to split on the first slash to get the hostname
        // lookupDomain will handle the enterprise base urls
        if (!url.includes("#")) {
            url = url.split("/")[0];
        }
        const domainEntry = await (0, lookup_domain_1.lookupDomain)(url, context.hubRequestOptions);
        const site = await (0, HubSites_1.fetchSite)(domainEntry.siteId, context.hubRequestOptions);
        catalog = (0, get_prop_1.getProp)(site, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else if ((0, is_guid_1.isGuid)(identifier)) {
        // Handle Item Backed entities
        let entityType;
        if (options === null || options === void 0 ? void 0 : options.hubEntityType) {
            entityType = options.hubEntityType;
        }
        else {
            const item = await (0, arcgis_rest_portal_1.getItem)(identifier, context.requestOptions);
            entityType = (0, getHubTypeFromItemType_1.getHubTypeFromItemType)(item.type);
        }
        const entity = await (0, fetchHubEntity_1.fetchHubEntity)(entityType, identifier, context);
        catalog = (0, get_prop_1.getProp)(entity, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else if ((0, is_cuid_1.isCuid)(identifier)) {
        // get the entity, use that to get the type, and then get the entity
        // and return the catalog
        const event = await (0, fetch_1.fetchEvent)(identifier, context.hubRequestOptions);
        catalog = (0, get_prop_1.getProp)(event, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else {
        throw new HubError_1.default("Catalog.init", "Identifier must be a url, item or event id");
    }
    return catalog;
}
//# sourceMappingURL=fetchEntityCatalog.js.map