import { FormCancelationReason, ModalFormData } from "@minecraft/server-ui";
import { system } from "@minecraft/server";
import FSModalFormResponse from "../resps/FSModalFormResp";
import FSFormManager from "../FSFormManager";
import FSLocalizedString from "../../FSLocalizedString";
const DEFAULT_MAX_RETRY_COUNT = 30;
const RETRY_DELIVERY_INTERVAL = 10; 
class FSModalFormData extends ModalFormData {
    constructor(formId, restoreFromStorage = false) {
        super();
        this.formDataStorage = {}; 
        this._navController = null;
        this._owningNavSessionId = ""; 
        this._responseCallback = () => { };
        this.response = null; 
        this._title = "";
        this._schema = [];
        this._retryCount = 0; 
        this.maxRetries = DEFAULT_MAX_RETRY_COUNT; 
        this.formId = formId;
        this.fieldMap = {};
        if (restoreFromStorage) {
            this._syncFormDataStorage();
        }
        else {
            this.formDataStorage = {}; 
        }
    }
    _syncFormDataStorage() {
        if (this._navController) {
            this.formDataStorage = this._navController._unifiedResponseDataByFormId[this.formId] || {}; 
        }
        else {
            this.formDataStorage = {}; 
        }
    }
    setTitle(title) {
        this._title = title; 
        super.title(title);
        return this;
    }
    title(title) {
        this.setTitle(title);
        return this;
    }
    addDropdown(id, label, options, defaultValueIndex, isRestoring = false, validator) {
        if (!isRestoring) {
            this._schema.push({
                id: id,
                label: label,
                type: "dropdown",
                options: options,
                default: defaultValueIndex
            });
        }
        const storedValue = this.formDataStorage[id];
        const newDefaultValueIndex = storedValue !== undefined ? storedValue : defaultValueIndex;
        this.fieldMap[id] = Object.keys(this.fieldMap).length;
        super.dropdown(label, options, newDefaultValueIndex);
        return this;
    }
    dropdown(label, options, defaultValueIndex, isRestoring = false) {
        console.error("[!] Deprecated method FSModalFormData.dropdown() called. Please use FSModalFormData.addDropdown() instead.");
        return this;
    }
    addTextField(id, label, placeholderText, defaultValue, isRestoring = false, validator) {
        if (!isRestoring) {
            this._schema.push({
                id: id,
                label: label,
                type: "textfield",
                placeholder: placeholderText,
                default: defaultValue
            });
        }
        const storedValue = this.formDataStorage[id];
        const newDefaultValue = storedValue !== undefined ? storedValue : defaultValue;
        this.fieldMap[id] = Object.keys(this.fieldMap).length;
        super.textField(label, placeholderText, newDefaultValue);
        return this;
    }
    textField(label, placeholderText, defaultValue) {
        console.error("[!] Deprecated method FSModalFormData.textField() called. Please use FSModalFormData.addTextField() instead.");
        return this;
    }
    addSlider(id, label, minimumValue, maximumValue, valueStep, defaultValue = null, isRestoring = false, validator) {
        if (!isRestoring) {
            this._schema.push({
                id: id,
                label: label,
                type: "slider",
                min: minimumValue,
                max: maximumValue,
                step: valueStep,
                default: defaultValue
            });
        }
        const storedValue = this.formDataStorage[id];
        const newDefaultValue = storedValue !== undefined ? storedValue : defaultValue;
        this.fieldMap[id] = Object.keys(this.fieldMap).length;
        super.slider(label, minimumValue, maximumValue, valueStep, newDefaultValue);
        return this;
    }
    slider(label, minimumValue, maximumValue, valueStep, defaultValue = null) {
        console.error("[!] Deprecated method FSModalFormData.slider() called. Please use FSModalFormData.addSlider() instead.");
        return this;
    }
    addToggle(id, label, defaultValue, isRestoring = false, validator) {
        if (!isRestoring) {
            this._schema.push({
                id: id,
                label: label,
                type: "toggle",
                default: defaultValue
            });
        }
        const storedValue = this.formDataStorage[id];
        let newDefaultValue = storedValue !== undefined ? storedValue : defaultValue;
        if (newDefaultValue === null) {
            newDefaultValue = 25;
        }
        this.fieldMap[id] = Object.keys(this.fieldMap).length;
        super.toggle(label, newDefaultValue);
        return this;
    }
    toggle(label, defaultValue) {
        console.error("[!] Deprecated method FSModalFormData.toggle() called. Please use FSModalFormData.addToggle() instead.");
        return this;
    }
    clone() {
        const clone = new FSModalFormData(this.formId);
        clone.formId = this.formId;
        clone._title = this._title;
        clone._schema = this._schema;
        clone.fieldMap = this.fieldMap;
        clone.formDataStorage = this.formDataStorage;
        clone._navController = this._navController;
        clone.fieldMap = {}; 
        clone._responseCallback = this._responseCallback;
        clone.setTitle(clone._title);
        for (const field of this._schema) {
            switch (field.type) {
                case "slider":
                    clone.addSlider(field.id, field.label, field.min, field.max, field.step, field.default, true);
                    break;
                case "toggle":
                    clone.addToggle(field.id, field.label, field.default, true);
                    break;
                case "dropdown":
                    clone.addDropdown(field.id, field.label, field.options, field.default, true);
                    break;
                case "textfield":
                    clone.addTextField(field.id, field.label, field.placeholder, field.default, true);
                    break;
                default:
                    console.error(`Unknown field type ${field.type} in schema for form ${this.formId}.`);
                    break;
            }
        }
        return clone;
    }
    show(player) {
        console.error("[!] Deprecated method FSModalFormData.show() called. Please use FSModalFormData.push() instead.");
        return super.show(player);
    }
    push(player, isRestoring = false, callback) {
        FSFormManager.sharedInstance().navigationControllerForPlayer(player, isRestoring).push(this, callback);
    }
    _show(player) {
        const promise = super.show(player);
        promise.then(response => {
            if (response.cancelationReason == FormCancelationReason.UserBusy) {
                this._retryCount++;
                if (this._retryCount < this.maxRetries) {
                    if (this._navController) {
                        if (this._navController._retryIntervalRef !== -1) {
                            system.clearRun(this._navController._retryIntervalRef);
                            this._navController._retryIntervalRef = -1;
                        }
                        this._navController._retryIntervalRef = system.runTimeout(() => {
                            this._show(player);
                        }, RETRY_DELIVERY_INTERVAL);
                    }
                }
                else {
                    player.runCommand("/playsound conduit.deactivate @s ~ ~ ~ 1 0.5");
                    player.sendMessage({
                        translate: FSLocalizedString.localize("ui.user_busy.timeout.2", "§cThat didn`t work! Try again.", "Generic error message")
                    });
                }
            }
            if (this._responseCallback) {
                var fsResponse = new FSModalFormResponse(response, this);
                if (!response.canceled) {
                    if (response.formValues || response) {
                        this.formDataStorage = fsResponse.data;
                        this._navController._unifiedResponseData = {
                            ...this._navController?._unifiedResponseData,
                            ...fsResponse.data
                        };
                        this._navController._unifiedResponseDataByFormId[this.formId] = {
                            ...this._navController._unifiedResponseDataByFormId[this.formId],
                            ...fsResponse.data
                        };
                    }
                }
                this.response = fsResponse; 
                this._responseCallback(fsResponse);
            }
        });
        return promise;
    }
}
export default FSModalFormData;

//# sourceMappingURL=../../../../../../_SBUNK_BPDebug/fs_sbunk/fs/forms/ui/FSModalFormData.js.map
