import { EntityComponentTypes, ItemStack, system, world } from "@minecraft/server";
const DP_NAME = `oct_db:is_not_new_player`;
export function initOnJoin() {
    world.afterEvents.playerSpawn.subscribe(result => {
        // Refresh animations if player first joins
        if (result.initialSpawn) {
            const interval = system.runInterval(() => {
                if (!result?.player?.isValid())
                    return;
                result.player.refreshLimbHealth();
                system.clearRun(interval);
            });
        }
        if (!isPlayerNew(result.player))
            return;
        givePlayerGuidebook(result.player);
        givePlayerMonitor(result.player);
        // Set player as not new
        result.player.setDynamicProperty(DP_NAME, true);
    });
    // DEBUG
    system.afterEvents.scriptEventReceive.subscribe(result => {
        if (result.id === "oct_db:join" && result.message === "reset") {
            result.sourceEntity.setDynamicProperty(DP_NAME, undefined);
        }
    });
}
;
function givePlayerGuidebook(player) {
    // Get guidebook
    const guidebook = new ItemStack(`oct_db:guidebook`, 1);
    // Give to player
    const invenComp = player.getComponent(EntityComponentTypes.Inventory);
    if (invenComp.container.emptySlotsCount > 0) {
        invenComp.container.addItem(guidebook);
    }
    else {
        player.dimension.spawnItem(guidebook, player.location);
    }
}
;
export function givePlayerMonitor(player) {
    // Get guidebook
    const monitor = new ItemStack(`oct_db:health_monitor.healthy`, 1);
    // Give to player
    const invenComp = player.getComponent(EntityComponentTypes.Inventory);
    if (invenComp.container.emptySlotsCount > 0) {
        invenComp.container.addItem(monitor);
    }
    else {
        player.dimension.spawnItem(monitor, player.location);
    }
}
;
function isPlayerNew(player) {
    return (player.getDynamicProperty(DP_NAME) === undefined);
}
;
