import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
/**
 * Render tests for the User detail drawer: overview facts, lazy-loaded Sessions
 * and Permissions tabs, and the security action surface.
 */
const DETAIL = {
    id: 'u1',
    name: 'Priya Nair',
    email: 'priya@actuatemedia.com',
    avatarUrl: null,
    initials: 'PN',
    role: 'EDITOR',
    roleName: 'Editor',
    status: 'active',
    mfaEnabled: true,
    mfaRequired: false,
    passwordSet: true,
    forcePasswordReset: false,
    failedLoginCount: 0,
    lockedAt: null,
    suspendedAt: null,
    authProvider: null,
    oauthProviders: [],
    ssoOnly: false,
    emailVerified: true,
    lastLoginAt: new Date(Date.now() - 86400000).toISOString(),
    lastActiveAt: new Date(Date.now() - 86400000).toISOString(),
    acceptedInviteAt: new Date('2026-01-02').toISOString(),
    createdAt: new Date('2026-01-01').toISOString(),
    invitedBy: { id: 'o1', name: 'Dana Chen', email: 'dana@actuatemedia.com' },
    externalDomain: false,
    contentOwnedCount: 4,
    isCurrentUser: false,
};
const SESSIONS = [
    {
        id: 's1',
        deviceLabel: 'Chrome on macOS',
        browser: 'Chrome',
        os: 'macOS',
        ipCountry: null,
        createdAt: new Date(Date.now() - 3600000).toISOString(),
        lastSeenAt: new Date().toISOString(),
        expiresAt: new Date(Date.now() + 86400000).toISOString(),
        revokedAt: null,
        current: false,
        active: true,
    },
];
const PERMISSIONS = {
    role: 'EDITOR',
    roleName: 'Editor',
    permissions: ['dashboard.view', 'content.publish'],
};
const { cmsApi } = vi.hoisted(() => ({ cmsApi: vi.fn() }));
vi.mock('../../lib/api.js', () => ({ cmsApi }));
const DIRECTORY = [
    {
        id: 'u1',
        name: 'Priya Nair',
        email: 'priya@actuatemedia.com',
        status: 'active',
        isCurrentUser: false,
    },
    {
        id: 'u2',
        name: 'Dana Chen',
        email: 'dana@actuatemedia.com',
        status: 'active',
        isCurrentUser: false,
    },
];
cmsApi.mockImplementation(async (endpoint) => {
    if (!endpoint)
        return { data: null, status: 200 };
    if (endpoint.includes('/transfer-content'))
        return { data: { documents: 4, media: 0, templates: 0, total: 4 }, status: 200 };
    if (endpoint.endsWith('/sessions'))
        return { data: SESSIONS, status: 200 };
    if (endpoint.endsWith('/permissions'))
        return { data: PERMISSIONS, status: 200 };
    if (endpoint.endsWith('/activity'))
        return { data: [], status: 200 };
    if (endpoint === '/users' || endpoint.startsWith('/users?'))
        return { data: DIRECTORY, status: 200 };
    if (endpoint.startsWith('/users/u1'))
        return { data: DETAIL, status: 200 };
    return { data: null, status: 200 };
});
const { UserDetailDrawer } = await import('../../views/users/UserDetailDrawer.js');
// Radix Tabs activates on mousedown (button 0), not a bare click.
function selectTab(name) {
    const tab = screen.getByRole('tab', { name });
    fireEvent.mouseDown(tab, { button: 0 });
    fireEvent.click(tab);
}
describe('UserDetailDrawer', () => {
    beforeEach(() => cmsApi.mockClear());
    it('renders overview facts for the user', async () => {
        render(_jsx(UserDetailDrawer, { userId: "u1", open: true, onOpenChange: () => { } }));
        expect(await screen.findByText('Priya Nair')).toBeTruthy();
        expect(screen.getByText('Content owned')).toBeTruthy();
        expect(screen.getByText('4 items')).toBeTruthy();
        expect(screen.getByText('Dana Chen')).toBeTruthy();
    });
    it('shows security actions on the Security tab', async () => {
        render(_jsx(UserDetailDrawer, { userId: "u1", open: true, onOpenChange: () => { } }));
        await screen.findByText('Priya Nair');
        selectTab('Security');
        expect(await screen.findByText('Multi-factor authentication')).toBeTruthy();
        expect(screen.getByRole('button', { name: 'Reset MFA' })).toBeTruthy();
        expect(screen.getByRole('button', { name: 'Send reset' })).toBeTruthy();
        expect(screen.getByRole('button', { name: 'Suspend' })).toBeTruthy();
    });
    it('lazy-loads the Sessions tab', async () => {
        render(_jsx(UserDetailDrawer, { userId: "u1", open: true, onOpenChange: () => { } }));
        await screen.findByText('Priya Nair');
        selectTab('Sessions');
        expect(await screen.findByText('Chrome on macOS')).toBeTruthy();
        expect(screen.getByRole('button', { name: 'Revoke all' })).toBeTruthy();
    });
    it('lazy-loads derived permissions', async () => {
        render(_jsx(UserDetailDrawer, { userId: "u1", open: true, onOpenChange: () => { } }));
        await screen.findByText('Priya Nair');
        selectTab('Permissions');
        await waitFor(() => expect(screen.getByText('content.publish')).toBeTruthy());
        expect(screen.getByText('dashboard.view')).toBeTruthy();
    });
    it('transfers content ownership to a chosen active recipient', async () => {
        render(_jsx(UserDetailDrawer, { userId: "u1", open: true, onOpenChange: () => { } }));
        await screen.findByText('Priya Nair');
        fireEvent.click(screen.getByRole('button', { name: 'Transfer content' }));
        // Modal opens and loads the directory, excluding the target (u1).
        expect(await screen.findByText('Transfer content ownership')).toBeTruthy();
        const select = (await screen.findByLabelText('Transfer to'));
        fireEvent.change(select, { target: { value: 'u2' } });
        fireEvent.click(screen.getByRole('button', { name: 'Transfer' }));
        await waitFor(() => expect(cmsApi.mock.calls.some(([endpoint]) => String(endpoint).includes('/users/u1/transfer-content'))).toBe(true));
    });
});
//# sourceMappingURL=user-detail-drawer.render.test.js.map