import { getItem } from "@esri/arcgis-rest-portal";
import { fetchEvent } from "../events/fetch";
import HubError from "../HubError";
import { fetchSite } from "../sites/HubSites";
import { lookupDomain } from "../sites/domains/lookup-domain";
import { getHubTypeFromItemType } from "../core/getHubTypeFromItemType";
import { fetchHubEntity } from "../core/fetchHubEntity";
import { getProp } from "../objects/get-prop";
import { stripProtocol } from "../urls/strip-protocol";
import { isCuid } from "../utils/is-cuid";
import { isGuid } from "../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
 */
export 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 = 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 lookupDomain(url, context.hubRequestOptions);
        const site = await fetchSite(domainEntry.siteId, context.hubRequestOptions);
        catalog = getProp(site, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else if (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 getItem(identifier, context.requestOptions);
            entityType = getHubTypeFromItemType(item.type);
        }
        const entity = await fetchHubEntity(entityType, identifier, context);
        catalog = getProp(entity, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else if (isCuid(identifier)) {
        // get the entity, use that to get the type, and then get the entity
        // and return the catalog
        const event = await fetchEvent(identifier, context.hubRequestOptions);
        catalog = getProp(event, (options === null || options === void 0 ? void 0 : options.prop) || "catalog");
    }
    else {
        throw new HubError("Catalog.init", "Identifier must be a url, item or event id");
    }
    return catalog;
}
//# sourceMappingURL=fetchEntityCatalog.js.map