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

import { initUtilitiesConstants } from "./Fo/utilities/fo_constants";
import { initEntitiesUtility } from "./Fo/eventUtilities/entities";

import { initAfk } from "./Fo/fo_Afk";
import { initAllDoors } from "./Fo/fo_AllDoors";
import { initAllTorches } from "./Fo/fo_AllTorches";
import { initAnimalPack } from "./Fo/fo_AnimalPack";
import { initBoat } from "./Fo/fo_Boat";
import { initCamera } from "./Fo/fo_Camera";
import { initCat } from "./Fo/fo_Cat";
import { initCaveCow } from "./Fo/fo_CaveCow";
import { initChest } from "./Fo/fo_Chest";
import { initCounter } from "./Fo/fo_Counter";
import { initCraftingTable } from "./Fo/fo_CraftingTable";
import { initCreepers } from "./Fo/fo_Creeper";
import { initCredits } from "./Fo/fo_Credits";
import { initCursedItems } from "./Fo/fo_CursedItems";
import { initDaycycle } from "./Fo/fo_Daycycle";
import { initDog } from "./Fo/fo_Dog";
import { initDoor } from "./Fo/fo_Door";
import { initDynamicProperties } from "./Fo/fo_DynamicProperties";
import { initEnderDragonInjure } from "./Fo/fo_EnderDragonInjure";
import { initEventSubscriptions } from "./Fo/fo_EventSubscriptions";
import { initFamine } from "./Fo/fo_Famine";
import { initFog } from "./Fo/fo_Fog";
import { initGamerules } from "./Fo/fo_gamerules";
import { initGlass } from "./Fo/fo_Glass";
import { initHorn } from "./Fo/fo_Horn"
import { initHostileMob } from "./Fo/fo_HostileMob";
import { initIceOcean } from "./Fo/fo_IceOcean";
import { initJukeboxCave } from "./Fo/fo_JukeboxCave";
import { initJukeboxHaunted } from "./Fo/fo_JukeboxHaunted";
import { initMinedShaft } from "./Fo/fo_MinedShaft";
import { initMineRestore } from "./Fo/fo_MineRestore";
import { initMirrorWorld } from "./Fo/fo_MirrorWorld";
import { initMixedEvents } from "./Fo/fo_MixedEvents";
import { initMobKiller } from "./Fo/fo_MobKiller";
import { initMoon } from "./Fo/fo_Moon";
import { initMossyPyramids } from "./Fo/fo_MossyPyramids";
import { initNetherPortal } from "./Fo/fo_NetherPortal";
import { initNightScream } from "./Fo/fo_NightScream";
import { initNoises } from "./Fo/fo_Noises";
import { initNoPause } from "./Fo/fo_NoPause";
import { initNoSleep } from "./Fo/fo_NoSleep";
import { initOnJoin } from "./Fo/fo_OnJoin";
import { initP82 } from "./Fo/fo_P82";
import { initPhantomFriend } from "./Fo/fo_PhantomFriend";
import { initPlayerPatterns } from "./Fo/fo_PlayerPatterns";
import { initPlayerShadowSteps } from "./Fo/fo_PlayerShadowSteps";
import { initRandomTick } from "./Fo/fo_RandomTick";
import { initRandomTorch } from "./Fo/fo_RandomTorch";
import { initSaddledHorse } from "./Fo/fo_SaddledHorse";
import { initSetUpAltar } from "./Fo/fo_SetUpAltar";
import { initSheep } from "./Fo/fo_Sheep";
import { initSignSwap } from "./Fo/fo_SignSwap";
import { initSoundDesign } from "./Fo/fo_SoundDesign";
import { initStare } from "./Fo/fo_Stare";
import { initStartConditionals } from "./Fo/fo_StartConditionals";
import { initTheFinal01 } from "./Fo/fo_TheFinal01";
import { initTorch } from "./Fo/fo_Torch";
import { initTunnelLoop } from "./Fo/fo_TunnelLoop";
import { initVoid } from "./Fo/fo_void";
import { initVoidChapter } from "./Fo/fo_VoidChapter";
import { initVoidEncounter } from "./Fo/fo_VoidEncounter";
import { initVoidFauna } from "./Fo/fo_VoidFauna";
import { initVoidShiaSurprise } from "./Fo/fo_VoidShiaSurprise";
import { initVoidVillage } from "./Fo/fo_VoidVillage";

import { initWorldGen } from "./Fo/fo_WorldGen";
import { initWorldInitialize } from "./Fo/fo_WorldInitialize";

const initStages = [



    initWorldInitialize,



    initDynamicProperties,


    initUtilitiesConstants,



    initCounter,









    initOnJoin,



    initFog,


    initGamerules,


    initEventSubscriptions,


    initEntitiesUtility,





    initHostileMob,


    initAfk,



    initAllDoors,



    initAllTorches,


    initAnimalPack,


    initBoat,


    initCamera,


    initCat,


    initCaveCow,


    initChest,


    initCraftingTable,


    initCreepers,


    initCredits,


    initCursedItems,



    initDaycycle,


    initDog,



    initDoor,


    initFamine,


    initGlass,


    initHorn,


    initIceOcean,


    initJukeboxCave,


    initJukeboxHaunted,


    initMinedShaft,


    initMineRestore,


    initMirrorWorld,



























    initMixedEvents,


    initMobKiller,


    initMoon,


    initMossyPyramids,


    initNetherPortal,


    initNightScream,


    initNoises,


    initNoSleep,


    initP82,


    initPhantomFriend,


    initPlayerPatterns,


    initPlayerShadowSteps,


    initRandomTorch,


    initSaddledHorse,


    initSheep,


    initSignSwap,


    initSoundDesign,



    initStare,


    initTheFinal01,


    initTorch,


    initTunnelLoop,


    initVoid,


    initVoidChapter,


    initVoidEncounter,


    initVoidFauna,


    initVoidShiaSurprise,


    initVoidVillage,






    initStartConditionals,


    initSetUpAltar,


    initRandomTick,


    initEnderDragonInjure,


    initNoPause,


    initWorldGen,
]

function runStaged(inits, delayPerStage = 3, i = 0) {
    if (i >= inits.length) return;

    system.runTimeout(() => {
        const fn = inits[i];
        const name = fn?.name || '<anonymous>';

        try {
            fn();

        } catch (e) {
            console.warn(`Function "${name}" at index ${i} failed to initialize`, e);
        }

        runStaged(inits, delayPerStage, i + 1);
    }, delayPerStage);
}



const worldLoadCallback = world.afterEvents.worldLoad.subscribe(() => {

    runStaged(initStages)

    world.afterEvents.worldLoad.unsubscribe(worldLoadCallback);
})