text: // Generated by purs version 0.11.7
  "use strict";
  var Control_Applicative = require("../Control.Applicative");
  var Control_Bind = require("../Control.Bind");
  var Control_Coroutine = require("../Control.Coroutine");
  var Control_Monad_Aff = require("../Control.Monad.Aff");
  var Control_Monad_Aff_AVar = require("../Control.Monad.Aff.AVar");
  var Control_Monad_Eff = require("../Control.Monad.Eff");
  var Control_Monad_Eff_Class = require("../Control.Monad.Eff.Class");
  var Control_Monad_Eff_Console = require("../Control.Monad.Eff.Console");
  var Control_Monad_Eff_Exception = require("../Control.Monad.Eff.Exception");
  var Control_Monad_Eff_Ref = require("../Control.Monad.Eff.Ref");
  var Control_Monad_Eff_Unsafe = require("../Control.Monad.Eff.Unsafe");
  var Control_Monad_Rec_Class = require("../Control.Monad.Rec.Class");
  var Control_Semigroupoid = require("../Control.Semigroupoid");
  var Data_Either = require("../Data.Either");
  var Data_Foldable = require("../Data.Foldable");
  var Data_Function = require("../Data.Function");
  var Data_Functor = require("../Data.Functor");
  var Data_List = require("../Data.List");
  var Data_List_Types = require("../Data.List.Types");
  var Data_Map = require("../Data.Map");
  var Data_Maybe = require("../Data.Maybe");
  var Data_Ord = require("../Data.Ord");
  var Data_Semiring = require("../Data.Semiring");
  var Data_Traversable = require("../Data.Traversable");
  var Data_Tuple = require("../Data.Tuple");
  var Data_Unit = require("../Data.Unit");
  var Halogen = require("../Halogen");
  var Halogen_Aff_Driver_Eval = require("../Halogen.Aff.Driver.Eval");
  var Halogen_Aff_Driver_State = require("../Halogen.Aff.Driver.State");
  var Halogen_Aff_Effects = require("../Halogen.Aff.Effects");
  var Halogen_Component = require("../Halogen.Component");
  var Halogen_Data_OrdBox = require("../Halogen.Data.OrdBox");
  var Halogen_Query_InputF = require("../Halogen.Query.InputF");
  var Prelude = require("../Prelude");
  var newLifecycleHandlers = Control_Monad_Eff_Ref.newRef({
      initializers: Data_List_Types.Nil.value,
      finalizers: Data_List_Types.Nil.value
  });
  var handleAff = function ($63) {
      return Data_Functor["void"](Control_Monad_Eff.functorEff)(Control_Monad_Aff.runAff(Control_Monad_Eff_Exception.throwException)(Data_Function["const"](Control_Applicative.pure(Control_Monad_Eff.applicativeEff)(Data_Unit.unit)))($63));
  };
  var runUI = function (renderSpec) {
      return function (component) {
          return function (i) {
              var subscribe = function (fresh) {
                  return function (ref) {
                      return function (consumer) {
                          return Control_Bind.bind(Control_Monad_Aff.bindAff)(Control_Monad_Aff_AVar.makeVar)(function (v) {
                              return Control_Bind.bind(Control_Monad_Aff.bindAff)(Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(function __do() {
                                  var v1 = Control_Monad_Eff_Ref.readRef(fresh)();
                                  Control_Monad_Eff_Ref.modifyRef(fresh)(function (v2) {
                                      return v2 + 1 | 0;
                                  })();
                                  Control_Monad_Eff_Ref.modifyRef(ref)(Data_Map.insert(Data_Ord.ordInt)(v1)(v))();
                                  return v1;
                              }))(function (v1) {
                                  var producer = Control_Coroutine.producer(Control_Monad_Aff.monadAff)(Data_Functor.map(Control_Monad_Aff.functorAff)(Data_Either.Left.create)(Control_Monad_Aff_AVar.takeVar(v)));
                                  return Data_Functor["void"](Control_Monad_Aff.functorAff)(Control_Monad_Aff.forkAff(Control_Bind.discard(Control_Bind.discardUnit)(Control_Monad_Aff.bindAff)(Control_Coroutine.runProcess(Control_Monad_Aff.monadRecAff)(Control_Coroutine.connect(Control_Monad_Aff.monadRecAff)(Control_Monad_Aff.parallelParAff)(producer)(consumer)))(function () {
                                      return Control_Bind.discard(Control_Bind.discardUnit)(Control_Monad_Aff.bindAff)(Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(Control_Monad_Eff_Ref.modifyRef(ref)(Data_Map["delete"](Data_Ord.ordInt)(v1))))(function () {
                                          return Control_Monad_Aff_AVar.killVar(v)(Control_Monad_Eff_Exception.error("ended"));
                                      });
                                  })));
                              });
                          });
                      };
                  };
              };
              var rootHandler = function (ref) {
                  return function (message) {
                      return Control_Bind.bind(Control_Monad_Aff.bindAff)(Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(Control_Monad_Eff_Ref.readRef(ref)))(function (v) {
                          return Data_Functor["void"](Control_Monad_Aff.functorAff)(Control_Monad_Aff.forkAll(Data_Map.foldableMap)(Data_Functor.map(Data_Map.functorMap)(function ($$var) {
                              return Control_Monad_Aff_AVar.putVar($$var)(message);
                          })(v)));
                      });
                  };
              };
              var handlePending = function (ref) {
                  return function __do() {
                      var v = Control_Monad_Eff_Ref.readRef(ref)();
                      Control_Monad_Eff_Ref.writeRef(ref)(Data_Maybe.Nothing.value)();
                      return Data_Foldable.for_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(v)(function ($64) {
                          return handleAff(Control_Monad_Aff.forkAll(Data_List_Types.foldableList)(Data_List.reverse($64)));
                      })();
                  };
              };
              var cleanupSubscriptions = function (v) {
                  return function __do() {
                      Control_Bind.bindFlipped(Control_Monad_Eff.bindEff)(Data_Foldable.traverse_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(function ($65) {
                          return handleAff(Control_Monad_Aff.forkAll(Data_Map.foldableMap)($65));
                      }))(Control_Monad_Eff_Ref.readRef(v.subscriptions))();
                      return Control_Monad_Eff_Ref.writeRef(v.subscriptions)(Data_Maybe.Nothing.value)();
                  };
              };
              var squashChildInitializers = function (lchs) {
                  return function (preInits) {
                      return Halogen_Aff_Driver_State.unDriverStateX(function (st) {
                          var parentInitializer = Data_Functor.map(Data_Maybe.functorMaybe)(function ($66) {
                              return evalF(st.selfRef)(Halogen_Query_InputF.Query.create($66));
                          })(st.component.initializer);
                          return Control_Monad_Eff_Ref.modifyRef(lchs)(function (handlers) {
                              return {
                                  initializers: new Data_List_Types.Cons(Control_Bind.discard(Control_Bind.discardUnit)(Control_Monad_Aff.bindAff)(Halogen_Aff_Driver_Eval.parSequenceAff_(Data_List.reverse(handlers.initializers)))(function () {
                                      return Control_Bind.discard(Control_Bind.discardUnit)(Control_Monad_Aff.bindAff)(Data_Foldable.sequence_(Control_Monad_Aff.applicativeAff)(Data_Foldable.foldableMaybe)(parentInitializer))(function () {
                                          return Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(function __do() {
                                              handlePending(st.pendingQueries)();
                                              return handlePending(st.pendingOuts)();
                                          });
                                      });
                                  }), preInits),
                                  finalizers: handlers.finalizers
                              };
                          });
                      });
                  };
              };
              var runComponent = function (lchs) {
                  return function (handler) {
                      return function (j) {
                          return function (prjQuery) {
                              return Halogen_Component.unComponent(function (c) {
                                  return function __do() {
                                      var v = newLifecycleHandlers();
                                      var v1 = Halogen_Aff_Driver_State.initDriverState(c)(j)(handler)(prjQuery)(v)();
                                      var v2 = Control_Monad_Eff_Ref.readRef(lchs)();
                                      Control_Monad_Eff_Ref.writeRef(lchs)({
                                          initializers: Data_List_Types.Nil.value,
                                          finalizers: v2.finalizers
                                      })();
                                      Control_Bind.bindFlipped(Control_Monad_Eff.bindEff)(Halogen_Aff_Driver_State.unDriverStateX(function ($67) {
                                          return render(lchs)((function (v3) {
                                              return v3.selfRef;
                                          })($67));
                                      }))(Control_Monad_Eff_Ref.readRef(v1))();
                                      Control_Bind.bindFlipped(Control_Monad_Eff.bindEff)(squashChildInitializers(lchs)(v2.initializers))(Control_Monad_Eff_Ref.readRef(v1))();
                                      return v1;
                                  };
                              });
                          };
                      };
                  };
              };
              var renderChild = function (lchs) {
                  return function (handler) {
                      return function (mkOrdBox) {
                          return function (childrenInRef) {
                              return function (childrenOutRef) {
                                  return Halogen_Component.unComponentSlot(function (p) {
                                      return function (ctor) {
                                          return function (input) {
                                              return function (inputQuery) {
                                                  return function (outputQuery) {
                                                      return function (prjQuery) {
                                                          var ordP = mkOrdBox(p);
                                                          return function __do() {
                                                              var v = Control_Monad_Eff_Ref.readRef(childrenInRef)();
                                                              var v1 = (function () {
                                                                  var v1 = Data_Map.pop(Halogen_Data_OrdBox.ordOrdBox)(ordP)(v);
                                                                  if (v1 instanceof Data_Maybe.Just) {
                                                                      return function __do() {
                                                                          Control_Monad_Eff_Ref.writeRef(childrenInRef)(v1.value0.value1)();
                                                                          Data_Foldable.for_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(inputQuery(input))(function (q) {
                                                                              return function __do() {
                                                                                  var v2 = Control_Monad_Eff_Ref.readRef(v1.value0.value0)();
                                                                                  return Halogen_Aff_Driver_State.unDriverStateX(function (st) {
                                                                                      return Data_Foldable.for_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(st.prjQuery(q))(function ($68) {
                                                                                          return handleAff(evalF(st.selfRef)(Halogen_Query_InputF.Query.create($68)));
                                                                                      });
                                                                                  })(v2)();
                                                                              };
                                                                          })();
                                                                          return v1.value0.value0;
                                                                      };
                                                                  };
                                                                  if (v1 instanceof Data_Maybe.Nothing) {
                                                                      return runComponent(lchs)(function ($69) {
                                                                          return Data_Maybe.maybe(Control_Applicative.pure(Control_Monad_Aff.applicativeAff)(Data_Unit.unit))(handler)(outputQuery($69));
                                                                      })(input)(prjQuery)(ctor);
                                                                  };
                                                                  throw new Error("Failed pattern match at Halogen.Aff.Driver line 260, column 14 - line 268, column 92: " + [ v1.constructor.name ]);
                                                              })()();
                                                              var v2 = Data_Functor.map(Control_Monad_Eff.functorEff)(Data_Map.member(Halogen_Data_OrdBox.ordOrdBox)(ordP))(Control_Monad_Eff_Ref.readRef(childrenOutRef))();
                                                              Control_Applicative.when(Control_Monad_Eff.applicativeEff)(v2)(Control_Monad_Eff_Unsafe.unsafeCoerceEff(Control_Monad_Eff_Console.warn("Halogen: Duplicate slot address was detected during rendering, unexpected results may occur")))();
                                                              Control_Monad_Eff_Ref.modifyRef(childrenOutRef)(Data_Map.insert(Halogen_Data_OrdBox.ordOrdBox)(ordP)(v1))();
                                                              return Control_Bind.bind(Control_Monad_Eff.bindEff)(Control_Monad_Eff_Ref.readRef(v1))(Halogen_Aff_Driver_State.renderStateX(Control_Monad_Eff.functorEff)(function (v3) {
                                                                  if (v3 instanceof Data_Maybe.Nothing) {
                                                                      return Control_Monad_Eff_Exception["throw"]("Halogen internal error: child was not initialized in renderChild");
                                                                  };
                                                                  if (v3 instanceof Data_Maybe.Just) {
                                                                      return Control_Applicative.pure(Control_Monad_Eff.applicativeEff)(renderSpec.renderChild(v3.value0));
                                                                  };
                                                                  throw new Error("Failed pattern match at Halogen.Aff.Driver line 274, column 36 - line 276, column 50: " + [ v3.constructor.name ]);
                                                              }))();
                                                          };
                                                      };
                                                  };
                                              };
                                          };
                                      };
                                  });
                              };
                          };
                      };
                  };
              };
              var render = function (lchs) {
                  return function ($$var) {
                      return function __do() {
                          var v = Control_Monad_Eff_Ref.readRef($$var)();
                          var v1 = Data_Functor.map(Control_Monad_Eff.functorEff)(Data_Maybe.isNothing)(Control_Monad_Eff_Ref.readRef(v.pendingHandlers))();
                          Control_Applicative.when(Control_Monad_Eff.applicativeEff)(v1)(Control_Monad_Eff_Ref.writeRef(v.pendingHandlers)(new Data_Maybe.Just(Data_List_Types.Nil.value)))();
                          Control_Monad_Eff_Ref.writeRef(v.childrenOut)(Data_Map.empty)();
                          Control_Monad_Eff_Ref.writeRef(v.childrenIn)(v.children)();
                          var handler = Halogen_Aff_Driver_Eval.queuingHandler(function ($70) {
                              return Data_Functor["void"](Control_Monad_Aff.functorAff)(evalF(v.selfRef)($70));
                          })(v.pendingHandlers);
                          var childHandler = Halogen_Aff_Driver_Eval.queuingHandler(function ($71) {
                              return handler(Halogen_Query_InputF.Query.create($71));
                          })(v.pendingQueries);
                          var v2 = renderSpec.render(function ($72) {
                              return handleAff(handler($72));
                          })(renderChild(lchs)(childHandler)(v.component.mkOrdBox)(v.childrenIn)(v.childrenOut))(v.component.render(v.state))(v.rendering)();
                          var v3 = Control_Monad_Eff_Ref.readRef(v.childrenOut)();
                          Control_Bind.bind(Control_Monad_Eff.bindEff)(Control_Monad_Eff_Ref.readRef(v.childrenIn))(Data_Foldable.traverse_(Control_Monad_Eff.applicativeEff)(Data_Map.foldableMap)(function (childVar) {
                              return function __do() {
                                  var v4 = Control_Monad_Eff_Ref.readRef(childVar)();
                                  Halogen_Aff_Driver_State.renderStateX_(Control_Monad_Eff.applicativeEff)(renderSpec.removeChild)(v4)();
                                  return finalize(lchs)(v4)();
                              };
                          }))();
                          Control_Monad_Eff_Ref.modifyRef(v.selfRef)(function (v4) {
                              return {
                                  rendering: new Data_Maybe.Just(v2),
                                  children: v3,
                                  component: v4.component,
                                  state: v4.state,
                                  refs: v4.refs,
                                  childrenIn: v4.childrenIn,
                                  childrenOut: v4.childrenOut,
                                  selfRef: v4.selfRef,
                                  handler: v4.handler,
                                  pendingQueries: v4.pendingQueries,
                                  pendingOuts: v4.pendingOuts,
                                  pendingHandlers: v4.pendingHandlers,
                                  prjQuery: v4.prjQuery,
                                  fresh: v4.fresh,
                                  subscriptions: v4.subscriptions,
                                  lifecycleHandlers: v4.lifecycleHandlers
                              };
                          })();
                          return Control_Applicative.when(Control_Monad_Eff.applicativeEff)(v1)(Data_Function.flip(Control_Monad_Rec_Class.tailRecM(Control_Monad_Rec_Class.monadRecEff))(Data_Unit.unit)(function (v4) {
                              return function __do() {
                                  var v5 = Control_Monad_Eff_Ref.readRef(v.pendingHandlers)();
                                  Control_Monad_Eff_Ref.writeRef(v.pendingHandlers)(new Data_Maybe.Just(Data_List_Types.Nil.value))();
                                  Data_Foldable.traverse_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(function ($73) {
                                      return handleAff(Control_Monad_Aff.forkAll(Data_List_Types.foldableList)(Data_List.reverse($73)));
                                  })(v5)();
                                  var v6 = Control_Monad_Eff_Ref.readRef(v.pendingHandlers)();
                                  var $57 = Data_Maybe.maybe(false)(Data_List["null"])(v6);
                                  if ($57) {
                                      return Data_Functor.voidLeft(Control_Monad_Eff.functorEff)(Control_Monad_Eff_Ref.writeRef(v.pendingHandlers)(Data_Maybe.Nothing.value))(new Control_Monad_Rec_Class.Done(Data_Unit.unit))();
                                  };
                                  return new Control_Monad_Rec_Class.Loop(Data_Unit.unit);
                              };
                          }))();
                      };
                  };
              };
              var finalize = function (lchs) {
                  return Halogen_Aff_Driver_State.unDriverStateX(function (st) {
                      return function __do() {
                          cleanupSubscriptions(st)();
                          Data_Foldable.for_(Control_Monad_Eff.applicativeEff)(Data_Foldable.foldableMaybe)(Data_Functor.map(Data_Maybe.functorMaybe)(function ($74) {
                              return evalF(st.selfRef)(Halogen_Query_InputF.Query.create($74));
                          })(st.component.finalizer))(function (f) {
                              return Control_Monad_Eff_Ref.modifyRef(lchs)(function (handlers) {
                                  return {
                                      initializers: handlers.initializers,
                                      finalizers: new Data_List_Types.Cons(f, handlers.finalizers)
                                  };
                              });
                          })();
                          return Data_Foldable.for_(Control_Monad_Eff.applicativeEff)(Data_Map.foldableMap)(st.children)(Control_Bind.composeKleisliFlipped(Control_Monad_Eff.bindEff)(finalize(lchs))(Control_Monad_Eff_Ref.readRef))();
                      };
                  });
              };
              var evalF = function (ref) {
                  return Halogen_Aff_Driver_Eval["eval"](render)(ref);
              };
              var evalDriver = function (ref) {
                  return function (prjQuery) {
                      return function (q) {
                          var v = prjQuery(q);
                          if (v instanceof Data_Maybe.Just) {
                              return evalF(ref)(new Halogen_Query_InputF.Query(v.value0));
                          };
                          if (v instanceof Data_Maybe.Nothing) {
                              return Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(Control_Monad_Eff_Exception.throwException(Control_Monad_Eff_Exception.error("Halogen internal error: query projection failed in runUI'")));
                          };
                          throw new Error("Failed pattern match at Halogen.Aff.Driver line 138, column 5 - line 140, column 110: " + [ v.constructor.name ]);
                      };
                  };
              };
              return Control_Bind.bind(Control_Monad_Aff.bindAff)(Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(newLifecycleHandlers))(function (v) {
                  return Control_Bind.bind(Control_Monad_Aff.bindAff)(Control_Monad_Eff_Class.liftEff(Control_Monad_Aff.monadEffAff)(Control_Monad_Eff_Ref.newRef(0)))(function (v1) {
                      return Halogen_Aff_Driver_Eval.handleLifecycle(v)(function __do() {
                          var v2 = Control_Monad_Eff_Ref.newRef(Data_Map.empty)();
                          return Control_Bind.bind(Control_Monad_Eff.bindEff)(Control_Bind.bind(Control_Monad_Eff.bindEff)(runComponent(v)(rootHandler(v2))(i)(Data_Maybe.Just.create)(component))(Control_Monad_Eff_Ref.readRef))(Halogen_Aff_Driver_State.unDriverStateX(function (st) {
                              return Control_Applicative.pure(Control_Monad_Eff.applicativeEff)({
                                  query: evalDriver(st.selfRef)(st.prjQuery),
                                  subscribe: subscribe(v1)(v2)
                              });
                          }))();
                      });
                  });
              });
          };
      };
  };
  module.exports = {
      runUI: runUI
  };
  