@use "../../core";
@use "../../core/size";
@use "variables" as *;

$vertical-padding: core.densify(size.$padding-025);
$horizontal-padding: size.$padding-050;
$underline-thickness: 3px;
$editable-min-height: calc(var(--f-font-size-standard) * var(--f-line-height-large) + ($vertical-padding * 2) + ($table-ng-selection-outline * 2));
$editable-margin-block: 2px;
$editable-border-width: 3px;
$cell-border-width: 1px;

.table-ng__cell {
    padding: $vertical-padding $horizontal-padding;
    text-wrap: nowrap;
    border: $cell-border-width solid transparent;

    &:focus-visible,
    &:has(:focus-visible) {
        box-shadow: none;
        outline: $table-ng-selection-outline solid $table-ng-cell-color-outline;
        outline-offset: -$table-ng-selection-outline;
    }

    @media (hover: hover) {
        &:not(&--menu-open):hover:has(button) {
            background: $table-ng-cell-color-hover-background;
        }
    }

    input,
    button,
    a,
    [role="combobox"] {
        &:focus-visible {
            box-shadow: none;
        }
    }

    &--static__text {
        text-wrap: wrap;
    }

    &--align-left {
        text-align: left;

        span,
        input {
            text-align: left;
        }

        .table-ng__cell--static__text {
            justify-content: flex-start;
            text-align: left;
        }
    }

    &--align-right {
        text-align: right;

        span,
        input {
            text-align: right;
        }

        .table-ng__cell--static__text {
            justify-content: flex-end;
            text-align: right;
        }
    }

    &--numeric {
        font-feature-settings: "tnum" 1;
    }

    &--select,
    &--text {
        padding: 0;

        @media (hover: hover) {
            &:hover {
                background: $table-ng-cell-color-hover-background;
            }
        }
    }

    // Select should have visible focus even on click when using focus-visible, but table select is a div.
    &--select {
        &:focus-within {
            box-shadow: none;
            outline: $table-ng-selection-outline solid $table-ng-cell-color-outline;
            outline-offset: -$table-ng-selection-outline;

            .table-ng__editable {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
                border-radius: 0;
            }
        }
    }

    &--text {
        &:focus-visible,
        &:has(:focus-visible) {
            .table-ng__editable {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
                border-radius: 0;
            }
        }

        @media (hover: hover) {
            &:hover:has(.table-ng__textedit:focus-visible) {
                background: transparent;
            }
        }
    }

    .table-ng__editable {
        min-block-size: $editable-min-height;

        &:has(:focus-visible) {
            box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
            border-radius: 0;
            background: transparent;
        }

        @media (hover: hover) {
            &:hover:not(:has(:focus-visible)) {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
                border-radius: 0;
            }
        }
    }

    &--select {
        .table-ng__editable {
            border: $editable-border-width solid transparent;
            box-sizing: border-box;
        }
    }

    &--text {
        &:focus-visible {
            box-shadow: none !important;
        }

        &.table-ng__cell--error:not(:has(:focus-visible)) {
            .table-ng__editable {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-error;
                border-radius: 0;
            }
        }

        @media (hover: hover) {
            &.table-ng__cell--error:hover:not(:has(:focus-visible)) {
                .table-ng__editable {
                    box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-error;
                    border-radius: 0;
                }
            }
        }

        &.table-ng__cell--error:focus-visible {
            .table-ng__editable {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-error;
                border-radius: 0;
            }
        }

        .table-ng__editable {
            border: $editable-border-width solid transparent;
            box-sizing: border-box;
            position: relative;

            &:has(:focus-visible) {
                box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
                border-radius: 0;

                .table-ng__editable__text {
                    visibility: hidden;
                }

                .table-ng__textedit {
                    position: absolute;
                    inset: 0;
                    width: 100%;
                    min-width: 0;
                    padding: $vertical-padding $horizontal-padding;
                }
            }

            @media (hover: hover) {
                &:hover {
                    box-shadow: inset 0 (-$underline-thickness) 0 0 $table-ng-cell-color-underline;
                    border-radius: 0;
                }
            }
        }
    }

    &--checkbox,
    &--radio {
        text-align: center;
        padding: core.densify(size.$padding-025);

        input {
            height: var(--f-icon-size-medium);
            width: var(--f-icon-size-medium);
            vertical-align: middle;
            accent-color: $table-ng-button-primary-color-background-default;
        }

        @media (hover: hover) {
            &:hover {
                background: $table-ng-cell-color-hover-background;
                accent-color: $table-ng-button-primary-color-background-hover;
            }
        }
    }

    &--expand,
    &--button {
        button {
            padding: 0;
            margin: 0;
            border: 0;
            min-width: var(--f-icon-size-medium);
            width: 100%;
            background: inherit;
            color: $table-ng-button-primary-color-background-default;
            cursor: pointer;

            @media (hover: hover) {
                &:hover {
                    color: $table-ng-button-primary-color-background-hover;
                }
            }

            svg {
                vertical-align: middle;
            }
        }
    }

    &--expand {
        width: var(--f-icon-size-medium);
        padding: core.densify(size.$padding-025);
    }

    &--selectable {
        width: var(--f-icon-size-medium);

        @media (hover: hover) {
            &:hover {
                background: $table-ng-cell-color-hover-background;
            }
        }
    }

    &--rowheader {
        font-weight: var(--f-font-weight-medium);
        text-align: left;
    }

    &--static {
        .table-ng__cell--static__text {
            display: flex;
            align-items: center;
            min-block-size: calc($editable-min-height - ($editable-margin-block * 2));
            border: $editable-border-width solid transparent;
        }
    }

    &--custom {
        padding: $vertical-padding $horizontal-padding;
        border: $cell-border-width solid transparent;

        &:focus-visible {
            box-shadow: none;
            outline: $table-ng-selection-outline solid $table-ng-cell-color-outline;
            outline-offset: -$table-ng-selection-outline;
        }
    }

    &--menu-open,
    &--menu-open:hover {
        background: $table-ng-cell-color-active-menu;

        .icon-button {
            color: $table-ng-cell-color-active-menu-text;
        }

        .table-ng__editable:hover {
            background: transparent;
        }
    }
}

