@use "sass:math";

//------------------------------------------------------------//

// * TeamSnap-UI Functions
// * 1. Basic Math Helper Functions
// * 2. Font Scaling Function
// * 3. Color Scaling Function
// * 4. Grid Scaling Function

// Notes:
// * Scale functions are used to create consistancy on our
// spacing, font sizing, and color varients

// TODO : look at updating scaleGrid() to use negative steps

//------------------------------------------------------------//

// * 1. Basic Math Helper Functions

@function quarter($number) {
  $number: round(math.div($number, 4));
  @return $number
}

@function halve($number) {
  $number: round(math.div($number, 2));
  @return $number
}

@function double($number) {
  $number: round($number * 2);
  @return $number
}

@function quadruple($number) {
  $number: round($number * 4);
  @return $number
}

@function negative($number) {
  $number: 0 - $number;
  @return $number
}

@function convertNegative($number) {
  $number: $number * -1;
  @return $number
}

// * 2. Font Scaling Function

@function scaleFont($property, $step) {

  // common rules of our font scale
  $ratio: 1.125;

  // rounding step to nearest whole
  $step: round($step);

  // handles base & positive steps for font scale
  @if ($step > 0) {
    @for $i from 1 through $step {
      $property: $property * $ratio;
    }
  }

  // handles nagative steps for font scale
  @else if ($step < 0) {
    $step: convertNegative($step);
    @for $i from 1 through $step {
      $property: math.div($property, $ratio);
    }
  }

  // round pixel values to nearest whole number
  @if (unit($property) == 'px') {
    $property: round($property);
  }

  @return $property;

}

// * 3. Color Scaling Function

@function scaleColor($color, $step) {

  // common rules of our color scale
  $tintMax:  10;
  $shadeMax: -4;
  $ratio:   10%;

  // rounding step to nearest whole
  $step: round($step);

  // adds an extra 5% tint after 90% (or six steps)
  @if ($step >= $tintMax) {
    $color: mix(white, $color, 95%);
  }

  // mixes the base color in increments of white by 15%
  @else if ($step >= 0 ) and ($step < $tintMax) {
    $color: mix(white, $color, ($step * $ratio));
  }

  // mixes the base color in increments of black by 15%
  @else if ($step < 0 ) and ($step >= $shadeMax) {
    $step: convertNegative($step);
    $color: mix(black, $color, ($step * $ratio));
  }

  // caps shading to a 2 step limit
  @else if ($step < $shadeMax) {
    $step: convertNegative($shadeMax);
    $color: mix(black, $color, ($ratio * $step));
  }

  @return $color

}

// * 4. Grid Scaling Function

@function scaleGrid($step) {

  // defines base grid spacing unit and rounds step value to enforce
  $spacing: 8px;

  // rounding step to nearest whole
  $step: round($step);

  // Handles all positive values
  @if $step > 0 {
    $spacing: $spacing * $step;
  }

  @else if $step < 0 {
    // converts negative values if $step <= 0
    $step: convertNegative($step);

    // halves base unit based on negative step values
    @for $i from 1 through $step {
      $spacing: math.div($spacing, 2);
    }
  }

  @return $spacing;

}
