@use '../variables'as *;
@use '../mixins'as *;

$table-cell-divider-color     : $component-border-default;
$table-cell-divider-color-dark: $component-border-default-dark;

$table-stripped-color     : $component-background-shade;
$table-stripped-color-dark: $component-background-shade-dark;

$table-color-default               : $component-background-default;
$table-header-color-accent         : $component-background-accent;
$table-header-color-secondary      : $component-background-secondary;
$table-color-default-dark          : $component-background-default-dark;
$table-header-color-accent-dark    : $component-background-accent-dark;
$table-header-color-secondary-dark : $component-background-secondary-dark;

$table-color-inverse      : $component-color-default;
$table-color-inverse-dark : $component-color-default-dark;

$table-background-inverse     : $component-background-shade;
$table-background-inverse-dark: $component-background-shade-dark;

@mixin table {
    table.#{$prefix}-table {
        width          : 100%;
        border-collapse: separate;
        table-layout   : fixed;
        border-spacing : 0;

        tr {
            th {
                font-weight  : 700;
                padding      : 1em;
                border-bottom: 1px solid $table-cell-divider-color;
                text-align   : left;

                &.#{$prefix}-narrow {
                    width: 50px;
                }

                &.#{$prefix}-wide {
                    width: 300px;
                }
            }

            td {
                @include component-padding;
                text-align: left;
            }
        }

        &.#{$prefix}-default {

            th,
            td {
                background-color: $table-color-default;
            }
        }
    }
}

@mixin table-column-divider {
    table.#{$prefix}-table {
        &.#{$prefix}-column-divider {
            tr {
                th {
                    border-right: 1px solid $table-cell-divider-color;

                    &:last-child {
                        border-right: none;
                    }
                }

                td {
                    border-right: 1px solid $table-cell-divider-color;

                    &:last-child {
                        border-right: none;
                    }
                }
            }
        }

        // Give a row divider
        &.#{$prefix}-divider {
            tr:last-child {
                td {
                    border-bottom: none;
                }
            }

            td {
                border-bottom: 1px solid $table-cell-divider-color;
            }
        }
    }
}

@mixin table-column-divider-dark {
    @include mode-dark {
        table.#{$prefix}-table {
            &.#{$prefix}-column-divider {
                tr {
                    th {
                        border-color: $table-cell-divider-color-dark;
                    }

                    td {
                        border-color: $table-cell-divider-color-dark;
                    }
                }
            }
        }
    }
}

@mixin table-column-divider-print {
    @include mode-print {
        table.#{$prefix}-table {
            &.#{$prefix}-column-divider {
                tr {
                    th {
                        border-color: $component-print-border-color;
                    }

                    td {
                        border-color: $component-print-border-color;
                    }
                }
            }
        }
    }
}

@mixin table-header-sticky {
    table.#{$prefix}-table {
        &.#{$prefix}-first-column-sticky {
            th {
                position: sticky;
                top     : 0;
                z-index : 1;

                &:first-child {
                    z-index: 2;
                    left   : 0;
                }
            }

            tr {
                td:first-child {
                    position: sticky;
                    left    : 0;
                    z-index : 0;
                }
            }
        }

        tr.#{$prefix}-row-sticky {

            td,
            th {
                position: sticky;
                top     : 0;
                z-index : 1;
            }
        }

        th,
        td {
            &.#{$prefix}-cell-sticky-top {
                position: sticky;
                top     : 0;
                z-index : 2;
            }

            &.#{$prefix}-cell-sticky-left {
                position: sticky;
                left    : 0;
                z-index : 1;
            }
        }

        th.#{$prefix}-cell-sticky {
            position: sticky;
            top     : 0;
            left    : 0;
            z-index : 2;
        }

        td.#{$prefix}-cell-sticky {
            position: sticky;
            left    : 0;
            z-index : 1;
        }
    }
}

@mixin table-header-inverse {
    table.#{$prefix}-table {
        &.#{$prefix}-header-inverse {
            th {
                background-color: $table-background-inverse;

                &:not(.#{$prefix}-accent):not(.#{$prefix}-secondary) {
                    color: $table-color-inverse;
                }
            }
        }
    }
}

@mixin table-header-inverse-dark {
    @include mode-dark {
        table.#{$prefix}-table {
            &.#{$prefix}-header-inverse {
                th {
                    background-color: $table-background-inverse-dark;

                    &:not(.#{$prefix}-accent):not(.#{$prefix}-secondary) {
                        color: $table-color-inverse-dark;
                    }
                }
            }
        }
    }
}

@mixin table-header-inverse-print {
    @include mode-print {
        table.#{$prefix}-table {
            &.#{$prefix}-header-inverse {
                th {
                    background-color: $component-print-background;
                }
            }
        }
    }
}

@mixin table-align {
    table.#{$prefix}-table {
        tr {
            th {
                &.#{$prefix}-center {
                    text-align: center;
                }

                &.#{$prefix}-right {
                    text-align: right;
                }

                &.#{$prefix}-nowrap {
                    max-width    : 0;
                    text-overflow: ellipsis;
                    white-space  : nowrap;
                    overflow     : hidden;
                }
            }

            td {
                &.#{$prefix}-center {
                    text-align: center;
                }

                &.#{$prefix}-right {
                    text-align: right;
                }

                &.#{$prefix}-nowrap {
                    max-width    : 0;
                    text-overflow: ellipsis;
                    white-space  : nowrap;
                    overflow     : hidden;
                }
            }
        }
    }
}

