import { modifier } from 'ember-modifier';
import { warn } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

/**
 * In DEBUG, warns when:
 * - Icon has no @title and no aria-hidden="true" (accessibility: should have one or the other).
 * - Icon has both @title and aria-hidden="true" (contradictory).
 */
export default modifier(
  (element: SVGSVGElement, _positional: [string | undefined, string | undefined]) => {
    if (!DEBUG) return;

    const [name, title] = _positional;
    const ariaHidden = element.getAttribute('aria-hidden');

    warn(
      `Please set @title for <MalibuIcon @name="${name ?? ''}" /> ` +
        'or set aria-hidden="true" if you believe the icon does not ' +
        'need a title and is solely decorative.',
      Boolean(title || ariaHidden === 'true'),
      {
        id: 'ember-malibu-icon.missing-title',
        url: 'https://github.com/heroku/ember-malibu-icon#accessibility',
      }
    );

    warn(
      `<MalibuIcon @name="${name ?? ''}" /> does not allow setting both @title and aria-hidden="true", ` +
        'as aria-hidden="true" would hide the text from screenreaders/accessibility tools. ' +
        'Please remove the @title argument if the icon is used in a decorative manner, or remove the `aria-hidden` attribute.',
      !(title && ariaHidden === 'true'),
      {
        id: 'ember-malibu-icon.mismatched-title-and-aria-hidden',
        url: 'https://github.com/heroku/ember-malibu-icon#accessibility',
      }
    );
  }
);
