@use 'sass:map';
@use '../../config';
@use '../../functions' as *;
@use '../../schemas/' as *;
@use '../../../utils/map' as *;
@use '../../../color/functions' as *;
@use '../../../elevations/' as *;

////
/// @package theming
/// @group themes
/// @access public
/// @author <a href="https://github.com/SisIvanova" target="_blank">Silvia Ivanova</a>
////

/// Contained Icon Button Theme
///
/// PRIMARY TOKENS:
/// - `$background` — Background color for the contained icon button.
/// - `$foreground` — Foreground (icon) color. Used as fallback for state foreground colors. Auto-derived from background.
/// - `$border-color` — Border color.
///
/// Derived colors are auto-calculated for contrast.
///
/// @param {Map} $schema [$light-material-schema] - The schema map or icon-button schema to use.
/// @param {Color} $background [null] - Background color for the contained icon button. PRIMARY - derives foreground, hover-background, focus-background, active-background.
/// @param {Color} $foreground [null] - Foreground (icon) color. Auto-derived from background.
/// @param {Color} $shadow-color [null] - Shadow color. Auto-derived from focus-background.
/// @param {Color} $hover-background [null] - Background color on hover. Auto-derived from background.
/// @param {Color} $hover-foreground [null] - Foreground color on hover. Auto-derived from foreground or hover-background.
/// @param {Color} $focus-background [null] - Background color on focus. Auto-derived from background.
/// @param {Color} $focus-foreground [null] - Foreground color on focus. Auto-derived from foreground or focus-background.
/// @param {Color} $focus-hover-background [null] - Background color on focus + hover. Auto-derived from focus-background.
/// @param {Color} $focus-hover-foreground [null] - Foreground color on focus + hover. Auto-derived from foreground or focus-hover-background.
/// @param {Color} $active-background [null] - Background color when active. Auto-derived from background.
/// @param {Color} $active-foreground [null] - Foreground color when active. Auto-derived from foreground or active-background.
/// @param {Length} $border-radius [null] - Border radius for the icon button.
/// @param {Color} $border-color [null] - Border color. PRIMARY - derives focus-border-color.
/// @param {Color} $focus-border-color [null] - Border color on focus. Auto-derived from foreground or border-color.
/// @param {Color} $disabled-background [null] - Background color when disabled. Auto-derived from background (bootstrap/indigo).
/// @param {Color} $disabled-foreground [null] - Foreground color when disabled. Auto-derived from foreground or disabled-background.
/// @param {Color} $disabled-border-color [null] - Border color when disabled.
/// @param {Length} $size [null] - Size of the icon button.
/// @requires $light-material-schema
/// @return {Map} A map containing the theme name, selector, and theme values for the contained icon button.
@function contained-icon-button-theme(
    $schema: $light-material-schema,

    $background: null,
    $foreground: null,
    $shadow-color: null,

    $hover-background: null,
    $hover-foreground: null,

    $focus-background: null,
    $focus-foreground: null,

    $focus-hover-background: null,
    $focus-hover-foreground: null,

    $active-background: null,
    $active-foreground: null,

    $border-radius: null,
    $border-color: null,
    $focus-border-color: null,

    $disabled-background: null,
    $disabled-foreground: null,
    $disabled-border-color: null,

    $size: null
) {
    $selector: (
        #{'[' + config.element-prefix() + 'IconButton="contained"]'},
        #{config.element-prefix() + '-' + 'icon-button[variant="contained"]'},
        '.' + #{config.element-prefix() + '-' + 'icon-button--contained'}
    );
    $icon-button-schema: ();

    @if map.has-key($schema, 'icon-button') {
        $icon-button-schema: map.get($schema, 'icon-button');

        @if map.has-key($icon-button-schema, 'contained') {
            $icon-button-schema: map.get($icon-button-schema, 'contained');
        } @else {
            $icon-button-schema: $schema;
        }
    }

    $theme: digest-schema($icon-button-schema);
    $variant: map.get($schema, '_meta', 'theme');

    @if not($hover-foreground) and $foreground {
        $hover-foreground: var(--foreground);
    }

    @if not($focus-foreground) and $foreground {
        $focus-foreground: var(--foreground);
    }

    @if not($focus-hover-foreground) and $foreground {
        $focus-hover-foreground: var(--foreground);
    }

    @if not($active-foreground) and $foreground {
        $active-foreground: var(--foreground);
    }

    @if $variant == 'indigo' {
        @if not($foreground) and $background {
            $foreground: hsl(from adaptive-contrast(var(--background)) h s l / 0.8);
        }

        @if not($hover-background) and $background {
            $hover-background: dynamic-shade(var(--background));
        }

        @if not($focus-background) and $background {
            $focus-background: var(--background);
        }

        @if not($focus-foreground) and $foreground {
            $focus-foreground: var(--foreground);
        }

        @if not($focus-hover-background) and $focus-background {
            $focus-hover-background: dynamic-shade(var(--focus-background));
        }

        @if not($active-background) and $background {
            $active-background: dynamic-shade(var(--background));
        }
    } @else {
        @if not($foreground) and $background {
            $foreground: adaptive-contrast(var(--background));
        }

        @if not($hover-background) and $background {
            $hover-background: dynamic-shade(var(--background));
        }

        @if not($focus-background) and $background {
            @if $variant == 'fluent' or $variant == 'bootstrap' {
                $focus-background: var(--background);
            } @else {
                $focus-background: dynamic-shade(var(--background), $offset: 10);
            }
        }

        @if not($focus-foreground) and $focus-background {
            $focus-foreground: adaptive-contrast(var(--focus-background));
        }

        @if not($focus-hover-background) and $focus-background {
            $focus-hover-background: dynamic-shade(var(--focus-background));
        }

        @if not($active-background) and $background {
            $active-background: dynamic-shade(var(--background), $offset: 10);
        }
    }

    @if not($hover-foreground) and $hover-background {
        $hover-foreground: adaptive-contrast(var(--hover-background));
    }

    @if not($focus-hover-foreground) and $focus-hover-background {
        $focus-hover-foreground: adaptive-contrast(var(--focus-hover-background));
    }

    @if not($active-foreground) and $active-background {
        $active-foreground: adaptive-contrast(var(--active-background));
    }

    @if not($shadow-color) and $focus-background {
        $shadow-color: hsl(from var(--focus-background) h s l / 0.5);
    }

    @if $variant == 'fluent' {
        @if not($focus-border-color) and $foreground {
            $focus-border-color: var(--foreground);
        }
    } @else {
        @if not($focus-border-color) and $border-color {
            $focus-border-color: dynamic-shade(var(--border-color), $offset: 10);
        }
    }

    @if $variant == 'indigo' {
        @if not($disabled-background) and $background {
            $disabled-background: hsl(from var(--background) h s l / 0.4);
        }

        @if not($disabled-foreground) and $foreground {
            $disabled-foreground: hsl(from var(--foreground) h s l / 0.5);
        }

        @if not($focus-border-color) and $focus-background {
            $focus-border-color: hsl(from var(--focus-background) h s l / 0.3);
        }
    } @else if $variant == 'bootstrap' {
        @if not($disabled-background) and $background {
            $disabled-background: hsl(from var(--background) h s l / 0.2);
        }

        @if not($disabled-foreground) and $disabled-background {
            $disabled-foreground: hsl(from var(--disabled-background) h s l / 0.7);
        }
    }

    @return extend(
        $theme,
        (
            selector: $selector,
            background: $background,
            foreground: $foreground,
            shadow-color: $shadow-color,

            hover-background: $hover-background,
            hover-foreground: $hover-foreground,

            focus-background: $focus-background,
            focus-foreground: $focus-foreground,

            focus-hover-background: $focus-hover-background,
            focus-hover-foreground: $focus-hover-foreground,

            active-background: $active-background,
            active-foreground: $active-foreground,

            border-radius: $border-radius,
            border-color: $border-color,
            focus-border-color: $focus-border-color,

            disabled-background: $disabled-background,
            disabled-foreground: $disabled-foreground,
            disabled-border-color: $disabled-border-color,
            size: $size,
        )
    );
}
