@use 'sass:math';
@import "config";

////////////////////////////// Functions
@function strip-units($number) {
  @return math.div($number, ($number * 0 + 1));
}

@function get-unit($value) {
  @return str-slice($value * 0 + "", 2, -1);
}

@function format-postfix($postfix) {
  $unit: get-unit($postfix);

  @return if($unit == '%', strip-units($postfix) + 'p', strip-units($postfix));
}

// get formatted value
@function format-value($value) {
  // 0px => 0
  @if $value == 0px or $value == 0 {
    @return 0;
  }

  // default unit is px
  @return to-number(if(unitless($value), unquote($value+'px'), $value));
}

// Is valid breakpoint
@function is-breakpoint($breakpoint) {
  @return map-has-key($breakpoints, $breakpoint);
}

// get previous breakpoint in responsive $data
//(
//  "values": 0 4 8 16 24 32 48 64 96 128,
//  "md": 0 4 8 15 22 30 46 60 90 120
//)
// get-previous-breakpoint(md,...) => "values"
@function get-previous-breakpoint($breakpoint,$data) {
  $keys: map-keys($data);
  $current_index: index($keys, $breakpoint);
  $previous_index: $current_index - 1;

  // first index is 1 (yes, not 0!!!)
  @if $previous_index < 1 {
    $previous_index: 1;
  }

  //@debug "Current #{$breakpoint} -> Prev #{nth($keys, $previous_index)}";

  @return nth($keys, $previous_index);
}


// get previous breakpoint value base on responsive breakpoint
//(
//  "values": 0 4 8 16 24 32 48 64 96 128,
//  "md": 0 4 8 15 22 30 46 60 90 120
//  "sm": 0 4 8 15 22 30 46 60 90 80
//)
// get-postfix(120,md,...) => 128
// get-postfix(80,sm,...) => 128
@function get-previous-breakpoint-value($index,$value,$current_breakpoint,$data) {
  @if is-breakpoint($current_breakpoint) {
    $default_values: map-get($data, "values");
    $previous_values: map-get($data, get-previous-breakpoint($current_breakpoint, $data));
    $current_values: map-get($data, $current_breakpoint);
    $previous_val: nth($previous_values, $index);

    // if responsive value is different
    @if $previous_val != $value {
      @return nth($previous_values, $index);
    }

    //@debug "----";
    //@debug "default: "+$result+" | #{$current_breakpoint}: "+$value;
  }

  // not found responsive value
  @return $value;
}

@function get-desktop-value($index,$data) {
  $default_values: map-get($data, "values");
  @return nth($default_values, $index);
}

///
/// Casts a string into a number
///
/// @param {String | Number} $value - Value to be parsed
///
/// @return {Number}
///
@function to-number($value) {
  @if type-of($value) == 'number' {
    @return $value;
  } @else if type-of($value) != 'string' {
    @warn ('Value for `to-number` should be a number or a string.');
  }

  $result: 0;
  $digits: 0;
  $minus: str-slice($value, 1, 1) == '-';
  $numbers: ('0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9);

  @for $i from if($minus, 2, 1) through str-length($value) {
    $character: str-slice($value, $i, $i);

    @if not (index(map-keys($numbers), $character) or $character == '.') {
      @return to-length(if($minus, -$result, $result), str-slice($value, $i))
    }

    @if $character == '.' {
      $digits: 1;
    } @else if $digits == 0 {
      $result: $result * 10 + map-get($numbers, $character);
    } @else {
      $digits: $digits * 10;
      $result: $result + map-get($numbers, $character) / $digits;
    }
  }

  @return if($minus, -$result, $result);
}

///
/// Add `$unit` to `$value`
///
/// @param {Number} $value - Value to add unit to
/// @param {String} $unit - String representation of the unit
///
/// @return {Number} - `$value` expressed in `$unit`
///
@function to-length($value, $unit) {
  $units: ('px': 1px, 'cm': 1cm, 'mm': 1mm, '%': 1%, 'ch': 1ch, 'pc': 1pc, 'in': 1in, 'em': 1em, 'rem': 1rem, 'pt': 1pt, 'ex': 1ex, 'vw': 1vw, 'vh': 1vh, 'vmin': 1vmin, 'vmax': 1vmax);

  @if not index(map-keys($units), $unit) {
    @warn ('Invalid unit `#{$unit}`.');
  }

  @return $value * map-get($units, $unit);
}

@function str-split($string, $separator) {
  // empty array/list
  $split-arr: ();
  // first index of separator in string
  $index: str-index($string, $separator);
  // loop through string
  @while $index != null {
    // get the substring from the first character to the separator
    $item: str-slice($string, 1, $index - 1);
    // push item to array
    $split-arr: append($split-arr, $item);
    // remove item and separator from string
    $string: str-slice($string, $index + 1);
    // find new index of separator
    $index: str-index($string, $separator);
  }
  // add the remaining string to list (the last item)
  $split-arr: append($split-arr, $string);

  @return $split-arr;
}

/// Replace `$search` with `$replace` in `$string`
/// @author Hugo Giraudel
/// @param {String} $string - Initial string
/// @param {String} $search - Substring to replace
/// @param {String} $replace ('') - New value
/// @return {String} - Updated string
@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

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

  @return $string;
}

