import { getModuleVisibilityScope, type RowVisibilityModule } from './visibility';

describe('visibility', () => {
  describe('getModuleVisibilityScope', () => {
    const permsQueryOnly = { query: 'view', booking: 'none' };
    const permsBookingOnly = { query: 'none', booking: 'view' };

    it('expands query list when flag on and query view granted', () => {
      expect(getModuleVisibilityScope(permsQueryOnly, 'query', true)).toBe('all');
      expect(getModuleVisibilityScope(permsQueryOnly, 'booking', true)).toBe('own');
    });

    it('expands booking list when flag on and booking view granted', () => {
      expect(getModuleVisibilityScope(permsBookingOnly, 'booking', true)).toBe('all');
      expect(getModuleVisibilityScope(permsBookingOnly, 'query', true)).toBe('own');
    });

    it('does not expand without module view even when flag is on', () => {
      expect(getModuleVisibilityScope({ query: 'none', booking: 'none' }, 'query', true)).toBe(
        'own',
      );
    });

    it('Booking=Delete does NOT grant see-all when the flag is off (single-control model)', () => {
      // Pre-2026-10-10 this returned 'all' via the legacy DELETE shortcut.
      // Now only the View-All flag grants cross-hierarchy visibility.
      expect(getModuleVisibilityScope({ booking: 'delete', query: 'view' }, 'query', false)).toBe(
        'own',
      );
      expect(getModuleVisibilityScope({ booking: 'delete', query: 'delete' }, 'booking', false)).toBe(
        'own',
      );
    });

    it('returns own when flag off regardless of permission level', () => {
      const modules: RowVisibilityModule[] = ['query', 'booking'];
      for (const mod of modules) {
        expect(getModuleVisibilityScope({ query: 'view', booking: 'view' }, mod, false)).toBe(
          'own',
        );
      }
    });
  });
});
