<commit_before>import { assert } from '@ember/debug';
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isEmpty, isPresent } from '@ember/utils';
import { isArray } from '@ember/array';
import { scheduleOnce } from '@ember/runloop';
import Ember from 'ember';
import ENV from '../config/environment';
import isBackgroundReplacedElement from 'ember-a11y-testing/utils/is-background-replaced-element';
import formatViolation from 'ember-a11y-testing/utils/format-violation';
import violationsHelper from 'ember-a11y-testing/utils/violations-helper';
import concurrentAxe from 'ember-a11y-testing/utils/concurrent-axe';

const VIOLATION_CLASS__LEVEL_1 = 'axe-violation--level-1';
const VIOLATION_CLASS__LEVEL_2 = 'axe-violation--level-2';
const VIOLATION_CLASS__LEVEL_3 = 'axe-violation--level-3';
const VIOLATION_CLASS__REPLACED = 'axe-violation--replaced-element';

/*
 * Mapping of violation class names to their corresponding `visualNoiseLevel`
 */
const VIOLATION_CLASS_MAP = {
  LEVEL_1: VIOLATION_CLASS__LEVEL_1,
  LEVEL_2: VIOLATION_CLASS__LEVEL_2,
  LEVEL_3: VIOLATION_CLASS__LEVEL_3,
  REPLACED_ELEMENT: VIOLATION_CLASS__REPLACED
};

const VIOLATION_CLASS_NAMES = Object.keys(VIOLATION_CLASS_MAP).map(key => VIOLATION_CLASS_MAP[key]);

/**
 * Variable to ensure that the initializer is only ran once. Though in this
 * particular case, running more than once shouldn't cause side-effects.
 * @type {Boolean}
 */
let hasRan = false;


export function initialize() {
  if (hasRan) { return; }

  const addonConfig = ENV['ember-a11y-testing'] || {};
  const { componentOptions = {} } = addonConfig;

  const {
    turnAuditOff: configuredTurnAuditOff,
    excludeAxeCore,
    axeOptions,
    axeCallback,
    visualNoiseLevel: configuredVisualNoiseLevel,
    axeViolationClassNames: configuredAxeViolationClassNames
  } = componentOptions;

  let visualNoiseLevel = configuredVisualNoiseLevel ? configuredVisualNoiseLevel : 1;
  let axeViolationClassNames = isPresent(configuredAxeViolationClassNames) ? configuredAxeViolationClassNames : [];
  let turnAuditOff = configuredTurnAuditOff || false;

  // Avoid modifying the Component class if the visual audit feature is configured disabled
  // and axe-core is excluded from the dev build
  if (turnAuditOff && excludeAxeCore) { return; }

  Component.reopen({
    /**
     * An optional callback to process the results from the a11yCheck.
     * Defaults to `undefined` if not set in the application's configuration.
     *
     * @public
     * @type {Function}
     */
    axeCallback,

    /**
     * An optional options object to be used in a11yCheck.
     * Defaults to `undefined` if not set in the application's configuration.
     * @public
     * @type {Object}
     */
    axeOptions,

    /**
     * Turns off the accessibility audit during rendering.
     *
     * @public
     * @default false
     * @type {Boolean}
     */<commit_msg>Fixes #113: provides empty object if axeOptions are not defined to prevent axe.run from failing<commit_after>import { assert } from '@ember/debug';
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isEmpty, isPresent } from '@ember/utils';
import { isArray } from '@ember/array';
import { scheduleOnce } from '@ember/runloop';
import Ember from 'ember';
import ENV from '../config/environment';
import isBackgroundReplacedElement from 'ember-a11y-testing/utils/is-background-replaced-element';
import formatViolation from 'ember-a11y-testing/utils/format-violation';
import violationsHelper from 'ember-a11y-testing/utils/violations-helper';
import concurrentAxe from 'ember-a11y-testing/utils/concurrent-axe';

const VIOLATION_CLASS__LEVEL_1 = 'axe-violation--level-1';
const VIOLATION_CLASS__LEVEL_2 = 'axe-violation--level-2';
const VIOLATION_CLASS__LEVEL_3 = 'axe-violation--level-3';
const VIOLATION_CLASS__REPLACED = 'axe-violation--replaced-element';

/*
 * Mapping of violation class names to their corresponding `visualNoiseLevel`
 */
const VIOLATION_CLASS_MAP = {
  LEVEL_1: VIOLATION_CLASS__LEVEL_1,
  LEVEL_2: VIOLATION_CLASS__LEVEL_2,
  LEVEL_3: VIOLATION_CLASS__LEVEL_3,
  REPLACED_ELEMENT: VIOLATION_CLASS__REPLACED
};

const VIOLATION_CLASS_NAMES = Object.keys(VIOLATION_CLASS_MAP).map(key => VIOLATION_CLASS_MAP[key]);

/**
 * Variable to ensure that the initializer is only ran once. Though in this
 * particular case, running more than once shouldn't cause side-effects.
 * @type {Boolean}
 */
let hasRan = false;


export function initialize() {
  if (hasRan) { return; }

  const addonConfig = ENV['ember-a11y-testing'] || {};
  const { componentOptions = {} } = addonConfig;

  const {
    turnAuditOff: configuredTurnAuditOff,
    excludeAxeCore,
    axeOptions = {},
    axeCallback,
    visualNoiseLevel: configuredVisualNoiseLevel,
    axeViolationClassNames: configuredAxeViolationClassNames
  } = componentOptions;

  let visualNoiseLevel = configuredVisualNoiseLevel ? configuredVisualNoiseLevel : 1;
  let axeViolationClassNames = isPresent(configuredAxeViolationClassNames) ? configuredAxeViolationClassNames : [];
  let turnAuditOff = configuredTurnAuditOff || false;

  // Avoid modifying the Component class if the visual audit feature is configured disabled
  // and axe-core is excluded from the dev build
  if (turnAuditOff && excludeAxeCore) { return; }

  Component.reopen({
    /**
     * An optional callback to process the results from the a11yCheck.
     * Defaults to `undefined` if not set in the application's configuration.
     *
     * @public
     * @type {Function}
     */
    axeCallback,

    /**
     * An optional options object to be used in a11yCheck.
     * Defaults to empty object if not set in the application's configuration.
     * @public
     * @type {Object}
     */
    axeOptions,

    /**
     * Turns off the accessibility audit during rendering.
     *
     * @public
     * @default false
     * @type {Boolean}
     */
{'fraction_non_alphanumeric': 0.0986292209963223, 'fraction_numerical': 0.009528585757271816, 'mean_word_length': 4.2253275109170305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3005465', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1806, 'n_words': 479}