"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mobx_react_1 = require("mobx-react");
const React = require("react");
const ListBoxUtil_1 = require("../../util/ListBoxUtil");
const Keys_1 = require("../../util/Keys");
const PiLogging_1 = require("../../util/PiLogging");
const Styles_1 = require("./styles/Styles");
const Rendering_1 = require("./Rendering");
const LOGGER = new PiLogging_1.PiLogger("PiComponent");
let PiComponent = class PiComponent extends React.Component {
    constructor() {
        super(...arguments);
        this.setElement = (element) => {
            if (element) {
                this.props.editor.projectedElement = element;
            }
        };
        this.onKeyDown = (event) => __awaiter(this, void 0, void 0, function* () {
            PiLogging_1.EVENT_LOG.info(this, "onKeyDown: " + event.key);
            event.persist();
            if (event.ctrlKey) {
                switch (event.keyCode) {
                    case Keys_1.ARROW_UP:
                        this.props.editor.selectParentBox();
                        this.eventHandled(event);
                        break;
                    case Keys_1.ARROW_DOWN:
                        this.props.editor.selectFirstLeafChildBox();
                        this.eventHandled(event);
                        break;
                }
            }
            else if (event.shiftKey) {
                switch (event.keyCode) {
                    case Keys_1.TAB:
                        yield this.props.editor.selectPreviousLeaf();
                        this.eventHandled(event);
                        break;
                }
            }
            else if (event.altKey) {
            }
            else {
                switch (event.keyCode) {
                    case Keys_1.BACKSPACE:
                    case Keys_1.ARROW_LEFT:
                        yield this.props.editor.selectPreviousLeaf();
                        this.eventHandled(event);
                        break;
                    case Keys_1.DELETE:
                        this.props.editor.deleteBox(this.props.editor.selectedBox);
                        break;
                    case Keys_1.TAB:
                    case Keys_1.ARROW_RIGHT:
                        this.props.editor.selectNextLeaf();
                        this.eventHandled(event);
                        break;
                    case Keys_1.ARROW_DOWN:
                        LOGGER.info(this, "Down: " + this.props.editor.selectedBox.role);
                        const down = ListBoxUtil_1.boxBelow(this.props.editor.selectedBox);
                        if (down !== null) {
                            this.props.editor.selectBox(down);
                        }
                        break;
                    case Keys_1.ARROW_UP:
                        LOGGER.info(this, "Up: " + this.props.editor.selectedBox.role);
                        const up = ListBoxUtil_1.boxAbove(this.props.editor.selectedBox);
                        if (up !== null) {
                            this.props.editor.selectBox(up);
                        }
                        break;
                }
            }
        });
    }
    render() {
        PiLogging_1.RENDER_LOG.info(this, "");
        if (this.props.editor.rootBox === undefined) {
            return null;
        }
        return (React.createElement("div", { className: Styles_1.STYLES.projectit, onKeyDown: this.onKeyDown, ref: this.setElement, tabIndex: 0 },
            React.createElement(Rendering_1.RenderBox, { box: this.props.editor.rootBox, editor: this.props.editor })));
    }
    eventHandled(e) {
        e.preventDefault();
        e.stopPropagation();
    }
};
PiComponent = __decorate([
    mobx_react_1.observer
], PiComponent);
exports.PiComponent = PiComponent;
//# sourceMappingURL=PiComponent.js.map