// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Tea_Url from "./Tea_Url.res.mjs";
import * as Belt_Array from "/node_modules/@rescript/runtime/lib/es6/Belt_Array.js";
import * as Tea_Navigation from "./Tea_Navigation.res.mjs";

var emptyConfig_guards = [];

var emptyConfig_asyncGuards = [];

var emptyConfig = {
  guards: emptyConfig_guards,
  asyncGuards: emptyConfig_asyncGuards
};

function runGuards(guards, url) {
  var _idx = 0;
  while(true) {
    var idx = _idx;
    if (idx >= guards.length) {
      return "Allow";
    }
    var result = Belt_Array.getExn(guards, idx)(url);
    if (typeof result === "object") {
      return result;
    }
    _idx = idx + 1 | 0;
    continue ;
  };
}

async function runAllGuards(config, url) {
  var result = runGuards(config.guards, url);
  if (typeof result === "object") {
    return Promise.resolve(result);
  }
  var checkAsync = async function (idx) {
    if (idx >= config.asyncGuards.length) {
      return Promise.resolve("Allow");
    }
    var result = await Belt_Array.getExn(config.asyncGuards, idx)(url);
    if (typeof result !== "object") {
      return await checkAsync(idx + 1 | 0);
    } else {
      return Promise.resolve(result);
    }
  };
  return await checkAsync(0);
}

function guardedPush(config, url) {
  var target = runGuards(config.guards, url);
  if (typeof target !== "object") {
    Tea_Navigation.execute({
          TAG: "Push",
          _0: url
        });
    return true;
  }
  if (target.TAG === "Block") {
    return false;
  }
  Tea_Navigation.execute({
        TAG: "Push",
        _0: Tea_Url.parse(target._0)
      });
  return true;
}

export {
  emptyConfig ,
  runGuards ,
  runAllGuards ,
  guardedPush ,
}
/* No side effect */
