import * as mc from "@minecraft/server";
import * as utils from "./utils/index";
import { PlayerCompanion } from "./playerCompanion/index";
import { Companion, CompanionBackupComponent } from "./npc/index";
export function InitializeCraftee() {
    PlayerCompanion.instantiateNPC({ type: "ldz_crcomp:craftee" }, PlayerCompanion);
    givePlayersStartingItems();
    mc.world.afterEvents.entitySpawn.subscribe(crafteeSpawn);
    mc.world.afterEvents.itemUse.subscribe(displayGuideBook);
}
async function givePlayersStartingItems() {
    await utils.waitForWorldLoad();
    utils.getAllPlayersSafe().forEach(setupPlayer);
    mc.world.afterEvents.playerSpawn.subscribe(event => {
        if (event.initialSpawn) {
            setupPlayer(new utils.SafetyCheckedPlayer(event.player));
        }
    });
    async function setupPlayer(player) {
        var _a;
        if (CompanionBackupComponent.restoreBackup(player))
            return;
        await utils.waitForTicks(5);
        if (Companion.hasCompanion(player, { type: "ldz_crcomp:craftee" }) || player.runCommand("testfor @s[hasitem={item=ldz_crcomp:craftee_spawn_egg,quantity=1..}]").successCount)
            return;
        if (utils.getInventoryContainer(player).emptySlotsCount < 2) {
            player.sendMessage({ translate: "ldz_crcomp:craftee.inventory_full" });
            player.playSound("ldz_crcomp:craftee.inventory_full");
        }
        await utils.waitForLoopEvent(5, () => utils.getInventoryContainer(player).emptySlotsCount >= 2);
        const spawnItem = new mc.ItemStack("ldz_crcomp:craftee_spawn_egg", 1);
        spawnItem.keepOnDeath = true;
        (_a = utils.getInventoryContainer(player)) === null || _a === void 0 ? void 0 : _a.addItem(spawnItem);
        player.runCommand('structure load "ldz_crcomp/book" ~ ~ ~');
    }
}
function crafteeSpawn(event) {
    if (event.entity.typeId === utils.addName("craftee")) {
        const subevent = (event) => {
            const spawnEggId = utils.addName("craftee_spawn_egg");
            if (event.itemStack.typeId === spawnEggId) {
                const player = new utils.SafetyCheckedPlayer(event.source);
                player.runCommand(`clear @s ${spawnEggId} 0 1`);
                const inventory = utils.getInventoryContainer(player);
                const tameItem = new mc.ItemStack(utils.addName("friendship_bracelet"), 1);
                tameItem.keepOnDeath = true;
                if (inventory.emptySlotsCount >= 1) {
                    inventory.addItem(tameItem);
                }
                else {
                    player.dimension.spawnItem(tameItem, player.location);
                }
                mc.world.afterEvents.itemUseOn.unsubscribe(subevent);
            }
        };
        mc.world.afterEvents.itemUseOn.subscribe(subevent);
        utils.waitForEvent(1, () => mc.world.afterEvents.itemUseOn.unsubscribe(subevent));
    }
}
function displayGuideBook(event) {
    if (event.itemStack.typeId === utils.GlobalNamespace.Namespace + "instruction_book") {
        GUIDE_BOOK.showMenuToPlayer(new utils.SafetyCheckedPlayer(event.source));
    }
}
const GUIDE_BOOK = new utils.ButtonMenu({ translate: "ldz_crcomp:book.title" }, [
    {
        text: { translate: "ldz_crcomp:book.close" },
        onClick() { },
    }
], {
    rawtext: [
        { translate: "ldz_crcomp:book.page.1", with: ["\n"] },
        { text: "\n\n" },
        { translate: "ldz_crcomp:book.page.2", with: ["\n"] },
    ]
});
