import ServiceLocator from '../../ServiceLocator';
import DesignDataService from '../DesignDataService';
import { setConf } from '../../configuration';

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.selectInitialDesignView', () => {
    it('selects the view for the area in designerPreSelection if available', () => {
        setConf('designerPreSelection', {
            designAreas: [{ identifier: 'sleeve', colorAmount: 2, productionMethod: 'print' }],
        });

        ServiceLocator.provide(
            new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    designArea: {
                        filteredDesignAreas: mockDesignAreas,
                    },
                    designView: {
                        designViews: [{ identifier: 'front_view' }, { identifier: 'side_view' }],
                        selectedDesignView: {
                            identifier: 'front_view',
                        },
                    },
                };
                dispatch = v => v;
            })()
        );
        const srv = new DesignDataService();

        srv.selectDesignView = jest.fn();

        srv.selectInitialDesignView();

        expect(srv.selectDesignView).toHaveBeenCalledWith('side_view');
    });

    it('selects the first view if the provided design area does not exist', () => {
        setConf('designerPreSelection', {
            designAreas: [{ identifier: 'nonExistingArea', colorAmount: 2, productionMethod: 'print' }],
        });

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

        srv.selectDesignView = jest.fn();

        srv.selectInitialDesignView();

        expect(srv.selectDesignView).toHaveBeenCalledWith('front_view');
    });
});
