// add-length
// --------------------------
/// Get a string and add extra spaces to make the string longer.
/// Usefull in debugging and making all strings the same length.
/// @access public
/// @group Character utilities
/// @type String
/// @output String
/// @parameter String

@function add-length($string, $length) {
  @if str-length($string) < $length {
    @while str-length($string) < $length {
      $string: "#{$string} ";
    }
    @return $string;
  } @else {
    @return $string;
  }
}

// add-zeros
// --------------------------
/// Add extra zeros to a number.
/// When you want to create classes with 01, 003 or something equivalent
/// @access public
/// @group Character utilities
/// @type String
/// @output String
/// @parameter String

@function add-zeros($val: 1, $length: 2) {
  $value: null;
  $str: "#{$val}";
  @if length($str) >= $length {
    @return $str;
  } @else {
    $str-length: length($str);
    $value: #{add-character("0", $length - $str-length)}#{$str};
  }
  @return $value;
}

// add-characters
// --------------------------
/// Create a string with given character times a given amount of times
/// @access public
/// @group Character utilities
/// @type String
/// @output String
/// @parameter String

@function add-character($char, $times: 1) {
  $value: null;
  @for $i from 1 through $times {
    $value: #{$value}#{$char};
  }
  @return $value;
}

// safe-characters
// --------------------------
/// Convert special characters in strings to their simple equivalent
/// to filter with characters.
/// @access public
/// @group Character utilities
/// @type String
/// @output String
/// @parameter String

@function safe-characters($str) {
  // prettier-ignore
  $needles: ('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', '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 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o', 'Α', 'α', 'Ε', 'ε', 'Ο', 'ο', 'Ω', 'ω', 'Ι', 'ι', 'ι', 'ι', 'Y', 'υ', 'υ', 'υ', 'Η', 'η');
  @return str-replace($str, $needles, $replacers);
}
// Allowed-characters function
// --------------------------
/// Takes a few arguments an responsds with a list of letters which can be used by other functions
/// to filter with characters.
/// @access public
/// @group Character utilities
/// @type String
/// @output List
/// @parameter List

@function character-set($characters) {
  $new-characters: ();
  $return-default: true;
  $character-set: (
    "numbers": "01234556789",
    "lowercase": "abcdefghijklmnopqrstuvwxyz",
    "uppercase": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  );
  @each $char in $characters {
    @if $char == "a-Z" {
      $return-default: false;
      $new-characters: join(
        $new-characters,
        to-letter-list(map-get($character-set, "lowercase"))
      );
      $new-characters: join(
        $new-characters,
        to-letter-list(map-get($character-set, "uppercase"))
      );
    } @else if $char == "a-z" {
      $return-default: false;
      $new-characters: join(
        $new-characters,
        to-letter-list(map-get($character-set, "lowercase"))
      );
    } @else if $char == "A-Z" {
      $return-default: false;
      $new-characters: join(
        $new-characters,
        to-letter-list(map-get($character-set, "uppercase"))
      );
    } @else if $char == "0-9" {
      $return-default: false;
      $new-characters: join(
        $new-characters,
        to-letter-list(map-get($character-set, "numbers"))
      );
    } @else if type-of($char) == "string" {
      $new-characters: append($new-characters, $char);
    } @else if type-of($char) == "list" {
      $new-characters: join($new-characters, $char);
    }
  }

  @return $new-characters;
}

// Allowed characters function
// --------------------------
/// Pass in the characters you want to allow and remove all others from a string
/// @access public
/// @group Character utilities
/// @type String
/// @output String
/// @parameter String

@function allowed-characters($str, $characters: ()) {
  @if type-of($characters) == "string" {
    $characters: to-letter-list($characters);
  } @else if type-of($characters) == "list" {
    $characters: character-set($characters);
  }
  $new-str: ();
  @for $i from 1 through str-length($str) {
    @if index($characters, str-slice($str, $i, $i)) != null {
      $new-str: append($new-str, str-slice($str, $i, $i));
    }
  }
  @return list-to-str($new-str, null);
}

@function has-unallowed-characters($str, $characters) {
  $letter-list: to-letter-list($str);
  $hasUnallowedCharacters: false;

  @each $letter in $letter-list {
    @if not list-contains($characters, $str) {
      $hasUnallowedCharacters: true;
    }
  }

  @return $hasUnallowedCharacters;
}

@function is-number($number) {
  $number-list: character-set(("0-9"));
  @if type-of($number) == "number" or list-contains($number-list, $number) {
    @return true;
  }

  @return false;
}
