import { ActionFormData, FormCancelationReason } from "@minecraft/server-ui";
import { system } from "@minecraft/server";
import FSActionFormResponse from "../resps/FSActionFormResp";
import FSFormManager from "../FSFormManager";
import FSLocalizedString from "../../FSLocalizedString";
const DEFAULT_MAX_RETRY_COUNT = 30;
const RETRY_DELIVERY_INTERVAL = 10; 
export default class FSActionFormData extends ActionFormData {
    constructor(formId, restoreFromStorage = false) {
        super();
        this.formDataStorage = {}; 
        this._navController = null;
        this._owningNavSessionId = ""; 
        this._responseCallback = () => { };
        this.response = null; 
        this._title = "";
        this._body = "";
        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;
    }
    addButton(id, text, iconPath, isRestoring = false) {
        if (!isRestoring) {
            this._schema.push({
                id: id,
                text: text,
                type: "button"
            });
        }
        this.fieldMap[id] = Object.keys(this.fieldMap).length;
        super.button(text, iconPath);
        return this;
    }
    button(text, iconPath) {
        return super.button(text, iconPath);
    }
    setBody(body) {
        super.body(body);
        return this;
    }
    setMessage(body) {
        return this.setBody(body);
    }
    body(body) {
        return super.body(body);
    }
    clone() {
        return this; 
    }
    push(player, isRestoring = false, callback) {
        FSFormManager.sharedInstance().navigationControllerForPlayer(player, isRestoring).push(this, callback);
    }
    show(player) {
        return this._show(player);
    }
    _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.1", "§cThat didn`t work! Try again.", "Generic error message")
                    });
                }
            }
            if (this._responseCallback) {
                var fsResponse = new FSActionFormResponse(response, this);
                if (!response.canceled) {
                    if (typeof response.selection !== "undefined") {
                        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);
            }
            else {
                console.error("No response callback for FSActionFormData: " + this.formId);
            }
        });
        return promise;
    }
}

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