import { world, system } from '@minecraft/server';
/**
 * Simple database class for player data using scoreboard objectives
 * Stores player data as JSON strings under fake players
 */
export class PlayerData {
    objectiveName;
    objective;
    constructor(objectiveName = "player_data_db") {
        this.objectiveName = objectiveName;
        this.initializeObjective();
    }
    /**
     * Initialize the scoreboard objective
     */
    initializeObjective() {
        system.run(() => {
            try {
                this.objective = world.scoreboard.getObjective(this.objectiveName);
            }
            catch (error) {
                try {
                    this.objective = world.scoreboard.addObjective(this.objectiveName, "Player Data DB");
                }
                catch (createError) {
                    // Failed to create scoreboard
                }
            }
        });
    }
    /**
     * Set player data - saves as JSON string under fake player in scoreboard
     */
    set(player, data) {
        if (!this.objective) {
            return false;
        }
        let success = false;
        system.run(() => {
            try {
                const playerData = {
                    user: player.name,
                    data: data
                };
                // Stringify the data - this becomes the fake player name
                const serializedData = JSON.stringify(playerData);
                // Set the JSON string as the fake player name with score 0
                this.objective.setScore(serializedData, 0);
                success = true;
            }
            catch (error) {
                console.error(`Failed to set player data for ${player}: ${error}`);
            }
        });
        return success;
    }
    /**
     * Get player data - retrieves and parses JSON string from fake player
     */
    get(player) {
        if (!this.objective) {
            return null;
        }
        let result = null;
        system.run(() => {
            try {
                // Get all participants and find the one for this user
                const participants = this.objective.getParticipants();
                for (const participant of participants) {
                    try {
                        // Parse the display name as JSON
                        const playerData = JSON.parse(participant.displayName);
                        // Check if this is the user we're looking for
                        if (playerData.user === player.name) {
                            result = playerData;
                            return;
                        }
                    }
                    catch {
                        // Skip invalid JSON entries
                        continue;
                    }
                }
            }
            catch (error) {
                console.error(`Failed to get player data for ${player}: ${error}`);
            }
        });
        return result;
    }
    /**
     * Check if player data exists
     */
    exists(player) {
        return this.get(player) !== null;
    }
    /**
     * Delete player data
     */
    delete(player) {
        if (!this.objective)
            return false;
        let success = false;
        system.run(() => {
            try {
                // Find and remove the participant for this user
                const participants = this.objective.getParticipants();
                for (const participant of participants) {
                    try {
                        const playerData = JSON.parse(participant.displayName);
                        if (playerData.user === player.name) {
                            this.objective.removeParticipant(participant.displayName);
                            success = true;
                            return;
                        }
                    }
                    catch {
                        continue;
                    }
                }
            }
            catch (error) {
                console.error(`Failed to delete player data for ${player}: ${error}`);
            }
        });
        return success;
    }
    /**
     * Get all player players that have data
     */
    getAllPlayers() {
        if (!this.objective)
            return [];
        const players = [];
        system.run(() => {
            try {
                const participants = this.objective.getParticipants();
                for (const participant of participants) {
                    try {
                        const playerData = JSON.parse(participant.displayName);
                        players.push(playerData.user);
                    }
                    catch {
                        // Skip invalid JSON entries
                        continue;
                    }
                }
            }
            catch (error) {
                console.error(`Failed to get all players: ${error}`);
            }
        });
        return players;
    }
}
