@use 'sass:list';
@use 'sass:meta';
@use 'sass:string';

/// Checks if a string contains only conventional naming chacters, which
/// includes the 26 character alphabet and the standard hyphen.
///
/// @param {String} $string - The string to check.
/// @param {Bool} $allow-nums [false] - Whether to allow for number
/// characters in the string.
/// @param {Bool} $allow-bem [false] - Whether to allow for BEM naming
/// convention characters, which effectively means adding the underscore '_'
/// character to the list of allowed characters.
/// @param {Bool} $allow-capitals [false] - If true, the function will
/// allow for capital letters. When false, the function will return as false if
/// $string contains capital letter characters.
///
/// @return {Bool} True if $string only contians hyphens and lowercase characters
/// from the 26 character alphabet. Will return true for capital letters if
/// $allow-capitals is flagged `true`, for numbers if $allow-nums is flagged
/// `true`, and for underscores if $allow-bem is true. Otherwise returns false.
///
/// @access public
/// @group Utilities
@function is-conventional(
  $string,
  $allow-nums: false,
  $allow-bem: false,
  $allow-capitals: false
) {
  $conventional-characters: (
    '-' 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r'
      's' 't' 'u' 'v' 'w' 'x' 'y' 'z'
  );

  @if $allow-nums {
    $conventional-characters: list.join(
      $conventional-characters,
      '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'
    );
  }

  @if $allow-bem {
    $conventional-characters: list.append($conventional-characters, '_');
  }

  @if $allow-capitals {
    $string: string.to-lower-case($string);
  }

  @for $i from 1 through string.length($string) {
    $char: string.slice($string, $i, $i);

    @if not list.index($conventional-characters, $char) {
      @return false;
    }
  }

  @return true;
}
