import joi from 'joi';
import lowerFirst from 'lodash/lowerFirst.js';

/**
 * Prevent Markdown formatting
 * @see {@link https://pandoc.org/chunkedhtml-demo/8.11-backslash-escapes.html}
 */
export function escapeMarkdown(answer) {
  const punctuation = ['`', "'", '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '.', '!'];
  for (const character of punctuation) {
    answer = answer.replaceAll(character, `\\${character}`);
  }
  return answer;
}
export const addClassOptionIfNone = (options, className) => {
  options.classes ??= className;
};

/**
 * Applies lowerFirst but preserves capitalisation of proper nouns
 * like "National Grid", "Ordnance Survey" and "OS".
 */
export function lowerFirstPreserveProperNouns(text) {
  const result = lowerFirst(text);
  return result.replace(/\bnational [Gg]rid\b/g, 'National Grid').replace(/\bordnance [Ss]urvey\b/g, 'Ordnance Survey').replace(/\b[oO][sS]\b/g, 'OS');
}

/**
 * Configuration for Joi expressions that use lowerFirst function
 */
export const lowerFirstExpressionOptions = {
  functions: {
    lowerFirst: lowerFirstPreserveProperNouns
  }
};

/**
 * Creates a Joi expression with lowerFirst function support
 * Used for error messages in location field components
 */
export const createLowerFirstExpression = template => joi.expression(template, lowerFirstExpressionOptions);
//# sourceMappingURL=index.js.map