
import { world, system, EquipmentSlot, GameMode, EntityDamageCause } from '@minecraft/server';

export const MythrilUtils = {
    durability: function (player, itemStack, equipmentSlot) {
        try {
            let equipComponent = itemStack?.getComponent('minecraft:durability');
            if (!equipComponent) return;

            let currentDamage = equipComponent.damage;
            let maxDurability = equipComponent.maxDurability;
            const inventory = player.getComponent('minecraft:equippable');

            if (currentDamage < maxDurability - 1) {
                equipComponent.damage = currentDamage + 1;
                inventory.setEquipment(equipmentSlot, itemStack);
            } else {
                MythrilMagic.createShatterRealityEffect(player);
                inventory.setEquipment(equipmentSlot, undefined);
            }
        } catch (error) {
            world.sendMessage('§d✦ Mythril reality fractures: ' + error);
        }
    },

    isNear: function (entity1, entity2, maxDistance, maxHeightDiff = 10) {
        const distance = Math.sqrt(
            Math.pow(entity1.location.x - entity2.location.x, 2) +
            Math.pow(entity1.location.y - entity2.location.y, 2) +
            Math.pow(entity1.location.z - entity2.location.z, 2)
        );

        return distance <= maxDistance &&
            Math.abs(entity1.location.y - entity2.location.y) <= maxHeightDiff;
    },

    spawnMythrilParticle: function (dimension, x, y, z, particleType = 'standard') {
        const particles = {
            'standard': 'myw:mythril_particle',
            'serpentine': 'myw:mythril_particle_serpentine',
            'minimalist': 'myw:mythril_particle_minimalist'
        };

        const fallbacks = {
            'standard': 'minecraft:enchant_table_particle',
            'serpentine': 'minecraft:soul_particle',
            'minimalist': 'minecraft:end_rod'
        };

        try {
            dimension.spawnParticle(particles[particleType] || particles.standard, { x, y, z });
        } catch (error) {
            dimension.spawnParticle(fallbacks[particleType] || fallbacks.standard, { x, y, z });
        }
    }
};


