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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../../App", "../../di/Inject", "./AtomControl", "./AtomItemsControl"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomComboBox = void 0;
    const App_1 = require("../../App");
    const Inject_1 = require("../../di/Inject");
    const AtomControl_1 = require("./AtomControl");
    const AtomItemsControl_1 = require("./AtomItemsControl");
    let AtomComboBox = class AtomComboBox extends AtomItemsControl_1.AtomItemsControl {
        constructor(app, e) {
            super(app, e || document.createElement("select"));
            this.allowMultipleSelection = false;
        }
        onCollectionChanged(key, index, item) {
            super.onCollectionChanged(key, index, item);
            try {
                this.isChanging = true;
                const se = this.element;
                se.selectedIndex = this.selectedIndex;
            }
            finally {
                this.isChanging = false;
            }
        }
        updateSelectionBindings() {
            super.updateSelectionBindings();
            try {
                if (this.isChanging) {
                    return;
                }
                this.isChanging = true;
                const se = this.element;
                se.selectedIndex = this.selectedIndex;
            }
            finally {
                this.isChanging = false;
            }
        }
        preCreate() {
            super.preCreate();
            this.itemTemplate = AtomComboBoxItemTemplate;
            this.runAfterInit(() => {
                this.bindEvent(this.element, "change", (s) => {
                    if (this.isChanging) {
                        return;
                    }
                    try {
                        this.isChanging = true;
                        const index = this.element.selectedIndex;
                        if (index === -1) {
                            this.selectedItems.clear();
                            return;
                        }
                        this.selectedItem = this.items[index];
                        // this.selectedIndex = (this.element as HTMLSelectElement).selectedIndex;
                    }
                    finally {
                        this.isChanging = false;
                    }
                });
            });
        }
    };
    AtomComboBox = __decorate([
        __param(0, Inject_1.Inject),
        __metadata("design:paramtypes", [App_1.App, HTMLElement])
    ], AtomComboBox);
    exports.AtomComboBox = AtomComboBox;
    class AtomComboBoxItemTemplate extends AtomControl_1.AtomControl {
        constructor(app, e) {
            super(app, e || document.createElement("option"));
        }
        create() {
            this.bind(this.element, "text", [["data"]], false, (v) => {
                const ip = this.element._templateParent;
                return v[ip.labelPath];
            });
        }
    }
});
//# sourceMappingURL=AtomComboBox.js.map