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

/// Radio Theme
///
/// PRIMARY TOKENS:
/// - `$fill-color` — The checked state border and dot color.
/// - `$empty-color` — The unchecked border color.
/// - `$error-color` — The invalid state color.
///
/// Setting just `$fill-color` will theme all checked states consistently.
///
/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
///
/// @param {Color} $label-color [null]- The text color used for the label text.
/// @param {Color} $label-color-hover [null]- The text color used for the label text on hover. Auto-derived from label-color.
/// @param {Color} $empty-color [null] - The unchecked border color. PRIMARY for unchecked states.
/// @param {Color} $empty-fill-color [null] - The unchecked radio fill color.
/// @param {Color} $fill-color [null] - The checked border and dot colors. PRIMARY - derives hover/focus colors.
/// @param {Color} $disabled-color [null] - The disabled border and dot colors.
/// @param {Color} $disabled-label-color [null] - The disabled label color.
/// @param {Color} $disabled-fill-color [null] - The disabled checked border and dot colors.
/// @param {Color} $hover-color [null] - The border and dot colors on hover. Auto-derived from empty-color.
/// @param {Color} $fill-color-hover [null] - The checked dot color on hover. Auto-derived from fill-color.
/// @param {Color} $fill-hover-border-color [null] - The checked border color on hover. Auto-derived from fill-color-hover.
/// @param {Color} $focus-border-color [null] - The focus border color. Auto-derived from fill-color (bootstrap).
/// @param {Color} $focus-outline-color [null] - The focus outlined color. Auto-derived from empty-color (indigo) or fill-color (bootstrap).
/// @param {Color} $focus-outline-color-filled [null] - The focus outline color when radio is filled. Auto-derived from fill-color (indigo).
/// @param {Color} $error-color [null] - The label, border and dot color in invalid state. PRIMARY for error states.
/// @param {Color} $error-color-hover [null] - The label, border and dot color in invalid state on hover. Auto-derived from error-color.
/// @param {Color} $focus-outline-color-error [null] - The focus outline color in invalid state. Auto-derived from error-color.
///
/// @requires $light-material-schema
///
/// Set light to true when the surrounding area is dark.
/// @example scss - Change the checked dot and border colors
///   $my-radio-theme: radio-theme($fill-color: magenta);
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-radio-theme);
@function radio-theme(
    $schema: $light-material-schema,

    $label-color: null,
    $label-color-hover: null,
    $empty-color: null,
    $empty-fill-color: null,
    $fill-color: null,
    $fill-hover-border-color: null,
    $focus-border-color: null,
    $focus-outline-color: null,
    $focus-outline-color-filled: null,
    $disabled-color: null,
    $disabled-label-color: null,
    $disabled-fill-color: null,
    $hover-color: null,
    $fill-color-hover: null,
    $error-color: null,
    $error-color-hover: null,
    $focus-outline-color-error: null
) {
    $selector: #{config.element-prefix() + '-' + 'radio'};
    $radio-schema: ();

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

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

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

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

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

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

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

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

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

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

    @if $variant == 'indigo' {
        @if not($focus-outline-color) and $empty-color {
            $focus-outline-color: hsl(from var(--empty-color) h s l / 0.5);
        }

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

    @return extend(
        $theme,
        (
            selector: $selector,
            label-color: $label-color,
            label-color-hover: $label-color-hover,
            empty-color: $empty-color,
            empty-fill-color: $empty-fill-color,
            fill-color: $fill-color,
            fill-hover-border-color: $fill-hover-border-color,
            disabled-color: $disabled-color,
            disabled-label-color: $disabled-label-color,
            disabled-fill-color: $disabled-fill-color,
            hover-color: $hover-color,
            fill-color-hover: $fill-color-hover,
            focus-border-color: $focus-border-color,
            focus-outline-color: $focus-outline-color,
            focus-outline-color-filled: $focus-outline-color-filled,
            error-color: $error-color,
            error-color-hover: $error-color-hover,
            focus-outline-color-error: $focus-outline-color-error,
        )
    );
}
