"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.stripPictogramPrefix = exports.isBrTag = exports.getUnorderedListBullet = exports.getOrderedListMarker = exports.extractPictogramName = exports.collectRawText = void 0;
var _pictograms = require("../pictograms");
const BULLET_FULL = "\u2022";
const BULLET_HOLLOW = "\u25E6";
const BULLET_SQUARE = "\u25B8";
const PICTOGRAM_REGEXP = /^\s*\[!(.*?)\]/;
const ROMAN_NUMERALS = [[1000, "m"], [900, "cm"], [500, "d"], [400, "cd"], [100, "c"], [90, "xc"], [50, "l"], [40, "xl"], [10, "x"], [9, "ix"], [5, "v"], [4, "iv"], [1, "i"]];

/**
 * Returns the bullet glyph used for unordered lists at a given nesting depth.
 */
const getUnorderedListBullet = listDepth => {
  switch (listDepth % 3) {
    case 0:
      return BULLET_FULL;
    case 1:
      return BULLET_HOLLOW;
    default:
      return BULLET_SQUARE;
  }
};

/**
 * Converts a positive integer to its lowercase Roman numeral representation.
 */
exports.getUnorderedListBullet = getUnorderedListBullet;
const toRomanNumeral = (value, index = 0) => {
  const numeral = ROMAN_NUMERALS[index];
  if (value <= 0 || numeral === undefined) {
    return "";
  }
  const [arabic, roman] = numeral;
  if (value >= arabic) {
    // Consume the current numeral and keep using it until the remainder is smaller.
    return `${roman}${toRomanNumeral(value - arabic, index)}`;
  }

  // Try the next Roman numeral when the current one no longer fits.
  return toRomanNumeral(value, index + 1);
};

/**
 * Converts a positive integer to a lowercase alphabetic sequence (`a`, `b`, `aa`).
 */
const toAlphabeticMarker = value => {
  if (value <= 0) {
    return "";
  }

  // Convert to a zero-based base-26 index so 1 -> a, 26 -> z, 27 -> aa.
  const normalizedValue = value - 1;
  const prefix = toAlphabeticMarker(Math.floor(normalizedValue / 26));
  const suffix = String.fromCodePoint(97 + normalizedValue % 26);
  return `${prefix}${suffix}`;
};

/**
 * Returns the ordered-list marker for a given item index and nesting depth.
 */
const getOrderedListMarker = (value, listDepth) => {
  switch (listDepth % 3) {
    case 0:
      return `${value}.`;
    case 1:
      return `${toRomanNumeral(value)}.`;
    default:
      return `${toAlphabeticMarker(value)}.`;
  }
};

/**
 * Extracts a banner pictogram name from a `[!pictogramName]` prefix.
 */
exports.getOrderedListMarker = getOrderedListMarker;
const extractPictogramName = text => {
  const match = PICTOGRAM_REGEXP.exec(text);
  const value = match?.[1];
  const isValid = value != null && value in _pictograms.IOPictogramsBleed;
  return isValid ? value : "notification";
};

/**
 * Removes the leading pictogram directive from blockquote content.
 */
exports.extractPictogramName = extractPictogramName;
const stripPictogramPrefix = text => text.replace(PICTOGRAM_REGEXP, "");

/**
 * Recursively collects plain text content from a Markdown AST node.
 */
exports.stripPictogramPrefix = stripPictogramPrefix;
const collectRawText = node => {
  if (node.content) {
    return node.content;
  }
  return node.children.map(collectRawText).join("");
};

/**
 * Returns true when a raw HTML fragment is a `<br>` tag.
 */
exports.collectRawText = collectRawText;
const isBrTag = content => {
  const match = new RegExp(/<([^\s/>]+)\s*\/?>/).exec(content);
  return match?.[1] === "br";
};
exports.isBrTag = isBrTag;
//# sourceMappingURL=utils.js.map