import assert from 'node:assert/strict'

import {
  appViews,
  navItems,
  resolveAppViewFromRoute,
  resolveTargetRouteName
} from '../src/composables/useNavigation.js'

function testDerivesViewFromRouteName() {
  assert.equal(resolveAppViewFromRoute({ name: 'app-log-detail', meta: {} }), 'settings')
  assert.equal(resolveAppViewFromRoute({ name: 'app-request-detail', meta: {} }), 'documents')
  assert.equal(resolveAppViewFromRoute({ name: 'app-policies', meta: { appView: 'settings' } }), 'policies')
}

function testFallsBackToValidMeta() {
  assert.equal(resolveAppViewFromRoute({ name: 'custom', meta: { appView: 'employees' } }), 'employees')
  assert.equal(resolveAppViewFromRoute({ name: 'custom', meta: { appView: 'unknown' } }), 'overview')
}

function testResolvesMainRouteNames() {
  assert.equal(resolveTargetRouteName('logs'), 'app-settings')
  assert.equal(resolveTargetRouteName('policies'), 'app-policies')
  assert.equal(resolveTargetRouteName('receiptFolder'), 'app-receiptFolder')
  assert.equal(resolveTargetRouteName('requests'), 'app-overview')
  assert.equal(resolveTargetRouteName('approval'), 'app-overview')
  assert.equal(resolveTargetRouteName('archive'), 'app-overview')
  assert.equal(resolveTargetRouteName('missing'), 'app-overview')
}

function testReceiptFolderFollowsDocumentCenter() {
  assert.equal(appViews[appViews.indexOf('documents') + 1], 'receiptFolder')
  const documentIndex = navItems.findIndex((item) => item.id === 'documents')
  const receiptIndex = navItems.findIndex((item) => item.id === 'receiptFolder')
  assert.equal(receiptIndex, documentIndex + 1)
  assert.equal(navItems[receiptIndex].label, '票据夹')
}

function testLegacyCentersAreRemovedFromNavigation() {
  assert.equal(appViews.includes('requests'), false)
  assert.equal(appViews.includes('approval'), false)
  assert.equal(appViews.includes('archive'), false)
  assert.equal(appViews.includes('logs'), false)
  assert.equal(navItems.some((item) => ['requests', 'approval', 'archive', 'logs'].includes(item.id)), false)
}

function run() {
  testDerivesViewFromRouteName()
  testFallsBackToValidMeta()
  testResolvesMainRouteNames()
  testReceiptFolderFollowsDocumentCenter()
  testLegacyCentersAreRemovedFromNavigation()
  console.log('navigation route resolution tests passed')
}

run()
