import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import { WorkspaceConfirmDialog, WorkspaceDialog } from './WorkspaceDialog';

afterEach(cleanup);

test('renders one shared Semi dialog shell with a localized close action', () => {
  const onCancel = vi.fn();
  render(<WorkspaceDialog
    visible
    ariaLabel="修改安全设置"
    closeLabel="关闭安全设置"
    title="安全设置"
    description="更新账号凭据"
    icon={<span>锁</span>}
    badge="账号安全"
    onCancel={onCancel}
    footer={<button>保存</button>}
  >
    <p>设置内容</p>
  </WorkspaceDialog>);

  const dialog = screen.getByRole('dialog', { name: '修改安全设置' });
  expect(dialog.closest('.v2-workspace-dialog') ?? document.querySelector('.v2-workspace-dialog')).toBeInTheDocument();
  expect(dialog).toHaveTextContent('安全设置更新账号凭据账号安全设置内容保存');
  fireEvent.click(screen.getByLabelText('关闭安全设置'));
  expect(onCancel).toHaveBeenCalledTimes(1);
});

test('keeps confirmation impact, cancel and destructive action in one accessible dialog', () => {
  const onCancel = vi.fn();
  const onConfirm = vi.fn();
  render(<WorkspaceConfirmDialog
    visible
    ariaLabel="确认批量操作"
    title="清空车辆权限？"
    description="确认后先修改当前草稿。"
    summaryItems={[
      { label: '影响对象', value: '43 辆', detail: '全部车辆权限', tone: 'danger' },
      { label: '正式生效', value: '保存后', detail: '当前仅修改草稿', tone: 'success' }
    ]}
    cancelLabel="返回核对"
    confirmLabel="确认清空"
    onCancel={onCancel}
    onConfirm={onConfirm}
  />);

  const dialog = screen.getByRole('dialog', { name: '确认批量操作' });
  expect(dialog).toHaveTextContent('影响对象43 辆全部车辆权限正式生效保存后当前仅修改草稿');
  expect(screen.getByRole('list', { name: '操作影响摘要' })).toBeInTheDocument();
  fireEvent.click(screen.getByRole('button', { name: '返回核对' }));
  fireEvent.click(screen.getByRole('button', { name: '确认清空' }));
  expect(onCancel).toHaveBeenCalledTimes(1);
  expect(onConfirm).toHaveBeenCalledTimes(1);
});
