// _shadows.scss
@use "sass:map";
@use "sass:math";
@use "sass:string";
@use "../config/feature-flags" as config-flags;
@use "../config/breakpoints" as config-breakpoint;
@use "../config/shadows" as config-shadow;
@use "../functions/feature-flags" as fn-flags;

// Shadow sizes are now configured in config/_shadows.scss

// Generates a CSS shadow string from a given size.
// @param {String} $size - The shadow size.
// @param {String} $color - The shadow color.
// @return {String} - The CSS shadow string.

@mixin shadow-base($x, $y, $blur, $spread, $color) {
    box-shadow: $x $y $blur $spread $color;
}

// Generates a CSS shadow string from a given size.
// @param {String} $size - The shadow size.
// @param {String} $color - The shadow color.
@mixin shadow($size: "md", $color: "default") {
    @if not map.has-key(config-shadow.$shadow-sizes, $size) {
        @error "Shadow size '#{$size}' not found in config-shadow.$shadow-sizes map";
        $size: "md"; // Fallback to default
    }
    @if not map.has-key(config-shadow.$shadow-colors, $color) {
        @error "Shadow color '#{$color}' not found in config-shadow.$shadow-colors map";
        $color: "default"; // Fallback to default
    }

    $shadow: map.get(config-shadow.$shadow-sizes, $size);
    $shadow-color: map.get(config-shadow.$shadow-colors, $color);
    @include shadow-base(map.get($shadow, "x"), map.get($shadow, "y"), map.get($shadow, "blur"), map.get($shadow, "spread"), $shadow-color);
}

@mixin shadow-inset($size: "md", $color: "default") {
    $shadow: map.get(config-shadow.$shadow-sizes, $size);
    $shadow-color: map.get(config-shadow.$shadow-colors, $color);

    box-shadow: inset map.get($shadow, "x") map.get($shadow, "y") map.get($shadow, "blur") map.get($shadow, "spread") $shadow-color;
}

@mixin elevation($i) {
    @if $i == 1 {
        @include shadow("sm");
    } @else if $i == 2 {
        @include shadow("md");
    } @else if $i == 3 {
        @include shadow("lg");
    } @else if $i == 4 {
        @include shadow("xl");
    } @else if $i == 5 {
        @include shadow("monster");
    }

    z-index: $i;
}

// Utility Classes
@if fn-flags.feature-enabled("shadows") {
    #{config-flags.$parent-selector} .shadow-none {
        box-shadow: none !important;
    }

    @each $size, $values in config-shadow.$shadow-sizes {
        #{config-flags.$parent-selector} .shadow-#{$size} {
            @include shadow($size);
        }

        // Shadow with color variants
        @each $color-name, $color-value in config-shadow.$shadow-colors {
            @if $color-name != "default" {
                #{config-flags.$parent-selector} .shadow-#{$size}-#{$color-name} {
                    @include shadow($size, $color-name);
                }
            }
        }

        #{config-flags.$parent-selector} .shadow-inset-#{$size} {
            @include shadow-inset($size);
        }

        // Inset shadows with color variants
        @each $color-name, $color-value in config-shadow.$shadow-colors {
            @if $color-name != "default" {
                #{config-flags.$parent-selector} .shadow-inset-#{$size}-#{$color-name} {
                    @include shadow-inset($size, $color-name);
                }
            }
        }

        // Hover shadows
        #{config-flags.$parent-selector} .hover\:shadow-#{$size}:hover {
            @include shadow($size);
        }

        // Hover shadows with color variants
        @each $color-name, $color-value in config-shadow.$shadow-colors {
            @if $color-name != "default" {
                #{config-flags.$parent-selector} .hover\:shadow-#{$size}-#{$color-name}:hover {
                    @include shadow($size, $color-name);
                }
            }
        }

        // Focus shadows
        #{config-flags.$parent-selector} .focus\:shadow-#{$size}:focus {
            @include shadow($size);
        }

        // Focus shadows with color variants
        @each $color-name, $color-value in config-shadow.$shadow-colors {
            @if $color-name != "default" {
                #{config-flags.$parent-selector} .focus\:shadow-#{$size}-#{$color-name}:focus {
                    @include shadow($size, $color-name);
                }
            }
        }

        // Active shadows
        #{config-flags.$parent-selector} .active\:shadow-#{$size}:active {
            @include shadow($size);
        }

        // Active shadows with color variants
        @each $color-name, $color-value in config-shadow.$shadow-colors {
            @if $color-name != "default" {
                #{config-flags.$parent-selector} .active\:shadow-#{$size}-#{$color-name}:active {
                    @include shadow($size, $color-name);
                }
            }
        }
    }

    // Responsive variants
    @each $breakpoint, $width in config-breakpoint.$breakpoints {
        @media (min-width: #{$width}) {
            @each $size, $values in config-shadow.$shadow-sizes {
                // Regular shadows with breakpoints
                #{config-flags.$parent-selector} .shadow-#{$size}\@#{$breakpoint} {
                    @include shadow($size);
                }

                // Shadows with color variants at breakpoints
                @each $color-name, $color-value in config-shadow.$shadow-colors {
                    @if $color-name != "default" {
                        #{config-flags.$parent-selector} .shadow-#{$size}-#{$color-name}\@#{$breakpoint} {
                            @include shadow($size, $color-name);
                        }
                    }
                }

                // Hover shadows with breakpoints
                #{config-flags.$parent-selector} .hover\:shadow-#{$size}\@#{$breakpoint}:hover {
                    @include shadow($size);
                }

                // Hover shadows with color variants at breakpoints
                @each $color-name, $color-value in config-shadow.$shadow-colors {
                    @if $color-name != "default" {
                        #{config-flags.$parent-selector} .hover\:shadow-#{$size}-#{$color-name}\@#{$breakpoint}:hover {
                            @include shadow($size, $color-name);
                        }
                    }
                }

                // Focus shadows with breakpoints
                #{config-flags.$parent-selector} .focus\:shadow-#{$size}\@#{$breakpoint}:focus {
                    @include shadow($size);
                }

                // Focus shadows with color variants at breakpoints
                @each $color-name, $color-value in config-shadow.$shadow-colors {
                    @if $color-name != "default" {
                        #{config-flags.$parent-selector} .focus\:shadow-#{$size}-#{$color-name}\@#{$breakpoint}:focus {
                            @include shadow($size, $color-name);
                        }
                    }
                }
            }
        }
    }

    @for $i from 1 through 5 {
        #{config-flags.$parent-selector} .elevation-#{$i} {
            @include elevation($i);
        }
    }
}
