import { FormCancelationReason } from '@minecraft/server-ui';
import FSModalFormData from './ui/FSModalFormData';
import FSActionFormData from './ui/FSActionFormData';
import FSUtil from '../../FSUtil';
const MAX_STACK_SIZE = 100; 
const ENABLE_DEBUGGING = false;
class FSFormNavigationController {
    constructor(owner) {
        this._player = null;
        this._navSessionId = '';
        this._unifiedResponseDataByFormId = {};
        this._retryIntervalRef = -1; 
        this._player = owner;
        this._navSessionId = FSUtil.uuid();
        this._stack = [];
        this._unifiedResponseData = {}; 
        this._unifiedResponseDataByFormId = {}; 
    }
    push(form, callback) {
        this._push(form, false, callback);
    }
    _push(form, isRestoring = false, callback) {
        if (this._stack.length >= MAX_STACK_SIZE) {
            ENABLE_DEBUGGING ? console.error('Stack exceeded hard limit, the stack was reset for player: ' + this._player.nameTag) : null;
            this._stack = [];
        }
        if (form instanceof FSModalFormData) {
            form._responseCallback = callback;
        }
        if (form instanceof FSActionFormData) {
            form._responseCallback = callback;
        }
        form._navController = this; 
        form._owningNavSessionId = this._navSessionId; 
        if (!isRestoring) {
            this._stack.push(form);
        }
        form._show(this._player).then((response) => {
            if (response.canceled) {
                if (response.cancelationReason === FormCancelationReason.UserBusy) {
                    return;
                }
            }
            ENABLE_DEBUGGING ? console.error('Stack is length: ' + this._stack.length + ", " + this._stack.map((form) => { return form.formId; }).join(', ')) : null;
            if (response.canceled) {
                this._pop();
                ENABLE_DEBUGGING ? console.error('Stack is now length: ' + this._stack.length) : null;
                ENABLE_DEBUGGING ? console.error('Stack is length: ' + this._stack.length + ", " + this._stack.map((form) => { return form.formId; }).join(', ')) : null;
            }
            if (response.canceled) {
                ENABLE_DEBUGGING ? console.error('Form was cancelled') : null;
                if (this._stack.length > 0) {
                    var nextForm = this._stack[this._stack.length - 1];
                    if (nextForm instanceof FSModalFormData) {
                        const reconstructedForm = nextForm.clone();
                        this._stack[this._stack.length - 1] = reconstructedForm;
                        nextForm = reconstructedForm;
                        this._push(nextForm, true, nextForm._responseCallback);
                    }
                    else if (nextForm instanceof FSActionFormData) {
                        const reconstructedForm = nextForm.clone();
                        this._stack[this._stack.length - 1] = reconstructedForm;
                        this._push(nextForm, true, nextForm._responseCallback);
                    }
                    else {
                        ENABLE_DEBUGGING ? console.error('Form does not implement cancellation protocol') : null;
                    }
                }
                else {
                }
            }
        }).catch((error) => {
            ENABLE_DEBUGGING ? console.error('Caught exception') : null;
            ENABLE_DEBUGGING ? console.error(error) : null;
            this._pop();
        });
    }
    _pop() {
        if (this._stack.length > 0) {
            this._stack.pop();
        }
    }
    clear() {
        this._stack = [];
        this._unifiedResponseData = {}; 
        this._unifiedResponseDataByFormId = {}; 
    }
    getResponseData(keyedByFormId = false) {
        return keyedByFormId ? this._unifiedResponseDataByFormId : this._unifiedResponseData;
    }
    finish(keyedByFormId = false) {
        var responseData = keyedByFormId ? this._unifiedResponseDataByFormId : this._unifiedResponseData;
        this.clear();
        return responseData;
    }
}
export default FSFormNavigationController;

//# sourceMappingURL=../../../../../../_SBUNK_BPDebug/fs_sbunk/fs/libs/forms/FSFormNavController.js.map
