import { render, screen } from '@testing-library/react';
import { ThemeProvider } from '../../../theme';

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => (key === 'currency_toman' ? 'Toman' : key),
  useLocale: () => 'en',
}));

import Money from './Money';

describe('<Money/>', () => {
  const wrap = (ui: React.ReactNode) => render(<ThemeProvider>{ui}</ThemeProvider>);

  it('renders the Toman-formatted amount with the unit label', () => {
    wrap(<Money amountIrr="45000000" />);
    expect(screen.getByText(/4,500,000/)).toBeInTheDocument();
    expect(screen.getByText(/Toman/)).toBeInTheDocument();
  });

  it('hides the unit label when hideUnit is set', () => {
    wrap(<Money amountIrr="45000000" hideUnit />);
    expect(screen.queryByText(/Toman/)).not.toBeInTheDocument();
  });

  it('prefixes an explicit minus sign for a deduction, held with the digits', () => {
    wrap(<Money amountIrr="45000000" deduction />);
    const el = screen.getByText((_, node) => node?.textContent === '−4,500,000');
    expect(el).toHaveAttribute('dir', 'ltr');
  });

  it('marks the root with data-deduction when a deduction', () => {
    const { container } = render(
      <ThemeProvider>
        <Money amountIrr="1000" deduction />
      </ThemeProvider>,
    );
    expect(container.querySelector('[data-money][data-deduction="true"]')).toBeInTheDocument();
  });
});
