import { system } from "@minecraft/server";
import ScriptingModule from "../../scriptingModule";
// CustomEffectModule class
export default class CustomEffectModule extends ScriptingModule {
    constructor() {
        super("CustomEffectModule");
        this.activeCustomEffects = new Map();
    }
    static getInstance() {
        if (!CustomEffectModule.instance) {
            CustomEffectModule.instance = new CustomEffectModule();
            CustomEffectModule.instance.initialize();
        }
        return CustomEffectModule.instance;
    }
    onInitialize() { }
    isValidVector3(vec) {
        return !isNaN(vec.x) && !isNaN(vec.y) && !isNaN(vec.z);
    }
    applyCustomEffect(target, customEffect, globalDuration) {
        const defaultCustomEffect = {
            type: customEffect.type,
            duration: customEffect.duration ?? globalDuration,
            properties: {
                amplifier: customEffect.properties?.amplifier ?? 1,
                showParticles: customEffect.properties?.showParticles ?? false,
            },
        };
        if (!this.activeCustomEffects.has(target)) {
            this.activeCustomEffects.set(target, new Map());
        }
        const currentEffects = this.activeCustomEffects.get(target);
        if (currentEffects.has(customEffect.type)) {
            const existingEffect = currentEffects.get(customEffect.type);
            system.clearRun(existingEffect.timeoutId); // Clear the existing timeout
            const newDuration = Math.max(existingEffect.effect.duration, defaultCustomEffect.duration);
            defaultCustomEffect.duration = newDuration;
        }
        const timeoutId = system.runTimeout(() => {
            this.removeCustomEffect(target, customEffect.type);
        }, defaultCustomEffect.duration * 20);
        currentEffects.set(customEffect.type, {
            effect: defaultCustomEffect,
            timeoutId,
        });
        switch (customEffect.type) {
        }
    }
    removeCustomEffect(target, customEffectType) {
        const currentEffects = this.activeCustomEffects.get(target);
        if (currentEffects && currentEffects.has(customEffectType)) {
            system.clearRun(currentEffects.get(customEffectType).timeoutId); // Clear the timeout
            currentEffects.delete(customEffectType);
        }
    }
    isCustomEffectActive(player, customEffectType) {
        return this.activeCustomEffects.get(player)?.has(customEffectType) ?? false;
    }
    getCustomEffectProperties(player, customEffectType) {
        return this.activeCustomEffects.get(player)?.get(customEffectType)?.effect;
    }
}
CustomEffectModule.DISTANCE_THRESHOLD = 15; // Distance in blocks
