import { Container, BindingEngine } from 'aurelia-framework';
import { ViewCompiler, ViewResources, ViewSlot } from 'aurelia-templating';
import { createOverrideContext } from 'aurelia-binding';
var Select2CellEditor = (function () {
    function Select2CellEditor() {
        this.entityMatcher = function (a, b) {
            var a_id = a != null && a.id != null ? a.id : a;
            var b_id = b != null && b.id != null ? b.id : b;
            if (a_id == "")
                a_id = 0;
            if (b_id == "")
                b_id = 0;
            return (a_id != null && b_id != null && a_id.toString != null && b_id.toString != null) ? a_id.toString() === b_id.toString() : a_id == b_id;
        };
        this.bindingEngine = Container.instance.get(BindingEngine);
    }
    Select2CellEditor.prototype.getValue = function () {
        return this.entity[this.valuePropertyName];
    };
    Select2CellEditor.prototype.getGui = function () {
        this.setupGui();
        return this.eGui;
    };
    Select2CellEditor.prototype.isPopup = function () {
        return false;
    };
    Select2CellEditor.prototype.afterGuiAttached = function () {
        $(this.eGui).find('select').select2('open');
    };
    Select2CellEditor.prototype.destroy = function () {
        this.view.detached();
        this.viewSlot.detached();
        this.disposable.dispose();
    };
    Select2CellEditor.prototype.init = function (params) {
        var _this = this;
        this.params = params;
        this.dataService = params.dataService;
        this.entity = params.data;
        this.valuePropertyName = params.valuePropertyName;
        this.displayPropertyName = params.displayPropertyName;
        var observer = this.bindingEngine.propertyObserver(this.entity, this.valuePropertyName);
        this.disposable = observer.subscribe(function (n, o) { return _this.propertyChanged(n, o); });
    };
    Select2CellEditor.prototype.propertyChanged = function (newValue, oldValue) {
        console.log("PropertyChanged NewValue = ".concat(newValue, " ; OldValue= ").concat(oldValue));
        if (newValue != oldValue) {
            this.params.stopEditing();
        }
    };
    Select2CellEditor.prototype.setupGui = function () {
        this.eGui = document.createElement('div');
        this.eGui.style.width = '100%';
        this.eGui.style.height = '100%';
        var display = "${entity." + this.displayPropertyName + ".denomination._translation}";
        var html = "\n        <template>\n            <select matcher.bind=\"entityMatcher\" value.bind=\"entity.".concat(this.valuePropertyName, " & validate\"\n                t=\"[data-placeholder]general.chooseAnOption\" \n                data-allow-clear=\"true\"\n                select2=\"value.bind: entity.").concat(this.valuePropertyName, ";                \n                    data-source.bind: dataService.gridDataSource;\n                    options-text: denomination._translation;\n                    options-value: id\">\n                <option></option> \n                <option value.bind=\"entity.").concat(this.valuePropertyName, "\">").concat(display, "</option>           \n            </select>\n        </template>");
        var viewCompiler = Container.instance.get(ViewCompiler);
        var viewResources = Container.instance.get(ViewResources);
        var viewFactory = viewCompiler.compile(html, viewResources);
        this.view = viewFactory.create(Container.instance);
        this.viewSlot = new ViewSlot(this.eGui, true);
        this.viewSlot.add(this.view);
        this.view.bind(this, createOverrideContext(this));
        this.viewSlot.attached();
    };
    return Select2CellEditor;
}());
export default Select2CellEditor;

//# sourceMappingURL=select2-cell-editor.js.map