@mixin table-theme {
    table.#{$prefix}-table {
        tr {
            th {
                &.#{$prefix}-accent {
                    color: $table-header-color-accent;
                }

                &.#{$prefix}-secondary {
                    color: $table-header-color-secondary;
                }
            }

            td {
                &.#{$prefix}-accent {
                    color: $table-header-color-accent;
                }

                &.#{$prefix}-secondary {
                    color: $table-header-color-secondary;
                }
            }
        }
    }
}

@mixin table-theme-dark {
    @include mode-dark {
        table.#{$prefix}-table {

            tr {
                th {
                    &.#{$prefix}-accent {
                        color: $table-header-color-accent-dark;
                    }

                    &.#{$prefix}-secondary {
                        color: $table-header-color-secondary-dark;
                    }
                }

                td {
                    &.#{$prefix}-accent {
                        color: $table-header-color-accent-dark;
                    }

                    &.#{$prefix}-secondary {
                        color: $table-header-color-secondary-dark;
                    }
                }
            }
        }
    }
}

@mixin table-theme-print {
    @include mode-print {
        table.#{$prefix}-table {
            tr {
                th {
                    &.#{$prefix}-accent {
                        color: $component-print-color;
                    }

                    &.#{$prefix}-secondary {
                        color: $component-print-color;
                    }
                }

                td {
                    &.#{$prefix}-accent {
                        color: $component-print-color;
                    }

                    &.#{$prefix}-secondary {
                        color: $component-print-color;
                    }
                }
            }
        }
    }

}

@mixin table-stripped {
    table.#{$prefix}-table {
        &.#{$prefix}-stripped {
            tr:nth-child(even) {
                background-color: $table-stripped-color;
            }
        }
    }
}

@mixin table-stripped-dark {
    @include mode-dark {
        table.#{$prefix}-table {
            &.#{$prefix}-stripped {
                tr:nth-child(even) {
                    background-color: $table-stripped-color-dark;
                }
            }
        }
    }
}

@mixin table-stripped-print {
    @include mode-print {
        table.#{$prefix}-table {
            &.#{$prefix}-stripped {
                tr:nth-child(even) {
                    background-color: $component-print-background;
                }
            }
        }
    }
}

@mixin table-dark {
    @include mode-dark {
        table.#{$prefix}-table {
            &.#{$prefix}-default {

                th,
                td {
                    background-color: $table-color-default-dark;
                }
            }

            &.#{$prefix}-divider {
                td {
                    border-color: $table-cell-divider-color-dark;
                }
            }


            tr {
                &.#{$prefix}-divider {
                    td {
                        border-color: $table-cell-divider-color-dark;
                    }
                }

                th {
                    border-color: $table-cell-divider-color-dark;
                }
            }
        }
    }
}

@mixin table-print {
    @include mode-print {
        table.#{$prefix}-table {
            &.#{$prefix}-divider {

                td {
                    border-color: $component-print-border-color;
                    color       : $component-print-color;
                }
            }

            tr {
                color: $component-print-color;

                &.#{$prefix}-divider {
                    td {
                        border-color: $component-print-border-color;
                        color       : $component-print-color
                    }
                }

                th {
                    border-color: $component-print-border-color;
                    color       : $component-print-color;
                }

            }
        }
    }
}

@mixin definitions($options:()) {
    @include table();

    @if(not-option-disabled($options, 'dark')) {
        @include table-dark()
    }

    @if(not-option-disabled($options, 'print')) {
        @include table-print();
    }

    @if(not-option-disabled($options, 'stripped')) {
        @include table-stripped();

        @if(not-option-disabled($options, 'dark')) {
            @include table-stripped-dark()
        }

        @if(not-option-disabled($options, 'print')) {
            @include table-stripped-print();
        }
    }

    @if(not-option-disabled($options, 'theme')) {
        @include table-theme();

        @if(not-option-disabled($options, 'print')) {
            @include table-theme-print();
        }

        @if(not-option-disabled($options, 'dark')) {
            @include table-theme-dark();
        }
    }

    @if(not-option-disabled($options, 'align')) {
        @include table-align();
    }

    @if(not-option-disabled($options, 'sticky')) {
        @include table-header-sticky();
    }

    @if(not-option-disabled($options, 'inverse')) {
        @include table-header-inverse();

        @if(not-option-disabled($options, 'dark')) {
            @include table-header-inverse-dark();
        }

        @if(not-option-disabled($options, 'print')) {
            @include table-header-inverse-print();
        }
    }

    @if(not-option-disabled($options, 'divider')) {
        @include table-column-divider();

        @if(not-option-disabled($options, 'dark')) {
            @include table-column-divider-dark();
        }

        @if(not-option-disabled($options, 'print')) {
            @include table-column-divider-print();
        }
    }
}