"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activityPubCollectionPagination = void 0;
const tslib_1 = require("tslib");
const validator_1 = tslib_1.__importDefault(require("validator"));
const core_utils_1 = require("../../helpers/core-utils");
const constants_1 = require("../../initializers/constants");
const core_utils_2 = require("../../../shared/core-utils");
function activityPubCollectionPagination(baseUrl, handler, page, size = constants_1.ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!page || !validator_1.default.isInt(page)) {
            const result = yield handler(0, 1);
            return {
                id: baseUrl,
                type: 'OrderedCollection',
                totalItems: result.total,
                first: result.data.length === 0
                    ? undefined
                    : baseUrl + '?page=1'
            };
        }
        const { start, count } = (0, core_utils_1.pageToStartAndCount)(page, size);
        const result = yield handler(start, count);
        let next;
        let prev;
        page = (0, core_utils_2.forceNumber)(page);
        if (result.total > page * size) {
            next = baseUrl + '?page=' + (page + 1);
        }
        if (page > 1) {
            prev = baseUrl + '?page=' + (page - 1);
        }
        return {
            id: baseUrl + '?page=' + page,
            type: 'OrderedCollectionPage',
            prev,
            next,
            partOf: baseUrl,
            orderedItems: result.data,
            totalItems: result.total
        };
    });
}
exports.activityPubCollectionPagination = activityPubCollectionPagination;
//# sourceMappingURL=collection.js.map