"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.CustomAllowClear = void 0;
var aurelia_framework_1 = require("aurelia-framework");
require("select2");
var ui_internal_1 = require("../../../utils/ui-internal");
var AllowClear = jQuery.fn.select2.amd.require('select2/selection/allowClear');
var Utils = jQuery.fn.select2.amd.require('select2/utils');
var KEYS = jQuery.fn.select2.amd.require('select2/keys');
var CustomAllowClear = (function (_super) {
    __extends(CustomAllowClear, _super);
    function CustomAllowClear() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CustomAllowClear.prototype.bind = function (decorated, container, $container) {
        var _this = this;
        decorated.call(this, container, $container);
        var self = this;
        if (this.placeholder == null) {
            if (this.options.get('debug') && window.console && console.error) {
                console.error('Select2: The `allowClear` option should be used in combination ' +
                    'with the `placeholder` option.');
            }
        }
        this.$selection.on('mousedown', '.select2-selection__clear', function (evt) {
            var _a;
            if (_this.isDisabled()) {
                return;
            }
            var $clear = _this.$selection.find('.select2-selection__clear');
            if ($clear.length === 0) {
                return;
            }
            evt.stopPropagation();
            _this.$selection.find('.select2-selection__clear').remove();
            var data = Utils.GetData($clear[0], 'data');
            var previousVal = _this.$element.val();
            _this.$element.val((_a = _this.placeholder) === null || _a === void 0 ? void 0 : _a.id);
            var unselectData = {
                data: data
            };
            _this.trigger('clear', unselectData);
            if (unselectData.prevented) {
                _this.$element.val(previousVal);
                return;
            }
            for (var d = 0; d < data.length; d++) {
                unselectData = {
                    data: data[d]
                };
                _this.trigger('unselect', unselectData);
                if (unselectData.prevented) {
                    _this.$element.val(previousVal);
                    return;
                }
            }
        });
        container.on('keypress', function (evt) {
            var _a;
            if (container.isOpen()) {
                return;
            }
            if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
                if (self.isDisabled()) {
                    return;
                }
                var $clear = _this.$selection.find('.select2-selection__clear');
                if ($clear.length === 0) {
                    return;
                }
                evt.stopPropagation();
                _this.$selection.find('.select2-selection__clear').remove();
                var data = Utils.GetData($clear[0], 'data');
                var previousVal = _this.$element.val();
                _this.$element.val((_a = self.placeholder) === null || _a === void 0 ? void 0 : _a.id);
                var unselectData = {
                    data: data
                };
                _this.trigger('clear', unselectData);
                if (unselectData.prevented) {
                    _this.$element.val(previousVal);
                    return;
                }
                for (var d = 0; d < data.length; d++) {
                    unselectData = {
                        data: data[d]
                    };
                    _this.trigger('unselect', unselectData);
                    if (unselectData.prevented) {
                        _this.$element.val(previousVal);
                        return;
                    }
                }
            }
        });
    };
    CustomAllowClear.prototype.update = function (decorated, data) {
        decorated.call(this, data);
        this.$selection.find('.select2-selection__clear').remove();
        this.$selection[0].classList.remove('select2-selection--clearable');
        if (this.$selection.find('.select2-selection__placeholder').length > 0 ||
            data.length === 0) {
            return;
        }
        var selectionId = this.$selection.find('.select2-selection__rendered')
            .attr('id');
        var removeAll = this.options.get('translations').get('removeAllItems');
        var html = "<template><ui-button icon=\"digi-cross\" class=\"select2-selection__clear\" type=\"tool\"></ui-button></template>";
        var view = ui_internal_1.UIInternal.compileTemplate(html, this, (0, aurelia_framework_1.createOverrideContext)(this));
        var $remove = $(view.fragment.children[0]);
        $remove.attr('title', removeAll());
        $remove.attr('aria-label', removeAll());
        $remove.attr('aria-describedby', selectionId);
        Utils.StoreData($remove[0], 'data', data);
        this.$selection.prepend($remove);
        this.$selection[0].classList.add('select2-selection--clearable');
    };
    ;
    return CustomAllowClear;
}(AllowClear));
exports.CustomAllowClear = CustomAllowClear;

//# sourceMappingURL=custom-allow-clear.js.map
