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

/// Query Builder Theme
///
/// PRIMARY TOKENS:
/// - `$background` — The background color.
/// - `$header-background` — The header background color.
/// - `$subquery-header-background` — The subquery header background.
/// - `$subquery-border-color` — The subquery border color.
///
/// 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 filtering row. PRIMARY - derives label-foreground, header-background.
/// @param {Color} $header-background [null] - The background color of the query builder header. PRIMARY - derives header-foreground, subquery-header-background. Auto-derived from background.
/// @param {Color} $header-foreground [null] - The foreground color of the query builder header. Auto-derived from header-background.
/// @param {Color} $header-border [null] - The border color of the query builder header. Auto-derived from subquery-border-color (bootstrap only).
///
/// @param {Map} $subquery-header-background [null] - The background color of the subquery header. PRIMARY - derives subquery-border-color. Auto-derived from header-background.
/// @param {Map} $subquery-border-color [null] - The border color of the query block. PRIMARY - derives separator-color. Auto-derived from subquery-header-background.
/// @param {Map} $separator-color [null] - The separator color of the query block. Auto-derived from subquery-border-color.
/// @param {Number} $subquery-border-radius [null] - The border radius of the subquery block.
///
/// @param {Map} $label-foreground [null] - The color for query builder labels "from" & "select". Auto-derived from background.
/// @param {Number} $border-radius [null] - The border radius of the query builder.
///
/// @param {Color} $color-expression-group-and [null] - The color of advanced filtering "AND" condition group.
/// @param {Color} $color-expression-group-or [null] - The color of advanced filtering "OR" condition group.
///
/// @example scss - Set custom query-builder colors
///   $my-query-builder-theme: query-builder-theme($background: red);
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-query-builder-theme);
@function query-builder-theme(
    $schema: $light-material-schema,
    $background: null,
    $header-background: null,
    $header-foreground: null,
    $header-border: null,
    $color-expression-group-and: null,
    $color-expression-group-or: null,
    $border-radius: null,
    $subquery-header-background: null,
    $subquery-border-color: null,
    $subquery-border-radius: null,
    $separator-color: null,
    $label-foreground: null
) {
    $selector: #{config.element-prefix() + '-' + 'query-builder'};
    $query-builder-schema: ();

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

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

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

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

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

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

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

    @if not($separator-color) and $subquery-border-color {
        $separator-color: dynamic-shade(var(--subquery-border-color));
    }

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

    @return extend(
        $theme,
        (
            selector: $selector,
            background: $background,
            header-background: $header-background,
            header-foreground: $header-foreground,
            header-border: $header-border,
            color-expression-group-and: $color-expression-group-and,
            color-expression-group-or: $color-expression-group-or,
            subquery-header-background: $subquery-header-background,
            subquery-border-color: $subquery-border-color,
            subquery-border-radius: $subquery-border-radius,
            separator-color: $separator-color,
            border-radius: $border-radius,
            label-foreground: $label-foreground,
        )
    );
}
