import { BlockPermutation, EnchantmentType, EquipmentSlot, ItemStack, world } from '@minecraft/server';
class onPlayerInteract {
    constructor() {
        this.onPlayerInteract = this.onPlayerInteract.bind(this);
    }
    onPlayerInteract(_componentData) { }
}
const FLOWER_BED_INTERACT = [
    'poppy',
    'blue_orchid',
    'allium',
    'azure_bluet',
    'red_tulip',
    'orange_tulip',
    'white_tulip',
    'pink_tulip',
    'oxeye_daisy',
    'cornflower',
    'lily_of_the_valley',
    'wither_rose',
    'torchflower',
    'dandelion'
];
export class flowerBed extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        if (item === undefined ||
            !FLOWER_BED_INTERACT.includes(item.typeId.split(':')[1]))
            return;
        const block = componentData.block;
        const blockPermutation = block.permutation.withState('bf_dc:flower_type', item.typeId.split(':')[1]);
        block.setPermutation(blockPermutation);
    }
}
export class hangingSign extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const block = componentData.block;
        const blockPermutation = block.permutation.getState('bf_dc:signage');
        block.setPermutation(block.permutation.withState('bf_dc:signage', (blockPermutation + 1) % 5));
    }
}
export class pepper extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        const permutation = componentData.block.permutation.getState('bf_dc:growth');
        if (item != undefined &&
            item.typeId === 'minecraft:bone_meal' &&
            permutation == 0)
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:growth', 1));
        else if (permutation == 1) {
            componentData.block.setPermutation(BlockPermutation.resolve(componentData.block.typeId, {
                'bf_dc:growth': 0
            }));
            let type;
            switch (componentData.block.typeId) {
                case 'bf_dc:bell_pepper_plant':
                    type = 'bf_dc:green_bell_pepper';
                    break;
                case 'bf_dc:bell_pepper_plant_red':
                    type = 'bf_dc:red_bell_pepper';
                    break;
                case 'bf_dc:bell_pepper_plant_yellow':
                    type = 'bf_dc:yellow_bell_pepper';
                    break;
                default:
                    break;
            }
            componentData.dimension.spawnItem(new ItemStack(type, 1), componentData.block.location);
        }
    }
}
export class berry extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        const permutation = componentData.block.permutation.getState('bf_dc:growth');
        if (item != undefined &&
            item.typeId === 'minecraft:bone_meal' &&
            permutation == 0)
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:growth', 1));
        else if (permutation == 1) {
            componentData.block.setPermutation(BlockPermutation.resolve(componentData.block.typeId, {
                'bf_dc:growth': 0
            }));
            componentData.dimension.spawnItem(new ItemStack('bf_dc:blue_berries', 1), componentData.block.location);
        }
    }
}
export class cauliflower extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        const permutation = componentData.block.permutation.getState('bf_dc:growth');
        if (item != undefined &&
            item.typeId === 'minecraft:bone_meal' &&
            permutation == 0)
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:growth', 1));
        else if (permutation == 1) {
            componentData.block.setPermutation(BlockPermutation.resolve(componentData.block.typeId, {
                'bf_dc:growth': 0
            }));
            componentData.dimension.spawnItem(new ItemStack('bf_dc:cauliflower_item', 1), componentData.block.location);
        }
    }
}
export class blinds extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const block = componentData.block;
        const blockPermutation = block.permutation.getState('bf_dc:closed');
        block.setPermutation(block.permutation.withState('bf_dc:closed', !blockPermutation));
    }
}
export class mug extends onPlayerInteract {
    onPlayerInteract(componentData) {
        const block = componentData.block;
        const blockPermutation = block.permutation.getState('bf_dc:drunk');
        if (!blockPermutation)
            block.setPermutation(block.permutation.withState('bf_dc:drunk', true));
    }
}
function vector3ToString(vector) {
    return JSON.stringify({ x: vector.x, y: vector.y, z: vector.z });
}
function stringToVector3(vector) {
    const obj = JSON.parse(vector);
    return { x: obj.x, y: obj.y, z: obj.z };
}
function areVectorsEqual(vector1, vector2) {
    return (vector1.x === vector2.x &&
        vector1.y === vector2.y &&
        vector1.z === vector2.z);
}
export class sword extends onPlayerInteract {
    onPlayerInteract(componentData) {
        let item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        let player = componentData.player;
        let equipment = player.getComponent('equippable');
        let permutation = componentData.block.permutation.getState('bf_dc:has_sword');
        let permutation2 = componentData.block.permutation.getState('bf_dc:sword_type');
        if (item !== undefined &&
            item.typeId.startsWith('minecraft:') &&
            item.typeId.includes('sword') &&
            !permutation) {
            // Set enchantments
            let dp = JSON.parse(world.getDynamicProperty('bf_dc:enchantments'));
            //console.warn(world.getDynamicProperty('bf_dc:enchantments'));
            let enchantments = [];
            item.getComponent('enchantable')
                .getEnchantments()
                .forEach((enchantment) => {
                enchantments.push(enchantment);
            });
            dp[vector3ToString(componentData.block.location)] =
                JSON.stringify(enchantments);
            world.setDynamicProperty('bf_dc:enchantments', JSON.stringify(dp));
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:has_sword', true));
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:sword_type', item.typeId.slice(10, -6)));
            equipment.setEquipment('Mainhand', undefined);
        }
        else if (item === undefined) {
            // Get enchantments
            let dp_new = JSON.parse(world.getDynamicProperty('bf_dc:enchantments'));
            for (const element in dp_new) {
                if (areVectorsEqual(componentData.block.location, stringToVector3(element))) {
                    let item = new ItemStack(`minecraft:${permutation2}_sword`, 1);
                    for (let index = 0; index < JSON.parse(dp_new[element]).length; index++) {
                        let enchant = {
                            level: JSON.parse(dp_new[element])[index]['level'],
                            type: new EnchantmentType(JSON.parse(dp_new[element])[index]['type']['id'])
                        };
                        item.getComponent('enchantable').addEnchantment(enchant);
                    }
                    componentData.dimension.spawnItem(item, componentData.block.location);
                    delete dp_new[element];
                    world.setDynamicProperty('bf_dc:enchantments', JSON.stringify(dp_new));
                    componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:has_sword', false));
                    componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:sword_type', 'none'));
                }
            }
        }
    }
}
export class axe extends onPlayerInteract {
    onPlayerInteract(componentData) {
        let item = componentData.player.getComponent('equippable').getEquipment(EquipmentSlot.Mainhand);
        let player = componentData.player;
        let equipment = player.getComponent('equippable');
        let permutation = componentData.block.permutation.getState('bf_dc:has_axe');
        let permutation2 = componentData.block.permutation.getState('bf_dc:axe_type');
        if (item !== undefined &&
            item.typeId.startsWith('minecraft:') &&
            item.typeId.includes('axe') &&
            !permutation) {
            // Set enchantments
            let dp = JSON.parse(world.getDynamicProperty('bf_dc:enchantments_axe'));
            //console.warn(world.getDynamicProperty('bf_dc:enchantments_axe'));
            let enchantments = [];
            item.getComponent('enchantable')
                .getEnchantments()
                .forEach((enchantment) => {
                enchantments.push(enchantment);
            });
            dp[vector3ToString(componentData.block.location)] =
                JSON.stringify(enchantments);
            world.setDynamicProperty('bf_dc:enchantments_axe', JSON.stringify(dp));
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:has_axe', true));
            componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:axe_type', item.typeId.slice(10, -4)));
            equipment.setEquipment('Mainhand', undefined);
        }
        else if (item === undefined) {
            // Get enchantments
            let dp_new = JSON.parse(world.getDynamicProperty('bf_dc:enchantments_axe'));
            for (const element in dp_new) {
                if (areVectorsEqual(componentData.block.location, stringToVector3(element))) {
                    let item = new ItemStack(`minecraft:${permutation2}_axe`, 1);
                    for (let index = 0; index < JSON.parse(dp_new[element]).length; index++) {
                        let enchant = {
                            level: JSON.parse(dp_new[element])[index]['level'],
                            type: new EnchantmentType(JSON.parse(dp_new[element])[index]['type']['id'])
                        };
                        item.getComponent('enchantable').addEnchantment(enchant);
                    }
                    componentData.dimension.spawnItem(item, componentData.block.location);
                    delete dp_new[element];
                    world.setDynamicProperty('bf_dc:enchantments_axe', JSON.stringify(dp_new));
                    componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:has_axe', false));
                    componentData.block.setPermutation(componentData.block.permutation.withState('bf_dc:axe_type', 'none'));
                }
            }
        }
    }
}
