"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetChatsByPageQueryHandler = void 0;
const ChatMapper_1 = require("../../DTOs/Mappers/ChatMapper");
const Logger_1 = require("../../Services/Logger");
class GetChatsByPageQueryHandler {
    constructor(chatRepo) {
        this.chatRepo = chatRepo;
    }
    async execute(query) {
        try {
            // Validate pagination parameters
            if (query.from < 0 || query.to < query.from) {
                throw new Error('Invalid pagination parameters');
            }
            const limit = query.to - query.from + 1;
            if (limit > 100) {
                throw new Error('Page size too large. Maximum 100 records per request');
            }
            (0, Logger_1.logRequest)('Get chats by page query started', undefined, undefined, {
                from: query.from,
                to: query.to,
                includeDeleted: query.includeDeleted || false
            });
            const result = query.includeDeleted
                ? await this.chatRepo.findByPageIncludingDeleted(query.from, query.to)
                : await this.chatRepo.findByPage(query.from, query.to);
            (0, Logger_1.logRequest)('Get chats by page query completed', undefined, undefined, {
                from: query.from,
                to: query.to,
                returned: result.chats.length,
                totalCount: result.totalCount,
                includeDeleted: query.includeDeleted || false
            });
            return {
                chats: ChatMapper_1.ChatMapper.toShortDtoList(result.chats),
                totalCount: result.totalCount
            };
        }
        catch (error) {
            (0, Logger_1.logError)('GetChatsByPageQueryHandler error', error instanceof Error ? error : new Error(String(error)));
            // Re-throw validation errors as-is
            if (error instanceof Error && (error.message.includes('Invalid pagination') || error.message.includes('Page size'))) {
                throw error;
            }
            throw new Error('Failed to retrieve chats page');
        }
    }
}
exports.GetChatsByPageQueryHandler = GetChatsByPageQueryHandler;
//# sourceMappingURL=GetChatsByPageQueryHandler.js.map