"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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 SelectBox_1 = require("../../boxes/SelectBox");
const AbstractChoiceComponent_1 = require("./AbstractChoiceComponent");
const DropdownOption_1 = require("../dropdown/DropdownOption");
const BehaviorUtils_1 = require("../../../util/BehaviorUtils");
const PiLogging_1 = require("../../../util/PiLogging");
const LOGGER = new PiLogging_1.PiLogger("SelectComponent");
let SelectComponent = class SelectComponent extends AbstractChoiceComponent_1.AbstractChoiceComponent {
    constructor(props) {
        super(props);
        this.handleSelected = (optionId) => {
            LOGGER.info(this, "handleSelected: " + optionId);
            if (optionId === "ESCAPE") {
                this.dropdownIsOpen = false;
                return;
            }
            if (SelectBox_1.isSelectBox(this.props.box)) {
                let option = (!!optionId ? this.getOptions()[DropdownOption_1.findExactOptionId(this.getOptions(), optionId)] : null);
                LOGGER.info(this, "handleSelected option: " + (!!option ? option.label : "null"));
                this.props.box.setSelectedOption(option);
                LOGGER.info(this, "handleSelected option done");
                this.dropdownIsOpen = false;
                this.element.innerText = (!!option ? option.label : "");
                this.text = (!!option ? option.label : "");
                this.hasError = false;
                this.element.focus();
                this.setCaretToMostRight();
            }
        };
        this.getOptions = () => {
            if (SelectBox_1.isSelectBox(this.props.box)) {
                if (!!this.element.innerText) {
                    LOGGER.info(this, "innerText [" + this.element.innerText + "]");
                    const options = this.props.box.getOptions().filter(s => {
                        LOGGER.info(this, "filtering [" + s.label + "]");
                        return s.label.startsWith(this.element.innerText);
                    });
                    LOGGER.info(this, "options [" + JSON.stringify(options) + "]");
                    return options;
                }
                else {
                    return this.props.box.getOptions();
                }
            }
            return [];
        };
        this.stopEditing = () => {
            LOGGER.info(this, "Stop Editing");
            this.isEditing = false;
            this.props.box.caretPosition = -1;
            if (!!this.element) {
                const selected = this.props.box.getSelectedOption();
                if (selected !== null) {
                    this.text = selected.label;
                }
                this.element.innerText = this.text;
            }
            this.dropdownIsOpen = false;
            this.hasError = false;
            this.clearSelection();
        };
        this.handleStringInput = (s) => __awaiter(this, void 0, void 0, function* () {
            LOGGER.info(this, "OOO handleStringInput for box " + this.props.box.role);
            const optionIndex = DropdownOption_1.findExactOption(this.getOptions(), s);
            if (optionIndex !== -1) {
                LOGGER.info(this, "SELECT COMPONENT SELECTED " + optionIndex);
                this.handleSelected(this.getOptions()[optionIndex].id);
                this.hasError = false;
                this.dropdownIsOpen = false;
                return BehaviorUtils_1.BehaviorExecutionResult.EXECUTED;
            }
            const partial = DropdownOption_1.findOption(this.getOptions(), s);
            if (partial !== -1) {
                this.hasError = false;
                this.setSelectedOption(this.getOptions()[0].id);
                return BehaviorUtils_1.BehaviorExecutionResult.PARTIAL_MATCH;
            }
            else {
                this.hasError = true;
                return BehaviorUtils_1.BehaviorExecutionResult.NO_MATCH;
            }
        });
        this.text = "";
        this.updateFromProps(props);
    }
    updateFromProps(props) {
        LOGGER.info(this, "updateFromProps");
        const selected = props.box.getSelectedOption();
        if (!!selected) {
            this.text = selected.label;
        }
        this.subscribeToBoxEvents(props.box);
    }
};
SelectComponent = __decorate([
    mobx_react_1.observer,
    __metadata("design:paramtypes", [Object])
], SelectComponent);
exports.SelectComponent = SelectComponent;
//# sourceMappingURL=SelectComponent.js.map