export const MythrilMagic = {

    onHitEffects: function (attacker, target) {
        const { x, y, z } = target.location;


        this.createMysticImpactWave(attacker.dimension, x, y + 1, z);


        this.summonMythrilSerpents(attacker, target);

        this.applyMysticCurse(target);


        try {
            target.applyDamage(8, { cause: EntityDamageCause.magic });

            target.addEffect('levitation', 30, { amplifier: 1, showParticles: false });
        } catch (error) {

        }


        try {
            attacker.addEffect('regeneration', 60, { amplifier: 1, showParticles: false });
        } catch (error) {

        }
    },

    activateSpecialAbility: function (player) {
        try {
            // Verificar cooldown
            const lastUsed = player.getDynamicProperty('myw:mythril_last_used') || 0;
            const currentTime = Date.now();
            const cooldownTime = 20000; // 20 segundos para habilidad 

            if (currentTime - lastUsed < cooldownTime) {
                const remaining = Math.ceil((cooldownTime - (currentTime - lastUsed)) / 1000);
                player.onScreenDisplay.setActionBar(`§d§l✦ Mythril Recharge: §f${remaining}s §d✦`);
                return;
            }

            player.setDynamicProperty('myw:mythril_last_used', currentTime);


            player.addEffect('speed', 300, { amplifier: 2, showParticles: false });
            player.addEffect('jump_boost', 300, { amplifier: 2, showParticles: false });


            this.unleashMythrilMagic(player);

            player.onScreenDisplay.setActionBar('§d§l⚡ MYTHRIL MAGIC UNLEASHED ⚡');

        } catch (error) {
            world.sendMessage('§cError en habilidad especial: ' + error);
        }
    },

    createMysticImpactWave: function (dimension, x, y, z) {

        for (let wave = 1; wave <= 5; wave++) {
            system.runTimeout(() => {
                const radius = wave * 1.5;
                const particleTypes = ['standard', 'serpentine', 'minimalist'];
                const currentParticle = particleTypes[wave % 3];


                for (let i = 0; i < 24; i++) {
                    const angle = (i / 24) * Math.PI * 2;
                    const particleX = x + Math.cos(angle) * radius;
                    const particleZ = z + Math.sin(angle) * radius;

                    MythrilUtils.spawnMythrilParticle(dimension, particleX, y, particleZ, currentParticle);


                    MythrilUtils.spawnMythrilParticle(dimension, particleX, y + wave * 0.3, particleZ, 'minimalist');
                }


                this.createMysticGeometry(dimension, x, y + wave * 0.5, z, wave);

            }, wave * 3);
        }
    },

    createMysticGeometry: function (dimension, x, y, z, complexity) {

        const points = 5;
        const radius = complexity * 0.8;

        for (let i = 0; i < points; i++) {
            const angle1 = (i * 2 * Math.PI) / points;
            const angle2 = ((i + 2) * 2 * Math.PI) / points;


            const steps = 10;
            for (let step = 0; step <= steps; step++) {
                const t = step / steps;
                const lineX = x + (Math.cos(angle1) * (1 - t) + Math.cos(angle2) * t) * radius;
                const lineZ = z + (Math.sin(angle1) * (1 - t) + Math.sin(angle2) * t) * radius;

                system.runTimeout(() => {
                    MythrilUtils.spawnMythrilParticle(dimension, lineX, y, lineZ, 'serpentine');
                }, step * 2);
            }
        }


        for (let ring = 1; ring <= 3; ring++) {
            const ringRadius = radius * ring * 0.3;
            for (let i = 0; i < 16; i++) {
                const angle = (i / 16) * Math.PI * 2;
                const ringX = x + Math.cos(angle) * ringRadius;
                const ringZ = z + Math.sin(angle) * ringRadius;

                system.runTimeout(() => {
                    MythrilUtils.spawnMythrilParticle(dimension, ringX, y + ring * 0.1, ringZ, 'minimalist');
                }, ring * 5);
            }
        }
    },

    summonMythrilSerpents: function (attacker, target) {
        const numSerpents = 3;

        for (let serpent = 0; serpent < numSerpents; serpent++) {
            system.runTimeout(() => {
                this.createHuntingSerpent(attacker, target, serpent);
            }, serpent * 10);
        }
    },

    createHuntingSerpent: function (attacker, target, serpentId) {
        const startX = attacker.location.x + (Math.random() - 0.5) * 4;
        const startY = attacker.location.y + 2;
        const startZ = attacker.location.z + (Math.random() - 0.5) * 4;

        let currentX = startX;
        let currentY = startY;
        let currentZ = startZ;

        let huntTime = 0;
        const maxHuntTime = 120;

        const serpentInterval = system.runInterval(() => {
            huntTime++;

            try {

                if (!target || !target.isValid()) {
                    system.clearRun(serpentInterval);
                    return;
                }

                const targetLoc = target.location;


                const dx = targetLoc.x - currentX;
                const dy = targetLoc.y + 1 - currentY;
                const dz = targetLoc.z - currentZ;

                const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);

                if (distance > 0.5) {

                    const speed = 0.4;
                    const wobble = Math.sin(huntTime * 0.3) * 0.5;

                    currentX += (dx / distance) * speed + wobble;
                    currentY += (dy / distance) * speed;
                    currentZ += (dz / distance) * speed + Math.cos(huntTime * 0.3) * 0.3;


                    MythrilUtils.spawnMythrilParticle(target.dimension, currentX, currentY, currentZ, 'serpentine');


                    for (let trail = 1; trail <= 3; trail++) {
                        system.runTimeout(() => {
                            MythrilUtils.spawnMythrilParticle(
                                target.dimension,
                                currentX - trail * 0.2,
                                currentY,
                                currentZ - trail * 0.1,
                                'minimalist'
                            );
                        }, trail);
                    }
                } else {

                    this.serpentAttackTarget(target, currentX, currentY, currentZ);
                    system.clearRun(serpentInterval);
                    return;
                }


                if (huntTime >= maxHuntTime) {
                    this.serpentDisappear(target.dimension, currentX, currentY, currentZ);
                    system.clearRun(serpentInterval);
                }

            } catch (error) {
                system.clearRun(serpentInterval);
            }
        }, 2);
    },

    serpentAttackTarget: function (target, x, y, z) {
        try {

            for (let i = 0; i < 20; i++) {
                const angle = Math.random() * Math.PI * 2;
                const radius = Math.random() * 2;
                const height = Math.random() * 2;

                MythrilUtils.spawnMythrilParticle(
                    target.dimension,
                    x + Math.cos(angle) * radius,
                    y + height,
                    z + Math.sin(angle) * radius,
                    'serpentine'
                );
            }


            target.applyDamage(5, { cause: EntityDamageCause.magic });


            this.createConfusionEffect(target);

        } catch (error) {

        }
    },

    createConfusionEffect: function (target) {

        let spinTime = 0;
        const spinInterval = system.runInterval(() => {
            spinTime++;

            try {
                const { x, y, z } = target.location;
                const radius = 2;

                for (let i = 0; i < 8; i++) {
                    const angle = (i / 8) * Math.PI * 2 + spinTime * 0.5;
                    const spinX = x + Math.cos(angle) * radius;
                    const spinZ = z + Math.sin(angle) * radius;
                    const spinY = y + 1 + Math.sin(spinTime * 0.2) * 0.5;

                    MythrilUtils.spawnMythrilParticle(target.dimension, spinX, spinY, spinZ, 'minimalist');
                }

                if (spinTime >= 40) {
                    system.clearRun(spinInterval);
                }
            } catch (error) {
                system.clearRun(spinInterval);
            }
        }, 2);
    },

    serpentDisappear: function (dimension, x, y, z) {

        for (let fade = 0; fade < 10; fade++) {
            system.runTimeout(() => {
                const fadeRadius = fade * 0.3;
                for (let i = 0; i < 6; i++) {
                    const angle = (i / 6) * Math.PI * 2;
                    MythrilUtils.spawnMythrilParticle(
                        dimension,
                        x + Math.cos(angle) * fadeRadius,
                        y + fade * 0.1,
                        z + Math.sin(angle) * fadeRadius,
                        'serpentine'
                    );
                }
            }, fade * 3);
        }
    },

    applyMysticCurse: function (target) {

        let curseTime = 0;
        const curseInterval = system.runInterval(() => {
            curseTime++;

            try {
                const { x, y, z } = target.location;


                for (let i = 0; i < 5; i++) {
                    const offsetX = (Math.random() - 0.5) * 3;
                    const offsetZ = (Math.random() - 0.5) * 3;
                    const height = Math.random() * 3;

                    MythrilUtils.spawnMythrilParticle(
                        target.dimension,
                        x + offsetX,
                        y + height,
                        z + offsetZ,
                        'serpentine'
                    );
                }


                if (curseTime % 20 === 0) {
                    this.createFloatingMysticSymbol(target.dimension, x, y + 3, z);
                }

                if (curseTime >= 100) {
                    system.clearRun(curseInterval);
                }
            } catch (error) {
                system.clearRun(curseInterval);
            }
        }, 3);
    },

    createFloatingMysticSymbol: function (dimension, x, y, z) {

        const runePoints = [
            { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0.5, y: 1 },
            { x: 0.3, y: 0.3 }, { x: 0.7, y: 0.3 }
        ];

        runePoints.forEach((point, index) => {
            system.runTimeout(() => {
                MythrilUtils.spawnMythrilParticle(
                    dimension,
                    x + point.x - 0.5,
                    y,
                    z + point.y - 0.5,
                    'minimalist'
                );
            }, index * 2);
        });
    },

    unleashMythrilMagic: function (player) {
        const { x, y, z } = player.location;


        this.createDimensionalStorm(player);


        this.createMysticPortals(player);


        this.createRealityWaves(player);


        this.summonMythrilGuardians(player);
    },

    createDimensionalStorm: function (player) {
        const { x, y, z } = player.location;
        const dimension = player.dimension;
        let stormTime = 0;

        const stormInterval = system.runInterval(() => {
            stormTime++;


            for (let height = 0; height < 8; height++) {
                const radius = 4 - height * 0.3;
                const numParticles = 12;

                for (let i = 0; i < numParticles; i++) {
                    const angle = (i / numParticles) * Math.PI * 2 + stormTime * 0.4;
                    const spiralX = x + Math.cos(angle) * radius;
                    const spiralZ = z + Math.sin(angle) * radius;
                    const spiralY = y + height + Math.sin(stormTime * 0.1) * 0.5;

                    const particleType = ['standard', 'serpentine', 'minimalist'][i % 3];
                    MythrilUtils.spawnMythrilParticle(dimension, spiralX, spiralY, spiralZ, particleType);
                }
            }


            try {
                dimension.getEntities().forEach(entity => {
                    if (entity.id !== player.id && entity.typeId !== 'minecraft:item' &&
                        MythrilUtils.isNear(player, entity, 6, 5)) {

                        try {
                            entity.applyDamage(3, { cause: EntityDamageCause.magic });
                            this.createMysticBindingEffect(entity);
                        } catch (error) {
                            // Continuar
                        }
                    }
                });
            } catch (error) {
                // Continuar
            }

            if (stormTime >= 150) {
                system.clearRun(stormInterval);
                this.createStormFinale(player);
            }
        }, 2);
    },

    createMysticBindingEffect: function (entity) {

        let bindTime = 0;
        const bindInterval = system.runInterval(() => {
            bindTime++;

            try {
                const { x, y, z } = entity.location;


                for (let chain = 0; chain < 3; chain++) {
                    const chainRadius = 1 + chain * 0.5;
                    const chainHeight = y + chain * 0.3;

                    for (let link = 0; link < 8; link++) {
                        const linkAngle = (link / 8) * Math.PI * 2 + bindTime * 0.2;
                        const chainX = x + Math.cos(linkAngle) * chainRadius;
                        const chainZ = z + Math.sin(linkAngle) * chainRadius;

                        MythrilUtils.spawnMythrilParticle(entity.dimension, chainX, chainHeight, chainZ, 'serpentine');
                    }
                }

                if (bindTime >= 60) {
                    system.clearRun(bindInterval);
                }
            } catch (error) {
                system.clearRun(bindInterval);
            }
        }, 2);
    },

    createMysticPortals: function (player) {
        const { x, y, z } = player.location;
        const dimension = player.dimension;

        // Crear 4 portales alrededor del jugador
        const portalPositions = [
            { x: x + 4, z: z },
            { x: x - 4, z: z },
            { x: x, z: z + 4 },
            { x: x, z: z - 4 }
        ];

        portalPositions.forEach((portal, index) => {
            system.runTimeout(() => {
                this.createSinglePortal(dimension, portal.x, y + 1, portal.z, index);
            }, index * 20);
        });
    },

    createSinglePortal: function (dimension, x, y, z, portalId) {
        let portalTime = 0;
        const portalInterval = system.runInterval(() => {
            portalTime++;

            // Crear anillo de portal
            const radius = 2;
            for (let i = 0; i < 16; i++) {
                const angle = (i / 16) * Math.PI * 2 + portalTime * 0.3;
                const portalX = x + Math.cos(angle) * radius;
                const portalZ = z + Math.sin(angle) * radius;
                const portalY = y + Math.sin(portalTime * 0.2) * 0.3;

                MythrilUtils.spawnMythrilParticle(dimension, portalX, portalY, portalZ, 'minimalist');
            }

            // Interior del portal
            for (let i = 0; i < 8; i++) {
                const innerRadius = Math.random() * 1.5;
                const innerAngle = Math.random() * Math.PI * 2;
                const innerX = x + Math.cos(innerAngle) * innerRadius;
                const innerZ = z + Math.sin(innerAngle) * innerRadius;

                MythrilUtils.spawnMythrilParticle(dimension, innerX, y, innerZ, 'serpentine');
            }

            if (portalTime >= 80) {
                system.clearRun(portalInterval);
                this.closePortal(dimension, x, y, z);
            }
        }, 2);
    },

    closePortal: function (dimension, x, y, z) {
        // Implosión del portal
        for (let implode = 8; implode >= 1; implode--) {
            system.runTimeout(() => {
                const radius = implode * 0.3;
                for (let i = 0; i < implode * 2; i++) {
                    const angle = (i / (implode * 2)) * Math.PI * 2;
                    const implodeX = x + Math.cos(angle) * radius;
                    const implodeZ = z + Math.sin(angle) * radius;

                    MythrilUtils.spawnMythrilParticle(dimension, implodeX, y, implodeZ, 'standard');
                }
            }, (8 - implode) * 3);
        }
    },

    createRealityWaves: function (player) {
        const { x, y, z } = player.location;
        const dimension = player.dimension;

        // Ondas expansivas
        for (let wave = 1; wave <= 8; wave++) {
            system.runTimeout(() => {
                const waveRadius = wave * 3;
                const particlesInWave = wave * 8;

                for (let i = 0; i < particlesInWave; i++) {
                    const waveAngle = (i / particlesInWave) * Math.PI * 2;
                    const waveX = x + Math.cos(waveAngle) * waveRadius;
                    const waveZ = z + Math.sin(waveAngle) * waveRadius;
                    const waveY = y + Math.sin(wave * 0.5) * 2;

                    const particleType = wave % 2 === 0 ? 'serpentine' : 'minimalist';
                    MythrilUtils.spawnMythrilParticle(dimension, waveX, waveY, waveZ, particleType);
                }
            }, wave * 10);
        }
    },

    summonMythrilGuardians: function (player) {
        const guardianCount = 2;

        for (let guardian = 0; guardian < guardianCount; guardian++) {
            system.runTimeout(() => {
                this.createMythrilGuardian(player, guardian);
            }, guardian * 40);
        }
    },

    createMythrilGuardian: function (player, guardianId) {
        const angle = (guardianId / 2) * Math.PI * 2;
        const guardianX = player.location.x + Math.cos(angle) * 3;
        const guardianZ = player.location.z + Math.sin(angle) * 3;
        const guardianY = player.location.y + 2;

        let guardianTime = 0;
        const guardianInterval = system.runInterval(() => {
            guardianTime++;


            this.createGuardianBody(player.dimension, guardianX, guardianY, guardianZ, guardianTime);

            // El guardian patrulla y ataca enemigos
            this.guardianPatrol(player, guardianX, guardianY, guardianZ, guardianTime);

            if (guardianTime >= 200) {
                system.clearRun(guardianInterval);
                this.guardianDisappear(player.dimension, guardianX, guardianY, guardianZ);
            }
        }, 3);
    },

    createGuardianBody: function (dimension, x, y, z, time) {

        MythrilUtils.spawnMythrilParticle(dimension, x, y + 1.8, z, 'standard');


        for (let torso = 0; torso < 3; torso++) {
            MythrilUtils.spawnMythrilParticle(dimension, x, y + 1.5 - torso * 0.3, z, 'minimalist');
        }


        const armSwing = Math.sin(time * 0.3) * 0.5;
        MythrilUtils.spawnMythrilParticle(dimension, x + 0.5 + armSwing, y + 1.2, z, 'serpentine');
        MythrilUtils.spawnMythrilParticle(dimension, x - 0.5 - armSwing, y + 1.2, z, 'serpentine');


        const legWalk = Math.sin(time * 0.4) * 0.3;
        MythrilUtils.spawnMythrilParticle(dimension, x + 0.2 + legWalk, y + 0.5, z, 'serpentine');
        MythrilUtils.spawnMythrilParticle(dimension, x - 0.2 - legWalk, y + 0.5, z, 'serpentine');
    },

    guardianPatrol: function (player, gx, gy, gz, time) {
        try {
            player.dimension.getEntities().forEach(entity => {
                if (entity.id !== player.id && entity.typeId !== 'minecraft:item' &&
                    MythrilUtils.isNear({ location: { x: gx, y: gy, z: gz } }, entity, 5, 3)) {


                    this.guardianAttack(player.dimension, gx, gy, gz, entity);
                }
            });
        } catch (error) {

        }
    },

    guardianAttack: function (dimension, gx, gy, gz, target) {
        const { x: tx, y: ty, z: tz } = target.location;


        let projectileX = gx;
        let projectileY = gy;
        let projectileZ = gz;

        const dx = tx - gx;
        const dy = ty - gy;
        const dz = tz - gz;
        const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);

        if (distance > 0) {
            const speed = 0.5;
            const stepX = (dx / distance) * speed;
            const stepY = (dy / distance) * speed;
            const stepZ = (dz / distance) * speed;

            const projectileInterval = system.runInterval(() => {
                projectileX += stepX;
                projectileY += stepY;
                projectileZ += stepZ;

                MythrilUtils.spawnMythrilParticle(dimension, projectileX, projectileY, projectileZ, 'standard');


                const currentDistance = Math.sqrt(
                    Math.pow(projectileX - tx, 2) +
                    Math.pow(projectileY - ty, 2) +
                    Math.pow(projectileZ - tz, 2)
                );

                if (currentDistance < 1) {
                    system.clearRun(projectileInterval);
                    try {
                        target.applyDamage(4, { cause: EntityDamageCause.magic });
                        this.createProjectileImpact(dimension, projectileX, projectileY, projectileZ);
                    } catch (error) {

                    }
                }
            }, 2);


            system.runTimeout(() => {
                system.clearRun(projectileInterval);
            }, 60);
        }
    },

    createProjectileImpact: function (dimension, x, y, z) {
        for (let i = 0; i < 15; i++) {
            const angle = Math.random() * Math.PI * 2;
            const radius = Math.random() * 1.5;
            const height = Math.random() * 1.5;

            MythrilUtils.spawnMythrilParticle(
                dimension,
                x + Math.cos(angle) * radius,
                y + height,
                z + Math.sin(angle) * radius,
                'serpentine'
            );
        }
    },

    guardianDisappear: function (dimension, x, y, z) {

        for (let fade = 0; fade < 20; fade++) {
            system.runTimeout(() => {
                const fadeHeight = y + fade * 0.1;
                for (let i = 0; i < 6; i++) {
                    const fadeAngle = (i / 6) * Math.PI * 2;
                    const fadeX = x + Math.cos(fadeAngle) * (1 - fade * 0.05);
                    const fadeZ = z + Math.sin(fadeAngle) * (1 - fade * 0.05);

                    MythrilUtils.spawnMythrilParticle(dimension, fadeX, fadeHeight, fadeZ, 'minimalist');
                }
            }, fade * 2);
        }
    },

    createStormFinale: function (player) {
        const { x, y, z } = player.location;


        for (let finale = 1; finale <= 10; finale++) {
            system.runTimeout(() => {
                const finaleRadius = finale * 2;
                const finaleParticles = finale * 12;

                for (let i = 0; i < finaleParticles; i++) {
                    const finaleAngle = (i / finaleParticles) * Math.PI * 2;
                    const finaleX = x + Math.cos(finaleAngle) * finaleRadius;
                    const finaleZ = z + Math.sin(finaleAngle) * finaleRadius;
                    const finaleY = y + Math.random() * 4;

                    const particleTypes = ['standard', 'serpentine', 'minimalist'];
                    const finalParticle = particleTypes[i % 3];
                    MythrilUtils.spawnMythrilParticle(player.dimension, finaleX, finaleY, finaleZ, finalParticle);
                }
            }, finale * 5);
        }
    },

    createShatterRealityEffect: function (player) {
        const { x, y, z } = player.location;
        const dimension = player.dimension;

        for (let shatter = 0; shatter < 100; shatter++) {
            const shatterAngle = Math.random() * Math.PI * 2;
            const shatterRadius = Math.random() * 8;
            const shatterHeight = Math.random() * 6;

            system.runTimeout(() => {
                MythrilUtils.spawnMythrilParticle(
                    dimension,
                    x + Math.cos(shatterAngle) * shatterRadius,
                    y + shatterHeight,
                    z + Math.sin(shatterAngle) * shatterRadius,
                    ['standard', 'serpentine', 'minimalist'][shatter % 3]
                );
            }, shatter * 2);
        }

        player.onScreenDisplay.setActionBar('§d§l⚡ REALITY SHATTERED - MYTHRIL DISPERSED ⚡');
        world.sendMessage(`§d${player.name}§f's Mythril Sword has torn through reality itself!`);
    }
};

