(function (_, kotlin_kotlin, kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core, kotlin_org_jetbrains_kotlin_wrappers_kotlin_js) {
  'use strict';
  //region block: imports
  var Unit_instance = kotlin_kotlin.$_$.b1;
  var ArrayList_init_$Create$ = kotlin_kotlin.$_$.l1;
  var sequenceOf = kotlin_kotlin.$_$.ve;
  var intercepted = kotlin_kotlin.$_$.ia;
  var CancellableContinuationImpl = kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core.$_$.l1;
  var thenToContinuation = kotlin_org_jetbrains_kotlin_wrappers_kotlin_js.$_$.b;
  //endregion
  //region block: pre-declaration
  //endregion
  function addEventHandler(_this__u8e3s4, type, handler) {
    return addEventHandler_0(_this__u8e3s4, type, undefined, handler);
  }
  function addEventHandler_0(_this__u8e3s4, type, options, handler) {
    _this__u8e3s4.addEventListener(type, handler, options);
    return addEventHandler$lambda(_this__u8e3s4, type, handler, options);
  }
  function addEventHandler$lambda($this_addEventHandler, $type, $handler, $options) {
    return function () {
      $this_addEventHandler.removeEventListener($type, $handler, $options);
      return Unit_instance;
    };
  }
  function safeAny(signal1, signal2) {
    if (signal1 == null)
      return signal2;
    if (signal1.aborted)
      return signal1;
    if (signal2.aborted)
      return signal2;
    var controller = new AbortController();
    // Inline function 'kotlin.collections.mutableListOf' call
    var handlers = ArrayList_init_$Create$();
    // Inline function 'web.events.EventHandler' call
    // Inline function 'js.reflect.unsafeCast' call
    // Inline function 'kotlin.js.unsafeCast' call
    // Inline function 'kotlin.js.asDynamic' call
    var abortHandler = safeAny$lambda(handlers, controller);
    // Inline function 'kotlin.sequences.mapTo' call
    var _iterator__ex2g4s = sequenceOf([signal1, signal2]).t();
    while (_iterator__ex2g4s.u()) {
      var item = _iterator__ex2g4s.v();
      // Inline function 'web.events.Event.Companion.ABORT' call
      var tmp$ret$6 = addEventHandler(item, 'abort', abortHandler);
      handlers.e1(tmp$ret$6);
    }
    return controller.signal;
  }
  function safeAny$lambda($handlers, $controller) {
    return function (event) {
      // Inline function 'kotlin.collections.forEach' call
      var _iterator__ex2g4s = $handlers.t();
      while (_iterator__ex2g4s.u()) {
        var element = _iterator__ex2g4s.v();
        element();
      }
      $controller.abort(event.currentTarget.reason);
      return Unit_instance;
    };
  }
  function awaitPromiseLike(promise, controller, $completion) {
    var cancellable = new CancellableContinuationImpl(intercepted($completion), 1);
    cancellable.b2w();
    cancellable.d2x(awaitPromiseLike$lambda(controller));
    // Inline function 'js.promise.PromiseLike.thenTo' call
    thenToContinuation(promise, cancellable);
    return cancellable.c2w();
  }
  function patchAbortOptions(options, controller) {
    var abortOptions = createAbortable(safeAny(options == null ? null : options.signal, controller.signal));
    var tmp = Object;
    // Inline function 'js.objects.unsafeJso' call
    var tmp$ret$0 = {};
    return tmp.assign(tmp$ret$0, options, abortOptions);
  }
  function awaitPromiseLike$lambda($controller) {
    return function (it) {
      $controller.abort();
      return Unit_instance;
    };
  }
  function createAbortable(signal) {
    // Inline function 'js.objects.unsafeJso' call
    // Inline function 'js.objects.unsafeJso' call
    // Inline function 'kotlin.apply' call
    var this_0 = {};
    this_0.signal = signal;
    return this_0;
  }
  //region block: exports
  _.$_$ = _.$_$ || {};
  _.$_$.a = awaitPromiseLike;
  _.$_$.b = patchAbortOptions;
  //endregion
  return _;
}(module.exports, require('./kotlin-kotlin-stdlib.js'), require('./kotlinx-coroutines-core.js'), require('./kotlin-js.js')));

//# sourceMappingURL=kotlin-web.js.map