.table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) {
    background: transparent;

    .table-ng__editable,
    .table-ng__editable:hover,
    .table-ng__editable:hover:not(:focus-within),
    .table-ng__editable:focus,
    .table-ng__editable:focus-within {
        box-shadow: none;
        border-radius: 0;
    }
}

.table-ng__textedit {
    flex: 0 1 0;
    width: 0;
    border: none;
    padding: 0;
    background: transparent;

    &:focus-visible {
        box-shadow: none !important;
    }
}

.table-ng__editable {
    padding: $vertical-padding $horizontal-padding;
    display: flex;
    align-items: center;
    margin-block: $editable-margin-block;

    &__text {
        flex: 1 1 auto;
        white-space: pre;
        text-wrap: wrap;
    }

    &__numeric {
        span,
        input {
            font-feature-settings: "tnum" 1;
        }
    }
}

@media (forced-colors: active) {
    .table-ng__editable {
        forced-color-adjust: none;
        color: CanvasText;
    }

    .table-ng__cell--static__text {
        forced-color-adjust: none;
        color: CanvasText;
    }

    @media (hover: hover) {
        .table-ng__cell--text:hover .table-ng__editable,
        .table-ng__cell--select:hover .table-ng__editable {
            box-shadow: inset 0 (-$underline-thickness) 0 0 Highlight;
            border-radius: 0;
        }
    }

    .table-ng__cell--text:has(:focus-visible) .table-ng__editable,
    .table-ng__cell--select:focus-within .table-ng__editable {
        box-shadow: inset 0 (-$underline-thickness) 0 0 Highlight;
        border-radius: 0;
    }

    .table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) .table-ng__editable,
    .table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) .table-ng__editable:hover,
    .table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) .table-ng__editable:hover:not(:focus-within),
    .table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) .table-ng__editable:focus,
    .table-ng__cell--select:has([role="combobox"][aria-expanded="true"]) .table-ng__editable:focus-within {
        box-shadow: none;
        border-radius: 0;
    }

    .table-ng [tabindex="0"]:focus-visible {
        outline: $table-ng-selection-outline solid CanvasText;
        outline-offset: -$table-ng-selection-outline;
        background: transparent;
    }

    .table-ng__cell :is(button, a, input, select, [role="combobox"]):focus-visible {
        outline: none;
    }
}
