@use 'sass:map';
@use '../../config';
@use '../../functions' as *;
@use '../../schemas/' as *;
@use '../../../utils/map' as *;
@use '../../../color/functions' as *;
@use '../../../elevations/' as *;
@forward './flat-button-theme';
@forward './contained-button-theme';
@forward './outlined-button-theme';
@forward './fab-button-theme';

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

/// Button Theme
///
/// PRIMARY TOKENS:
/// - `$background` — The main button background.
/// - `$hover-background` — Hover state background.
/// - `$focus-background` — Focus state background.
///
/// Setting just `$background` will create a properly contrasted button.
///
/// @deprecated Use flat-button-theme(), contained-button-theme(), outlined-button-theme(), or fab-button-theme() instead.
/// @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 and icon-color.
/// @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 hover-foreground.
/// @param {Color} $hover-background [null] - The hover background color of the button.
/// @param {Color} $hover-foreground [null] - The hover text color of the button. Auto-derived from hover-background.
/// @param {Color} $focus-background [null] - The focus background color of the button.
/// @param {Color} $focus-foreground [null] - The focus text color of the button. Auto-derived from focus-background.
/// @param {Color} $focus-hover-background [null] - The background color on focus hovered state of the button.
/// @param {Color} $focus-hover-foreground [null] - The text color on focus hovered state of the button. Auto-derived from focus-hover-background.
/// @param {Color} $focus-visible-background [null] - The focus-visible background color of the button. Auto-derived from focus-background.
/// @param {Color} $focus-visible-foreground [null] - The focus-visible text color of the button. Auto-derived from focus-visible-background.
/// @param {Color} $active-background [null] - The active background of the button.
/// @param {Color} $active-foreground [null] - The active text color of the button.
/// @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 of the button. Auto-derived from focus-border-color.
/// @param {Color} $active-border-color [null] - The active border color of the button.
/// @param {Color} $shadow-color [null] - The shadow color of the button.
/// @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 active state.
/// @param {Color} $disabled-background [null] - The disabled background color of the button.
/// @param {Color} $disabled-foreground [null] - The disabled text color of the button.
/// @param {Color} $disabled-icon-color [null] - The disabled icon color of the button.
/// @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 contained buttons
///   $my-button-theme: button-theme(
///     $foreground: white,
///     $background: black
///   );
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-button-theme);
/// @see {function} flat-button-theme
/// @see {function} contained-button-theme
/// @see {function} outlined-button-theme
/// @see {function} fab-button-theme
@function 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
) {
    @warn 'button-theme() is deprecated. Use the variant-specific theme functions instead: flat-button-theme(), contained-button-theme(), outlined-button-theme(), fab-button-theme().';

    $selector: #{config.element-prefix() + '-' + 'button'};
    $button-schema: ();

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

    $themes: ();
    $variant: null;

    @each $_name, $_schema in $button-schema {
        $_resting-shadow: $resting-shadow;
        $_hover-shadow: $hover-shadow;
        $_focus-shadow: $focus-shadow;
        $_active-shadow: $active-shadow;
        $_border-radius: $border-radius;

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

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

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

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

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

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

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

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

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

        $themes: map.merge(
            $themes,
            (
                $_name: extend(
                        digest-schema($_schema),
                        (
                            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,
                        )
                    ),
            )
        );
    }

    @return (selector: $selector, variant: $variant, themes: $themes);
}
