/* xeslint-disable */
import store from "../store";
import roles from "./roles";
import lt from "../api/locale";

function addNavItem(title, icon, route) {
  store.commit("addNavItem", {
    title,
    icon,
    route
  });
}

/////////////////////////////////////
// Initialize the app
// on change of authentication status
export default function initialize() {
  if (store.state.authenticated) {
    //fetch the required localized text keys into the cache
    lt.fetch([
      "Home",
      "Service",
      "Dispatch",
      "Inventory",
      "Accounting",
      "Administration",
      "Operations",
      "HelpAboutAyaNova",
      "Logout"
    ])
      .then(function() {
        //put nav items into store
        //Everyone has a home
        addNavItem(lt.get("Home"), "home", "/");

        if (
          roles.hasRole(roles.AuthorizationRoles.TechLimited) ||
          roles.hasRole(roles.AuthorizationRoles.TechFull) ||
          roles.hasRole(roles.AuthorizationRoles.SubContractorLimited) ||
          roles.hasRole(roles.AuthorizationRoles.SubContractorFull)
        ) {
          addNavItem(lt.get("Service"), "toolbox", "/service");
        }

        if (
          roles.hasRole(roles.AuthorizationRoles.DispatchLimited) ||
          roles.hasRole(roles.AuthorizationRoles.DispatchFull)
        ) {
          addNavItem(lt.get("Dispatch"), "shipping-fast", "/dispatch");
        }

        if (
          roles.hasRole(roles.AuthorizationRoles.InventoryLimited) ||
          roles.hasRole(roles.AuthorizationRoles.InventoryFull)
        ) {
          addNavItem(lt.get("Inventory"), "dolly", "/inventory");
        }

        if (roles.hasRole(roles.AuthorizationRoles.AccountingFull)) {
          addNavItem(
            lt.get("Accounting"),
            "file-invoice-dollar",
            "/accounting"
          );
        }

        if (
          roles.hasRole(roles.AuthorizationRoles.BizAdminLimited) ||
          roles.hasRole(roles.AuthorizationRoles.BizAdminFull)
        ) {
          addNavItem(lt.get("Administration"), "user-tie", "/admin");
        }

        if (
          roles.hasRole(roles.AuthorizationRoles.OpsAdminFull) ||
          roles.hasRole(roles.AuthorizationRoles.OpsAdminLimited)
        ) {
          addNavItem(lt.get("Operations"), "cogs", "ops");
        }

        //Everyone can see about and logout
        addNavItem(lt.get("HelpAboutAyaNova"), "info-circle", "/about");
        addNavItem(lt.get("Logout"), "sign-out-alt", "/login");
      })
      .catch(function(error) {
        store.commit("logItem", "Initialize::() -> error" + error);
        throw error;
      });
  }
}
