// When color definition differs for dark and light variant,
// it gets @if ed depending on $variant

@use "sass:color";
@use "sass:math";
@use "sass:string";
@use "theme";

@function gtkalpha($color, $alpha) {
  @return string.unquote("alpha(#{$color}, #{$alpha})");
}

// Determine whether the color has alpha.
@function _has-alpha($color) {
  @return if(color.alpha($color) < 1, true, false);
}

// Determine whether the color is dark.
@function _is-dark($color) {
  // Calculate color brightness as per https://www.w3.org/TR/AERT/#color-contrast
  $brightness: (color.red($color) * 299 + color.green($color) * 587 + color.blue($color) * 114) / 1000;

  @return if($brightness < 128, true, false);
}

$on-light: #000;
$on-dark: #fff;

// Determine whether to use dark or light color on top of given color
// to meet accessibility standards for contrast.
@function on($color) {
  $contrast-color: if(_is-dark($color), $on-dark, $on-light);

  @if color.saturation($color) > 50% or color.alpha($color) < 1 {
    @return $contrast-color;
  } @else {
    @return rgba($contrast-color, .87);
  }
}

// Determine the strength of highlight on top of given color.
@function highlight($color) {
  @if color.lightness($color) >= 80% {
    @return rgba(white, .4);
  } @else if color.lightness($color) >= 40% {
    @return rgba(white, .2);
  } @else {
    @return rgba(white, .05);
  }
}

// Make translucent color opaque by blending with the background color.
@function opacify($color, $on) {
  @return color.mix(color.change($color, $alpha: 1), $on, color.alpha($color) * 100%);
}

//
// Main colors
//

$surface-z0: %BG%;
$surface-z1: %MATERIA_VIEW%;
$surface-z8: %MATERIA_SURFACE%;
$surface-switch-thumb: %MATERIA_SURFACE%;
$on-surface: %FG%;

$primary-on-light: %SEL_BG%;
$primary-on-dark: %SEL_BG%;
$primary: $primary-on-light;
$on-primary: on($primary);

$error-on-light: %TERMINAL_COLOR9%;
$error-on-dark: %TERMINAL_COLOR9%;
$error: $error-on-light;
$on-error: on($error);

$warning-on-light: %TERMINAL_COLOR11%;
$warning-on-dark: %TERMINAL_COLOR11%;
$warning: $warning-on-light;
$on-warning: on($warning);

$success-on-light: %TERMINAL_COLOR10%;
$success-on-dark: %TERMINAL_COLOR10%;
$success: $success-on-light;
$on-success: on($success);

$visited-on-light: %TERMINAL_COLOR5%;
$visited-on-dark: %TERMINAL_COLOR5%;
$visited: $visited-on-light;
$on-visited: on($visited);

$system: %HDR_BG3%;
$on-system: %HDR_FG%;

$tooltip: rgba(%HDR_BG%, .9);
$on-tooltip: %HDR_FG%;

$scrim: rgba(black, %MATERIA_PANEL_OPACITY%);
$on-scrim: on($scrim);

$scrim-alt: rgba(black, .3);
$on-scrim-alt: on($scrim-alt);

$panel: %HDR_BG3%;
$on-panel: %HDR_FG%;

// for Unity panel which doesn't allow translucent colors
$panel-solid: %HDR_BG3%;
$on-panel-solid: %HDR_FG%;

$titlebar: %HDR_BG%;
$titlebar-backdrop: %HDR_BG2%;
$on-titlebar: %HDR_FG%;

$titlebar-indicator: currentcolor;

@if theme.$dark-theme {
  $surface-z0: if(_is-dark(%BG%), %BG%, %FG%);
  $surface-z1: if(_is-dark(%BG%), %MATERIA_VIEW%, color.mix(white, $surface-z0, 5%));
  $surface-z8: if(_is-dark(%BG%), %MATERIA_SURFACE%, color.mix(white, $surface-z0, 12%));
  $surface-switch-thumb: if(_is-dark(%BG%), %MATERIA_SURFACE%, color.mix(white, $surface-z0, 60%));
  $on-surface: if(_is-dark(%BG%), %FG%, on($surface-z0));

  $primary: $primary-on-dark;
  $on-primary: on($primary);

  $error: $error-on-dark;
  $on-error: on($error);

  $warning: $warning-on-dark;
  $on-warning: on($warning);

  $success: $success-on-dark;
  $on-success: on($success);

  $visited: $visited-on-dark;
  $on-visited: on($visited);

  $titlebar: if(_is-dark(%BG%), %HDR_BG%, color.mix(white, $surface-z0, 9%));
  $titlebar-backdrop: if(_is-dark(%BG%), %HDR_BG2%, $surface-z1);
  $on-titlebar: if(_is-dark(%BG%), %HDR_FG%, on($titlebar));
}

@if theme.$light-topbar {
  $panel: $scrim;
  $on-panel: on($panel);

  $titlebar-indicator: $primary;
}

//
// Overlay state colors
//

// Determine the overlay color depending on the given color and opacity.
@function _overlay($color, $opacity, $on) {
  $opacity-modifier: 0;

  @if color.saturation($color) > 50% or color.saturation($on) > 50% {
    $opacity-modifier: .04;
  }

  @return color.mix(rgba($color, 1), $on, math.percentage($opacity + $opacity-modifier));
}

@function hover-overlay($color, $opacity-modifier: 0, $on: transparent, $alt: false) {
  $opacity: .08;

  @if $alt {
    $opacity: .04;
  }

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function focus-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function pressed-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function dragged-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function activated-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function selected-overlay($color, $opacity-modifier: 0) {
  $opacity: %MATERIA_SELECTION_OPACITY%;

  @if $color == $on-surface {
    $color: $primary;
  }

  @return rgba($color, $opacity + $opacity-modifier);
}

$selected-overlay: selected-overlay($on-surface);

//
// For “on” colors
//

@function primary($color) {
  @return if(_is-dark($color), $primary-on-light, $primary-on-dark);
}

@function error($color) {
  @return if(_is-dark($color), $error-on-light, $error-on-dark);
}

@function hint($color) {
  @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
}

@function disabled($color) {
  @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
}

@function disabled-hint($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function stroke($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function disabled-stroke($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function divider($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function fill($color) {
  @return if(_has-alpha($color), rgba($color, .08), rgba($color, .08));
}

@function entry-fill($color) {
  @return if(_has-alpha($color), rgba($color, .04), rgba($color, .04));
}

@function scrollbar-thumb($color, $state: null) {
  @if $state == null {
    @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
  } @else if $state == "hover" {
    @return if(_has-alpha($color), rgba($color, .48), rgba($color, .6));
  } @else if $state == "pressed" {
    @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
  } @else if $state == "disabled" {
    @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
  } @else {
    @error "Invalid type: '#{$state}'";
  }
}
