@use 'sass:math';
@use 'ngx-tethys/styles/variables.scss';
@use '../../../styles/variables.scss' as theVariables;
@use './mixins.scss' as mixins;

$expand-size: 12px;
$controls-border: 1px solid variables.$gray-300;
$table-scroll-wrap-padding-top: 31px;
$dot-z-index: 20;
$dot-size: 19px;
$dot-padding-top: 5px;
$dot-shadow-top: math.div($dot-size - 1, 2);
$table-scroll-padding: 2px;
$control-width: 11px;
$control-corner-width: 12px;
$header-cell-bg-color: #f3f3f3;
$top-cell-z-index: 13;

.slate-block-card-table {
    z-index: 0;
}

.slate-element-table {
    display: block;
    width: calc(100% - 2px);
    .the-table {
        box-sizing: border-box;
        width: 100%;
        margin: 13px 8px 0 0;
        margin-right: 0;
        margin-left: 0;
        overflow: visible;
        word-wrap: break-word;
        table-layout: fixed;
        border-bottom: $controls-border;
        border-collapse: unset;
        border-spacing: 0;

        td {
            position: relative;
            padding: 8px;
            vertical-align: top;
            word-wrap: break-word;
            border-top: $controls-border;
            border-right: $controls-border;
            cursor: text;

            &.the-sticky-cell {
                &:not(.focused-cell):not(.selected-cell):after {
                    position: absolute;
                    top: 0;
                    right: -1px;
                    z-index: $top-cell-z-index;
                    display: block;
                    width: 1px;
                    height: 100%;
                    background: variables.$gray-300;
                    content: '';
                }
                &.the-table-left-shadow {
                    z-index: 12;
                    @include mixins.tableLeftShadow;
                    &:before {
                        top: 0;
                        right: -8px;
                        left: unset;
                        z-index: 0;
                        height: 100%;
                    }
                }
                &.the-table-left-shadow.focused-cell {
                    z-index: $top-cell-z-index;
                }
            }

            &.selected-cell,
            &.focused-cell:not(.selected-cell):not(.dangerous-cell) {
                &::after {
                    @include mixins.cellInset;
                    z-index: -1;
                    width: unset;
                    height: unset;
                    background: unset;
                    border: 1px solid variables.$primary;
                }
            }

            &.selected-cell {
                &::after {
                    background: rgba($color: variables.$primary, $alpha: 0.1);
                }
            }

            &.dangerous-cell {
                &::after {
                    @include mixins.cellInset;
                    z-index: 10;
                    background: rgba($color: variables.$danger, $alpha: 0.1);
                    border: 1px solid variables.$danger;
                }
            }

            &.slate-element-table-cell {
                margin: 0;
            }
        }

        .focused-cell,
        .selected-cell {
            z-index: 10;
        }

        .the-header-cell {
            background: $header-cell-bg-color;
        }

        .the-sticky-cell {
            position: sticky;
            left: 0;
        }

        tbody {
            tr {
                min-height: 41px;
                td:first-child {
                    border-left: $controls-border;
                }
            }
        }
    }
    &.the-table-selection-hide ::selection {
        background: transparent;
    }

    &:not(.the-table-disabled-horizontal-scrollbar-fixed) {
        .the-table-container {
            .the-table-wrapper {
                scrollbar-width: none;
                &::-webkit-scrollbar {
                    display: none;
                }
            }
        }
    }

    &.the-table-disabled-horizontal-scrollbar-fixed {
        .the-table-container {
            .the-table-horizontal-scrollbar-container {
                display: none;
            }
        }
    }

    .the-table-container {
        position: relative;
        box-sizing: border-box;
        margin: 0 auto 16px;
        .the-table-wrapper {
            position: relative;
            margin-top: -10px;
            padding-top: 10px;
            overflow-x: auto;
            overflow-y: auto;
        }

        .the-table-row-controls-wrapper {
            position: absolute;
            top: 23px;
            left: -$control-width;
            user-select: none;
        }

        .the-table-horizontal-scrollbar-container {
            position: absolute;
            left: 0;
            height: 16px;
            width: 100%;
            overflow-x: auto;
            overflow-y: hidden;
            transition: opacity 0.3s ease;
            background: transparent;

            &.fixed {
                position: fixed;
                bottom: 0;
                z-index: 9999;
            }

            .the-table-horizontal-scrollbar-content {
                height: 1px;
                background: transparent;
            }
        }
    }

    .header-row-shadow {
        display: none;
    }
    .the-table-container.the-table-right-shadow {
        @include mixins.tableRightShadow;
    }
    &:not(.the-table-with-sticky-column) {
        .the-table-container.the-table-left-shadow {
            @include mixins.tableLeftShadow;
        }
    }
}

