import { system, EquipmentSlot, world } from "@minecraft/server";
import { getItemInSlot } from "./utils";
import { globalLore, namespace, productNameForLore } from "../config/_config";
/**
 * Represents an item with its options.
 */
export class Item {
    constructor({ item, options = {} }) {
        this.item = item;
        this.options = options;
    }
    static register(item) {
        Item.items.push(item);
    }
    static onInitialize() {
        Item.items.forEach((item) => {
            if (item.options.dynamicLighting) {
                system.runInterval(() => {
                    dynamicLighting(item);
                }, 1);
            }
            system.runInterval(() => {
                const players = world.getPlayers();
                players.forEach((player) => {
                    const inventory = player.getComponent("inventory");
                    const container = inventory.container;
                    for (let i = 0; i < container.size; i++) {
                        let itemStack = container.getItem(i);
                        if (!itemStack)
                            continue;
                        let lore = itemStack.getLore() || []; // Ensure lore is always an array
                        if (itemStack.typeId.startsWith(namespace)) {
                            let loreChanged = false;
                            // Add custom lore for registered items
                            Item.items.forEach((registeredItem) => {
                                if (itemStack.typeId === registeredItem.item && registeredItem.options.lore) {
                                    const itemLore = Array.isArray(registeredItem.options.lore)
                                        ? registeredItem.options.lore
                                        : [registeredItem.options.lore];
                                    itemLore.forEach((loreLine) => {
                                        if (!lore.includes(loreLine)) {
                                            lore = [...itemLore, ...lore];
                                            loreChanged = true;
                                        }
                                    });
                                }
                            });
                            // Conditionally append global lore if not already present
                            if (globalLore && !lore.includes(productNameForLore)) {
                                lore.push("", `${productNameForLore}`);
                                loreChanged = true;
                            }
                            // Only update the lore if changes were made
                            if (loreChanged) {
                                itemStack.setLore(lore);
                                container.setItem(i, itemStack);
                            }
                        }
                    }
                });
            }, 1);
        });
    }
}
Item.items = [];
function dynamicLighting(item) {
    const players = world.getPlayers();
    players.forEach((player) => {
        if (getItemInSlot(player, EquipmentSlot.Mainhand)?.typeId !== item.item)
            return;
        emitDynamicLight(item.options.dynamicLighting?.strength || 0, player);
    });
}
export function emitDynamicLight(strength, player) {
    const { x, y, z } = player.location;
    system.run(() => {
        player.dimension.runCommand(`fill ${x} ${y} ${z} ${x} ${y + 3} ${z} minecraft:light_block["block_light_level"=${strength}] replace minecraft:air`);
    });
    system.runTimeout(() => {
        player.dimension.runCommand(`fill ${x} ${y} ${z} ${x} ${y + 3} ${z} minecraft:air replace minecraft:light_block["block_light_level"=${strength}]`);
    }, 2);
}
