import { ItemEnchantableComponent, system, world } from "@minecraft/server";
import { BlockComponentLogStrip } from "./Block Component Constructors/Log Components/strip";
import { BlockManager } from "./Block/manager";
import { BlockComponentSlabInteraction } from "./Block Component Constructors/Slab Components/interact";
import { FenceManager } from "./Block/Fence/manager";
import { doorManager } from "./Block/Door/manager";
import { BlockComponentDoor } from "./Block/Door/constructor";
import { BlockComponentFenceGate } from "./Block Component Constructors/Fence Gate/constructor";
import { BlockComponentStairs } from "./Block/Stairs/constructor";
import { BlockComponentFence } from "./Block Component Constructors/Fence/constructor";
import { CustomTrapdoorBlockComponent } from "./Block/Trapdoor/trapdoor";
/* REQUIRED EVENTS */
world.beforeEvents.playerBreakBlock.subscribe((data) => {
    system.runTimeout(() => {
        FenceManager.updateFencesAround(data.block);
    });
    if (data.block.hasTag(doorManager.doorTag)) {
        //if the block has the door tag, breakDoor
        data.cancel = true;
        doorManager.breakDoor(data.block.typeId, data.block, data.block.permutation.getState("gs_cb:upper_block_bit"));
        return;
    }
    else
        try {
            const blockAbove = data.block.above(1);
            //if the above block has the door tag, breakDoor
            if (blockAbove?.hasTag(doorManager.doorTag)) {
                doorManager.breakDoor(blockAbove.typeId, blockAbove, blockAbove.permutation.getState("gs_cb:upper_block_bit"));
                return;
            }
        }
        catch { }
    if (!data.block.hasTag("gs_cb:disable_silktouch") || !data.itemStack)
        return;
    if (!data.itemStack.getComponent(ItemEnchantableComponent.componentId)?.hasEnchantment("silk_touch"))
        return;
    data.cancel = true;
    system.runTimeout(() => {
        BlockManager.destroy(data.dimension, data.block.location);
    });
});
world.afterEvents.playerPlaceBlock.subscribe((data) => {
    FenceManager.updateFencesAround(data.block);
});
new BlockComponentLogStrip([
    { fromId: "gs_cb:frozen_log", toId: "gs_cb:frozen_stripped_log", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:frozen_wood", toId: "gs_cb:frozen_wood_stripped", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:burnt_log", toId: "gs_cb:burnt_stripped_log", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:burnt_wood", toId: "gs_cb:burnt_wood_stripped", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:charred_burnt_log", toId: "gs_cb:charred_burnt_stripped_log", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:charred_burnt_wood", toId: "gs_cb:charred_burnt_wood_stripped", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:smoldering_burnt_log", toId: "gs_cb:smoldering_burnt_stripped_log", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } },
    { fromId: "gs_cb:smoldering_burnt_wood", toId: "gs_cb:smoldering_burnt_wood_stripped", keepStates: true, sound: { id: "use.wood", volume: 1, pitch: 0.80 } }
    //Add more objects here to add more stripped logs
]);
world.beforeEvents.worldInitialize.subscribe((data) => {
    const { blockComponentRegistry } = data;
    blockComponentRegistry.registerCustomComponent("gs_cb:trapdoor", CustomTrapdoorBlockComponent);
    blockComponentRegistry.registerCustomComponent("gs_cb:fence", new BlockComponentFence());
    blockComponentRegistry.registerCustomComponent("gs_cb:fence_gate", new BlockComponentFenceGate("gs_cb:open", "gs_cb:display", "gs_cb:powered", { id: "open.fence_gate", volume: 1, pitch: 1 }, { id: "close.fence_gate", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_obsidian_tiles", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:obsidian_tiles_slab", { id: "dig.stone", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_obsidian_bricks", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:obsidian_bricks_slab", { id: "dig.stone", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_burnt", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:burnt_slab", { id: "dig.wood", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_charred_burnt", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:charred_burnt_slab", { id: "dig.wood", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_smoldering_burnt", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:smoldering_burnt_slab", { id: "dig.wood", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:slab_frozen", new BlockComponentSlabInteraction("gs_cb:full_block", "gs_cb:frozen_slab", { id: "dig.wood", volume: 1, pitch: 1 }));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_obsidian_tiles", new BlockComponentStairs("gs_cb:obsidian_tiles_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_obsidian_bricks", new BlockComponentStairs("gs_cb:obsidian_bricks_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_frozen", new BlockComponentStairs("gs_cb:frozen_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_burnt", new BlockComponentStairs("gs_cb:burnt_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_charred_burnt", new BlockComponentStairs("gs_cb:charred_burnt_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:stairs_smoldering_burnt", new BlockComponentStairs("gs_cb:smoldering_burnt_stairs", "gs_cb:connected_front", "gs_cb:connected_back", "gs_cb:display"));
    blockComponentRegistry.registerCustomComponent("gs_cb:door", new BlockComponentDoor([
        {
            id: "gs_cb:burnt_door",
            itemID: "gs_cb:burnt_door_placer",
            openSound: {
                id: "open.wooden_door",
                volume: 1,
                pitch: 1
            },
            closeSound: {
                id: "close.wooden_door",
                volume: 1,
                pitch: 1
            }
        },
        {
            id: "gs_cb:charred_burnt_door",
            itemID: "gs_cb:charred_burnt_door_placer",
            openSound: {
                id: "open.wooden_door",
                volume: 1,
                pitch: 1
            },
            closeSound: {
                id: "close.wooden_door",
                volume: 1,
                pitch: 1
            }
        },
        {
            id: "gs_cb:smoldering_burnt_door",
            itemID: "gs_cb:smoldering_burnt_door_placer",
            openSound: {
                id: "open.wooden_door",
                volume: 1,
                pitch: 1
            },
            closeSound: {
                id: "close.wooden_door",
                volume: 1,
                pitch: 1
            }
        },
        {
            id: "gs_cb:ice_door",
            itemID: "gs_cb:ice_door_placer",
            openSound: {
                id: "open.wooden_door",
                volume: 1,
                pitch: 1
            },
            closeSound: {
                id: "close.wooden_door",
                volume: 1,
                pitch: 1
            }
        },
        //ADD MORE OBJECTS BELOW TO HAVE MORE DOORS
        /* like so
        {
            id: "gs_cb:door_oak",
            itemID: "gs_cb:door_oak_placer",
            openSound: {
                id: "open.wooden_door",
                volume: 1,
                pitch: 1
            },
            closeSound: {
                id: "close.wooden_door",
                volume: 1,
                pitch: 1
            }
        }
        */
    ]));
});
