// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Belt_List from "rescript/lib/es6/belt_List.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";

var empty_roles = [];

var empty = {
  title: undefined,
  breadcrumb: undefined,
  requiresAuth: false,
  roles: empty_roles,
  custom: undefined
};

function withTitle(meta, title) {
  return {
          title: title,
          breadcrumb: meta.breadcrumb,
          requiresAuth: meta.requiresAuth,
          roles: meta.roles,
          custom: meta.custom
        };
}

function withBreadcrumb(meta, breadcrumb) {
  return {
          title: meta.title,
          breadcrumb: breadcrumb,
          requiresAuth: meta.requiresAuth,
          roles: meta.roles,
          custom: meta.custom
        };
}

function withAuth(meta) {
  return {
          title: meta.title,
          breadcrumb: meta.breadcrumb,
          requiresAuth: true,
          roles: meta.roles,
          custom: meta.custom
        };
}

function withRoles(meta, roles) {
  return {
          title: meta.title,
          breadcrumb: meta.breadcrumb,
          requiresAuth: true,
          roles: roles,
          custom: meta.custom
        };
}

function withCustom(meta, custom) {
  return {
          title: meta.title,
          breadcrumb: meta.breadcrumb,
          requiresAuth: meta.requiresAuth,
          roles: meta.roles,
          custom: Caml_option.some(custom)
        };
}

function build(routeBuilder, getMeta) {
  return {
          parse: (function (url) {
              var route = routeBuilder.parse(url);
              if (route === undefined) {
                return ;
              }
              var route$1 = Caml_option.valFromOption(route);
              return {
                      route: route$1,
                      meta: getMeta(route$1)
                    };
            }),
          toString: routeBuilder.toString,
          getMeta: getMeta
        };
}

function checkAuth(routeWithMeta, isLoggedIn, userRoles) {
  var meta = routeWithMeta.meta;
  var route = routeWithMeta.route;
  if (!meta.requiresAuth) {
    return {
            TAG: "Allowed",
            _0: route
          };
  }
  if (!isLoggedIn) {
    return {
            TAG: "RequiresLogin",
            _0: route
          };
  }
  if (meta.roles.length === 0) {
    return {
            TAG: "Allowed",
            _0: route
          };
  }
  var hasRole = Belt_Array.some(meta.roles, (function (role) {
          return Belt_Array.some(userRoles, (function (userRole) {
                        return userRole === role;
                      }));
        }));
  if (hasRole) {
    return {
            TAG: "Allowed",
            _0: route
          };
  } else {
    return {
            TAG: "InsufficientRoles",
            _0: route,
            _1: meta.roles
          };
  }
}

function MakeBreadcrumbs(Config) {
  var collectBreadcrumbs = function (_route, _acc) {
    while(true) {
      var acc = _acc;
      var route = _route;
      var meta = Config.definition.getMeta(route);
      var url = Config.definition.toString(route);
      var b = meta.breadcrumb;
      var label;
      if (b !== undefined) {
        label = b;
      } else {
        var t = meta.title;
        label = t !== undefined ? t : "/";
      }
      var item_route = Caml_option.some(route);
      var item = {
        label: label,
        route: item_route,
        url: url
      };
      var newAcc = {
        hd: item,
        tl: acc
      };
      var parent = Config.getParent(route);
      if (parent === undefined) {
        return newAcc;
      }
      _acc = newAcc;
      _route = Caml_option.valFromOption(parent);
      continue ;
    };
  };
  var breadcrumbs = function (route) {
    return Belt_List.toArray(collectBreadcrumbs(route, /* [] */0));
  };
  return {
          breadcrumbs: breadcrumbs
        };
}

function make(title, breadcrumb, requiresAuthOpt, rolesOpt, custom, param) {
  var requiresAuth = requiresAuthOpt !== undefined ? requiresAuthOpt : false;
  var roles = rolesOpt !== undefined ? rolesOpt : [];
  return {
          title: title,
          breadcrumb: breadcrumb,
          requiresAuth: requiresAuth,
          roles: roles,
          custom: custom
        };
}

function public_(title, breadcrumb, param) {
  return make(title, breadcrumb, false, undefined, undefined, undefined);
}

function authenticated(title, breadcrumb, rolesOpt, param) {
  var roles = rolesOpt !== undefined ? rolesOpt : [];
  return make(title, breadcrumb, true, roles, undefined, undefined);
}

export {
  empty ,
  withTitle ,
  withBreadcrumb ,
  withAuth ,
  withRoles ,
  withCustom ,
  build ,
  checkAuth ,
  MakeBreadcrumbs ,
  make ,
  public_ ,
  authenticated ,
}
/* No side effect */
