@use "sass:color"
@use "sass:list"
@use "sass:map"
@use "sass:math"
@use "sass:meta"
@use "sass:string"

@function mergeColorMaps($bulma-colors, $custom-colors)
  // We return at least Bulma's hard-coded colors
  $merged-colors: $bulma-colors

  // We want a map as input
  @if meta.type-of($custom-colors) == 'map'
    @each $name, $components in $custom-colors
      // The color name should be a string
      // and the components either a single color
      // or a colors list with at least one element
      @if meta.type-of($name) == 'string' and (meta.type-of($components) == 'list' or meta.type-of($components) == 'color') and list.length($components) >= 1
        $color-base: null
        $color-invert: null
        $color-light: null
        $color-dark: null
        $value: null

        // The param can either be a single color
        // or a list of 2 colors
        @if meta.type-of($components) == 'color'
          $color-base: $components
          $color-invert: findColorInvert($color-base)
          $color-light: findLightColor($color-base)
          $color-dark: findDarkColor($color-base)
        @else if meta.type-of($components) == 'list'
          $color-base: list.nth($components, 1)
          // If Invert, Light and Dark are provided
          @if list.length($components) > 3
            $color-invert: list.nth($components, 2)
            $color-light: list.nth($components, 3)
            $color-dark: list.nth($components, 4)
            // If only Invert and Light are provided
          @else if list.length($components) > 2
            $color-invert: list.nth($components, 2)
            $color-light: list.nth($components, 3)
            $color-dark: findDarkColor($color-base)
            // If only Invert is provided
          @else
            $color-invert: list.nth($components, 2)
            $color-light: findLightColor($color-base)
            $color-dark: findDarkColor($color-base)

        $value: ($color-base, $color-invert, $color-light, $color-dark)

        // We only want to merge the map if the color base is an actual color
        @if meta.type-of($color-base) == 'color'
          // We merge this colors elements as map with Bulma's colors map
          // (we can override them this way, no multiple definition for the same name)
          // $merged-colors: map_merge($merged-colors, ($name: ($color-base, $color-invert, $color-light, $color-dark)))
          $merged-colors: map.merge($merged-colors, ($name: $value))

  @return $merged-colors

$-light-breakpoint: 0.55
/// Returns the luminance of `$color` as a float (between 0 and 1)
/// 1 is pure white, 0 is pure black
/// @param {Color} $color - Color
/// @return {Number}
/// @link http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef Reference
@function colorLuminance($color)
  @if meta.type-of($color) != 'color'
    @return $-light-breakpoint
  $color-rgb: ('red': color.channel($color, "red", $space: rgb),'green': color.channel($color, "green", $space: rgb),'blue': color.channel($color, "blue", $space: rgb))
  @each $name, $value in $color-rgb
    $value: math.div($value, 255)
    @if $value < 0.03928
      $value: math.div($value, 12.92)
    @else
      $value: math.div(($value + 0.055), 1.055)
      $value: math.pow($value, 2)
    $color-rgb: map.merge($color-rgb, ($name: $value))
  @return (map.get($color-rgb, 'red') * 0.2126) + (map.get($color-rgb, 'green') * 0.7152) + (map.get($color-rgb, 'blue') * 0.0722)

@function isLightColor($color)
  @return colorLuminance($color) > $-light-breakpoint

@function isDarkColor($color)
  @return not isLightColor($color)

/// Invert color calculation
@function findColorInvert($color)
  @if meta.type-of($color) == 'color'
    @if isLightColor($color)
      @return #{var(--invert-dark-color)}
    @else
      @return #{var(--invert-light-color)}
  @return $color

/// Light color calculation
@function _calculateLightColor($color, $l: 96%)
  @if color.channel($color, "lightness", $space: hsl) > 96%
    $l: color.channel($color, "lightness", $space: hsl)
  @return color.change($color, $lightness: $l)

@function findLightColor($color, $fallback: null)
  @if meta.type-of($color) == 'color'
    @return _calculateLightColor($color)
  @else if meta.type-of($fallback) == 'color'
    @return _calculateLightColor($fallback)
  @return $color

