import { isTabSelectedByRoute } from '@/helpers/tab.helper';
import { Tab } from '@/models/tab.model';
import { RouteLocation } from 'vue-router';
import { test } from 'vitest';

const createTab = (overrides: Partial<Tab> = {}): Tab => ({
  id: 'tab-1',
  name: 'Tab 1',
  ...overrides,
});

const createRoute = (overrides: Partial<RouteLocation> = {}): RouteLocation =>
  ({
    path: '/default',
    name: 'default',
    ...overrides,
  }) as RouteLocation;

describe('tab.helper', () => {
  describe('isTabSelectedByRoute', () => {
    describe('with routePath', () => {
      test.each([
        ['/users', '/users', true],
        ['/users', '/users/123', true],
        ['/users', '/admin', false],
        ['/users', '/admin/users', true],
      ])(
        'tab with routePath "%s" and currentRoute.path "%s" should return %s',
        (routePath, path, expected) => {
          const tab = createTab({ routePath });
          const route = createRoute({ path });
          expect(isTabSelectedByRoute(tab, route)).toBe(expected);
        },
      );
    });

    describe('with routeName (no routePath)', () => {
      test.each([
        ['users', 'users', true],
        ['users', 'users-list', true],
        ['users', 'admin', false],
        ['users', 'admin-users', true],
      ])(
        'tab with routeName "%s" and currentRoute.name "%s" should return %s',
        (routeName, name, expected) => {
          const tab = createTab({ routeName });
          const route = createRoute({ name });
          expect(isTabSelectedByRoute(tab, route)).toBe(expected);
        },
      );

      test('should return false when currentRoute.name is undefined', () => {
        const tab = createTab({ routeName: 'users' });
        const route = createRoute({ name: undefined });
        expect(isTabSelectedByRoute(tab, route)).toBe(false);
      });

      test('should return false when currentRoute.name is null', () => {
        const tab = createTab({ routeName: 'users' });
        const route = createRoute({ name: null as unknown as string });
        expect(isTabSelectedByRoute(tab, route)).toBe(false);
      });
    });

    describe('without routePath and routeName', () => {
      test('should return false', () => {
        const tab = createTab();
        const route = createRoute({ path: '/users', name: 'users' });
        expect(isTabSelectedByRoute(tab, route)).toBe(false);
      });
    });

    describe('routePath takes priority over routeName', () => {
      test('should use routePath when both are defined', () => {
        const tab = createTab({ routePath: '/admin', routeName: 'users' });
        const route = createRoute({ path: '/admin', name: 'other' });
        expect(isTabSelectedByRoute(tab, route)).toBe(true);
      });

      test('should not match routeName when routePath is defined but does not match', () => {
        const tab = createTab({ routePath: '/admin', routeName: 'users' });
        const route = createRoute({ path: '/other', name: 'users' });
        expect(isTabSelectedByRoute(tab, route)).toBe(false);
      });
    });
  });
});