.the-table-with-sticky-row {
    .the-sticky-row {
        position: fixed;
        z-index: 120;
        display: grid;
        grid-auto-flow: column;
        box-sizing: content-box;
        padding-top: 8px;
        // border-bottom: $controls-border;
        overflow-x: hidden;
        background: variables.$white;
        box-shadow: 0 4px 4px -2px rgba(0, 0, 0, 0.1);

        td.the-header-cell.selected-cell,
        td.the-header-cell.focused-cell:not(.selected-cell):not(.dangerous-cell) {
            &::after {
                bottom: 0;
            }
        }
    }

    .header-row-shadow {
        position: fixed;
        z-index: 121;
        width: 8px;
        background: -webkit-gradient(linear, left, right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.1));
    }

    .the-table-container.the-table-right-shadow {
        .header-row-right-shadow {
            background: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.1));
            display: block;
        }
    }
    .the-table-container.the-table-left-shadow {
        .header-row-left-shadow {
            background: linear-gradient(to left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.1));
            display: block;
        }
    }

    .the-table-col-controls-wrapper.the-sticky-row {
        display: none;
    }

    &:not(.the-table-with-controls) {
        .the-table-numbered {
            .the-table {
                margin-top: 13px;
            }
        }
    }

    .the-table-row-controls {
        top: 1px;
    }
    &.the-numbered-table {
        .the-table-row-controls {
            top: 1px;
            left: 1px;
        }
        .the-table-corner-controls,
        .the-table-row-control-button-wrap {
            width: 44px;
        }
    }
    &.the-table-with-controls.the-numbered-table {
        .the-table-row-controls-wrapper {
            .the-table-corner-controls {
                width: 74px; // width + insert-mask: 55px + 19px
                .the-table-corner-button {
                    width: 55px; // 按钮宽度 = insert-mask: 55px
                }
            }
        }
        .the-table-row-control-button-wrap {
            &:first-child {
                width: 74px; // width + insert-mask: 55px + 19px
            }
        }
    }

    .the-table-row-controls-inner {
        .the-table-row-control-button-wrap {
            &:first-child {
                position: fixed;
                z-index: $top-cell-z-index;
                margin-top: 8px;
                box-shadow: 0px -8px var(--ds-surface, variables.$white);
            }
        }
    }
    .the-table-corner-controls {
        position: fixed;
        width: 11px;
        height: 11px;
        z-index: $top-cell-z-index;
        .the-table-corner-button {
            height: 11px;
        }
    }
    &.the-table-with-controls {
        .the-table-row-controls-wrapper {
            .the-table-corner-controls {
                padding-left: 30px; // control width + insert-mask: 11px + 19px
                margin-left: -19px; // insert-mask width: 19px
                padding-bottom: 11px; // control height: 11px
                .the-table-corner-button {
                    padding-left: 10px; // 10px + 1px border: 11
                    margin-left: -11px;
                }
            }
        }
        .the-table-row-controls-inner {
            z-index: 4;
            .the-table-row-control-button-wrap {
                &:first-child {
                    padding-left: 19px; // insert-mask width: 19px
                    margin-left: -19px;
                    background: variables.$white;
                    .the-table-controls-insert-wrapper {
                        left: 0;
                    }
                }
            }
        }
    }
}

.the-numbered-table:not(.the-table-with-sticky-column) {
    .the-table-container {
        &.the-table-left-shadow {
            &::before {
                left: 44px;
            }
        }
    }
}

.the-numbered-table {
    .the-table-container .the-table-wrapper {
        margin-left: -44px;
        padding-left: 44px;
    }
}

