"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 MatchUtil_1 = require("../../../util/MatchUtil");
const PiAction_1 = require("../../PiAction");
const AbstractChoiceComponent_1 = require("./AbstractChoiceComponent");
const BehaviorUtils_1 = require("../../../util/BehaviorUtils");
const PiLogging_1 = require("../../../util/PiLogging");
const LOGGER = new PiLogging_1.PiLogger("AliasComponent");
let AliasComponent = class AliasComponent extends AbstractChoiceComponent_1.AbstractChoiceComponent {
    constructor(props) {
        super(props);
        this.handleSelected = (optionId) => {
            LOGGER.info(this, "handleSelected " + optionId);
            BehaviorUtils_1.executeBehavior(this.props.box, optionId, this.props.editor);
            this.dropdownIsOpen = false;
        };
        this.getOptions = () => {
            const result = this.props.editor.behaviors
                .filter(a => a.activeInBoxRoles.includes(this.props.box.role) && MatchUtil_1.MatchUtil.partialMatch(this.element.innerText, a.trigger))
                .map(a => {
                return {
                    id: PiAction_1.triggerToString(a.trigger),
                    label: PiAction_1.triggerToString(a.trigger),
                    description: "alias " + PiAction_1.triggerToString(a.trigger)
                };
            });
            return result;
        };
        this.handleStringInput = (s) => __awaiter(this, void 0, void 0, function* () {
            LOGGER.info(this, "handleStringInput for box " + this.props.box.role + " isOpen " + this.OPEN + " text is [" + this.text + "]");
            const options = this.getOptions();
            if (options.length > 1) {
                LOGGER.info(this, " > 1 option");
                this.hasError = false;
                this.setSelectedOption(options[0].id);
                this.dropdownIsOpen = true;
            }
            const aliasResult = yield BehaviorUtils_1.executeBehavior(this.props.box, s, this.props.editor);
            switch (aliasResult) {
                case BehaviorUtils_1.BehaviorExecutionResult.EXECUTED:
                    if (this.element) {
                        this.element.innerText = "";
                    }
                    this.hasError = false;
                    break;
                case BehaviorUtils_1.BehaviorExecutionResult.PARTIAL_MATCH:
                    LOGGER.info(this, "PARTIAL_MATCH");
                    this.hasError = false;
                    this.dropdownIsOpen = true;
                    break;
                case BehaviorUtils_1.BehaviorExecutionResult.NO_MATCH:
                    LOGGER.info(this, "NO MATCH");
                    this.hasError = true;
                    break;
            }
            return aliasResult;
        });
        this.onClick = () => __awaiter(this, void 0, void 0, function* () {
            PiLogging_1.EVENT_LOG.info(this, "onClick");
            this.startEditing();
            if (!!this.element && this.element.innerText === this.props.box.placeholder) {
                this.setCaretToMostLeft();
            }
            this.props.box.caretPosition = this.getCaretPosition();
        });
    }
};
AliasComponent = __decorate([
    mobx_react_1.observer,
    __metadata("design:paramtypes", [Object])
], AliasComponent);
exports.AliasComponent = AliasComponent;
//# sourceMappingURL=AliasComponent.js.map