"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupportUserService = void 0;
class SupportUserService {
    userRepository;
    logger;
    constructor(userRepository, logger) {
        this.userRepository = userRepository;
        this.logger = logger;
    }
    async findOrCreate(input) {
        const existing = await this.userRepository.findByTelegramUserId(input.telegramUserId);
        if (existing) {
            if (existing.chatId !== input.chatId ||
                existing.username !== input.username ||
                existing.firstName !== input.firstName ||
                existing.lastName !== input.lastName) {
                return ((await this.userRepository.update(existing.id, {
                    chatId: input.chatId,
                    username: input.username,
                    firstName: input.firstName,
                    lastName: input.lastName,
                    language: input.language ?? existing.language
                })) ?? existing);
            }
            return existing;
        }
        this.logger.info("Creating support user.", { telegramUserId: input.telegramUserId });
        return this.userRepository.create(input);
    }
    async getById(id) {
        return this.userRepository.findById(id);
    }
    async getByTelegramUserId(telegramUserId) {
        return this.userRepository.findByTelegramUserId(telegramUserId);
    }
}
exports.SupportUserService = SupportUserService;
