import ServiceLocator from '../../ServiceLocator';
import DesignDataService from '../DesignDataService';

const mockDesignAreas = [
    {
        identifier: 'front',
        sequenceNumber: '10',
        title: 'Vorne',
        baseShape: {},
        position: {},
        designProductionMethods: [
            {
                identifier: 'print',
                title: 'Druck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 5 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
            {
                identifier: 'digital_print',
                title: 'Digitaldruck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 8 },
                isDefault: true,
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
        ],
        customData: {},
        designView: { identifier: 'front_view' },
    },
    {
        identifier: 'sleeve',
        sequenceNumber: '20',
        title: '\u00c4rmel',
        baseShape: {},
        position: {},
        designProductionMethods: [
            {
                identifier: 'print',
                title: 'Druck',
                height: 20,
                width: 25,
                options: { minFontSize: 5 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
            {
                identifier: 'digital_print',
                title: 'Digitaldruck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 8 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
        ],
        customData: {},
        designView: { identifier: 'side_view' },
    },
];

describe('DesignDataService.selectViewForComponent', () => {
    it('calls selectDesignView with the view identifier referenced by the selected component', () => {
        const componentWithCreatorView = {
            identifier: 'id2',
            title: 'title2',
            creatorView: { identifier: 'side_view' },
        };

        ServiceLocator.provide(
            new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    designArea: {
                        filteredDesignAreas: mockDesignAreas,
                    },
                    designView: {
                        designViews: [{ identifier: 'front_view' }, { identifier: 'side_view' }],
                        selectedDesignView: {
                            identifier: 'front_view',
                        },
                    },
                    configurator: {
                        configuration: {
                            optionclassifications: [
                                {
                                    identifier: 'id1',
                                    title: 'title1',
                                    creatorView: null,
                                },
                                componentWithCreatorView,
                            ],
                        },
                    },
                };
                dispatch = v => v;
            })()
        );
        const srv = new DesignDataService();

        srv.selectDesignView = jest.fn();

        srv.selectViewForComponent(componentWithCreatorView);

        expect(srv.selectDesignView).toHaveBeenCalledWith('side_view', { saveLast: false });
    });

    it('calls selectDesignView with the default view identifier if no creatorView is referenced by the selected component', () => {
        const componentWithNoCreatorView = {
            identifier: 'id1',
            title: 'title1',
            creatorView: null,
        };

        ServiceLocator.provide(
            new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    designArea: {
                        filteredDesignAreas: mockDesignAreas,
                        selectedDesignArea: {},
                    },
                    designView: {
                        designViews: [{ identifier: 'front_view' }, { identifier: 'side_view' }],
                        selectedDesignView: {
                            identifier: 'side_view',
                        },
                    },
                    configurator: {
                        configuration: {
                            optionclassifications: [
                                componentWithNoCreatorView,
                                {
                                    identifier: 'id2',
                                    title: 'title2',
                                    creatorView: { identifier: 'side_view' },
                                },
                            ],
                        },
                    },
                };
                dispatch = v => v;
            })()
        );
        const srv = new DesignDataService();

        srv.selectDesignView = jest.fn();

        srv.selectViewForComponent(componentWithNoCreatorView);

        expect(srv.selectDesignView).toHaveBeenCalledWith('front_view', { saveLast: false });
    });
});