export function registerMythrilSword(itemComponentRegistry) {
    itemComponentRegistry.registerCustomComponent('myw:mythril_sword', {
        onHitEntity(eventData) {
            const { attackingEntity, hitEntity, itemStack } = eventData;

            if (attackingEntity.getGameMode() === GameMode.survival) {
                MythrilUtils.durability(attackingEntity, itemStack, EquipmentSlot.Mainhand);
            }

            MythrilMagic.onHitEffects(attackingEntity, hitEntity);
        },

        onUse({ itemStack, source }) {
            if (source.getGameMode() === GameMode.survival) {
                MythrilUtils.durability(source, itemStack, EquipmentSlot.Mainhand);
            }

            MythrilMagic.activateSpecialAbility(source);
        }
    });
}

export function initializeSwordPassiveEffects() {
    system.runInterval(() => {
        try {
            world.getAllPlayers().forEach(player => {
                const mainhand = player.getComponent('minecraft:equippable')?.getEquipment(EquipmentSlot.Mainhand);

                if (mainhand?.typeId === 'myw:mythril_sword') {
                    const { x, y, z } = player.location;


                    if (Math.random() < 0.7) {
                        const auraRadius = 3;
                        const auraAngle = Math.random() * Math.PI * 2;
                        const auraX = x + Math.cos(auraAngle) * auraRadius;
                        const auraZ = z + Math.sin(auraAngle) * auraRadius;
                        const auraY = y + Math.random() * 3;

                        const auraParticles = ['standard', 'serpentine', 'minimalist'];
                        const auraParticle = auraParticles[Math.floor(Math.random() * 3)];

                        MythrilUtils.spawnMythrilParticle(player.dimension, auraX, auraY, auraZ, auraParticle);
                    }


                    if (Math.random() < 0.1) {
                        MythrilMagic.createFloatingMysticSymbol(player.dimension, x, y - 0.5, z);
                    }
                }
            });
        } catch (error) {

        }
    }, 5);
}