import InteractionService from '../InteractionService';
import { WidgetAfterSaveMethods } from '../../configuration';

const interactionSrvc = new InteractionService();

const mockWindow = {
    open: jest.fn(),
    location: {
        search: '?_deliverycountry=de',
    },
    parent: {
        postMessage: jest.fn(),
    },
};

jest.mock('../../../Framework/Api', () => ({
    getGETParameters: () => ({
        _display_net_prices: '1',
        _language: 'de_DE',
        _channel: '_default',
        _client: 'mobile',
        _not_used_parameter: '123',
    }),
}));

describe('Services/InteractionService', () => {
    test('openConfigurator opens the configuration in a new window', () => {
        interactionSrvc.openConfigurator(
            { code: 'abc123' },
            { method: WidgetAfterSaveMethods.OpenNewWindow, window: mockWindow }
        );
        expect(mockWindow.open).toHaveBeenCalledWith('/code:abc123?_deliverycountry=de');
    });
    test('openConfigurator sends the configuration code via postMessage api', () => {
        interactionSrvc.openConfigurator(
            { code: 'abc123' },
            { method: WidgetAfterSaveMethods.PostMessage, window: mockWindow }
        );
        expect(mockWindow.parent.postMessage).toHaveBeenCalledWith(
            { method: 'openConfigurator', code: 'abc123' },
            '*'
        );
    });
    test('createPdfUrl creates the correct url from the configuration', async () => {
        const url = await interactionSrvc.createPdfUrl('widget', { code: 'abc123' });
        expect(typeof url).toEqual('string');
        expect(url.indexOf('frontendapi/configurations/pdf/widget/abc123')).not.toBe(-1);
    });
    test('createPdfUrl includes the necessary query parameters in the url', async () => {
        const url = await interactionSrvc.createPdfUrl('widget', { code: 'abc123' });
        expect(url.indexOf('_display_net_prices=1')).not.toBe(-1);
        expect(url.indexOf('_language=de_DE')).not.toBe(-1);
        expect(url.indexOf('_channel=_default')).not.toBe(-1);
        expect(url.indexOf('_client=mobile')).not.toBe(-1);
        expect(url.indexOf('_not_used_parameter=123')).toBe(-1);
    });
});
