import React from 'react';
import {render, screen} from '@testing-library/react';

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
}));
jest.mock('@/components/global/CarteLeafLetMultiMarker', () => ({
  __esModule: true,
  default: ({lieux, positionMap, zoomMap}: {lieux: {title: string}[]; positionMap: number[]; zoomMap: number}) => (
    <div data-testid="carte-multi" data-count={lieux.length} data-position={positionMap.join(',')} data-zoom={zoomMap}>
      {lieux.map(l => l.title).join('|')}
    </div>
  ),
}));

import SessionsMap from '../SessionsMap';

const rennes = {vilNom: 'Rennes', vilCodepostal: '35000', vilGeoX: 48.11, vilGeoY: -1.68, vilGeoZoom: 12};
const brest = {vilNom: 'Brest', vilCodepostal: '29200', vilGeoX: 48.39, vilGeoY: -4.49, vilGeoZoom: 12};
const stage = {
  fstId: 1521,
  fstDescription: '',
  fstTarifs: '',
  fstAdresseorganisateur: 0,
  sessions: [],
  fstEve: {eveId: 9, eveDate: '2026-10-03', eveHeure: null, eveLibelle: 'Stage', eveTev: {tevLibelle: 'Stage'}, eveVil: rennes, evePla: null, aenOrganisateurs: []},
};
const session = (fssId: number, fodNom: string, extra: object) => ({
  fssId, fssFod: {fodNom}, fssSoustitre: '', fssDateDebut: '2026-10-03', fssHeureDebut: '', fssDateFin: '2026-10-03', fssHeureFin: '',
  fssTarifs: '', fssDescription: '', animateurs: [], ...extra,
});

describe('SessionsMap', () => {
  it('shouldPlaceOneMarkerPerSessionAtItsOwnLocation', () => {
    const sessions = [
      session(1, 'Bombarde', {fssAdressestage: 0, fssVil: brest}),
      session(2, 'Biniou', {fssAdressestage: 1}),
    ];

    render(<SessionsMap stage={{...stage, sessions}} locale="fr" />);

    expect(screen.getByTestId('sessions-map')).toHaveTextContent('sessions_map_title');
    const carte = screen.getByTestId('carte-multi');
    expect(carte).toHaveAttribute('data-count', '2');
    expect(carte).toHaveTextContent('Bombarde|Biniou');
    expect(carte).toHaveAttribute('data-position', '48.11,-1.68');
  });

  it('shouldRenderNothingWhenEverySessionIsAtTheStageLocation', () => {
    const sessions = [session(1, 'Bombarde', {fssAdressestage: 1}), session(2, 'Biniou', {fssAdressestage: 1})];

    const {container} = render(<SessionsMap stage={{...stage, sessions}} locale="fr" />);

    expect(container).toBeEmptyDOMElement();
  });
});
