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

/// Expansion Panel Theme
///
/// PRIMARY TOKENS:
/// - `$header-background` — The panel header background.
/// - `$body-background` — The panel body background.
///
/// Derived colors are auto-calculated for contrast.
///
/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
/// @param {Color} $header-background [null] - The panel header background color. PRIMARY - derives header colors, focus, disabled.
/// @param {Color} $header-focus-background [null] - The panel header focus background color. Auto-derived from header-background.
/// @param {Color} $header-title-color [null] - The panel header title text color. Auto-derived from header-background.
/// @param {Color} $header-description-color [null] - The panel header description text color. Auto-derived from header-background (muted).
/// @param {Color} $header-icon-color [null] - The panel header icon color. Auto-derived from header-background.
/// @param {Color} $body-color [null] - The panel body text color. Auto-derived from body-background.
/// @param {Color} $body-background [null] - The panel body background color. PRIMARY for body - derives body-color.
/// @param {Color} $disabled-text-color [null] - The panel disabled text color. Auto-derived from header-background.
/// @param {Color} $disabled-description-color [null] - The panel disabled header description text color. Auto-derived from header-background.
/// @param {Number} $expanded-margin [null] - The expansion panel margin in expanded state when positioned inside accordion.
/// @param {List} $border-radius [null] - The border radius used for expansion-panel component.
/// @requires $light-material-schema
/// @example scss - Change the header background color
///   $my-expansion-panel-theme: expansion-panel-theme($header-background: black);
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-expansion-panel-theme);
@function expansion-panel-theme(
    $schema: $light-material-schema,

    $border-radius: null,
    $header-background: null,
    $header-focus-background: null,
    $header-title-color: null,
    $header-description-color: null,
    $header-icon-color: null,
    $body-color: null,
    $body-background: null,
    $disabled-text-color: null,
    $disabled-description-color: null,
    $expanded-margin: null
) {
    $selector: #{config.element-prefix() + '-' + 'expansion-panel'};
    $expansion-panel-schema: ();

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

    $theme: digest-schema($expansion-panel-schema);

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

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

    @if not($header-description-color) and $header-background {
        $header-description-color: hsl(from adaptive-contrast(var(--header-background)) h s l / 0.8);
    }

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

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

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

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

    @return extend(
        $theme,
        (
            selector: $selector,
            header-background: $header-background,
            border-radius: $border-radius,
            header-focus-background: $header-focus-background,
            header-title-color: $header-title-color,
            header-description-color: $header-description-color,
            header-icon-color: $header-icon-color,
            body-color: $body-color,
            body-background: $body-background,
            disabled-text-color: $disabled-text-color,
            disabled-description-color: $disabled-description-color,
            expanded-margin: $expanded-margin,
        )
    );
}

/// Accordion Theme
///
/// PRIMARY TOKENS:
/// - `$header-background` — The panel header background.
/// - `$body-background` — The panel body background.
///
/// Derived colors are auto-calculated for contrast.
///
/// @alias expansion-panel-theme
@function accordion-theme($args...) {
    @return expansion-panel-theme($args...);
}