.the-table-with-controls {
    .the-table {
        margin-top: 13px;
    }
    .the-table-row-controls,
    .the-table-controls-insert-wrapper {
        display: block;
    }
    .the-table-corner-controls {
        .the-table-corner-button {
            position: unset;
        }
        .the-table-corner-controls-insert-column-marker {
            position: relative;
            .the-table-insert-mark {
                position: absolute;
                top: -11px;
            }
        }
    }

    .the-table-col-controls-wrapper .the-table-col-control {
        background: variables.$gray-100;
        background-clip: padding-box;
        border-top: $controls-border;
        border-right: $controls-border;
        cursor: pointer;

        &:first-child {
            border-left: $controls-border;
        }

        &:last-child {
            .the-table-controls-insert-wrapper {
                right: 0;

                &:after {
                    left: 9px;
                }
            }

            .the-table-controls-insert-wrapper:hover {
                &:after {
                    left: 0;
                }
            }
        }

        &.control-active,
        &.dangerous {
            &::before,
            &::after {
                position: absolute;
                top: -1px;
                width: 1px;
                height: 12px;
                background: variables.$primary;
                content: '';
            }

            &::before {
                left: -1px;
            }

            &::after {
                right: -1px;
            }
        }

        &.control-active {
            @include mixins.controlSelected;
            z-index: 11;
        }

        &.dangerous {
            @include mixins.controlDangerous;
            z-index: 11;

            &::before,
            &::after {
                background: variables.$danger;
            }
        }

        &.the-sticky-cell:not(.avtive) {
            &:not(.active):not(.danger) {
                &:after {
                    position: absolute;
                    top: 0;
                    right: -1px;
                    z-index: $top-cell-z-index;
                    display: block;
                    width: 1px;
                    height: 100%;
                    background: variables.$gray-300;
                    content: '';
                }
            }
            &.the-table-left-shadow {
                z-index: $top-cell-z-index;
                @include mixins.tableLeftShadow;
                &:before {
                    top: 0;
                    right: -8px;
                    left: unset;
                    z-index: 0;
                    height: 100%;
                }
            }
        }

        &.the-top-cell {
            z-index: $top-cell-z-index;
        }
    }
    .the-table-container {
        &.the-table-right-shadow::after {
            top: $expand-size + 10px + 1;
        }

        .the-table-wrapper {
            margin-left: -11px;
            padding-left: 11px;
        }
    }
    &:not(.the-table-with-sticky-column) {
        .the-table-container {
            &.the-table-left-shadow::before {
                top: $expand-size + 10px + 1;
            }

            &.the-table-left-shadow {
                .the-table-row-controls-wrapper {
                    &::after {
                        top: 0;
                    }
                }
            }
        }
    }

    &.the-numbered-table {
        .the-table-wrapper {
            margin-left: -55px;
            padding-left: 55px;
        }

        .the-table-row-controls {
            width: 55px;
            margin-left: 0;
        }

        .the-table-corner-controls-insert-column-marker .the-table-controls-insert-wrapper {
            left: 46px;
        }
    }

    &.the-table-with-sticky-row {
        .slate-element-table-row.the-sticky-row {
            z-index: 115;
            padding-top: 0;
        }
        .the-table-row-controls-inner {
            .the-table-row-control-button-wrap {
                &:first-child {
                    margin-top: 36px;
                }
            }
        }
        &.the-numbered-table {
            .the-table-row-controls {
                left: 0;
            }
            .the-table-corner-controls,
            .the-table-row-control-button-wrap {
                width: 55px;
            }
        }
        .the-table-col-controls-wrapper.the-sticky-row {
            display: grid;
        }
        .the-table-col-controls-wrapper.the-sticky-row,
        .the-table-corner-controls {
            padding-top: 25px;
            background: variables.$white;
            border: none;
            box-shadow: none;
        }
    }
}

.the-table-corner-controls {
    position: relative;
    width: 100%;
    height: $control-corner-width;
    visibility: hidden;
    z-index: 11;

    .the-table-corner-button {
        position: absolute;
        top: 0;
        left: 0;
        box-sizing: border-box;
        width: 100%;
        height: 11px;
        padding: 0;
        background: variables.$gray-100;
        border: $controls-border;
        border-right: none;
        border-bottom: none;
        cursor: pointer;
    }

    .the-table-corner-controls-insert-row-marker .the-table-controls-insert-wrapper {
        top: 2px;
        left: -$dot-size;
    }

    .the-table-corner-controls-insert-column-marker .the-table-controls-insert-wrapper {
        top: -$dot-size;
        left: 2px;
    }

    &.control-active {
        .the-table-corner-button {
            @include mixins.controlSelected;
        }
    }

    &.dangerous {
        .the-table-corner-button {
            @include mixins.controlDangerous;
        }
    }
}

