import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
mc.world.afterEvents.playerSpawn.subscribe(event => {
    var _a;
    if (event.initialSpawn) {
        const player = new utils.SafetyCheckedPlayer(event.player);
        const version = utils.readDynamicProperty(player, "version");
        /**
         * Version 1.0.0 did not have a version system, so their version value is undefined. However, new players also are undefined so we need to distinguish them.
         * Version 1.0.0 used -1 to indicate empty slots, but this was changed to 0 in 1.1.0+ as utils automatically defaults to 0 if a score is not set.
         * So we check if the player has no version set and that at least one slot index is non-zero.
         * This check could theoretically be a false positive if a player legitimately has a score of 0 in all slots (meaning 30 chicken hearts), but this is unlikely.
         */
        if (!version && !allSlotsZero(player)) {
            // Update their stored version to 1.0.0, they should not recieve a patch note message for 1.1.0 as the minor version is not set to trigger.
            utils.writeDynamicProperty(player, "version", "1.0.0");
            const obtained = [];
            for (let index = 0; index < 30; index++) {
                const score = player.getScore(`Slot_${index}`);
                player.setScore(`Slot_${index}`, score + 1);
                obtained.push([`Obtained_${index + 1}`, player.getScore(`Obtained_${index}`)]);
            }
            (_a = mc.world.scoreboard.getObjective("Obtained_0")) === null || _a === void 0 ? void 0 : _a.removeParticipant(player.scoreboardIdentity);
            for (const [objective, score] of obtained) {
                player.setScore(objective, score);
            }
        }
    }
});
function allSlotsZero(player) {
    for (let index = 0; index < 30; index++) {
        const score = player.getScore(`Slot_${index}`);
        if (score !== 0) {
            return false;
        }
    }
    return true;
}
