import { AssemblerView } from "./AssemblerView.js";
import GridView from "../Assembler/Grid/GridView.js";
export default class MicroView extends AssemblerView {
    constructor(options) {
        super(options);
    }
    get autoFocusOnLoad() {
        return this.state.get("autoFocusOnLoad", true);
    }
    get grid() {
        return this.m_grid;
    }
    template() {
        return `<div class="-fb-layout-group -fb-layout-group-micro"></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() {
        if (!this.autoFocusOnLoad) {
            return;
        }
        let found = false;
        this.grid.forEachInput(input => {
            if (!found && input.editorModel.enabled) {
                input.editorView.focus();
                found = true;
            }
        });
    }
}
