'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const ESCAPED_OPENING_CHEVRON = '&lt;';
const ESCAPED_CLOSING_CHEVRON = '&gt;';
class EmphasisHtmlTransformer {
  tags;
  constructor(whitelistedTags) {
    this.tags = (whitelistedTags || []).map(tag => {
      return {
        transformed: {
          opening: `<em class="emphasis emphasis--${tag}">`,
          closing: '</em>'
        },
        escapedRegex: {
          opening: new RegExp(`${ESCAPED_OPENING_CHEVRON}${tag}${ESCAPED_CLOSING_CHEVRON}`, 'g'),
          closing: new RegExp(`${ESCAPED_OPENING_CHEVRON}/${tag}${ESCAPED_CLOSING_CHEVRON}`, 'g')
        }
      };
    });
  }
  // Algorithm:
  // 1) Escape all dangerous characters (<,>,&)
  // 2) Replace all escaped, whitelisted tags with styled spans.
  // 3) Transform new line characters to `<br />`s
  //
  // Note: for simplicity this doesn't support tags with non-standard whitespaces, e.g. <positive></ positive>
  transform(unsafe) {
    if (!unsafe) {
      return null;
    }
    const safe = unsafe.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return this.tags.reduce((accumulator, tag) => {
      return accumulator.replace(tag.escapedRegex.opening, tag.transformed.opening).replace(tag.escapedRegex.closing, tag.transformed.closing);
    }, safe).replace(/\\n|\n/g, '<br />');
  }
}

exports.default = EmphasisHtmlTransformer;
//# sourceMappingURL=EmphasisHtmlTransformer.js.map
