@use 'sass:map';
@use '../../functions' as *;
@use '../../schemas/' as *;
@use '../../../utils/map' as *;
@use '../../../color/functions' as *;
@use '../../../elevations/' as *;

////
/// @group themes
/// @access public
/// @author <a href="https://github.com/desig9stein" target="_blank">Marin Popov</a>
////

/// Chip Theme
/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
/// @param {List} $border-radius [null] - The chip border-radius.
///
/// @param {Color} $text-color [null] - The chip text color.
/// @param {Color} $background [null] - The chip background color.
/// @param {Color} $border-color [null] - The chip border color.
///
/// @param {Color} $disabled-text-color [null] - The disabled chip text color.
/// @param {Color} $disabled-background [null] - The disabled chip background color.
/// @param {Color} $disabled-border-color [null] - The disabled chip border color.
///
/// @param {Color} $ghost-background [null] - The chip ghost background color.
/// @param {box-shadow} $ghost-shadow [null] - The chip ghost shadow.
///
/// @param {Color} $hover-text-color [null] - The chip text hover color.
/// @param {Color} $hover-background [null] - The chip hover background color.
/// @param {Color} $hover-border-color [null] - The chip hover border color.
///
/// @param {Color} $focus-text-color [null] - The chip text focus color.
/// @param {Color} $focus-background [null] - The chip focus background color.
/// @param {color} $focus-border-color [null] - The chip focus border color.
///
/// @param {color} $selected-text-color [null] - The selected chip text color.
/// @param {color} $selected-background [null] - The selected chip background color.
/// @param {color} $selected-border-color [null] The selected chip border color.
///
/// @param {color} $hover-selected-text-color [null] - The selected chip hover text color.
/// @param {color} $hover-selected-background [null] - The selected chip hover background color.
/// @param {color} $hover-selected-border-color [null] - The selected chip hover border color.
///
/// @param {color} $focus-selected-text-color [null] - The selected chip text focus color.
/// @param {color} $focus-selected-background [null] - The selected chip focus background color.
/// @param {color} $focus-selected-border-color [null] - The selected chip focus border color.
///
/// @param {color} $remove-icon-color [null] - The remove icon color for the chip.
/// @param {color} $remove-icon-color-focus [null] - The remove icon color on focus for the chip.
///
/// @requires $light-material-schema
///
/// @example scss Change the background color
///   $my-chip-theme: igx-chip-theme($background: black);
///   // Pass the theme to the css-vars mixin
///   @include css-vars($my-chip-theme);
@function chip-theme(
    $schema: $light-material-schema,
    $border-radius: null,

    $text-color: null,
    $background: null,
    $border-color: null,
    $ghost-background: null,

    $disabled-text-color: null,
    $disabled-background: null,
    $disabled-border-color: null,

    $hover-text-color: null,
    $hover-background: null,
    $hover-border-color: null,

    $focus-text-color: null,
    $focus-background: null,
    $focus-border-color: null,
    $focus-outline-color: null,

    $selected-text-color: null,
    $selected-background: null,
    $selected-border-color: null,

    $hover-selected-text-color: null,
    $hover-selected-background: null,
    $hover-selected-border-color: null,

    $focus-selected-text-color: null,
    $focus-selected-background: null,
    $focus-selected-border-color: null,
    $focus-selected-outline-color: null,

    $ghost-shadow: null,
    $remove-icon-color: null,
    $remove-icon-color-focus: null,
    $size: null
) {
    $name: 'igx-chip';
    $selector: 'igx-chip, .igx-chip__ghost';
    $chip-schema: ();

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

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

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

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

    @if not($hover-background) and $background {
        $hover-background: hsl(from var(--background) h s calc(l * 0.9));
    }

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

    @if not($focus-background) and $background {
        $focus-background: hsl(from var(--background) h s calc(l * 0.8));
    }

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

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

    @if $variant != 'indigo' and $variant != 'fluent' {
        @if not($selected-background) and $background {
            $selected-background: var(--background);
        }
    } @else {
        @if not($selected-background) and $background {
            $selected-background: hsl(from var(--background) h s calc(l * 1.1));
        }
    }

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

    @if not($hover-selected-background) and $selected-background {
        $hover-selected-background: hsl(from var(--selected-background) h s calc(l * 0.9));
    }

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

    @if $variant != 'indigo' {
        @if not($focus-selected-background) and $selected-background {
            $focus-selected-background: hsl(from var(--selected-background) h s calc(l * 0.8));
        }
    } @else {
        @if not($focus-selected-background) and $selected-background {
            $focus-selected-background: var(--selected-background);
        }
    }

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

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

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

    @if not($selected-border-color) and $selected-background {
        $selected-border-color: var(--selected-background);
    }

    @if not($hover-selected-border-color) and $hover-selected-background {
        $hover-selected-border-color: var(--hover-selected-background);
    }

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

    @if $variant == 'bootstrap' or $variant == 'indigo' {
        @if not($focus-outline-color) and $focus-background {
            $focus-outline-color: hsla(from var(--focus-background) h s l / 0.4);
        }

        @if not($focus-selected-outline-color) and $focus-selected-background {
            $focus-selected-outline-color: hsla(from var(--focus-selected-background) h s l / 0.4);
        }
    }

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

    @return extend(
        $theme,
        (
            name: $name,
            selector: $selector,
            border-radius: $border-radius,

            text-color: $text-color,
            background: $background,
            border-color: $border-color,
            ghost-background: $ghost-background,

            disabled-text-color: $disabled-text-color,
            disabled-background: $disabled-background,
            disabled-border-color: $disabled-border-color,

            hover-text-color: $hover-text-color,
            hover-background: $hover-background,
            hover-border-color: $hover-border-color,

            focus-text-color: $focus-text-color,
            focus-background: $focus-background,
            focus-border-color: $focus-border-color,

            selected-text-color: $selected-text-color,
            selected-background: $selected-background,
            selected-border-color: $selected-border-color,

            hover-selected-text-color: $hover-selected-text-color,
            hover-selected-background: $hover-selected-background,
            hover-selected-border-color: $hover-selected-border-color,

            focus-selected-text-color: $focus-selected-text-color,
            focus-selected-background: $focus-selected-background,
            focus-selected-border-color: $focus-selected-border-color,

            ghost-shadow: $ghost-shadow,
            remove-icon-color: $remove-icon-color,
            remove-icon-color-focus: $remove-icon-color-focus,
            focus-selected-outline-color: $focus-selected-outline-color,
            focus-outline-color: $focus-outline-color,
            size: $size,
        )
    );
}
