@use 'sass:map';
@use 'sass:meta';
@use '../../base' as *;
@use '../../themes/schemas' as *;

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

/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
/// @param {Color} $background-color [null] - The summaries background color is inherited form igx-grid__tfoot
/// @param {Color} $focus-background-color [null] - The background color when a summary item is on focus.
/// @param {Color} $label-color [null] - The summaries label color.
/// @param {Color} $result-color [null] - The summaries value/result color.
/// @param {Color} $border-color [null] - The summaries border color.
/// @param {Color} $pinned-border-width [null] - The border width of the summary panel.
/// @param {Color} $pinned-border-style [null] - The border style of the summary panel.
/// @param {Color} $pinned-border-color [null] - The border color of the summary panel.
/// @param {Color} $label-hover-color [null] - The summaries hover label color.
/// @requires $light-material-schema
/// @example scss Change the summaries background and labels color
///   $my-summary-theme: grid-summary-theme(
///     $background-color: black,
///     $label-color: white
///   );
///   // Pass the theme to the css-vars mixin
///   @include css-vars($my-summary-theme);
@function grid-summary-theme(
    $schema: $light-material-schema,

    $background-color: null,
    $focus-background-color: null,
    $label-color: null,
    $result-color: null,
    $border-color: null,
    $pinned-border-width: null,
    $pinned-border-style: null,
    $pinned-border-color: null,
    $label-hover-color: null
) {
    $name: 'igx-grid-summary';
    $selector: '.igx-grid-summary';
    $grid-summary-schema: ();

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

    $theme: digest-schema($grid-summary-schema);
    $meta: map.get($theme, '_meta');

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

    @if not($border-color) and $background-color {
        @if type-of($background-color) == 'color' {
            $border-color: rgba(text-contrast($background-color), .26);
        }
    }

    @if not($pinned-border-color) and $background-color {
        @if type-of($background-color) == 'color' {
            $pinned-border-color: rgba(text-contrast($background-color), .26);
        }
    }

    @return extend($theme, (
        name: $name,
        selector: $selector,
        background-color: $background-color,
        focus-background-color: $focus-background-color,
        label-color: $label-color,
        result-color: $result-color,
        border-color: $border-color,
        pinned-border-width: $pinned-border-width,
        pinned-border-style: $pinned-border-style,
        pinned-border-color: $pinned-border-color,
        label-hover-color: $label-hover-color,
        theme: map.get($schema, '_meta', 'theme'),
        _meta: map.merge(if($meta, $meta, ()), (
            variant: map.get($schema, '_meta', 'theme')
        )),
    ));
}

/// @deprecated Use the `css-vars` mixin instead.
/// @see {mixin} css-vars
/// @param {Map} $theme - The theme used to style the component.
@mixin grid-summary($theme) {
    @include css-vars($theme);

    $variant: map.get($theme, '_meta', 'variant');

    $cell-pin: (
        style: var-get($theme, 'pinned-border-width') var-get($theme, 'pinned-border-style'),
        color: var-get($theme, 'pinned-border-color')
    );

    $item-padding-block: (
        comfortable: rem(6px),
        cosy: rem(2px),
        compact: 0
    );

    $summary-padding-inline: (
        comfortable: rem(24px),
        cosy: rem(16px),
        compact: rem(12px)
    );

    %igx-grid-summary {
        position: relative;
        display: flex;
        flex-direction: column;
        flex: 1 1 0%;
        padding-block: 0;

        @if $variant != 'indigo' {
            padding-inline: pad-inline(map.get($summary-padding-inline, 'compact'), map.get($summary-padding-inline, 'cosy'), map.get($summary-padding-inline, 'comfortable'));
        } @else {
            padding-inline: pad-inline(rem(8px), rem(12px), rem(16px));
        }

        background: var-get($theme, 'background-color', inherit);
        overflow: hidden;
        outline-style: none;

        &::after {
            position: absolute;
            inset: 0;
        }

        &:focus::after {
            background: var-get($theme, 'focus-background-color');
        }

        @if $variant == 'indigo' {
            border-top: rem(1px) solid var-get($theme, 'border-color');
        }
    }

    %igx-grid-summary--pinned {
        position: relative;
        z-index: 1;
    }

    %grid-summary--fixed-width {
        flex-grow: 0;
    }

    %igx-grid-summary--pinned-last {
        border-inline-end: map.get($cell-pin, 'style') map.get($cell-pin, 'color');
        @media print {
            border-inline-end: map.get($cell-pin, 'style') #999;
        }
    }

    %igx-grid-summary--pinned-first {
        border-inline-start: map.get($cell-pin, 'style') map.get($cell-pin, 'color');
        @media print {
            border-inline-start: map.get($cell-pin, 'style') #999;
        }
    }

    %igx-grid-summary__item {
        display: flex;
        align-items: center;
        padding-block: pad(map.get($item-padding-block, 'compact'), map.get($item-padding-block, 'cosy'), map.get($item-padding-block, 'comfortable'));
        padding-inline: 0;

        @if $variant != 'indigo' {
            font-size: rem(12px);
        } @else {
            min-height: sizable(rem(24px), rem(30px), rem(36px));
        }

        position: relative;
    }

    %igx-grid-summary__label {
        color: var-get($theme, 'label-color');
        min-width: rem(30px);
        margin-inline-end: rem(3px);

        @if $variant == 'indigo' {
            @include type-style('caption');

            margin-inline-end: initial;
        }

        &:hover {
            color: var-get($theme, 'label-hover-color');
        }
    }

    %igx-grid-summary__result {
        @if $variant == 'indigo' {
            @include type-style('detail-2', false);
        }

        color: var-get($theme, 'result-color');

        @if $variant != 'indigo' {
            font-weight: 600;
        }

        flex: 1 1 auto;
        text-align: end;
    }

    %igx-grid-summary__label,
    %igx-grid-summary__result {
        @include ellipsis();
    }
}
