var HuSNS = window.HuSNS || {};
HuSNS.Toast = {
    _container: null,
    _timer: null,
    
    init: function() {
        if (!this._container) {
            this._container = document.createElement('div');
            this._container.className = 'husns-toast-container';
            document.body.appendChild(this._container);
        }
    },
    
    show: function(message, type = 'info', duration = 2000) {
        this.init();
        
        if (this._timer) {
            clearTimeout(this._timer);
        }
        
        while (this._container.firstChild) {
            this._container.removeChild(this._container.firstChild);
        }
        
        var toast = document.createElement('div');
        toast.className = 'husns-toast husns-toast-' + type;
        toast.textContent = message;
        
        this._container.appendChild(toast);
        
        setTimeout(function() {
            toast.classList.add('show');
        }, 10);
        
        this._timer = setTimeout(function() {
            toast.classList.remove('show');
            setTimeout(function() {
                if (toast.parentNode) {
                    toast.parentNode.removeChild(toast);
                }
            }, 300);
        }, duration);
    },
    
    hide: function() {
        if (this._timer) {
            clearTimeout(this._timer);
        }
        if (this._container) {
            while (this._container.firstChild) {
                this._container.removeChild(this._container.firstChild);
            }
        }
    },
    
    success: function(message, duration) {
        this.show(message, 'success', duration);
    },
    
    error: function(message, duration) {
        this.show(message, 'error', duration);
    },
    
    warning: function(message, duration) {
        this.show(message, 'warning', duration);
    }
};

if (typeof module !== 'undefined' && module.exports) {
    module.exports = HuSNS.Toast;
}