// align-items => al
@function shorten-prop($prop) {
  $split-prop: str-split($prop, "-");
  $short-prefix: '';
  @each $word in $split-prop {
    $short-prefix: str-insert($short-prefix, str-slice($word, 0, 1), 100);
  }
  @return $short-prefix;
}

// ignore based on prop:val
@function is-ignored($search-prop, $search-value) {
  @each $val, $props in $ignore {
    @if $val == $search-value and index($props,$search-prop) != null {
      @return true;
    }
  }

  @return false;
}

@function replace-prop($search-prop) {
  @if map-has-key($replace_prop, $search-prop) {
    @return map-get($replace_prop, $search-prop);
  }

  // to string
  $search-prop: $search-prop+"";

  // remove %
  $search-prop: str-replace($search-prop, "%", "");

  // remove dot
  $search-prop: str-replace($search-prop, ".", "");

  @return $search-prop;
}

////////////////////////////// Mixins

// Get minix
@mixin get-mixin($name) {
  @if ($name==break-word) {
    @include break-word;
  }
}

// A utility class combining all word-break strategies
// when you absolutely must break a word.
@mixin break-word {
  word-break: break-word;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

/// Manage responsive breakpoints
@mixin media-screen($breakpoint) {
  @if is-breakpoint($breakpoint) {
    /* Breakpoint #{$breakpoint} */
    @media only screen and (max-width: map-get($breakpoints, $breakpoint)) {
      @content;
    }
  } @else {
    @content;
  }
}

@mixin responsive($breakpoint,$value,$postfix) {
  @if is-breakpoint($breakpoint) {
    // skip not responsive values
    //@debug $value + ' - ' + $postfix +' -> '+format-value($value) +' - '+ format-value($postfix);

    @if format-value($value) != format-value($postfix) {
      @content;
    }
  } @else {
    @content;
  }
}

@mixin print($dataset) {
  @each $prop, $data in $dataset {
    $input-type: type-of($data);
    $value: $data;
    $prefix: shorten-prop($prop);

    $has_hyphen: true;
    $has_type: false;
    $is_mixin: false;
    $is_value_key_as_postfix: false;
    $is_inline: false;

    @if ($input-type=='map') {
      $value: map-get($data, "value");
      $prefix: map-get($data, "prefix");

      // has hyphen
      @if map-has-key($data, "hyphen") {
        $has_hyphen: map-get($data, "hyphen");
      }

      // type
      $has_type: map-has-key($data, "type");
      $type: map-get($data, "type");
      $is_value_key_as_postfix: $has_type and $type == value-key-as-postfix;
      $is_mixin: $has_type and $type == mixin;

      // auto prefix for type map
      @if ($input-type=='map' and not map-has-key($data, "prefix") and not $is_mixin) {
        $prefix: shorten-prop($prop);
      }

      // type: inline
      @if map-has-key($data, "inline") {
        $type: inline;
        $is_inline: true;
      }
    }

    /* #{$prop} */
    @debug "------v------";
    @debug #{$prop};

    // Type: default
    @if (not $has_type) {
      @debug "Type: default";
      $hyphen: "-";
      @if not $has_hyphen {
        $hyphen: '';
      }

      @each $val in $value {
        @if not is-ignored($prop, $val) {
          .#{$prefix}#{$hyphen}#{replace-prop($val)} {
            #{$prop}: #{$val};
          }
        }
      }
    }

    // Type: mixin
    @if ($is_mixin) {
      @debug "Type: Mixin";

      $mixin: map-get($data, "mixin");

      // use prop as a fallback mixin name
      @if (not $mixin) {
        $mixin: $prop;
      }

      .#{$prop} {
        @include get-mixin($mixin);
      }
    }

    // Type: value-key-as-postfix
    @if ($is_value_key_as_postfix) {
      @debug "Type: value-key-as-postfix";

      @each $postfix, $val in $value {
        @if $val and not is-ignored($prop, $val) {
          .#{$prefix}-#{replace-prop($postfix)} {
            #{$prop}: #{$val};
          }
        }
      }
    }

    // Type: inline
    @if ($is_inline) {
      $inline: map-get($data, 'inline');
      @if map-has-key($data, 'selectors') {
        @include get-inline(map-get($data, 'selectors'), $inline);
      } @else {
        @include get-inline(str-insert($prop, '.', -100), $inline);
      }
    }

  }
}

