@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>
////

/// Outlined Icon Button Theme
///
/// PRIMARY TOKENS:
/// - `$foreground` — Foreground (icon) color.
/// - `$hover-background` — Background color on hover.
/// - `$focus-background` — Background color on focus.
/// - `$active-background` — Background color when active.
///
/// For outlined icon buttons, the foreground color is the primary token.
///
/// @param {Map} $schema [$light-material-schema] - The schema map or icon-button schema to use.
/// @param {Color} $background [null] - Background color for the outlined icon button.
/// @param {Color} $foreground [null] - Foreground (icon) color. PRIMARY - derives hover/focus/active backgrounds, foregrounds, and borders.
/// @param {Color} $shadow-color [null] - Shadow color. Auto-derived from focus-background (bootstrap).
/// @param {Color} $hover-background [null] - Background color on hover. PRIMARY - derives hover-foreground. Auto-derived from foreground.
/// @param {Color} $hover-foreground [null] - Foreground color on hover. Auto-derived from hover-background or foreground.
/// @param {Color} $focus-background [null] - Background color on focus. PRIMARY - derives focus-foreground. Auto-derived from foreground.
/// @param {Color} $focus-foreground [null] - Foreground color on focus. Auto-derived from focus-background or foreground.
/// @param {Color} $focus-hover-background [null] - Background color on focus + hover. Auto-derived from foreground or focus-background.
/// @param {Color} $focus-hover-foreground [null] - Foreground color on focus + hover. Auto-derived from focus-hover-background or foreground.
/// @param {Color} $active-background [null] - Background color when active. Auto-derived from foreground.
/// @param {Color} $active-foreground [null] - Foreground color when active. Auto-derived from active-background or hover-foreground.
/// @param {Length} $border-radius [null] - Border radius for the icon button.
/// @param {Color} $border-color [null] - Border color. Auto-derived from foreground.
/// @param {Color} $focus-border-color [null] - Border color on focus. Auto-derived from foreground or active-background.
/// @param {Color} $disabled-background [null] - Background color when disabled.
/// @param {Color} $disabled-foreground [null] - Foreground color when disabled. Auto-derived from foreground (bootstrap/indigo).
/// @param {Color} $disabled-border-color [null] - Border color when disabled. Auto-derived from border-color (bootstrap/indigo).
/// @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 outlined icon button.
@function outlined-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
) {
    $name: #{config.variable-prefix() + '-' + 'outlined-icon-button'};
    $selector: (
        #{'[' + config.element-prefix() + 'IconButton="outlined"]'},
        #{config.element-prefix() + '-' + 'icon-button[variant="outlined"]'},
        '.' + #{config.element-prefix() + '-' + 'icon-button--outlined'}
    );
    $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, 'outlined') {
            $icon-button-schema: map.get($icon-button-schema, 'outlined');
        } @else {
            $icon-button-schema: $schema;
        }
    }

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

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

    @if not($focus-foreground) and $focus-background {
        $focus-foreground: adaptive-contrast(var(--focus-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 $variant == 'material' or $variant == 'fluent' {
        @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 not($hover-background) and $foreground {
            $hover-background: hsl(from var(--foreground) h s l / 0.08);
        }

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

        @if $variant == 'material' {
            @if not($focus-background) and $foreground {
                $focus-background: hsl(from var(--foreground) h s l / 0.16);
            }

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

            @if not($border-color) and $foreground {
                $border-color: hsl(from var(--foreground) h s l / 0.4);
            }
        } @else {
            @if not($focus-hover-background) and $hover-background {
                $focus-hover-background: var(--hover-background);
            }

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

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

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

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

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

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

        @if not($focus-border-color) and $foreground {
            $focus-border-color: hsl(from var(--foreground) h s l / 0.2);
        }

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

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

    @if $variant == 'bootstrap' {
        @if not($border-color) and $foreground {
            $border-color: var(--foreground);
        }

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

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

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

        @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($focus-hover-foreground) and $focus-hover-background {
            $focus-hover-foreground: adaptive-contrast(var(--focus-hover-background));
        }

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

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

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

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

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

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

    @return extend(
        $theme,
        (
            name: $name,
            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,
        )
    );
}
