import { AssemblerView } from "./AssemblerView.js";
import GridView from "../Assembler/Grid/GridView.js";
export default class MicroView extends AssemblerView {
    get grid() {
        return this.m_grid;
    }
    template() {
        return `<div style="position:absolute;inset:10px;"></div>`;
    }
    onActivate(e) {
        super.onActivate(e);
        this.m_grid = new GridView({
            source: this.source
        });
    }
    onInitialView() {
        super.onInitialView();
        if (this.m_grid) {
            this.controls.add(this.m_grid);
        }
    }
    onFocusedView() {
        super.onFocusedView();
        this.focusInput();
    }
    focusInput() {
        if (this.focusPostbackInput()) {
            return;
        }
        if (this.focusLastFocusedInput()) {
            return;
        }
        this.focusFirstFocusableInput();
    }
    focusPostbackInput() {
        let result = false;
        let postbackInputId = this.source.globals.storage.postbackInputId;
        let isPostback = !Jade.String.isNullOrWhiteSpace(postbackInputId);
        if (isPostback) {
            this.grid.forEachInput(input => {
                if (result === false && input.editorModel.uid === postbackInputId) {
                    input.editorView.focus();
                    result = true;
                    this.source.globals.storage.postbackInputId == null;
                }
            });
        }
        return result;
    }
    focusLastFocusedInput() {
        let result = false;
        let focusedInputId = this.source.locals.storage.focusedInputId;
        let hasLastFocued = !Jade.String.isNullOrWhiteSpace(focusedInputId);
        if (hasLastFocued) {
            this.grid.forEachInput(input => {
                if (result === false && input.editorModel.uid === focusedInputId) {
                    input.editorView.focus();
                    result = true;
                }
            });
        }
        return result;
    }
    focusFirstFocusableInput() {
        let found = false;
        this.grid.forEachInput(input => {
            if (!found && input.editorModel.enabled) {
                input.editorView.focus();
                found = true;
            }
        });
    }
}
