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

const GunReloadComponent = {
    onUse({ itemStack, source }) {
        if (!source || !itemStack) return;

        const typeId = itemStack.typeId;

        // 10mm Pistol
        if (typeId === "fw:pistol_10mm_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:pistol_10mm_emp"] && now - globalThis.reloadCooldown["fw:pistol_10mm_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for 10mm Pistol.");
                return;
            }
            globalThis.reloadCooldown["fw:pistol_10mm_emp"] = now;

            system.run(() => {
                // ✅ Delay 2 seconds (40 ticks) before running reload logic
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:10mm_ammunition";
                    const ammoMax = 12;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s pistol_10mm ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 25); // 0 ticks = 0 seconds
            });
        }
        // Pipe Rifle
        else if (typeId === "fw:pipe_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:pipe_rifle_emp"] && now - globalThis.reloadCooldown["fw:pipe_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Pipe Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:pipe_rifle_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before running the reload logic
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:38_ammunition";
                    const ammoMax = 12; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s pipe_rifle ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 43); // Delay = 40 ticks = 2 seconds
            });
        }
        // Sniper Rifle
        else if (typeId === "fw:sniper_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:sniper_rifle_emp"] && now - globalThis.reloadCooldown["fw:sniper_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Sniper Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:sniper_rifle_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before running the reload logic
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:308_ammunition";
                    const ammoMax = 10; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s sniper_rifle ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 43); // Delay = 40 ticks = 2 seconds
            });
        }
        // Classic 10mm Pistol
        else if (typeId === "fw:classic_10mm_pistol_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:classic_10mm_pistol_emp"] && now - globalThis.reloadCooldown["fw:classic_10mm_pistol_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Classic 10mm Pistol.");
                return;
            }
            globalThis.reloadCooldown["fw:classic_10mm_pistol_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before running the reload logic
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:10mm_ammunition";
                    const ammoMax = 8; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s classic_10mm_pistol ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 43); // Delay = 40 ticks = 2 seconds
            });
        }
        // Gauss Rifle
        else if (typeId === "fw:gauss_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:gauss_rifle_emp"] && now - globalThis.reloadCooldown["fw:gauss_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Gauss Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:gauss_rifle_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before running the reload logic
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:2mm_ec";
                    const ammoMax = 1; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s gauss_rifle 7`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 25); // Delay = 40 ticks = 2 seconds
            });
        }
        // Pipe Pistol
        else if (typeId === "fw:pipe_pistol_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:pipe_pistol_emp"] && now - globalThis.reloadCooldown["fw:pipe_pistol_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Pipe Pistol.");
                return;
            }
            globalThis.reloadCooldown["fw:pipe_pistol_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before starting the reload
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:38_ammunition";
                    const ammoMax = 12; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s pipe_pistol ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 30); // 0 ticks = 0 seconds
            });
        }
        // Laser Pistol
        else if (typeId === "fw:laser_pistol_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:laser_pistol_emp"] && now - globalThis.reloadCooldown["fw:laser_pistol_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Laser Pistol.");
                return;
            }
            globalThis.reloadCooldown["fw:laser_pistol_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before starting the reload
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:universal_fusion_cell";
                    const ammoMax = 1; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s laser_pistol 30`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 25); // 0 ticks = 0 seconds
            });
        }

        // Laser Rifle
        else if (typeId === "fw:laser_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:laser_rifle_emp"] && now - globalThis.reloadCooldown["fw:laser_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Laser Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:laser_rifle_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before starting the reload
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:universal_fusion_cell";
                    const ammoMax = 1; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s laser_rifle 30`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 36); // 0 ticks = 0 seconds
            });
        }

        // Institute Pistol
        else if (typeId === "fw:institute_pistol_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:institute_pistol_emp"] && now - globalThis.reloadCooldown["fw:institute_pistol_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Institute Pistol.");
                return;
            }
            globalThis.reloadCooldown["fw:institute_pistol_emp"] = now;

            system.run(() => {
                // ✅ Wait 2 seconds (40 ticks) before starting the reload
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:universal_fusion_cell";
                    const ammoMax = 1; // Max ammo

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) {
                            totalAmmo += item.amount;
                        }
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s institute_pistol 30`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else {
                        console.warn("[RELOAD] Not enough ammo to reload.");
                    }

                }, 25); // 0 ticks = 0 seconds
            });
        }
        // Institute Rifle
        else if (typeId === "fw:institute_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:institute_rifle_emp"] && now - globalThis.reloadCooldown["fw:institute_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Institute Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:institute_rifle_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:universal_fusion_cell";
                    const ammoMax = 1;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s institute_rifle 30`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 36);
            });
        }

        // Hunting Rifle
        else if (typeId === "fw:hunting_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:hunting_rifle_emp"] && now - globalThis.reloadCooldown["fw:hunting_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Hunting Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:hunting_rifle_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:308_ammunition";
                    const ammoMax = 5;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s hunting_rifle ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 50);
            });
        }

        // Combat Rifle
        else if (typeId === "fw:combat_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:combat_rifle_emp"] && now - globalThis.reloadCooldown["fw:combat_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Combat Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:combat_rifle_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:45_ammunition";
                    const ammoMax = 20;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s combat_rifle ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 29);
            });
        }

        // Assault Rifle
        else if (typeId === "fw:assault_rifle_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:assault_rifle_emp"] && now - globalThis.reloadCooldown["fw:assault_rifle_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Assault Rifle.");
                return;
            }
            globalThis.reloadCooldown["fw:assault_rifle_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:5_56_ammunition";
                    const ammoMax = 30;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s assault_rifle ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 29);
            });
        }

        // Machine Gun
        else if (typeId === "fw:machine_gun_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:machine_gun_emp"] && now - globalThis.reloadCooldown["fw:machine_gun_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Machine Gun.");
                return;
            }
            globalThis.reloadCooldown["fw:machine_gun_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:5_56_ammunition";
                    const ammoMax = 40;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s machine_gun ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 43);
            });
        }

        // Minigun
        else if (typeId === "fw:minigun_emp") {
            if (!globalThis.reloadCooldown) globalThis.reloadCooldown = {};
            const now = Date.now();
            if (globalThis.reloadCooldown["fw:minigun_emp"] && now - globalThis.reloadCooldown["fw:minigun_emp"] < 5000) {
                console.warn("[RELOAD] Reload on cooldown for Minigun.");
                return;
            }
            globalThis.reloadCooldown["fw:minigun_emp"] = now;

            system.run(() => {
                system.runTimeout(async () => {
                    const inventory = source.getComponent("inventory").container;
                    if (!inventory) return;

                    let totalAmmo = 0;
                    const ammoType = "fw:5mm_ammunition";
                    const ammoMax = 500;

                    for (let slot = 0; slot < inventory.size; slot++) {
                        const item = inventory.getItem(slot);
                        if (item && item.typeId === ammoType) totalAmmo += item.amount;
                    }

                    if (totalAmmo >= 1) {
                        const ammoToAdd = Math.min(totalAmmo, ammoMax);
                        await source.runCommandAsync(`scoreboard players add @s minigun ${ammoToAdd}`);
                        console.warn(`[RELOAD] Added ${ammoToAdd} bullets to the scoreboard.`);

                        let ammoLeftToRemove = ammoToAdd;
                        for (let slot = 0; slot < inventory.size; slot++) {
                            const item = inventory.getItem(slot);
                            if (item && item.typeId === ammoType) {
                                if (item.amount <= ammoLeftToRemove) {
                                    inventory.setItem(slot, null);
                                    ammoLeftToRemove -= item.amount;
                                } else {
                                    item.amount -= ammoLeftToRemove;
                                    inventory.setItem(slot, item);
                                    break;
                                }
                            }
                            if (ammoLeftToRemove <= 0) break;
                        }
                    } else console.warn("[RELOAD] Not enough ammo to reload.");
                }, 0);
            });
        }
    },
};

world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("fw:gun_reload", GunReloadComponent);
});
