"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._INTERNAL_FNS = void 0;
exports.searchOgcItems = searchOgcItems;
exports.formatOgcItemsResponse = formatOgcItemsResponse;
exports.formatDiscussionPostTargetEntityResponse = formatDiscussionPostTargetEntityResponse;
exports.formatItemTargetEntityResponse = formatItemTargetEntityResponse;
exports.getNextOgcCallback = getNextOgcCallback;
const getOgcItemQueryParams_1 = require("./getOgcItemQueryParams");
const ogcApiRequest_1 = require("./ogcApiRequest");
const ogcItemToSearchResult_1 = require("./ogcItemToSearchResult");
const ogcItemToDiscussionPostResult_1 = require("./ogcItemToDiscussionPostResult");
exports._INTERNAL_FNS = {
    searchOgcItems,
    formatOgcItemsResponse,
    formatDiscussionPostTargetEntityResponse,
    formatItemTargetEntityResponse,
    getNextOgcCallback,
};
async function searchOgcItems(url, query, options) {
    const queryParams = (0, getOgcItemQueryParams_1.getOgcItemQueryParams)(query, options);
    const rawResponse = (await (0, ogcApiRequest_1.ogcApiRequest)(url, queryParams, options));
    return exports._INTERNAL_FNS.formatOgcItemsResponse(rawResponse, url, query, options);
}
async function formatOgcItemsResponse(response, url, originalQuery, originalOptions) {
    if (originalQuery.targetEntity === "discussionPost") {
        return exports._INTERNAL_FNS.formatDiscussionPostTargetEntityResponse(response, url, originalQuery, originalOptions);
    }
    return formatItemTargetEntityResponse(response, url, originalQuery, originalOptions);
}
async function formatDiscussionPostTargetEntityResponse(response, url, originalQuery, originalOptions) {
    const formattedResults = await Promise.all(response.features.map((f) => (0, ogcItemToDiscussionPostResult_1.ogcItemToDiscussionPostResult)(f)));
    const next = exports._INTERNAL_FNS.getNextOgcCallback(response, url, originalQuery, originalOptions);
    const nextLink = response.links.find((l) => l.rel === "next");
    return {
        total: response.numberMatched,
        results: formattedResults,
        hasNext: !!nextLink,
        next,
    };
}
async function formatItemTargetEntityResponse(response, url, originalQuery, originalOptions) {
    const formattedResults = await Promise.all(response.features.map((f) => (0, ogcItemToSearchResult_1.ogcItemToSearchResult)(f, originalOptions.include, originalOptions.requestOptions)));
    const next = exports._INTERNAL_FNS.getNextOgcCallback(response, url, originalQuery, originalOptions);
    const nextLink = response.links.find((l) => l.rel === "next");
    return {
        total: response.numberMatched,
        results: formattedResults,
        hasNext: !!nextLink,
        next,
    };
}
function getNextOgcCallback(response, url, originalQuery, originalOptions) {
    const nextLink = response.links.find((l) => l.rel === "next");
    let callback = () => null;
    if (nextLink) {
        callback = () => {
            const nextUrl = new URL(nextLink.href);
            const start = +nextUrl.searchParams.get("startindex");
            const nextOptions = Object.assign(Object.assign({}, originalOptions), { start });
            return exports._INTERNAL_FNS.searchOgcItems(url, originalQuery, nextOptions);
        };
    }
    return callback;
}
//# sourceMappingURL=searchOgcItems.js.map