import { enumToString, getOrCreateWorldData, setWorldData, stringToEnum, translate } from "./miscUtils";
import { addonRes } from "../main";
export class ConfigEntry {
    constructor(name, defaultValue) {
        this.configKey = addonRes(name);
        this.name = name;
        this.defaultValue = defaultValue;
    }
    getValue() {
        return this.cachedValue;
    }
}
export class NumberEntry extends ConfigEntry {
    constructor(name, min, max, defaultValue, step) {
        super(name, defaultValue);
        this.min = min;
        this.max = max;
        this.defaultValue = defaultValue;
        this.step = step;
        this.cachedValue = getOrCreateWorldData(this.configKey, defaultValue);
    }
    addToForm(form, player, ui) {
        form.slider(ui.translate(this.name), this.min, this.max, this.step, this.cachedValue);
    }
    setValue(responseValue, player, book) {
        this.cachedValue = responseValue;
        setWorldData(this.configKey, responseValue);
    }
}
export class StringEntry extends ConfigEntry {
    constructor(name, defaultValue, validator) {
        super(name, defaultValue);
        this.defaultValue = defaultValue;
        this.validator = validator;
        this.cachedValue = getOrCreateWorldData(this.configKey, defaultValue);
    }
    addToForm(form, player, ui) {
        form.textField(ui.translate(this.name), this.cachedValue);
    }
    setValue(responseValue, player, book) {
        if (this.validator(responseValue)) {
            this.cachedValue = responseValue;
            setWorldData(this.configKey, responseValue);
        }
        else {
            player.sendMessage(translate("message", "invalid_config_string", [this.configKey]));
        }
    }
}
export class BooleanEntry extends ConfigEntry {
    constructor(name, defaultValue) {
        super(name, defaultValue);
        this.defaultValue = defaultValue;
        this.cachedValue = getOrCreateWorldData(this.configKey, defaultValue);
    }
    addToForm(form, player, ui) {
        form.toggle(ui.translate(this.name), this.cachedValue);
    }
    setValue(responseValue, player, book) {
        this.cachedValue = responseValue;
        setWorldData(this.configKey, responseValue);
    }
}
export class EnumEntry extends ConfigEntry {
    constructor(name, enumType, // The enum object
    defaultValue) {
        // Store the default value as a string representation of the enum value
        super(name, defaultValue);
        this.enumType = enumType;
        const stringData = getOrCreateWorldData(this.configKey, enumToString(enumType, defaultValue));
        this.cachedValue = stringToEnum(enumType, stringData);
    }
    addToForm(form, player, ui) {
        const options = Object.keys(this.enumType).map(key => {
            return translate("enum", key.toLowerCase());
        });
        const valueIndex = Object.values(this.enumType).indexOf(this.cachedValue);
        form.dropdown(ui.translate(this.name), options, valueIndex);
    }
    setValue(responseValue, player, book) {
        //hack. actually we know response here will be a int
        const responseEnumValue = Object.values(this.enumType)[responseValue];
        this.cachedValue = responseValue;
        setWorldData(this.configKey, enumToString(this.enumType, responseEnumValue));
    }
}
