@use "sass:string";
@use "sass:math";
@use "sass:map";
@use "sass:list";
@use "sass:meta";
@use "config" as *;

@function str-replace($string, $search, $replace: " ") {
  $index: string.index($string, $search);
  @if $index {
    @return string.slice($string, 1, $index - 1) + $replace + str-replace(string.slice($string, $index + string.length($search)), $search, $replace);
  }
  @return $string;
}

@function get-breakpoint-value($bp, $debug: false) {
  @if map.has-key($breakpoints, #{$bp}) {
    @return map.get($breakpoints, #{$bp});
  } @else if meta.type-of($bp) == number {
    @return $bp;
  } @else {
    @error 'Invalid breakpoint: #{$bp}';
    @return null;
  }
}

@function strip-unit($value) {
  @if meta.type-of($value) != number {
    @error "strip-unit() requires a number";
  }
  @return math.div($value, ($value * 0 + 1));
}

@function safe-unit-name($unit) {
  @if $unit == "%" {
    @return "per";
  } @else if $unit == "." {
    @return "dot";
  } @else {
    @return $unit;
  }
}

/**
 * Ensures a value has a unit, adding $default-unit if none exists
 * @param {Number|String} $val - The value to check
 * @return {String} - The value with units
 */
@function fix-units($val, $unit: $default-unit, $debug: null) {
  @if meta.type-of($val) != number and meta.type-of($val) != string {
    @error "fix-units() requires a number or string value";
    @return null;
  }

  @if meta.type-of($val) == number {
    @if math.is-unitless($val) {
      @return $val + $unit;
    }
    @return $val;
  }

  $val: strip-quotes($val);

  @if $val == auto or $val == inherit or $val == initial or $val == "min-content" or $val == "max-content" or $val == "fit-content" {
    @return #{$val};
  }

  @if string.index($val, "px") or string.index($val, "em") or string.index($val, "rem") or string.index($val, "%") or string.index($val, "vh") or string.index($val, "vw") or string.index($val, "vmin") or string.index($val, "vmax") {
    @return #{$val};
  }

  @if $debug {
    @debug "fix-units() value: " + $val;
  }

  @return string.unquote($val + $unit);
}

@function strip-quotes($string) {
  @if meta.type-of($string) == string {
    @if string.slice($string, 1, 1) == '"' and string.slice($string, -1) == '"' {
      @return string.slice($string, 2, -2);
    } @else if string.slice($string, 1, 1) == "'" and string.slice($string, -1) == "'" {
      @return string.slice($string, 2, -2);
    }
  }

  @return $string;
}

@function str-trim($string) {
  @while string.length($string) > 0 and string.slice($string, 1, 1) == " " {
    $string: string.slice($string, 2);
  }

  @while string.length($string) > 0 and string.slice($string, -1) == " " {
    $string: string.slice($string, 1, string.length($string) - 1);
  }

  @return $string;
}

@function split($string, $delimiter) {
  $result: ();
  $index: string.index($string, $delimiter);
  @while $index != null {
    $item: string.slice($string, 1, $index - 1);
    $result: list.append($result, str-trim(string.unquote(strip-quotes($item))));
    $string: string.slice($string, $index + string.length($delimiter));
    $index: string.index($string, $delimiter);
  }

  @return list.append($result, str-trim($string));
}
