"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
require("ui-select");
var angular = require("angular");
var _ = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __transform = typescript_angular_utilities_1.services.transform.transform;
var input_ng1_1 = require("../input.ng1");
var componentValidator_service_ng1_1 = require("../../../services/componentValidator/componentValidator.service.ng1");
var jquery_service_1 = require("../../../services/jquery/jquery.service");
exports.moduleName = 'rl.ui.components.select';
exports.componentName = 'rlSelect';
exports.controllerName = 'SelectController';
var SelectController = (function (_super) {
    __extends(SelectController, _super);
    function SelectController($scope, $attrs, $q, $transclude, object, componentValidatorFactory, jqueryUtility) {
        var _this = _super.call(this, $scope, $attrs, componentValidatorFactory) || this;
        _this.$q = $q;
        _this.object = object;
        _this.inputType = 'select';
        _this.transform = _this.transform || _this.selector;
        if (!_this.template) {
            $transclude(function (clone) {
                if (clone.length) {
                    _this.template = jqueryUtility.getHtml(clone);
                }
                else {
                    _this.template = '{{select.getDisplayName($item)}}';
                }
            });
        }
        return _this;
    }
    Object.defineProperty(SelectController.prototype, "selection", {
        get: function () {
            return this.ngModel.$viewValue;
        },
        set: function (value) {
            this.ngModel.$setViewValue(value);
            this.select({ item: this.ngModel.$viewValue });
        },
        enumerable: true,
        configurable: true
    });
    SelectController.prototype.$onInit = function () {
        var _this = this;
        _super.prototype.$onInit.call(this);
        if (_.isUndefined(this.options)) {
            this.loading = true;
            this.loadItems().then(function (options) {
                _this.options = options;
                _this.loading = false;
            });
        }
    };
    SelectController.prototype.getDisplayName = function (item) {
        return __transform.getValue(item, this.transform);
    };
    SelectController.prototype.loadItems = function () {
        var promise;
        promise = this.getOptions();
        if (promise == null) {
            promise = this.$q.when(this.options);
        }
        return promise;
    };
    SelectController.prototype.toggle = function () {
        this.showOptions = !this.showOptions;
    };
    SelectController.prototype.close = function () {
        this.showOptions = false;
    };
    SelectController.prototype.selectOption = function (value) {
        this.selection = value;
        this.showOptions = false;
    };
    return SelectController;
}(input_ng1_1.InputController));
SelectController.$inject = ['$scope', '$attrs', '$q', '$transclude', typescript_angular_utilities_1.downgrade.objectServiceName, componentValidator_service_ng1_1.factoryName, jquery_service_1.serviceName];
exports.SelectController = SelectController;
var select = input_ng1_1.buildInput({
    transclude: true,
    template: require('./select.ng1.html'),
    controller: exports.controllerName,
    controllerAs: 'select',
    bindings: {
        options: '<?',
        getOptions: '&',
        transform: '<?',
        ngDisabled: '<?',
        nullOption: '@',
        select: '&',
        itemAs: '@',
        // deprecated
        selector: '<?',
        // private
        template: '<?',
    },
});
angular.module(exports.moduleName, ['ui.select', typescript_angular_utilities_1.downgrade.moduleName, input_ng1_1.moduleName, jquery_service_1.moduleName])
    .component(exports.componentName, select)
    .controller(exports.controllerName, SelectController);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VsZWN0Lm5nMS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInNlbGVjdC5uZzEudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEscUJBQW1CO0FBRW5CLGlDQUFtQztBQUNuQywwQkFBNEI7QUFFNUIsNkVBQW1FO0FBRW5FLElBQU8sV0FBVyxHQUFHLHVDQUFRLENBQUMsU0FBUyxDQUFDLFNBQVMsQ0FBQztBQUVsRCwwQ0FBc0Y7QUFDdEYsc0hBQStKO0FBQy9KLDBFQUF1STtBQUUxSCxRQUFBLFVBQVUsR0FBVyx5QkFBeUIsQ0FBQztBQUMvQyxRQUFBLGFBQWEsR0FBVyxVQUFVLENBQUM7QUFDbkMsUUFBQSxjQUFjLEdBQVcsa0JBQWtCLENBQUM7QUFNekQ7SUFBc0Msb0NBQWU7SUEwQnBELDBCQUFZLE1BQXNCLEVBQzlCLE1BQTJCLEVBQ25CLEVBQXFCLEVBQzdCL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