import { EnchantmentType, ItemLockMode, ItemStack } from "@minecraft/server";
//import { LigthLevel, parseID } from "./utils";
export class Belt {
    id;
    slot;
    constructor(slot) {
        this.id = Date.now();
        slot.setDynamicProperty('ID', this.id);
        this.slot = slot;
    }
    // get id() {
    //     return this.slot.getDynamicProperty('ID') as string;
    // }
    get data() {
        const data = this.slot.getDynamicProperty('inventory');
        return data ? JSON.parse(data) : [];
    }
    set data(data) {
        this.slot.setDynamicProperty('inventory', JSON.stringify(data));
    }
    static getData(item) {
        const data = item.getDynamicProperty('inventory');
        return data ? JSON.parse(data) : [];
    }
    loadIventory(container) {
        const items = this.data;
        for (const { typeId, amount, nameTag, lore, canDestroy, canPlaceOn, damage, enchantments, dynamicProperties, slot } of items) {
            try {
                const item = new ItemStack(typeId, amount);
                item.nameTag = nameTag;
                item.setLore(lore);
                item.setCanDestroy(canDestroy);
                item.setCanPlaceOn(canPlaceOn);
                if (damage)
                    item.durability.damage = damage;
                enchantments?.forEach(({ type, level }) => {
                    const ench = new EnchantmentType(type.id);
                    item.enchantable?.addEnchantment({ type: ench, level });
                });
                dynamicProperties.forEach(([id, value]) => item.setDynamicProperty(id, value));
                container.setItem(slot, item);
            }
            catch { }
        }
    }
    saveInventory(container) {
        const items = [];
        const lore = [];
        for (let i = 0; i < container.size; i++) {
            const item = container.getItem(i);
            if (!item) {
                lore.push(`§r- §8§o(...Empty)`);
                continue;
            }
            items.push({
                slot: i,
                ...serializeItem(item)
            });
            const light = LigthLevel[item.typeId];
            lore.push('§r- ' + parseID(item.typeId) + (light ? ' []' : ''));
        }
        this.data = items;
        this.slot.setLore([
            '§r§lContents:',
            ...lore
        ]);
    }
    parse() {
        return this.data.map(d => parseItem(d));
    }
}
export function serializeItem(item) {
    return {
        typeId: item.typeId,
        amount: item.amount,
        lore: item.getLore(),
        nameTag: item.nameTag,
        lockMode: item.lockMode,
        keepOnDeath: item.keepOnDeath,
        canDestroy: item.getCanDestroy(),
        canPlaceOn: item.getCanDestroy(),
        damage: item.durability?.damage,
        enchantments: item.enchantable?.getEnchantments(),
        dynamicProperties: item.getDynamicPropertyIds().map(id => ([id, item.getDynamicProperty(id)])),
    };
}
export function parseItem(data) {
    const { typeId, amount, nameTag, lore, canDestroy, canPlaceOn, damage, enchantments, dynamicProperties } = data;
    const item = new ItemStack(typeId, amount);
    item.nameTag = nameTag;
    item.setLore(lore);
    item.setCanDestroy(canDestroy);
    item.setCanPlaceOn(canPlaceOn);
    if (damage)
        item.durability.damage = damage;
    enchantments?.forEach(({ type, level }) => {
        const ench = new EnchantmentType(type.id);
        item.enchantable?.addEnchantment({ type: ench, level });
    });
    dynamicProperties.forEach(([id, value]) => item.setDynamicProperty(id, value));
    return item;
}
export function unlockItems(player, type) {
    const inv = player.inventory.container;
    for (let i = 0; i < inv.size; i++) {
        const item = inv.getItem(i);
        if (item?.typeId !== type)
            continue;
        item.lockMode = ItemLockMode.none;
        inv.setItem(i, item);
    }
}
// Not used yet
export class Tool {
    static is_axe(item) {
        return !!item.typeId.match(/\w+_axe/) || item.getTags().includes('minecraft:is_axe');
    }
    static is_shovel(item) {
        return !!item.typeId.match(/\w+_shovel/) || item.getTags().includes('minecraft:is_shovel');
    }
    static is_pickaxe(item) {
        return !!item.typeId.match(/\w+_pickaxe/) || item.getTags().includes('minecraft:is_pickaxe');
    }
    static is_sword(item) {
        return !!item.typeId.match(/\w+_sword/) || item.getTags().includes('minecraft:is_sword');
    }
    static is_hoe(item) {
        return !!item.typeId.match(/\w+_hoe/) || item.getTags().includes('minecraft:is_hoe');
    }
    static is_trident(item) {
        return !!item.typeId.match(/\w+_trident/) || item.getTags().includes('minecraft:is_trident');
    }
    static is_shears(item) {
        return !!item.typeId.match(/shears/) || item.getTags().includes('minecraft:is_shears');
    }
}

export function parseID(id) {
    return capitalizeEachWord(id.split(':').pop().replace(/_/g, ' '));
}
export function capitalizeEachWord(str) {
    let words = str.split(" ");
    for (let i = 0; i < words.length; i++) {
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
    }
    return words.join(" ");
}

export const LigthLevel = {
    "minecraft:torch": 12,
    "minecraft:end_rod": 13,
    "minecraft:shroomlight": 11,
    "minecraft:soul_torch": 13,
    "minecraft:glowstone": 12,
    "minecraft:lantern": 11,
    "minecraft:soul_lantern": 12,
    "minecraft:redstone_block": 4,
    "minecraft:redstone_torch": 6,
    "minecraft:glowstone_dust": 7,
    "minecraft:lit_pumpkin": 10,
    "minecraft:sea_lantern": 12,
    "minecraft:pearlescent_froglight": 10,
    "minecraft:verdant_froglight": 10,
    "minecraft:ochre_froglight": 10,
    "minecraft:crying_obsidian": 2,
    "minecraft:campfire": 12,
    "minecraft:soul_campfire": 13,
    "minecraft:blaze_rod": 6,
    "minecraft:enchanting_table": 9,
    "minecraft:blaze_powder": 6,
    "minecraft:ender_chest": 9,
    "minecraft:glow_ink_sac": 6,
    "minecraft:glow_berries": 6,
    "minecraft:blaze": 6,
    "minecraft:glow_squid": 6,
    "minecraft:magma_cube": 3
};