@use 'sass:map';
@use 'sass:list';
@use 'sass:meta';
@use 'sass:math';
@use 'sass:color';
@use 'sass:string';
@use '../utils/meta' as *;

////
/// @package theming
////

// Tracks added CSS variables for color shades
$_added: () !default;

/// Generates CSS variables for a shade color
/// @access private
@mixin _shade($color, $shade, $value, $contrast) {
    $_shade: meta.type-of($shade) == 'string';
    $_hsl: if($_shade, string.index($shade, 'hsl'), false);
    $_contrast: if($_shade, string.index($shade, 'contrast'), false);
    $_color: '' + $color;
    $_exists: list.index($_added, #{$_color}-#{$shade});

    @if not($_hsl) {
        $_added: list.append($_added, #{$_color}-#{$shade}) !global;

        --ig-#{$_color}-#{$shade}: #{$value};
    }
}

/// Sets up CSS custom properties for WCAG contrast calculations.
/// These properties are used to determine the appropriate text contrast color
/// based on WCAG accessibility guidelines.
/// @access public
/// @group Color
/// @param {String} $level ['aa'] - WCAG contrast level ('a', 'aa', or 'aaa')
///
/// @example scss - Using the mixin with default AA level
///   .my-component {
///     @include adaptive-contrast();
///   }
///
/// @example scss - Using the mixin with AAA level
///   .my-component {
///     @include adaptive-contrast('aaa');
///   }
///
/// @example scss - Generated CSS custom properties
///   :root {
///     --ig-wcag-a: 0.31;    // Level A threshold
///     --ig-wcag-aa: 0.185;   // Level AA threshold
///     --ig-wcag-aaa: 0.178;  // Level AAA threshold
///     --ig-contrast-level: var(--ig-wcag-aa);
///     --y: clamp(0, (y / var(--ig-contrast-level) - 1) * -infinity, 1);
///     --y-contrast: xyz-d65 var(--y) var(--y) var(--y);
///   }
@mixin adaptive-contrast($level: 'aa') {
    $scope: if(is-root(), ':root', '&');

    #{$scope} {
        --ig-wcag-a: 0.31;
        --ig-wcag-aa: 0.185;
        --ig-wcag-aaa: 0.178;
        --ig-contrast-level: var(--ig-wcag-#{$level});
        --y: clamp(0, (y / var(--ig-contrast-level) - 1) * -infinity, 1);
        --y-contrast: xyz-d65 var(--y) var(--y) var(--y);
    }
}

/// Generates CSS variables for a given palette.
/// @access public
/// @group Palettes
/// @param {Map} $palette - The palette used to generate CSS variables.
/// @param {Boolean} $contrast [true] - Specify if contrast colors should be included.
///
/// @example scss - Generate css variables for a palette
///    $palette: palette($primary: red, $secondary: blue, $gray: #000);
///    @include palette($palette);
///
/// @require {function} is-root
/// @require {function} to-string
/// @require {mixin} adaptive-contrast
@mixin palette($palette, $contrast: true, $contrast-level: 'aa') {
    $scope: if(is-root(), ':root', '&');

    @if $contrast {
        @include adaptive-contrast($contrast-level);
    }

    #{$scope} {
        @each $color, $shades in map.remove($palette, '_meta') {
            @each $shade, $value in $shades {
                @if not(string.index(to-string($shade), raw)) {
                    @include _shade($color, $shade, $value, $contrast);
                }
            }
        }
    }
}
