@use 'sass:color';
@use 'sass:math';
@use 'sass:string';
@use 'functions';

@function to-rgb($color, $fractionDigits: 1) {
  $red: functions.round(color.channel($color, 'red', $space: rgb), $fractionDigits);
  $green: functions.round(color.channel($color, 'green', $space: rgb), $fractionDigits);
  $blue: functions.round(color.channel($color, 'blue', $space: rgb), $fractionDigits);

  @return rgb($red, $green, $blue);
}

@function to-rgba($color, $fractionDigits: 1) {
  $red: functions.round(color.channel($color, 'red', $space: rgb), $fractionDigits);
  $green: functions.round(color.channel($color, 'green', $space: rgb), $fractionDigits);
  $blue: functions.round(color.channel($color, 'blue', $space: rgb), $fractionDigits);
  $alpha: functions.round(color.channel($color, 'alpha', $space: rgb), $fractionDigits);

  @return rgba($red, $green, $blue, $alpha);
}

@function to-oklch($color, $fractionDigits: 2) {
  $lightness: functions.round(color.channel($color, 'lightness', $space: oklch), $fractionDigits);
  $chroma: functions.round(color.channel($color, 'chroma', $space: oklch), $fractionDigits);
  $hue: functions.round(color.channel($color, 'hue', $space: oklch), $fractionDigits);
  $alpha: functions.round(color.channel($color, 'alpha', $space: oklch), $fractionDigits);

  @return oklch($lightness $chroma $hue / #{$alpha});
}

@function to-hex($color) {
  $ie-hex: color.ie-hex-str($color);
  $c1: string.slice($ie-hex, 2, 3);

  @if string.length($ie-hex) == 9 and $c1 == 'FF' {
    @return string.unquote('##{string.slice($ie-hex, 4)}');
  } @else {
    $c2: string.slice($ie-hex, 4);
    @return string.unquote('##{$c2}#{$c1}');
  }
}

// Returns range from 0 to 255
@function brightness($color) {
  $red: color.channel($color, 'red') * 299;
  $green: color.channel($color, 'green') * 587;
  $blue: color.channel($color, 'blue') * 114;

  @return math.div(($red + $green + $blue), 1000);
}

// Returns range from 0% to 100%
@function lightnessLevel($color) {
  @return color.channel($color, 'lightness', $space: oklch);
}

@function set-color-contrast($color, $lightColor, $darkColor, $lightness: 75%) {
  @return functions.fn-if(lightnessLevel($color) > $lightness, $lightColor, $darkColor);
}

$white: #fff;
$black: #000;
$default-active-bgcolor: to-oklch(#5f4a97);

// Returns color in oklch color space
@function set-alt-bgcolor($color, $transparency: 0, $lightnessLevel: 66%) {
  @if (lightnessLevel($color) > 99%) {
    @if (($transparency > 0) and ($transparency < 1)) {
      @return to-oklch(color.change($default-active-bgcolor, $alpha: $transparency));
    } @else {
      @return to-oklch($white);
    }
  } @else if (lightnessLevel($color) > $lightnessLevel) {
    @if (($transparency > 0) and ($transparency < 1)) {
      @return color.change(
        to-oklch(color.adjust($color, $lightness: -35%)),
        $alpha: ($transparency + 0.1)
      );
    } @else {
      @return color.change(to-oklch(color.adjust($color, $lightness: -25%)), $alpha: 0.2);
    }
  } @else {
    @if (($transparency > 0) and ($transparency < 1)) {
      @return color.change(
        to-oklch(color.adjust($color, $lightness: 30%)),
        $alpha: ($transparency + 0.1)
      );
    } @else {
      @return color.change(to-oklch(color.adjust($color, $lightness: 25%)), $alpha: 0.2);
    }
  }
}

// Tint a color with black/white and returns color in oklch color space
//
// $lightness is negative it will tint with black and the color result becomes darker
// $lightness is positive it will tint with white and the color result becomes lighter
@function shift-color($color, $lightness: 10%) {
  $weight: math.abs($lightness);
  $tint-color: functions.fn-if($lightness > 0, $white, $black);

  @return to-oklch(color.mix($tint-color, $color, $weight, oklch));
}
