// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { NewTabLink } from 'common/components/new-tab-link';
import { AdHocTestkeys } from 'common/types/store-data/adhoc-test-keys';
import { RuleAnalyzerConfiguration } from 'injected/analyzers/analyzer';
import * as React from 'react';
import { needsReviewRules } from 'scanner/get-rule-inclusions';
import { TestMode } from '../../common/configs/test-mode';
import { VisualizationConfiguration } from '../../common/configs/visualization-configuration';
import { Messages } from '../../common/messages';
import { TelemetryDataFactory } from '../../common/telemetry-data-factory';
import { VisualizationType } from '../../common/types/visualization-type';
import { generateUID } from '../../common/uid-generator';
import { ScannerUtils } from '../../injected/scanner-utils';
import { VisualizationInstanceProcessor } from '../../injected/visualization-instance-processor';

const needsReviewTestKey = AdHocTestkeys.NeedsReview;

const needsReviewRuleAnalyzerConfiguration: RuleAnalyzerConfiguration = {
    rules: needsReviewRules,
    resultProcessor: (scanner: ScannerUtils) => scanner.getFailingInstances,
    telemetryProcessor: (telemetryFactory: TelemetryDataFactory) =>
        telemetryFactory.forNeedsReviewAnalyzerScan,
    key: needsReviewTestKey,
    testType: VisualizationType.NeedsReview,
    analyzerMessageType: Messages.Visualizations.Common.ScanCompleted,
};

export const NeedsReviewAdHocVisualization: VisualizationConfiguration = {
    key: needsReviewTestKey,
    testMode: TestMode.Adhoc,
    testViewType: 'AdhocNeedsReview',
    getStoreData: data => data.adhoc[needsReviewTestKey],
    enableTest: data => (data.enabled = true),
    disableTest: data => (data.enabled = false),
    getTestStatus: data => data.enabled,
    shouldShowExportReport: () => false,
    displayableData: {
        title: 'Needs review',
        subtitle: (
            <>
                Sometimes automated checks identify <i>possible</i> accessibility problems that need
                to be reviewed and verified by a human. Because most accessibility problems can only
                be discovered through manual testing, we recommend an{' '}
                <NewTabLink href="https://accessibilityinsights.io/docs/en/web/getstarted/assessment">
                    assessment
                </NewTabLink>
                .
            </>
        ),
        adHoc: {
            enableMessage: 'Running needs review checks...',
            toggleLabel: 'Show elements needing review',
            linkToDetailsViewText: 'List view and filtering',
        },
    },
    chromeCommand: '06_toggle-needsReview',
    launchPanelDisplayOrder: 6,
    adhocToolsPanelDisplayOrder: 7,
    getAnalyzer: provider =>
        provider.createRuleAnalyzerUnifiedScanForNeedsReview(needsReviewRuleAnalyzerConfiguration),
    getIdentifier: () => needsReviewTestKey,
    visualizationInstanceProcessor: () => VisualizationInstanceProcessor.nullProcessor,
    getNotificationMessage: (selectorMap, key, warnings) => null,
    getDrawer: provider => provider.createHighlightBoxDrawer(),
    getSwitchToTargetTabOnScan: () => false,
    getInstanceIdentiferGenerator: () => generateUID,
    getTestViewContainer: (provider, props) => provider.createNeedsReviewTestViewContainer(props),
};
