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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    // Register all custom item components
    registerComponents(initEvent);
});

function registerComponents(initEvent) {
    // Tegasword components
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:tegasword', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function off');
            } catch (error) {
                console.warn(`Error executing tegasword command: ${error}`);
            }
        }
    });

    // Tegajune components
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:tegajune', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function off');
            } catch (error) {
                console.warn(`Error executing tegajune command: ${error}`);
            }
        }
    });
    
    // Silver Tegasword component
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:tegasword_silver', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function off');
            } catch (error) {
                console.warn(`Error executing tegasword_silver command: ${error}`);
            }
        }
    });
    
    // Tegajune Open component
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:call_gao_king', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function call_gao_king');
            } catch (error) {
                console.warn(`Error executing tegasword_open command: ${error}`);
            }
        }
    });
    
    // Tegasword components
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:goodeburn', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function off');
            } catch (error) {
                console.warn(`Error executing tegasword command: ${error}`);
            }
        }
    });

    // Tegasword Open component
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:tegasword_open', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function tegasword_open');
            } catch (error) {
                console.warn(`Error executing tegasword_open command: ${error}`);
            }
        }
    });
    
    // Tegajune Open component
    initEvent.itemComponentRegistry.registerCustomComponent('sentai:tegajune_open', {
        onUse: async ({ source, itemStack }) => {
            try {
                await source.runCommandAsync('function tegajune_open');
            } catch (error) {
                console.warn(`Error executing tegasword_open command: ${error}`);
            }
        }
    });
    
    // Super Sentai Ring component
    initEvent.itemComponentRegistry.registerCustomComponent("sentai:ss_ring", {
        onUse: async ({ source, itemStack }) => {
            const commands = [
                "function ss_kingohger",
                "function ss_gaoranger",
                "function ss_zyuranger",
                "function ss_boonboomger",
                "function ss_shinkenger",
                "function ss_dekaranger",
                "function ss_timeranger",
                "function ss_kyoryuger",
                "function ss_ryusoulger",
                "function ss_eagle",
                "function ss_tyranno",
                "function ss_wolf",
                "function ss_unicorn",
                "function ss_leon",
            ];

            try {
                const randomIndex = Math.floor(Math.random() * commands.length);
                const selectedCommand = commands[randomIndex];
                await source.runCommandAsync(selectedCommand);
            } catch (error) {
                console.warn(`Error executing ss_ring command: ${error}`);
            }
        }
    });
    
    // Super Sentai Ring component
    initEvent.itemComponentRegistry.registerCustomComponent("sentai:attack", {
        onUse: async ({ source, itemStack }) => {
            const commands = [
                "function attack1",
                "function attack2",
                "function attack3",
                "function attack4",
            ];

            try {
                const randomIndex = Math.floor(Math.random() * commands.length);
                const selectedCommand = commands[randomIndex];
                await source.runCommandAsync(selectedCommand);
            } catch (error) {
                console.warn(`Error executing ss_ring command: ${error}`);
            }
        }
    });
}