.the-table-controls-insert-wrapper {
    position: absolute;
    top: -19px;
    right: -10px;
    display: none;
    width: $dot-size;
    height: $dot-size;
    cursor: pointer;

    &::after,
    &::before {
        position: absolute;
        transform: scale(0.16);
        content: '';
    }

    &::before {
        top: $dot-padding-top;
        left: $dot-shadow-top;
        z-index: 3;
        width: 1px;
        height: $dot-size - $dot-padding-top * 2;
        background-color: transparent;
    }

    &::after {
        top: 0;
        left: 0;
        width: $dot-size;
        height: $dot-size;
        background-color: #d8d8d8;
        border-radius: math.div($dot-size + 1, 2);
        box-shadow:
            inset $dot-padding-top 0 0 0 #d8d8d8,
            inset (-$dot-padding-top) 0 0 0 #d8d8d8,
            inset 0 (-$dot-shadow-top) 0 0 #d8d8d8,
            inset 0 $dot-shadow-top 0 0 #d8d8d8;
    }

    &:not(.disabled) {
        &:hover:after,
        &:hover:before {
            background-color: variables.$white !important;
            transform: scale(1);
        }

        &:hover:after {
            border-radius: 4px;
            box-shadow:
                inset $dot-padding-top 0 0 0 variables.$primary,
                inset (-$dot-padding-top) 0 0 0 variables.$primary,
                inset 0 (-$dot-shadow-top) 0 0 variables.$primary,
                inset 0 $dot-shadow-top 0 0 variables.$primary !important;
        }

        &:hover:before {
            opacity: 1;
        }
    }

    .the-table-controls-insert-line {
        position: fixed;
        z-index: $dot-z-index;
        background-color: variables.$primary;
    }
}

.the-table-row-controls {
    position: relative;
    display: none;
    box-sizing: border-box;
    width: $control-width;
    z-index: 11;

    .the-table-row-controls-inner {
        display: flex;
        flex-direction: column;
    }

    .the-table-row-control-button-wrap {
        position: relative;
        margin-top: -1px;

        .the-table-controls-button {
            display: block;
            box-sizing: border-box;
            width: $control-width;
            height: 42px;
            padding: 0;
            background-color: variables.$gray-100;
            border: 1px solid variables.$gray-300;
            border-right: none;
            outline: none;
            cursor: pointer;
        }

        &.control-active {
            z-index: 1;

            .the-table-controls-button,
            .the-table-numbered-controls-button {
                @include mixins.controlSelected;
            }
        }

        &.dangerous {
            z-index: 1;

            .the-table-controls-button,
            .the-table-numbered-controls-button {
                @include mixins.controlDangerous;
            }
        }
    }

    .the-table-controls-insert-wrapper {
        top: auto;
        bottom: -$dot-shadow-top;
        left: -$dot-size;
    }
}

.the-table-col-controls-wrapper {
    overflow-y: hidden;
    height: $control-width;
    line-height: $control-width;

    .the-table-col-control {
        position: relative;
        margin: 0;
        padding: 0;
        border: 0;
        border-top: 1px solid transparent;
    }
}

.table-options-panel {
    .thy-popover-container {
        min-width: 200px;
    }
}

.the-numbered-table {
    .the-table-container {
        padding-left: 44px;
    }

    .the-table-row-controls {
        top: 0px;
        display: block;
        width: 45px;
        margin-left: 10px;
        cursor: pointer;

        .the-table-numbered-controls-button {
            background-color: variables.$gray-100;
            border: $controls-border;
            border-right: none;
        }
    }

    .row-number {
        justify-content: center;
        float: right;
        width: 45px;
        height: 100%;
        margin: 0;
    }
}

.safari {
    .the-table-row-controls {
        top: 1px;
    }
}

.the-editor-readonly {
    .the-table-row-controls,
    .the-table-col-control {
        cursor: default;
    }

    .safari {
        .the-table-row-controls {
            top: 0px;
        }
    }
}

/* table readonly mode */
.the-editor-readonly {
    .the-numbered-table {
        .the-table-row-controls-wrapper {
            display: none;
        }
        .the-table-container {
            padding-left: 0;
        }
        .the-table-number-col {
            position: sticky;
            left: 0;
            width: 45px;
        }
        .the-table-number-column {
            position: sticky !important;
            left: 0;
            z-index: $top-cell-z-index;
            background-color: variables.$gray-100;
            text-align: center;
        }
        // 存在序列号、标题列时，正确设置标题列 sticky left
        .the-table-numbered .the-sticky-cell {
            left: 45px;
            z-index: $top-cell-z-index;
        }

        // 滑动时阴影偏移到序号列 td 上
        &:not(.the-table-with-sticky-column) {
            .the-table-container.the-table-left-shadow {
                &::before {
                    left: 45px;
                }
            }
        }
        .the-table-wrapper {
            margin-left: 0;
            padding-left: 0;
        }
    }
}
