'use strict';

var index$1 = require('../../../../../character-entities-legacy@3.0.0/node_modules/character-entities-legacy/index.js');
var index = require('../../../../../character-entities-html4@2.1.0/node_modules/character-entities-html4/index.js');
var dangerous = require('../constant/dangerous.js');

const own = {}.hasOwnProperty;

/**
 * `characterEntitiesHtml4` but inverted.
 *
 * @type {Record<string, string>}
 */
const characters = {};

/** @type {string} */
let key;

for (key in index.characterEntitiesHtml4) {
  if (own.call(index.characterEntitiesHtml4, key)) {
    characters[index.characterEntitiesHtml4[key]] = key;
  }
}

const notAlphanumericRegex = /[^\dA-Za-z]/;

/**
 * Configurable ways to encode characters as named references.
 *
 * @param {number} code
 * @param {number} next
 * @param {boolean|undefined} omit
 * @param {boolean|undefined} attribute
 * @returns {string}
 */
function toNamed(code, next, omit, attribute) {
  const character = String.fromCharCode(code);

  if (own.call(characters, character)) {
    const name = characters[character];
    const value = '&' + name;

    if (
      omit &&
      index$1.characterEntitiesLegacy.includes(name) &&
      !dangerous.dangerous.includes(name) &&
      (!attribute ||
        (next &&
          next !== 61 /* `=` */ &&
          notAlphanumericRegex.test(String.fromCharCode(next))))
    ) {
      return value
    }

    return value + ';'
  }

  return ''
}

exports.toNamed = toNamed;
//# sourceMappingURL=to-named.js.map
