"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomAjaxAdapter = exports.CustomSelectAdapter = void 0;
var aurelia_framework_1 = require("aurelia-framework");
require("select2");
var AjaxAdapter = jQuery.fn.select2.amd.require('select2/data/ajax');
var SelectAdapter = jQuery.fn.select2.amd.require('select2/data/select');
var DataAdapterUtils = (function () {
    function DataAdapterUtils() {
    }
    DataAdapterUtils.select = function (customDataAdapterInstance, superClass, data, withTags) {
        var taskQueue = aurelia_framework_1.Container.instance.get(aurelia_framework_1.TaskQueue);
        var $options = customDataAdapterInstance.$element.find('option');
        $options.removeAttr('data-select2-id');
        try {
            customDataAdapterInstance.uiSelect.ignoreChange = true;
            if (Array.isArray(customDataAdapterInstance.uiSelect.value)) {
                customDataAdapterInstance.uiSelect.value.push(data.id);
                taskQueue.queueTask(function () {
                    if (false === withTags || typeof data.id !== 'string') {
                        superClass.prototype.select.call(customDataAdapterInstance, data);
                    }
                    customDataAdapterInstance.uiSelect.ignoreChange = false;
                });
            }
            else {
                if (customDataAdapterInstance.uiSelect.value != null && Object.keys(customDataAdapterInstance.uiSelect.value).length > 0 && Object.keys(customDataAdapterInstance.uiSelect.value).indexOf('id') > -1) {
                    customDataAdapterInstance.uiSelect.value = { id: data.id.toString(), text: data.text };
                }
                else {
                    if (customDataAdapterInstance.uiSelect.value != data.id) {
                        var numberValue = Number(data.id);
                        if (!Number.isNaN(numberValue)) {
                            customDataAdapterInstance.uiSelect.value = numberValue;
                        }
                        else {
                            customDataAdapterInstance.uiSelect.value = data.id;
                        }
                    }
                }
                taskQueue.queueTask(function () {
                    superClass.prototype.select.call(customDataAdapterInstance, data);
                    customDataAdapterInstance.uiSelect.ignoreChange = false;
                });
            }
        }
        catch (_a) {
            customDataAdapterInstance.uiSelect.ignoreChange = false;
        }
    };
    DataAdapterUtils.unselect = function (customDataAdapterInstance, superClass, data) {
        var $options = customDataAdapterInstance.$element.find('option');
        $options.removeAttr('data-select2-id');
        if (Array.isArray(customDataAdapterInstance.uiSelect.value)) {
            if (customDataAdapterInstance.uiSelect.value.some(function (x) { return x == data.id; })) {
                var index = customDataAdapterInstance.uiSelect.value.findIndex(function (x) { return x == data.id; });
                customDataAdapterInstance.uiSelect.value.splice(index, 1);
            }
        }
        else {
            if (customDataAdapterInstance.uiSelect.value == data.id) {
                customDataAdapterInstance.uiSelect.value = null;
            }
            else if (Object.keys(customDataAdapterInstance.uiSelect.value).length > 0 && Object.keys(customDataAdapterInstance.uiSelect.value).indexOf('id') > -1 && customDataAdapterInstance.uiSelect.value.id == data.id) {
                customDataAdapterInstance.uiSelect.value = null;
            }
        }
        return superClass.prototype.unselect.call(customDataAdapterInstance, data);
    };
    return DataAdapterUtils;
}());
var CustomSelectAdapter = (function (_super) {
    __extends(CustomSelectAdapter, _super);
    function CustomSelectAdapter($element, options) {
        var _this = _super.call(this, $element, options) || this;
        _this.$element = $element;
        _this.uiSelect = $element.parent().get(0).au.controller.viewModel;
        return _this;
    }
    CustomSelectAdapter.prototype.select = function (data) {
        DataAdapterUtils.select(this, SelectAdapter, data, this.options.options.tags);
    };
    CustomSelectAdapter.prototype.unselect = function (data) {
        return DataAdapterUtils.unselect(this, SelectAdapter, data);
    };
    return CustomSelectAdapter;
}(SelectAdapter));
exports.CustomSelectAdapter = CustomSelectAdapter;
var CustomAjaxAdapter = (function (_super) {
    __extends(CustomAjaxAdapter, _super);
    function CustomAjaxAdapter($element, options) {
        var _this = _super.call(this, $element, options) || this;
        _this.$element = $element;
        _this.uiSelect = $element.parent().get(0).au.controller.viewModel;
        return _this;
    }
    CustomAjaxAdapter.prototype.select = function (data) {
        DataAdapterUtils.select(this, AjaxAdapter, data, this.options.options.tags);
    };
    CustomAjaxAdapter.prototype.unselect = function (data) {
        return DataAdapterUtils.unselect(this, SelectAdapter, data);
    };
    CustomAjaxAdapter.prototype.item = function ($option) {
        if ($option.length > 0 && $option.get(0).value == "null")
            return { id: null };
        return _super.prototype.item.call(this, $option);
    };
    return CustomAjaxAdapter;
}(AjaxAdapter));
exports.CustomAjaxAdapter = CustomAjaxAdapter;

//# sourceMappingURL=custom-ajax-adapter.js.map
