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

describe('[INTEGRATION] handleCloseIfNeeded', () => {
    test(
        'in 3D mode: calls the DesignDataService.closeDesignAreaEditor function ' +
            'if the designer is open and returns true because it has performed an action',
        () => {
            const mockStoreSrv = new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    configurator: {
                        configuration: {
                            item: {
                                settings: {
                                    visualizationCreator: '3d',
                                    visualizationDesigner: '3d',
                                },
                            },
                        },
                    },
                    configurationMode: {
                        selectedMode: 'designer',
                    },
                };
                dispatch = jest.fn();
            })();
            ServiceLocator.provide(mockStoreSrv);
            const mockUISrv = new (class UIService {
                static serviceName = 'UIService';
                isVisible = jest.fn(() => true);
            })();
            ServiceLocator.provide(mockUISrv);

            const srv = new DesignDataService();
            srv.closeDesignAreaEditor = jest.fn();
            ServiceLocator.provide(srv);

            const result = srv.handleCloseIfNeeded();

            expect(mockUISrv.isVisible).toHaveBeenCalledWith('designer');
            expect(srv.closeDesignAreaEditor).toHaveBeenCalled();
            expect(result).toBe(true);
        }
    );

    test(
        'in 2D mode and the designer is open: does not call the DesignDataService.closeDesignAreaEditor function ' +
            'and returns false because it has not performed any action',
        () => {
            const mockStoreSrv = new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    configurator: {
                        configuration: {
                            item: {
                                settings: {
                                    visualizationCreator: '2d',
                                    visualizationDesigner: '2d',
                                },
                            },
                        },
                    },
                    configurationMode: {
                        selectedMode: 'designer',
                    },
                    designArea: {
                        selectedDesignArea: {},
                    },
                };
                dispatch = jest.fn();
            })();
            ServiceLocator.provide(mockStoreSrv);

            const srv = new DesignDataService();
            srv.closeDesignAreaEditor = jest.fn();
            ServiceLocator.provide(srv);

            const result = srv.handleCloseIfNeeded();

            expect(srv.closeDesignAreaEditor).not.toHaveBeenCalled();
            expect(result).toBe(false);
        }
    );

    test(
        'in 3D mode if the designer is already closed: calls the DesignDataService.clearDesignAreaSelection ' +
            'function if any design area is selected and returns true',
        () => {
            const mockStoreSrv = new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    configurator: {
                        configuration: {
                            item: {
                                settings: {
                                    visualizationCreator: '3d',
                                    visualizationDesigner: '3d',
                                },
                            },
                        },
                    },
                    configurationMode: {
                        selectedMode: 'designer',
                    },
                    designArea: {
                        selectedDesignArea: {
                            identifier: 'area-51',
                        },
                    },
                };
                dispatch = jest.fn();
            })();
            ServiceLocator.provide(mockStoreSrv);
            const mockUISrv = new (class UIService {
                static serviceName = 'UIService';
                isVisible = jest.fn(() => false);
            })();
            ServiceLocator.provide(mockUISrv);

            const srv = new DesignDataService();
            srv.clearDesignAreaSelection = jest.fn();
            ServiceLocator.provide(srv);

            const result = srv.handleCloseIfNeeded();

            expect(mockUISrv.isVisible).toHaveBeenCalledWith('designer');
            expect(srv.clearDesignAreaSelection).toHaveBeenCalled();
            expect(result).toBe(true);
        }
    );

    test(
        'in 2D: calls the DesignDataService.clearDesignAreaSelection function if any design area is selected ' +
            'and returns true',
        () => {
            const mockStoreSrv = new (class StoreService {
                static serviceName = 'StoreService';
                state = {
                    configurator: {
                        configuration: {
                            item: {
                                settings: {
                                    visualizationCreator: '2d',
                                    visualizationDesigner: '2d',
                                },
                            },
                        },
                    },
                    configurationMode: {
                        selectedMode: 'designer',
                    },
                    designArea: {
                        selectedDesignArea: {
                            identifier: 'area-51',
                        },
                    },
                };
                dispatch = jest.fn();
            })();
            ServiceLocator.provide(mockStoreSrv);

            const srv = new DesignDataService();
            srv.clearDesignAreaSelection = jest.fn();
            ServiceLocator.provide(srv);

            const result = srv.handleCloseIfNeeded();

            expect(srv.clearDesignAreaSelection).toHaveBeenCalled();
            expect(result).toBe(true);
        }
    );
});
