import { getEnrichmentErrors, } from "../items/_enrichments";
import { hubApiRequest } from "../request";
import { cloneObject } from "../util";
import { parseDatasetId } from "./slugs";
import { isMapOrFeatureServerUrl } from "../urls/feature-service-urls";
import { shouldFetchData } from "./_internal/shouldFetchData";
/**
 * get the default list of enrichments to fetch for content
 * @param item
 * @returns the default list of enrichments to fetch for content
 * @private
 */
export const getContentEnrichments = (item) => {
    // we fetch these enrichments for all content types
    const enrichments = [
        "groupIds",
        "metadata",
        "ownerUser",
        "org",
    ];
    // we only fetch data for certain content
    if (shouldFetchData(item)) {
        enrichments.push("data");
    }
    // we fetch server and layers for map and feature services
    return isMapOrFeatureServerUrl(item.url)
        ? enrichments.concat("server", "layers")
        : enrichments;
};
// build up request options to only include the above enrichments
// that we fetch from the Hub API, and to optionally filter by slug
const getHubEnrichmentsOptions = (requestOptions, slug) => {
    const opts = cloneObject(requestOptions);
    opts.params = Object.assign(Object.assign({}, opts.params), { 
        // TODO: we should fetch errors too
        // TODO: stop fetching recordCount at next breaking change
        "fields[datasets]": "slug,boundary,extent,recordCount,searchDescription,statistics" });
    if (slug) {
        opts.params["filter[slug]"] = slug;
    }
    return opts;
};
// extract the ids and enrichments from the Hub API response
const getDatasetEnrichments = (dataset) => {
    const { itemId, layerId: layerIdString } = parseDatasetId(dataset.id);
    const layerId = layerIdString && parseInt(layerIdString, 10);
    const { slug, boundary, extent, recordCount, searchDescription, statistics } = dataset.attributes;
    return {
        itemId,
        layerId,
        slug,
        boundary,
        extent,
        recordCount,
        searchDescription,
        statistics,
    };
};
/**
 * fetch enrichment from the Hub API by slug
 * @param slug
 * @param requestOptions
 * @returns enrichments from the Hub API (slug, boundary, statistic, etc)
 * @private
 */
export const fetchHubEnrichmentsBySlug = async (slug, requestOptions) => {
    // NOTE: we don't catch errors here b/c
    // searching by slug is the first step in fetchContent()
    // and if this fails, we don't have an id to fall back on
    const response = await hubApiRequest(`/datasets`, getHubEnrichmentsOptions(requestOptions, slug));
    return getDatasetEnrichments(response.data[0]);
};
/**
 * fetch enrichment from the Hub API by id
 * @param slug
 * @param requestOptions
 * @returns enrichments from the Hub API (slug, boundary, statistic, etc)
 * @private
 */
export const fetchHubEnrichmentsById = async (hubId, requestOptions) => {
    try {
        const response = await hubApiRequest(`/datasets/${hubId}`, getHubEnrichmentsOptions(requestOptions));
        return getDatasetEnrichments(response.data);
    }
    catch (e) {
        // dataset record not found, just log the error
        // b/c we can still look up the item and enrichments by id
        return { errors: getEnrichmentErrors(e) };
    }
};
//# sourceMappingURL=_fetch.js.map