import * as jQuery from 'jquery';
function on(elem, types, selector, data, fn, one) {
    var origFn, type;
    if (typeof types === "object") {
        if (typeof selector !== "string") {
            data = data || selector;
            selector = undefined;
        }
        for (type in types) {
            on(elem, type, selector, data, types[type], one);
        }
        return elem;
    }
    if (data == null && fn == null) {
        fn = selector;
        data = selector = undefined;
    }
    else if (fn == null) {
        if (typeof selector === "string") {
            fn = data;
            data = undefined;
        }
        else {
            fn = data;
            data = selector;
            selector = undefined;
        }
    }
    if (fn === false) {
        fn = returnFalse;
    }
    else if (!fn) {
        return elem;
    }
    if (one === 1) {
        origFn = fn;
        fn = function (event) {
            jQuery().off(event);
            return origFn.apply(this, arguments);
        };
        fn.guid = origFn.guid || (origFn.guid = jQuery.guid++);
    }
    return elem.each(function () {
        var el = this;
        var [type, id] = types.split('?');
        if (id) {
            id = `__${type}_${id}`;
            var has = id in el;
            el[id] = fn;
            if (has) {
                return;
            }
            fn = function (...args) {
                return el[id].call(this, ...args);
            };
        }
        jQuery.event.add(el, type, fn, data, selector);
    });
}
jQuery.fn.extend({
    on: function (types, selector, data, fn) {
        return on(this, types, selector, data, fn);
    }
});
