import {expect} from '@playwright/test';

import datalensTest from '../../../../utils/playwright/globalTestDefinition';
import {openTestPage, slct} from '../../../../utils';
import {ChartKitQa, WizardPageQa, WizardVisualizationId} from '../../../../../src/shared';
import WizardPage from '../../../../page-objects/wizard/WizardPage';
import {PlaceholderName} from '../../../../page-objects/wizard/SectionVisualization';
import {SMALL_SCREENSHOT_VIEWPORT_SIZE} from '../constants';
import {ChartSettingsItems} from '../../../../page-objects/wizard/ChartSettings';

datalensTest.describe('Wizard', () => {
    datalensTest.describe('Bar-y chart', () => {
        datalensTest.beforeEach(async ({page, config}) => {
            await openTestPage(page, config.wizard.urls.WizardBasicDataset);
            await page.setViewportSize(SMALL_SCREENSHOT_VIEWPORT_SIZE);
            const wizardPage = new WizardPage({page});
            await wizardPage.setVisualization(WizardVisualizationId.Bar);
        });

        datalensTest(
            'The legend should not be displayed when the setting is turned off @screenshot',
            async ({page}) => {
                const wizardPage = new WizardPage({page});

                await wizardPage.createNewFieldWithFormula('SalesSum', 'sum([Sales])');

                const preview = page.locator(slct(WizardPageQa.SectionPreview));
                const previewLoader = preview.locator(slct(ChartKitQa.Loader));

                await wizardPage.sectionVisualization.addFieldByClick(PlaceholderName.Y, 'segment');
                await wizardPage.sectionVisualization.addFieldByClick(
                    PlaceholderName.X,
                    'SalesSum',
                );
                await wizardPage.sectionVisualization.addFieldByClick(
                    PlaceholderName.Colors,
                    'Measure Values',
                );

                await wizardPage.chartSettings.open();
                await wizardPage.chartSettings.toggleSettingItem(ChartSettingsItems.Legend, 'off');
                await wizardPage.chartSettings.apply();

                // Put the mouse away so that the presence of hover elements does not interfere with taking screenshots
                await page.mouse.move(-1, -1);
                await expect(previewLoader).not.toBeVisible();
                await expect(preview).toHaveScreenshot();
            },
        );
    });
});
