import { EntityComponentTypes, EntityInventoryComponent, ItemStack, system, world } from "@minecraft/server";


class LoreSystem {
    #loreMap = new Map();
    constructor() {
        this.#start();
    }

    #start() {
        system.runInterval(() => { this.#run() }, 2);
    }

    #run() {
        world.getAllPlayers().forEach((p) => {
            /** @type {EntityInventoryComponent} */
            const inventory = p.getComponent(EntityComponentTypes.Inventory);

            if (!inventory || !inventory.container) return;

            const container = inventory.container;
            for (let i = 0; i < container.size; i++) {
                const item = container.getItem(i);
                if (!item) continue;

                const loreEntry = this.#loreMap.get(item.typeId);
                if (loreEntry && item.getLore().length === 0) {
                    item.setLore(loreEntry);
                    container.setItem(i, item);
                }
            }
        })
    }

    /**
     * 
     * @param {string} itemType 
     * @param {string[]} lore 
     */
    addLore(itemType, lore) {
        this.#loreMap.set(itemType, lore);
    }
}

export const loreSystem = new LoreSystem();