/**
 * Game Iframe SDK - Message Sender
 * Gửi message đến iframe
 */
import { MESSAGE_TYPES } from './types';
/**
 * MessageSender - Gửi messages đến iframe
 */
export class MessageSender {
    constructor(config) {
        this.iframe = null;
        this.config = config;
    }
    /**
     * Set iframe element
     */
    setIframe(iframe) {
        this.iframe = iframe;
        return this;
    }
    /**
     * Get current iframe
     */
    getIframe() {
        return this.iframe;
    }
    /**
     * Check if iframe is available
     */
    isReady() {
        return !!this.iframe?.contentWindow;
    }
    /**
     * Send raw message to iframe
     */
    sendRaw(message) {
        if (!this.iframe?.contentWindow) {
            return {
                success: false,
                error: new Error('Iframe not available'),
            };
        }
        try {
            this.iframe.contentWindow.postMessage(message, this.config.targetOrigin);
            this.log('Sent message', { type: message.type });
            return { success: true };
        }
        catch (error) {
            const err = error;
            this.log('Send failed', { error: err.message });
            return { success: false, error: err };
        }
    }
    /**
     * Send game data (SERVER_PUSH_DATA)
     */
    sendGameData(payload) {
        // Inline message creation
        const message = {
            type: MESSAGE_TYPES.SERVER_PUSH_DATA,
            jsonData: payload,
        };
        const result = this.sendRaw(message);
        if (result.success) {
            const dataLength = payload.data?.length || 0;
            this.log('Sent game data', {
                game_id: payload.game_id,
                items: dataLength,
            });
        }
        return result;
    }
    /**
     * Send leaderboard (SERVER_PUSH_LEADERBOARD)
     */
    sendLeaderboard(data) {
        // Inline message creation
        const message = {
            type: MESSAGE_TYPES.SERVER_PUSH_LEADERBOARD,
            leaderboardData: data,
        };
        const result = this.sendRaw(message);
        if (result.success) {
            this.log('Sent leaderboard', {
                players: data.top_players?.length || 0,
                hasUserRank: !!data.user_rank,
            });
        }
        return result;
    }
    /**
     * Reload iframe
     */
    reloadIframe() {
        if (!this.iframe) {
            return false;
        }
        const currentSrc = this.iframe.src;
        if (!currentSrc || currentSrc === 'about:blank') {
            return false;
        }
        this.iframe.src = '';
        setTimeout(() => {
            if (this.iframe) {
                this.iframe.src = currentSrc;
                this.log('Iframe reloaded');
            }
        }, 100);
        return true;
    }
    /**
     * Debug log
     */
    log(message, data) {
        if (this.config.debug) {
            console.log('[MessageSender]', message);
            if (data) {
                try {
                    console.log(JSON.stringify(data, null, 2));
                }
                catch (e) {
                    console.log(data);
                }
            }
        }
    }
}
//# sourceMappingURL=MessageSender.js.map