<commit_before>import { router } from '@vue-storefront/core/app'
import VueRouter, { RouteConfig, Route } from 'vue-router'

const RouterManager = {
  _registeredRoutes: new Array<RouteConfig>(),
  _routeQueue: new Array<RouteConfig>(),
  _routeQueueFlushed: false,
  _routeLock: null,
  _routeDispatched: false,
  _callbacks: [],
  addRoutes: function (routes: RouteConfig[], routerInstance: VueRouter = router, useRouteQueue: boolean = false, priority: number = 0): void {
    if (useRouteQueue && !this._routeQueueFlushed) {
      this._routeQueue.push(...routes.map(route => { return { route: route, priority: priority } }))
    } else {
      const uniqueRoutes = routes.filter((route) => !this.isRouteAdded(this._registeredRoutes, route)) // to do - priority
      if (uniqueRoutes.length > 0) {
        this._registeredRoutes.push(...uniqueRoutes)
        routerInstance.addRoutes(uniqueRoutes)
      }
    }
  },
  flushRouteQueue: function (routerInstance: VueRouter = router): void {
    if (!this._routeQueueFlushed) {
      const readyQueue = this._routeQueue.map(queueItem => queueItem.route).filter(item => typeof item === 'object') // to do - priority, add once only
      // console.log('readyQueue', readyQueue)
      this._registeredRoutes.push(...readyQueue)
      routerInstance.addRoutes(readyQueue)
      this._routeQueueFlushed = true
      this._routeQueue = []
    }
  },
  isRouteAdded: function (addedRoutes: RouteConfig[], route: RouteConfig) {
    return addedRoutes.findIndex((addedRoute) => addedRoute.name === route.name && addedRoute.path === route.path) >= 0
  },
  addDispatchCallback: function (callback: Function) {
    this._callbacks.push(callback)
  },
  findByName: function (name: string): RouteConfig {
    const registeredRoute = this._registeredRoutes.findIndex(r => r.name === name)
    if (registeredRoute) return registeredRoute
    if (this._routeQueueFlushed) return null
    const queuedRoute = this._routeQueue.find(queueItem => queueItem.route.name === name)
    return queuedRoute ? queuedRoute.route : null
  },
  findByPath: function (path: string): RouteConfig {
    const registeredRoute = this._registeredRoutes.find(r => r.path === path)
    if (registeredRoute) return registeredRoute
    if (this._routeQueueFlushed) return null
    const queuedRoute = this._routeQueue.find(queueItem => queueItem.route.path === path)
    return queuedRoute ? queuedRoute.route : null
  },
  lockRoute: function () {
    let resolver
    this._routeLock = {
      lockPromise: new Promise(resolve => { resolver = resolve }),
      resolver
    }
  },
  isRouteProcessing: function () {
    return !!this._routeLock
  },
  isRouteDispatched: function () {
    return !!this._routeDispatched
  },
  getRouteLockPromise: function () {
    if (this._routeLock) return this._routeLock.lockPromise
    return Promise.resolve()
  },
  unlockRoute: function () {
    if (this._routeLock) {
      this._routeLock.resolver()
      this._routeLock = null
    }<commit_msg>Router Manager add routes by priority<commit_after>import { router } from '@vue-storefront/core/app'
import VueRouter, { RouteConfig, Route } from 'vue-router'

const RouterManager = {
  _registeredRoutes: new Array<RouteConfig>(),
  _routeQueue: new Array<RouteConfig>(),
  _routeQueueFlushed: false,
  _routeLock: null,
  _routeDispatched: false,
  _callbacks: [],
  addRoutes: function (routes: RouteConfig[], routerInstance: VueRouter = router, useRouteQueue: boolean = false, priority: number = 0): void {
    if (useRouteQueue && !this._routeQueueFlushed) {
      this._routeQueue.push(...routes.map(route => { return { route: route, priority: priority } }))
    } else {
      const uniqueRoutes = routes.filter((route) => !this.isRouteAdded(this._registeredRoutes, route))
      if (uniqueRoutes.length > 0) {
        this._registeredRoutes.push(...uniqueRoutes.map(route => { return { route: route, priority: priority } }))
        routerInstance.addRoutes(uniqueRoutes)
      }
    }
  },
  flushRouteQueue: function (routerInstance: VueRouter = router): void {
    if (!this._routeQueueFlushed) {
      // const readyQueue = this._routeQueue.map(queueItem => queueItem.route) // .filter(item => typeof item === 'object') // to do - priority, add once only
      // console.log('readyQueue', readyQueue)
      // this._registeredRoutes.push(...readyQueue)
      // routerInstance.addRoutes(readyQueue)
      this.addRoutesByPriority(this._routeQueue, routerInstance)
      this._routeQueueFlushed = true
      this._routeQueue = []
    }
  },
  addRoutesByPriority: function (routesData, routerInstance: VueRouter) {
    const routesToAdd = []
    for (const routeData of routesData) {
      let exisitingIndex = routesToAdd.findIndex(r => r.route.name === routeData.route.name && r.route.path === routeData.route.path)
      if ((exisitingIndex >= 0) && (routesToAdd[exisitingIndex].priority < routeData.priority)) { // same priority doesn't override exisiting
        routesToAdd.splice(exisitingIndex, 1)
        console.log('exisiting', routeData.route.name)
        exisitingIndex = -1
      }
      if (exisitingIndex < 0) {
        routesToAdd.push(routeData)
      }
    }
    this._registeredRoutes.push(...routesToAdd)
    routerInstance.addRoutes(routesToAdd.map(r => r.route))
  },
  isRouteAdded: function (addedRoutes: any[], route: RouteConfig) {
    return addedRoutes.findIndex((addedRoute) => addedRoute.route.name === route.name && addedRoute.route.path === route.path) >= 0
  },
  addDispatchCallback: function (callback: Function) {
    this._callbacks.push(callback)
  },
  findByName: function (name: string): RouteConfig {
    const registeredRoute = this._registeredRoutes.find(r => r.route.name === name)
    if (registeredRoute) return registeredRoute
    if (this._routeQueueFlushed) return null
    const queuedRoute = this._routeQueue.find(queueItem => queueItem.route.name === name)
    return queuedRoute ? queuedRoute.route : null
  },
  findByPath: function (path: string): RouteConfig {
    const registeredRoute = this._registeredRoutes.find(r => r.route.path === path)
    if (registeredRoute) return registeredRoute
    if (this._routeQueueFlushed) return null
    const queuedRoute = this._routeQueue.find(queueItem => queueItem.route.path === path)
    return queuedRoute ? queuedRoute.route : null
  },
  lockRoute: function () {
    let resolver
    this._routeLock = {
      lockPromise: new Promise(resolve => { resolver = resolve }),
      resolver
    }
  },
  isRouteProcessing: function () {
    return !!this._routeLock
  },
  isRouteDispatched: function () {
    return !!this._routeDispatched
  },
  getRouteLockPromise: function () {
    if (this._routeLock) return this._routeLock.lockPromise
    return Promise.resolve()
  },
  unlockRoute: function () {
    if (this._routeLock) {
      this._routeLock.resolver()
      this._routeLock = null
    }
{'fraction_non_alphanumeric': 0.12471098265895954, 'fraction_numerical': 0.001445086705202312, 'mean_word_length': 3.69857433808554, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6856376', 'n_tokens_mistral': 2062, 'n_tokens_neox': 2051, 'n_words': 444}