////
/// Core Function : string
/// @group core
////

@use "sass:math";
@use "sass:meta";
@use "sass:string";
@use "sass:map";

@function char-is-int($str) {
  @for $i from 0 through 9 {
    @if '#{$i}' == $str {
      @return true;
    }
  }
  @return false;
}

@function char-to-int($str) {
  @for $i from 0 through 9 {
    @if '#{$i}' == $str {
      @return $i;
    }
  }
  @return $str;
}

@function str-is-num($value) {
  $char: string.slice($value, 1, 1);
  @if char-is-int($char) {
    @return true;
  }
  @if $char == '-' and string.length($value) >= 2 {
    $char: string.slice($value, 2, 2);
    @if char-is-int($char) {
      @return true;
    }
  }
  @return false;
}

/// Fonction de transformation d'une string en nombre intégral
/// @access public
/// @param {String} $value - le nombre en chaîne de caractères
@function str-to-num($value) {
  @if meta.type-of($value) != string {
    @return $value;
  }
  $length: str-length($value);
  $int: ();
  $float: ();
  $floating: false;
  $neg: false;

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

    @if $n == '.' {
      $floating: true;
    }
    @else if $n == '-' {
      $neg: true;
    }
    @else {
      @if char-is-int($n) {
        @if $floating {
          $float: append($float, char-to-int($n));
        }
        @else {
          $int: append($int, char-to-int($n));
        }
      }
    }
  }

  $result: 0;

  $length: length($int);
  @if $length > 0 {
    @for $i from 1 through $length {
      $result: $result + (nth($int, $i) * math.pow(10, $length - $i));
    }
  }

  $length: length($float);
  @if $length > 0 {
    @for $i from 1 through $length {
      $result: $result + math.div(nth($float, $i), math.pow(10, $i));
    }
  }

  @if $neg {
    $result: -$result;
  }

  @if '#{$result}' == $value {
    @return $result;
  }

  $unit: string.slice($value, string.length('#{$result}' + 1));

  $units: ('v': 1v, 'w': 1w, 'px': 1px, 'cm': 1cm, 'mm': 1mm, '%': 1%, 'ch': 1ch, 'in': 1in, 'em': 1em, 'rem': 1rem, 'pt': 1pt, 'pc': 1pc, 'ex': 1ex, 'vw': 1vw, 'vh': 1vh, 'vmin': 1vmin, 'vmax': 1vmax, 'deg': 1deg, 'turn': 1turn, 'rad': 1rad, 'grad': 1grad, 's': 1s, 'ms': 1ms, 'Hz': 1Hz, 'kHz': 1kHz, 'dppx': 1dppx, 'dpcm': 1dpcm, 'dpi': 1dpi);

  @if map.has-key($units, $unit) {
    @return $result * map.get($units, $unit);
  }

  @return $result;
}

/// Remplacement dans une chaine de caractères
/// @access public
/// @param {String} $haystack - chaine de caractère où chercher
/// @param {String} $needle - élément à chercher
/// @param {String} $content - élément de remplacement
/// @return {String} la chaîne de caractères transformée
@function str-replace($haystack, $needle, $content, $start: 1) {
  $chunks: str-split($haystack, $needle);
  $result: str-join($chunks, $content);
  @return $result;
}

/// Découpage d'une chaine de caractères avec un séparateur particulier
/// @access public
/// @param {String} $string - chaine de caractère à découper
/// @param {String} $separator - séparateur
@function str-split($haystack, $needle:',', $separator: comma) {
  @if type-of($haystack) != string {
    $haystack: '#{$haystack}';
  }
  @if type-of($needle) != string {
    $needle: '#{$needle}';
  }

  $list: ();
  $length: string.length($needle);
  $index: -1;

  @while $index != null {
    $index: string.index($haystack, $needle);

    @if $index != null {
      $list: append($list, string.slice($haystack, 1, $index - 1), $separator);
      $haystack: string.slice($haystack, $index + $length);
    }
    @else {
      $list: append($list, $haystack, $separator);
    }
  }

  @return $list;
}

@function str-join($list, $separator:',') {
  $result: '';
  $s: '';

  @each $item in $list {
    $result: '#{$result}#{$s}#{$item}';
    $s: $separator;
  }

  @return $result;
}

@function is-heading($name) {
  @return string.index('#{$name}', 'h') == 1;
}