@mixin get-inline-item($prop_val) {
  $parts: str-split($prop_val, ':');
  $prop: nth($parts, 1);
  $val: nth($parts, 2);
  #{$prop}: #{$val};
}

@mixin get-inline($prop, $inline) {
  #{$prop} {
    @if type-of($inline) == 'list' {
      @each $prop_val in $inline {
        @include get-inline-item($prop_val);
      }
    }

    @if type-of($inline) == 'string' {
      @include get-inline-item($inline);
    }
  }
}


// Print CSS variables
@mixin print-var-prop($prefix,$name,$value) {
  @if $value != '' {
    $project-prefix: '';

    @if map-has-key($info, 'variable-prefix') {
      $i-prefix: map-get($info, 'variable-prefix');
      @if $i-prefix != '' {
        $project-prefix: str-insert($i-prefix, '-', 100);
      }
    }

    --#{$project-prefix}#{$prefix}-#{$name}: #{$value};
  }
}

@mixin print-variables($data, $prefix) {
  :root {
    @each $name, $value in $data {
      @include print-var-prop($prefix, $name, $value);
    }
  }
}

// Return --prop:value; with project prefix
@mixin get-variable($prop,$value) {
  @if $value != '' {
    $project-prefix: '';

    @if map-has-key($info, 'variable-prefix') {
      $i-prefix: map-get($info, 'variable-prefix');
      @if $i-prefix != '' {
        $project-prefix: str-insert($i-prefix, '-', 100);
      }
    }

    --#{$project-prefix}#{$prop}: #{$value};
  }
}

// Return --prop with project prefix
@function get-variable($prop) {
  $project-prefix: '';

  @if map-has-key($info, 'variable-prefix') {
    $i-prefix: map-get($info, 'variable-prefix');
    @if $i-prefix != '' {
      $project-prefix: str-insert($i-prefix, '-', 100);
    }
  }

  @return --#{$project-prefix}#{$prop};
}


// Return true if a list contains a specified value.
// ($list:0 1 2 4 6 8 12 16 24 32 48 64 96 128, $value: 48) => true

@function map-search($list, $value) {
  @for $i from 1 through length($list) {
    @if (nth($list, $i) == $value) {
      @return true;
    }
  }
  @return false;

}

// Check if the value is containing any CSS unit, create CSS unit if it not
@mixin print-variables-in-root($list_responsive_values, $list_responsive_names,  $variable_name) {
  // get value list from $list_responsive_names
  $allowed_values_list: map-get($list_responsive_values, "values");

  :root {
    @each $responsive_name, $responsive_value in $list_responsive_names {
      $newValue: format-value($responsive_value); // e.g. 90px

      // check if $responsive_value is containing any CSS unit
      $has_unit: get-unit($responsive_value) != "";

      // check if $responsive_value in $list_responsive_names
      $is_allowed_value: map-search($allowed_values_list, $responsive_value);

      // use responsive variable
      @if ($is_allowed_value and not $has_unit) {
        $newValue: var(#{get-variable(#{$variable_name}-#{replace-prop($responsive_value)})}); // => var(--$variable_name-90);
      }

      @if ($variable_name == "spacing") {
        @include get-variable(#{$variable_name}-#{replace-prop($responsive_name)}, $newValue); //  => --$variable_name-$responsive_name: $newValue;
      } @else {
        @include get-variable(#{replace-prop($responsive_name)}, $newValue); //  => --$responsive_name: $newValue;
      }
    }
  }
}
