text: // Generated by BUCKLESCRIPT VERSION 5.0.6, PLEASE EDIT WITH CARE
  'use strict';
  
  var Block = require("bs-platform/lib/js/block.js");
  var Curry = require("bs-platform/lib/js/curry.js");
  var $$Event = require("./Event.bs.js");
  var Utils = require("../../utils/Utils.bs.js");
  var Belt_Set = require("bs-platform/lib/js/belt_Set.js");
  var EventLog = require("./EventLog.bs.js");
  var Caml_option = require("bs-platform/lib/js/caml_option.js");
  var BitcoinjsLib = require("bitcoinjs-lib");
  var CamlinternalOO = require("bs-platform/lib/js/camlinternalOO.js");
  var PrimitiveTypes = require("../PrimitiveTypes.bs.js");
  var AccountKeyChain = require("../wallet/AccountKeyChain.bs.js");
  var PayoutTransaction = require("../wallet/PayoutTransaction.bs.js");
  
  var class_tables = [
    0,
    0,
    0
  ];
  
  function make(param, log) {
    var payoutTx = param[/* data */2][/* payoutTx */1];
    var payoutProcess = param[/* processId */0];
    var match = Curry._3(EventLog.reduce, (function (param, param$1) {
            var $$event = param$1[/* event */0];
            var systemIssuer = param[5];
            var keyChains = param[4];
            var custodiansThatSigned = param[3];
            var currentCustodians = param[2];
            var txs = param[1];
            var broadcast = param[0];
            var exit = 0;
            switch ($$event.tag | 0) {
              case 0 : 
                  return /* tuple */[
                          broadcast,
                          txs,
                          currentCustodians,
                          custodiansThatSigned,
                          keyChains,
                          $$event[0][/* systemIssuer */7]
                        ];
              case 19 : 
                  return /* tuple */[
                          broadcast,
                          txs,
                          Belt_Set.add(currentCustodians, $$event[0][/* data */2][/* partnerId */0]),
                          custodiansThatSigned,
                          keyChains,
                          systemIssuer
                        ];
              case 24 : 
                  return /* tuple */[
                          broadcast,
                          txs,
                          Belt_Set.remove(currentCustodians, $$event[0][/* data */2][/* custodianId */0]),
                          custodiansThatSigned,
                          keyChains,
                          systemIssuer
                        ];
              case 26 : 
                  var match = $$event[0];
                  if (PrimitiveTypes.ProcessId[/* eq */5](match[/* processId */0], payoutProcess)) {
                    return /* tuple */[
                            broadcast,
                            /* :: */[
                              payoutTx,
                              txs
                            ],
                            currentCustodians,
                            Belt_Set.add(custodiansThatSigned, match[/* proposerId */4]),
                            keyChains,
                            systemIssuer
                          ];
                  } else {
                    exit = 1;
                  }
                  break;
              case 32 : 
                  var match$1 = $$event[0];
                  if (PrimitiveTypes.ProcessId[/* eq */5](match$1[/* processId */0], payoutProcess)) {
                    return /* tuple */[
                            broadcast,
                            /* :: */[
                              match$1[/* payoutTx */2],
                              txs
                            ],
                            currentCustodians,
                            Belt_Set.add(custodiansThatSigned, match$1[/* custodianId */1]),
                            keyChains,
                            systemIssuer
                          ];
                  } else {
                    exit = 1;
                  }
                  break;
              case 33 : 
                  if (PrimitiveTypes.ProcessId[/* eq */5]($$event[0][/* processId */0], payoutProcess)) {
                    return /* tuple */[
                            false,
                            txs,
                            currentCustodians,
                            custodiansThatSigned,
                            keyChains,
                            systemIssuer
                          ];
                  } else {
                    exit = 1;
                  }
                  break;
              case 38 : 
                  return /* tuple */[
                          broadcast,
                          txs,
                          currentCustodians,
                          custodiansThatSigned,
                          AccountKeyChain.Collection[/* add */1]($$event[0][/* keyChain */0], keyChains),
                          systemIssuer
                        ];
              default:
                exit = 1;
            }
            if (exit === 1) {
              return /* tuple */[
                      broadcast,
                      txs,
                      currentCustodians,
                      custodiansThatSigned,
                      keyChains,
                      systemIssuer
                    ];
            }
            
          }), /* tuple */[
          true,
          /* [] */0,
          PrimitiveTypes.UserId[/* emptySet */9],
          PrimitiveTypes.UserId[/* emptySet */9],
          AccountKeyChain.Collection[/* empty */0],
          BitcoinjsLib.ECPair.makeRandom()
        ], log);
    var keyChains = match[4];
    var custodiansThatSigned = match[3];
    var currentCustodians = match[2];
    var needsFinalizing = match[0];
    var missingSigs = PayoutTransaction.missingSignatures(currentCustodians, custodiansThatSigned, keyChains, payoutTx);
    var sigsReady = function (param) {
      return Belt_Set.size(Belt_Set.union(param[/* mandatory */0], param[/* additional */1])) === 0;
    };
    if (!class_tables[0]) {
      var $$class = CamlinternalOO.create_table([
            "processCompleted",
            "receive",
            "pendingEvent"
          ]);
      var env = CamlinternalOO.new_variable($$class, "");
      var ids = CamlinternalOO.new_methods_variables($$class, [
            "receive",
            "processCompleted",
            "pendingEvent"
          ], [
            "finalTransaction",
            "signedTxs",
            "custodians",
            "signatures",
            "missingSigs",
            "delivered"
          ]);
      var receive = ids[0];
      var processCompleted = ids[1];
      var pendingEvent = ids[2];
      var finalTransaction = ids[3];
      var signedTxs = ids[4];
      var custodians = ids[5];
      var signatures = ids[6];
      var missingSigs$1 = ids[7];
      var delivered = ids[8];
      CamlinternalOO.set_methods($$class, /* array */[
            receive,
            (function (self$1, param) {
                var env$1 = self$1[env];
                var $$event = param[/* event */0];
                switch ($$event.tag | 0) {
                  case 19 : 
                      self$1[custodians][0] = Belt_Set.add(self$1[custodians][0], $$event[0][/* data */2][/* partnerId */0]);
                      return /* () */0;
                  case 24 : 
                      self$1[custodians][0] = Belt_Set.remove(self$1[custodians][0], $$event[0][/* data */2][/* custodianId */0]);
                      return /* () */0;
                  case 32 : 
                      var match = $$event[0];
                      var payoutTx = match[/* payoutTx */2];
                      if (PrimitiveTypes.ProcessId[/* eq */5](match[/* processId */0], env$1[0])) {
                        self$1[signedTxs][0] = /* :: */[
                          payoutTx,
                          self$1[signedTxs][0]
                        ];
                        self$1[signatures][0] = Belt_Set.add(self$1[signatures][0], match[/* custodianId */1]);
                        self$1[missingSigs$1][0] = PayoutTransaction.missingSignatures(self$1[custodians][0], self$1[signatures][0], env$1[6], payoutTx);
                        var match$1 = env$1[5] && Curry._1(env$1[7], self$1[missingSigs$1][0]);
                        self$1[finalTransaction][0] = match$1 ? Caml_option.some(PayoutTransaction.finalize(self$1[signedTxs][0])) : undefined;
                        return /* () */0;
                      } else {
                        return /* () */0;
                      }
                  case 33 : 
                      if (PrimitiveTypes.ProcessId[/* eq */5]($$event[0][/* processId */0], env$1[0])) {
                        self$1[delivered][0] = true;
                        return /* () */0;
                      } else {
                        return /* () */0;
                      }
                  default:
                    return /* () */0;
                }
              }),
            processCompleted,
            (function (self$1, param) {
                if (self$1[env][5] === false) {
                  return true;
                } else {
                  return self$1[delivered][0];
                }
              }),
            pendingEvent,
            (function (self$1, param) {
                var env$1 = self$1[env];
                return Utils.mapOption((function (tx) {
                              return /* tuple */[
                                      env$1[4],
                                      /* PayoutFinalized */Block.__(33, [Curry._3($$Event.Payout[/* Finalized */10][/* make */0], env$1[0], tx.getId(), /* record */[
                                                /* txHex */tx.toHex(),
                                                /* usedInputs */env$1[1],
                                                /* misthosFeeAddress */env$1[2],
                                                /* changeAddress */env$1[3]
                                              ])])
                                    ];
                            }), self$1[finalTransaction][0]);
              })
          ]);
      var env_init = function (env$1) {
        var self = CamlinternalOO.create_object_opt(0, $$class);
        var match = env$1[1] && Curry._1(env$1[6], env$1[5]);
        self[finalTransaction] = /* record */[/* contents */match ? Caml_option.some(PayoutTransaction.finalize(env$1[2])) : undefined];
        self[signedTxs] = /* record */[/* contents */env$1[2]];
        self[custodians] = /* record */[/* contents */env$1[3]];
        self[signatures] = /* record */[/* contents */env$1[4]];
        self[missingSigs$1] = /* record */[/* contents */env$1[5]];
        self[delivered] = /* record */[/* contents */false];
        self[env] = env$1[0];
        return self;
      };
      CamlinternalOO.init_class($$class);
      class_tables[0] = env_init;
    }
    return Curry._1(class_tables[0], [
                [
                  payoutProcess,
                  payoutTx[/* usedInputs */1],
                  payoutTx[/* misthosFeeAddress */2],
                  payoutTx[/* changeAddress */3],
                  match[5],
                  needsFinalizing,
                  keyChains,
                  sigsReady
                ],
                needsFinalizing,
                match[1],
                currentCustodians,
                custodiansThatSigned,
                missingSigs,
                sigsReady
              ]);
  }
  
  exports.make = make;
  /* Event Not a pure module */
  