"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextPortalCallback = getNextPortalCallback;
const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
const util_1 = require("../../../util");
const get_prop_1 = require("../../../objects/get-prop");
/**
 * @private
 * Create a `.next()` function for portal search type
 * @param request
 * @param nextStart
 * @param total
 * @param fn
 * @returns
 */
function getNextPortalCallback(request, nextStart, total, fn) {
    const clonedRequest = (0, util_1.cloneObject)(request);
    // clone will not handle authentication so we do it manually
    if (request.authentication) {
        clonedRequest.authentication = arcgis_rest_request_1.ArcGISIdentityManager.deserialize(request.authentication.serialize());
    }
    // do the same workaround for requestOptions.authentication
    if ((0, get_prop_1.getProp)(request, "requestOptions.authentication")) {
        clonedRequest.requestOptions.authentication =
            arcgis_rest_request_1.ArcGISIdentityManager.deserialize(request.requestOptions.authentication.serialize());
    }
    // figure out the start
    clonedRequest.start = nextStart > -1 ? nextStart : total + 1;
    return () => fn(clonedRequest);
}
//# sourceMappingURL=getNextPortalCallback.js.map