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);
};
import { ServiceBase } from '../../data/service-base';
import { bindable, bindingMode, customElement, TaskQueue } from "aurelia-framework";
import * as breeze from 'breeze-client';
import { KeyboardShortcut } from "../../constants/constants";
export var UIMultiValueInput = (function () {
    function UIMultiValueInput(element, taskQueue) {
        this.element = element;
        this.taskQueue = taskQueue;
        this.placeholder = "";
        this.disabled = false;
        this.readonly = false;
        this.type = "text";
        this.targetService = null;
    }
    UIMultiValueInput.prototype.attached = function () {
        var _this = this;
        var navigationProperty = this.entity.entityType.navigationProperties.find(function (fkp) { return fkp.name == _this.targetArrayName; });
        this.targetIdName = navigationProperty.invForeignKeyNames[0];
        this.values = this.entity[this.targetArrayName];
    };
    UIMultiValueInput.prototype.bind = function () {
        if (this.targetService == null)
            throw new Error("Parameter 'targetService' is mandatory");
    };
    Object.defineProperty(UIMultiValueInput.prototype, "canEdit", {
        get: function () {
            return (false == (this.readonly || this.disabled));
        },
        enumerable: false,
        configurable: true
    });
    UIMultiValueInput.prototype.addNewLine = function () {
        var _a;
        var _this = this;
        if (this.canEdit) {
            var targetEntity = this.targetService.createEntity((_a = {},
                _a[this.targetIdName] = this.entity['id'],
                _a), true);
            if (this.callback != null) {
                this.callback({ targetEntity: targetEntity });
            }
            this.values.push(targetEntity);
            this.taskQueue.queueMicroTask(function () {
                var inputs = _this.element.getElementsByTagName('input');
                if (inputs.length > 0) {
                    inputs[inputs.length - 1].focus();
                }
            });
        }
    };
    UIMultiValueInput.prototype.blur = function (entry) {
        if (false == this.isEntryValid(entry)) {
            this.delete(entry);
        }
    };
    UIMultiValueInput.prototype.keypress = function ($event) {
        if ($event.key === KeyboardShortcut.Enter) {
            $event.stopEvent();
            this.addNewLine();
        }
        return true;
    };
    UIMultiValueInput.prototype.isEntryValid = function (entry) {
        return entry[this.optionsText] !== null && (typeof entry[this.optionsText] !== "string" || entry[this.optionsText].trim().length > 0);
    };
    Object.defineProperty(UIMultiValueInput.prototype, "isLastEntryValid", {
        get: function () {
            return Array.isArray(this.values) && (this.values.length == 0 || this.isEntryValid(this.values[this.values.length - 1]));
        },
        enumerable: false,
        configurable: true
    });
    UIMultiValueInput.prototype.delete = function (entity) {
        if (this.canEdit) {
            if (entity.entityAspect.entityState.isAdded()) {
                entity.entityAspect.setDetached();
            }
            else {
                entity.entityAspect.setDeleted();
            }
        }
    };
    __decorate([
        bindable(),
        __metadata("design:type", String)
    ], UIMultiValueInput.prototype, "placeholder", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], UIMultiValueInput.prototype, "disabled", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], UIMultiValueInput.prototype, "readonly", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.oneTime }),
        __metadata("design:type", Object)
    ], UIMultiValueInput.prototype, "entity", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.oneTime }),
        __metadata("design:type", String)
    ], UIMultiValueInput.prototype, "optionsText", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.oneTime }),
        __metadata("design:type", String)
    ], UIMultiValueInput.prototype, "targetArrayName", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", String)
    ], UIMultiValueInput.prototype, "type", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.oneTime }),
        __metadata("design:type", ServiceBase)
    ], UIMultiValueInput.prototype, "targetService", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.oneTime }),
        __metadata("design:type", Function)
    ], UIMultiValueInput.prototype, "callback", void 0);
    UIMultiValueInput = __decorate([
        customElement("ui-multi-value-input"),
        __metadata("design:paramtypes", [Element, TaskQueue])
    ], UIMultiValueInput);
    return UIMultiValueInput;
}());

//# sourceMappingURL=ui-multi-value-input.js.map
