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

@function contained-icon-button-theme(
    $schema: $light-material-schema,

    $background: null,
    $foreground: null,
    $shadow-color: null,

    $hover-background: null,
    $hover-foreground: null,

    $focus-background: null,
    $focus-foreground: null,

    $focus-hover-background: null,
    $focus-hover-foreground: null,

    $active-background: null,
    $active-foreground: null,

    $border-radius: null,
    $border-color: null,
    $focus-border-color: null,

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

    $size: null
) {
    $name: 'igx-contained-icon-button';
    $icon-button-schema: ();

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

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

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

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

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

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

    @if not($active-foreground) and $foreground {
        $active-foreground: var(--foreground);
    }

    @if $variant == 'indigo' {
        @if not($foreground) and $background {
            $foreground: hsla(from adaptive-contrast(var(--background)) h s l / 0.8);
        }

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

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

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

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

        @if not($active-background) and $background {
            $active-background: hsl(from var(--background) h s calc(l * 1.1));
        }
    } @else {
        @if not($foreground) and $background {
            $foreground: adaptive-contrast(var(--background));
        }

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

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

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

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

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

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

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

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

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

    @if $variant == 'fluent' {
        @if not($focus-border-color) and $foreground {
            $focus-border-color: var(--foreground);
        }
    } @else {
        @if not($focus-border-color) and $border-color {
            $focus-border-color: hsl(from var(--border-color) h s calc(l * 0.8));
        }
    }

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

        @if not($disabled-foreground) and $foreground {
            $disabled-foreground: hsla(from var(--foreground) h s l / 0.5);
        }

        @if not($focus-border-color) and $focus-background {
            $focus-border-color: hsla(from var(--focus-background) h s l / 0.3);
        }
    } @else if $variant == 'bootstrap' {
        @if not($disabled-background) and $background {
            $disabled-background: hsla(from var(--background) h s l / 0.2);
        }

        @if not($disabled-foreground) and $disabled-background {
            $disabled-foreground: hsla(from var(--disabled-background) h s l / 0.7);
        }
    }

    @return extend(
        $theme,
        (
            name: $name,
            background: $background,
            foreground: $foreground,
            shadow-color: $shadow-color,

            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,

            active-background: $active-background,
            active-foreground: $active-foreground,

            border-radius: $border-radius,
            border-color: $border-color,
            focus-border-color: $focus-border-color,

            disabled-background: $disabled-background,
            disabled-foreground: $disabled-foreground,
            disabled-border-color: $disabled-border-color,
            size: $size,
        )
    );
}
