var _a;
import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import * as comp from "./components/index";
export class BotBase extends utils.Actor {
    constructor() {
        super(...arguments);
        this.spawnEggEvent = undefined;
    }
    get SpawnEggEvent() {
        return this.spawnEggEvent;
    }
    get Fuel() {
        var _b, _c;
        return (_c = (_b = this.getActorComponent(comp.BotFuelComponent)) === null || _b === void 0 ? void 0 : _b.Fuel) !== null && _c !== void 0 ? _c : 0;
    }
    set Fuel(v) {
        const component = this.getActorComponent(comp.BotFuelComponent);
        if (component)
            component.Fuel = v;
    }
    get EffectExceptions() {
        return {};
    }
    onConstructed() {
        this.addActorComponent(utils.InteractComponent);
        this.addActorComponent(comp.BotHealthComponent);
        this.addActorComponent(comp.FuelOnSpawnComponent);
        this.addActorComponent(comp.MobEffectResistanceComponent, { exceptions: this.EffectExceptions });
        this.runCommand('execute as @s[tag=!ldz_240927:spawn_sound] run playsound ldz_240927:entity.bot.spawn @a ~ ~ ~');
        this.runCommand('tag @s add ldz_240927:spawn_sound');
        if (BotBase.eggQueue.has(this.id)) {
            this.spawnEggEvent = BotBase.eggQueue.get(this.id);
            utils.waitForEvent(1, () => this.raiseEvent("spawned_from_egg"));
            BotBase.eggQueue.delete(this.id);
        }
    }
    onInteract(player) {
        var _b;
        utils.mclog(`${player.name} interacted with ${this.ActorId} ${this.id}`);
        (_b = this.getActorComponent(comp.BotFuelComponent)) === null || _b === void 0 ? void 0 : _b.fuelInteraction(player);
    }
    selfDestruct() {
        this.runCommand(`stopsound @a ldz_240927:entity.bot.power_down`);
        this.dimension.playSound(utils.addName("entity.bot.destroy"), this.location);
        this.dropItems();
        this.remove();
    }
    setItemLore(item, fuel, percent) {
        const tags = this.getTags();
        let itemType = 'fuel';
        tags.forEach(tag => {
            if (tag.includes('fuelType ')) {
                itemType = tag.replace('fuelType ', '').replace('minecraft:', '').replace("_", " ");
                if (itemType.includes("arrow")) {
                    itemType = itemType.replace("arrow", "arrows"); // makes the word arrow plural
                }
            }
        });
        const ITEMS_WITH_FUEL = {
            "ldz_240927:drill_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:guard_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:hover_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:lumber_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:medic_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:miner_bot_dummy_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:portal_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:sprinkler_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:torch_bot_spawn_egg": [comp.loreLine + percent + "%", comp.lineColor + fuel + " " + itemType + " stored"],
            "ldz_240927:angler_bot_spawn_egg": [comp.botThatDoesntUseFuel],
            "ldz_240927:scuffle_bot_spawn_egg": [comp.botThatDoesntUseFuel],
            "ldz_240927:time_bot_spawn_egg": [comp.botThatDoesntUseFuel],
            "ldz_240927:vacuum_bot_spawn_egg": [comp.botThatDoesntUseFuel],
            "ldz_240927:weather_bot_spawn_egg": [comp.botThatDoesntUseFuel],
        };
        const lore = ITEMS_WITH_FUEL[item.typeId];
        if (lore) {
            item.setLore(lore);
        }
    }
    dropItems() {
        var _b, _c;
        let item;
        if (this.typeId === utils.addName("miner_bot")) {
            item = new mc.ItemStack(this.typeId + "_dummy_spawn_egg", 1);
        }
        else {
            item = new mc.ItemStack(this.typeId + "_spawn_egg", 1);
        }
        const fuel = this.Fuel;
        //this.Fuel -= fuel;
        const max = (_c = (_b = this.getActorComponent(comp.BotFuelComponent)) === null || _b === void 0 ? void 0 : _b.max) !== null && _c !== void 0 ? _c : 0;
        const percent = Math.floor(utils.normalize(0, max, fuel) * 100);
        this.setItemLore(item, fuel, percent);
        this.dimension.spawnItem(item, this.location);
        if (this.Inventory) {
            for (let index = 0; index < this.Inventory.size; index++) {
                const item = this.Inventory.getItem(index);
                if (item)
                    this.dimension.spawnItem(item, this.location);
            }
        }
        const virtualInventory = this.getActorComponent(comp.VirtualInventoryComponent);
        if (virtualInventory) {
            virtualInventory.getAllItems(true).forEach(item => {
                this.dimension.spawnItem(item, this.location);
            });
        }
    }
    onFuelExhausted() { }
    onFuelRestored() { }
    onNonFuelInteract(player) { }
}
_a = BotBase;
BotBase.eggQueue = new Map();
(() => {
    mc.world.afterEvents.entitySpawn.subscribe(event => {
        if (event.entity.matches({ families: ["ldz_240927:utility_bot"] })) {
            const id = event.entity.id;
            const itemUseOn = (event) => {
                const typeId = event.itemStack.typeId.replace("_spawn_egg", "");
                if (event.itemStack.typeId.startsWith(typeId)) {
                    _a.eggQueue.set(id, event);
                    utils.waitForEvent(1, () => _a.eggQueue.delete(typeId));
                }
            };
            mc.world.afterEvents.itemUseOn.subscribe(itemUseOn);
            utils.waitForEvent(1, () => mc.world.afterEvents.itemUseOn.unsubscribe(itemUseOn));
        }
    });
})();
// utils.Actor.makeActors({families: ["ldz_240927:utility_bot"]}, BotBase);
