#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8

import time
from pqai_agent.logging_service import logger

class MessageSenderRateLimiter:
    MAX_CHAR_PER_SECOND = 5

    def __init__(self):
        self.last_send_time = {}

    def wait_for_sending(self, sender_id: str, next_message: str):
        current_time = time.time()
        last_send_time = self.last_send_time.get(sender_id, 0)
        elapsed_time = current_time - last_send_time
        required_time = len(next_message) / self.MAX_CHAR_PER_SECOND
        if elapsed_time < required_time:
            logger.debug(f"Rate limit exceeded. Waiting for {required_time - elapsed_time:.2f} seconds.")
            time.sleep(required_time - elapsed_time)
        current_time = time.time()
        self.last_send_time[sender_id] = current_time
        return