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