@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/desig9stein" target="_blank">Marin Popov</a>
/// @author <a href="https://github.com/simeonoff" target="_blank">Simeon Simeonoff</a>
////

/// FAB Button Theme
///
/// PRIMARY TOKENS:
/// - `$background` — The background color of the button.
/// - `$foreground` — The text color of the button. Used as fallback for state foreground colors.
/// - `$icon-color` — The icon color in the button. Used as fallback for icon-color-hover.
///
/// Derived colors are auto-calculated for contrast.
///
/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
/// @param {Color} $background [null] - The background color of the button. PRIMARY - derives foreground, icon-color, hover-background, focus-background, active-background.
/// @param {Color} $foreground [null] - The text color of the button. Auto-derived from background.
/// @param {Color} $icon-color [null] - The icon color in the button. Auto-derived from background.
/// @param {Color} $icon-color-hover [null] - The icon color in the button on hover. Auto-derived from icon-color or hover-background.
/// @param {Color} $hover-background [null] - The hover background color. Auto-derived from background.
/// @param {Color} $hover-foreground [null] - The hover text color. Auto-derived from foreground or hover-background.
/// @param {Color} $focus-background [null] - The focus background color. Auto-derived from background or focus-hover-background.
/// @param {Color} $focus-foreground [null] - The focus text color. Auto-derived from foreground or focus-background.
/// @param {Color} $focus-hover-background [null] - The background color on focus hovered state. Auto-derived from hover-background.
/// @param {Color} $focus-hover-foreground [null] - The text color on focus hovered state. Auto-derived from foreground or focus-hover-background.
/// @param {Color} $focus-visible-background [null] - The focus-visible background color. Auto-derived from focus-background or background.
/// @param {Color} $focus-visible-foreground [null] - The focus-visible text color. Auto-derived from focus-visible-background.
/// @param {Color} $active-background [null] - The active background. Auto-derived from background, focus-background, or hover-background.
/// @param {Color} $active-foreground [null] - The active text color. Auto-derived from active-background.
/// @param {List} $border-radius [null] - The border radius of the button.
/// @param {Color} $border-color [null] - The border color of the button.
/// @param {Color} $hover-border-color [null] - The hover border color of the button.
/// @param {Color} $focus-border-color [null] - The focus border color of the button.
/// @param {Color} $focus-visible-border-color [null] - The focus-visible border color. Auto-derived from focus-visible-foreground (fluent).
/// @param {Color} $active-border-color [null] - The active border color of the button.
/// @param {Color} $shadow-color [null] - The shadow color. Auto-derived from focus-visible-background (bootstrap/indigo).
/// @param {Color} $resting-shadow [null] - The shadow of the button in its idle state.
/// @param {Color} $hover-shadow [null] - The shadow of the button in its hover state.
/// @param {Color} $focus-shadow [null] - The shadow of the button in its focus state.
/// @param {Color} $active-shadow [null] - The shadow of the button in its focus state.
/// @param {Color} $disabled-background [null] - The disabled background color. Auto-derived from background (bootstrap/indigo).
/// @param {Color} $disabled-foreground [null] - The disabled text color. Auto-derived from disabled-background.
/// @param {Color} $disabled-icon-color [null] - The disabled icon color. Auto-derived from disabled-foreground or disabled-background.
/// @param {Color} $disabled-border-color [null] - The disabled border color of the button.
///
/// @requires $light-material-schema
///
/// @example scss - Change the background and text colors in fab buttons
///   $my-button-theme: fab-button-theme(
///     $foreground: white,
///     $background: black
///   );
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-button-theme);
@function fab-button-theme(
    $schema: $light-material-schema,

    $background: null,
    $foreground: null,

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

    $icon-color: $foreground,
    $icon-color-hover: $hover-foreground,

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

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

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

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

    $border-radius: null,
    $border-color: null,
    $hover-border-color: null,
    $focus-border-color: null,
    $focus-visible-border-color: null,
    $active-border-color: null,

    $shadow-color: null,

    $resting-shadow: null,
    $hover-shadow: null,
    $focus-shadow: null,
    $active-shadow: null,

    $disabled-background: null,
    $disabled-foreground: null,
    $disabled-icon-color: $disabled-foreground,
    $disabled-border-color: null,
    $size: null
) {
    $selector: (
        #{'[' + config.element-prefix() + 'Button="fab"]'},
        #{config.element-prefix() + '-' + 'button[variant="fab"]'},
        #{'.' + config.element-prefix() + '-' + 'button--fab'}
    );
    $button-schema: ();

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

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

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

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

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

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

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

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

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

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

    @if $variant == 'fluent' {
        @if not($focus-visible-background) and $background {
            $focus-visible-background: var(--background);
        }
    } @else {
        @if not($focus-visible-background) and $focus-background {
            $focus-visible-background: var(--focus-background);
        }
    }

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

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

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

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

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

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

    @if $variant == 'bootstrap' or $variant == 'indigo' {
        @if not($shadow-color) and $focus-visible-background {
            $shadow-color: hsl(from var(--focus-visible-background) h s l / 0.5);
        }

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

        @if not($disabled-icon-color) and $disabled-foreground {
            $disabled-icon-color: var(--disabled-foreground);
        }

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

        @if not($disabled-icon-color) and $disabled-background {
            $disabled-icon-color: hsl(from adaptive-contrast(var(--disabled-background)) h s l / 0.5);
        }
    }

    @if not($resting-shadow) {
        $resting-elevation: map.get($button-schema, 'resting-elevation');
        $resting-shadow: elevation($resting-elevation);
    }

    @if not($hover-shadow) {
        $hover-elevation: map.get($button-schema, 'hover-elevation');
        $hover-shadow: elevation($hover-elevation);
    }

    @if not($focus-shadow) {
        $focus-elevation: map.get($button-schema, 'focus-elevation');
        $focus-shadow: elevation($focus-elevation);
    }

    @if not($active-shadow) {
        $active-elevation: map.get($button-schema, 'active-elevation');
        $active-shadow: elevation($active-elevation);
    }

    @return extend(
        $theme,
        (
            selector: $selector,
            background: $background,
            foreground: $foreground,
            icon-color: $icon-color,
            icon-color-hover: $icon-color-hover,
            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,
            focus-visible-background: $focus-visible-background,
            focus-visible-foreground: $focus-visible-foreground,
            active-background: $active-background,
            active-foreground: $active-foreground,
            border-radius: $border-radius,
            border-color: $border-color,
            hover-border-color: $hover-border-color,
            focus-border-color: $focus-border-color,
            focus-visible-border-color: $focus-visible-border-color,
            active-border-color: $active-border-color,
            shadow-color: $shadow-color,
            resting-elevation: $resting-shadow,
            hover-elevation: $hover-shadow,
            focus-elevation: $focus-shadow,
            active-elevation: $active-shadow,
            disabled-background: $disabled-background,
            disabled-foreground: $disabled-foreground,
            disabled-icon-color: $disabled-icon-color,
            disabled-border-color: $disabled-border-color,
            size: $size,
        )
    );
}
