import { world } from "@minecraft/server";
const defaultData = {
    whitelisted: [],
    sponges: {
        antiEnderSponge: true,
        clearWeatherSponge: true,
        decaySponge: true,
        effectsSponge: true,
        explosionSponge: true,
        feedingSponge: true,
        fireSponge: true,
        frostSponge: true,
        gravitySponge: true,
        growthSponge: true,
        healingSponge: true,
        jumpSponge: true,
        lavaSponge: true,
        lightSponge: true,
        magnetSponge: true,
        nightSponge: true,
        nightVisionSponge: true,
        poisonSponge: true,
        redstoneSponge: true,
        shadowSponge: true,
        silkSponge: true,
        smokeSponge: true,
        speedSponge: true,
        stickySponge: true,
        teleportSponge: true,
        thornSponge: true,
        tntSponge: true,
        voidSponge: true,
        witherSponge: true,
        xpSponge: true
    },
    version: 1
};
export class SettingsHandler {
    static getSettings() {
        try {
            const property = world.getDynamicProperty("wildphire_spg_settings");
            if (!property) {
                this.setSettings(defaultData);
                return defaultData;
            }
            // Validate that property is a string
            if (typeof property !== 'string') {
                this.setSettings(defaultData);
                return defaultData;
            }
            const parsedProperty = JSON.parse(property);
            // Validate the parsed settings structure
            const validatedSettings = this.validateSettings(parsedProperty);
            return validatedSettings;
        }
        catch (error) {
            // Try to reset to defaults
            try {
                this.setSettings(defaultData);
            }
            catch (resetError) {
                // Silent fail
            }
            return defaultData;
        }
    }
    static setSettings(newSettings) {
        try {
            // Validate settings before saving
            const validatedSettings = this.validateSettings(newSettings);
            world.setDynamicProperty("wildphire_spg_settings", JSON.stringify(validatedSettings));
            return true;
        }
        catch (error) {
            return false;
        }
    }
    static validateSettings(settings) {
        if (!settings || typeof settings !== 'object') {
            throw new Error("Settings is not a valid object");
        }
        // Validate version
        if (typeof settings.version !== 'number' || settings.version < 1) {
            settings.version = defaultData.version;
        }
        // Validate whitelisted array
        if (!Array.isArray(settings.whitelisted)) {
            settings.whitelisted = [];
        }
        else {
            // Ensure all whitelist entries are strings
            settings.whitelisted = settings.whitelisted.filter((item) => typeof item === 'string');
        }
        // Validate sponges object
        if (!settings.sponges || typeof settings.sponges !== 'object') {
            settings.sponges = { ...defaultData.sponges };
        }
        else {
            // Ensure all sponge settings exist and are booleans
            for (const [key, defaultValue] of Object.entries(defaultData.sponges)) {
                if (typeof settings.sponges[key] !== 'boolean') {
                    settings.sponges[key] = defaultValue;
                }
            }
            // Remove any unknown sponge settings
            for (const key of Object.keys(settings.sponges)) {
                if (!(key in defaultData.sponges)) {
                    delete settings.sponges[key];
                }
            }
        }
        return settings;
    }
    static isWhitelisted(playerName) {
        try {
            if (!playerName || typeof playerName !== 'string') {
                return false;
            }
            const settings = this.getSettings();
            return settings.whitelisted.includes(playerName);
        }
        catch (error) {
            return false;
        }
    }
    static addWhitelist(playerName) {
        try {
            if (!playerName || typeof playerName !== 'string') {
                return false;
            }
            const settings = this.getSettings();
            if (!settings.whitelisted.includes(playerName)) {
                settings.whitelisted.push(playerName);
                return this.setSettings(settings);
            }
            return true; // Already whitelisted
        }
        catch (error) {
            return false;
        }
    }
    static removeWhitelist(playerName) {
        try {
            if (!playerName || typeof playerName !== 'string') {
                return false;
            }
            const settings = this.getSettings();
            if (settings.whitelisted.includes(playerName)) {
                settings.whitelisted = settings.whitelisted.filter(name => name !== playerName);
                return this.setSettings(settings);
            }
            return true; // Already not whitelisted
        }
        catch (error) {
            return false;
        }
    }
    static resetWhitelist() {
        try {
            const settings = this.getSettings();
            settings.whitelisted = [];
            return this.setSettings(settings);
        }
        catch (error) {
            return false;
        }
    }
    static getWhitelist() {
        try {
            const settings = this.getSettings();
            return [...settings.whitelisted]; // Return a copy to prevent direct modification
        }
        catch (error) {
            return [];
        }
    }
    static toggleSponge(spongeName) {
        try {
            if (!spongeName || typeof spongeName !== 'string') {
                return false;
            }
            const settings = this.getSettings();
            if (!(spongeName in settings.sponges)) {
                return false;
            }
            settings.sponges[spongeName] = !settings.sponges[spongeName];
            return this.setSettings(settings);
        }
        catch (error) {
            return false;
        }
    }
    static isSpongeEnabled(spongeName) {
        try {
            if (!spongeName || typeof spongeName !== 'string') {
                return true; // Default to enabled for safety
            }
            const settings = this.getSettings();
            if (!(spongeName in settings.sponges)) {
                return true;
            }
            return settings.sponges[spongeName];
        }
        catch (error) {
            return true; // Default to enabled for safety
        }
    }
    static getAllSpongeToggleStates() {
        try {
            const settings = this.getSettings();
            return { ...settings.sponges }; // Return a copy to prevent direct modification
        }
        catch (error) {
            return { ...defaultData.sponges };
        }
    }
}