/// Dark color calculation
@function _calculateDarkColor($color, $base-l: 29%)
  $luminance: colorLuminance($color)
  $luminance-delta: (0.53 - $luminance)
  $target-l: math.round($base-l + ($luminance-delta * 53))
  @return color.change($color, $lightness: math.max($base-l, $target-l))

@function findDarkColor($color, $fallback: null)
  @if meta.type-of($color) == 'color'
    @return _calculateDarkColor($color)
  @else if meta.type-of($fallback) == 'color'
    @return _calculateDarkColor($fallback)
  @return $color

@function bulmaRgba($color, $alpha)
  @if meta.type-of($color) != 'color'
    @return $color
  @return rgba($color, $alpha)

@function bulmaDarken($color, $amount)
  @if meta.type-of($color) != 'color'
    @return $color
  @return color.adjust($color, $lightness: -$amount)

@function bulmaLighten($color, $amount)
  @if meta.type-of($color) != 'color'
    @return $color
  @return color.adjust($color, $lightness: $amount)

/// State colors calculation base on color lightness
@function findColorState($color, $amount)
  @if meta.type-of($color) != 'color'
    @return $color
  @if isLightColor($color)
    @return bulmaDarken($color, $amount)
  @else
    @return bulmaLighten($color, $amount)
//
// Additional @funtion for bulvar
//

/// Camelize string
/// @param [string] $string
/// @return [string]
@function camel-case($str)
  $camelCase: ''
  $prevCharWasDash: false
  @for $i from 1 to string.length($str) + 1
    $char: string.slice($str, $i, $i)
    @if $char == '-'
      // If $prevCharWasDash is true, then we've encountered a double dash '--'
      @if $prevCharWasDash
        // Turn double dashes into single dashes
        $camelCase: $camelCase + '-'
        $prevCharWasDash: false
      @else
        // This is a single dash. Skip adding it to the string and uppercase the next
        // character.
        $prevCharWasDash: true
    @else if $prevCharWasDash
      // The current $char is not a dash. If the previous character
      // was a dash and we should uppercase this char.
      $camelCase: $camelCase + string.to-upper-case($char)
      $prevCharWasDash: false
    @else
      // Add current $char as-is
      $camelCase: $camelCase + $char
      $prevCharWasDash: false

  @return $camelCase

// assign variable to a property
// base on $css-variables flag
@function assign($name-or-value, $fallback, $force-fallback: false)
  @if $css-variables
    @if $force-fallback
      @return var(--#{$name-or-value}, #{$fallback})
    @else
      @return var(--#{$name-or-value})
  @else
    @return $fallback

// extracts var(--var-name) -> var-name
@function unwrap($variable)
  @if meta.type-of($variable) == 'string' and string.index($variable, "var(") == 1
    $start-at: string.index($variable, "--") + string.length("--")
    $end-at: (string.index($variable, ',') or -1) - 1
    @return string.slice($variable, $start-at, $end-at)
  @return $variable

@function bulmaToRGB($color)
  @if meta.type-of($color) != 'color'
      @return $color
  @return color.channel($color, "red", $space: rgb), color.channel($color, "green", $space: rgb), color.channel($color, "blue", $space: rgb)

/// lighten & darken with css vars

@function bulmaVarOpacity($color, $alpha)
  $name: unwrap($color)
  @if meta.type-of($name) == 'string'
    @return hsla(var(--#{$name}-h), var(--#{$name}-s), var(--#{$name}-l), #{$alpha})
  @return $color

@function bulmaVarLighteness($color, $amount)
  $name: unwrap($color)
  @if meta.type-of($name) == 'string' // and map-has-key($colors, $name)
    @return hsla(var(--#{$name}-h), var(--#{$name}-s), #{$amount}, var(--#{$name}-a))
  @return $color

@function bulmaVarLighten($color, $amount)
  $name: unwrap($color)
  @if meta.type-of($name) == 'string' // and map-has-key($colors, $name)
    $name-l: calc(var(--#{$name}-l) + #{if(math.is-unitless($amount), ($amount * 1%), $amount)})
    @return hsla(var(--#{$name}-h), var(--#{$name}-s), #{$name-l}, var(--#{$name}-a))
  @return $color

@function bulmaVarDarken($color, $amount)
  @return bulmaVarLighten($color, ($amount * -1))
