// (C) 2025-2026 GoodData Corporation

$border-radius: 20px;
$background-color: var(--gd-palette-complementary-0);
$border-color: var(--gd-palette-complementary-4);
$box-shadow: var(--gd-button-dropShadow, 0 -1px 0 0) var(--gd-palette-complementary-5-t40) inset;

$active-background-color: var(--gd-palette-complementary-2);

$delete-border-color: var(--gd-palette-error-base-t50);
$delete-background-color: var(--gd-palette-error-lightest);
$delete-box-shadow: var(--gd-button-dropShadow, 0 -1px 0 0) var(--gd-palette-error-base-t50) inset;

$action-border-color: var(--gd-palette-complementary-4);
$action-background-color: var(--gd-palette-complementary-2);
$action-box-shadow: var(--gd-button-dropShadow, 0 -1px 0 0) var(--gd-palette-complementary-5-t40) inset;

.gd-ui-kit-chip {
    $root: &;

    height: 27px;
    max-width: 245px;

    display: inline-flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    position: relative;

    border-radius: $border-radius;
    border: 1px solid $border-color;
    background-color: $background-color;

    &:has(#{$root}__trigger:hover) {
        border-color: $border-color;

        &::after {
            content: "";
            border-radius: inherit;
            box-shadow: $box-shadow;
            position: absolute;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            pointer-events: none;
        }

        #{$root}__trigger:not(:disabled) {
            background-color: $active-background-color;
        }

        #{$root}__trigger--isDeletable:not(:disabled) {
            border-right: 1px solid $border-color;
        }
        #{$root}__trigger--isDeletable#{$root}__trigger--variant-inactive:not(:disabled) {
            border-right: 1px dashed $border-color;
        }
        #{$root}__trigger--isActionable:not(:disabled) {
            border-right: 1px solid $border-color;
        }
        #{$root}__trigger--isActionable#{$root}__trigger--variant-inactive:not(:disabled) {
            border-right: 1px dashed $border-color;
        }

        &:has(#{$root}__trigger--isLocked:hover) {
            &::after {
                content: none;
            }

            #{$root}__trigger--isLocked:not(:disabled) {
                // not `border: none`: dropping the 1px separator resizes the trigger, and
                // transition-all then animates the label's width and flickers its ellipsis.
                border-color: transparent;
                background-color: $background-color;
            }
        }
    }

    &:has(#{$root}__delete:hover) {
        border-color: $delete-border-color;
        background-color: $delete-background-color;

        &::after {
            box-shadow: $delete-box-shadow;
        }

        #{$root}__trigger:not(#{$root}__trigger--isLocked:disabled) {
            border-color: $delete-border-color;
            border-right: 1px solid $delete-border-color;
        }
        #{$root}__trigger#{$root}__trigger--variant-inactive:not(#{$root}__trigger--isLocked:disabled) {
            border-right: 1px dashed $delete-border-color;
        }

        #{$root}__trigger--isActive {
            background-color: $delete-background-color;
        }
    }

    &:has(#{$root}__action:hover) {
        border-color: $action-border-color;
        background-color: $action-background-color;

        &::after {
            box-shadow: $action-box-shadow;
        }

        #{$root}__trigger:not(#{$root}__trigger--isLocked:disabled) {
            border-color: $action-border-color;
            border-right: 1px solid $action-border-color;
        }
        #{$root}__trigger#{$root}__trigger--variant-inactive:not(#{$root}__trigger--isLocked:disabled) {
            border-right: 1px dashed $action-border-color;
        }

        #{$root}__trigger--isActive {
            background-color: $action-background-color;
        }
    }

    &:has(#{$root}__delete:hover),
    &:has(#{$root}__action:hover) {
        &::after {
            content: "";
            border-radius: inherit;
            position: absolute;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            pointer-events: none;
        }

        #{$root}__delete {
            color: var(--gd-palette-error-base);
        }
        #{$root}__action {
            color: var(--gd-palette-complementary-2);
        }
    }

    &--variant-inactive {
        border-style: dashed;
    }

    #{$root}__trigger--isDeletable {
        border-right: 1px solid transparent;
    }
    #{$root}__trigger--isDeletable#{$root}__trigger--variant-inactive {
        border-right: 1px dashed transparent;
    }
    #{$root}__trigger--hasIconAfter#{$root}__trigger--isDeletable {
        padding-right: 3px;
    }
    #{$root}__trigger--isActionable {
        border-right: 1px solid transparent;
    }
    #{$root}__trigger--isActionable#{$root}__trigger--variant-inactive {
        border-right: 1px dashed transparent;
    }
    #{$root}__trigger--hasIconAfter#{$root}__trigger--isActionable {
        padding-right: 3px;
    }

    &__trigger,
    &__delete,
    &__action {
        color: var(--gd-palette-complementary-7);
        background-color: transparent;
        border-radius: var(--gd-button-borderRadius);
        transition: var(--gd-transition-all);
        display: inline-flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        height: 100%;
        min-width: 0;
        flex-shrink: 1;
        gap: 0;
        text-decoration: none;
        padding: 0 10px;
        border: none;

        &:disabled {
            cursor: default;
            color: var(--gd-palette-complementary-5);
        }
    }

    &__trigger {
        border-radius: $border-radius 0 0 $border-radius;
        border-right: 1px solid transparent;

        &--variant-inactive {
            border-right: 1px dashed transparent;
            color: var(--gd-palette-complementary-6);
        }

        &--isActive {
            color: var(--gd-palette-complementary-8);
            background-color: $active-background-color;
            border-right: 1px solid $border-color;
        }
        &--isActive#{$root}__trigger--variant-inactive {
            border-right: 1px dashed $border-color;
        }

        // No border-radius: a deletable locked chip keeps the left-only rounding so it butts against
        // the delete button. A lone trigger is rounded by the :not(--isDeletable) rule below.
        &--isLocked {
            cursor: default;
            color: var(--gd-palette-complementary-7);

            &:disabled {
                background-color: transparent;
                color: var(--gd-palette-complementary-7);
                border: none;
            }
        }

        &:not(&:disabled):not(#{$root}__trigger--variant-inactive) {
            &:hover,
            &:focus,
            &:active {
                color: var(--gd-palette-complementary-8);

                svg {
                    color: var(--gd-palette-primary-base);
                }
            }
        }

        &:not(&--isDeletable):not(&--isActionable) {
            border-radius: $border-radius;
            border: none;
        }
    }

    &__delete {
        flex-shrink: 0;
        border-radius: 0 $border-radius $border-radius 0;
        padding: 0 7px 0 3px;

        &:not(&:disabled) {
            &:hover,
            &:focus,
            &:active {
                color: var(--gd-palette-complementary-8);

                svg {
                    color: var(--gd-palette-error-base);
                }
            }
        }
    }

    &__action {
        flex-shrink: 0;
        border-radius: 0 $border-radius $border-radius 0;
        padding: 0 7px 0 3px;

        &:not(&:disabled) {
            &:hover,
            &:focus,
            &:active {
                color: var(--gd-palette-complementary-8);

                svg {
                    color: var(--gd-palette-complementary-2);
                }
            }
        }
    }

    // No transition on the text spans: Safari leaves them unpainted when an overlay reveals them.
    &__label {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    &__tag {
        margin-left: 2px;
    }

    &__icon-before {
        display: flex;
        align-items: center;
        justify-content: center;
        transition: var(--gd-transition-all);
        margin-right: 5px;
    }

    &__icon-after {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 6px;
    }

    &__icon-chevron {
        align-items: center;
        justify-content: center;
        transition: var(--gd-transition-all);
        display: none;
        margin-left: 5px;

        #{$root}:hover &,
        #{$root}:active & {
            display: flex;
        }

        &--isActive {
            display: flex;
        }
    }

    &__icon-lock {
        display: flex;
        align-items: center;
        justify-content: center;
        transition: var(--gd-transition-all);

        margin-left: 5px;
    }

    &__icon-delete {
        display: flex;
        align-items: center;
        justify-content: center;
        transition: var(--gd-transition-all);
    }

    &__icon-action {
        display: flex;
        align-items: center;
        justify-content: center;
        transition: var(--gd-transition-all);
    }
}
