@use '../../styles/bootstrap/mixins/border-radius';
@use '../../styles/mixins/hover';
@use '../../styles/bootstrap/functions-variables';
@use '../../styles/bootstrap/variables' as bootstrap-variables;
@use '../../styles/mixins/shadow';
@use '../../styles/variables';
@use './button.scss';
@use 'mixin';
// stylelint-disable selector-no-qualifying-type

// Make the div behave like a button
.btn-group,
.btn-group-vertical {
    position: relative;
    display: inline-flex;
    vertical-align: middle; // match .btn alignment given font-size hack above

    > .btn {
        position: relative;
        flex: 0 1 auto;
        // Bring the hover, focused, and "active" buttons to the front to overlay
        // the borders properly
        @include hover.hover {
            z-index: 1;
        }
        &:focus,
        &:active,
        &.active {
            z-index: 1;
        }

        i,
        .wtf,
        .thy-icon {
            vertical-align: middle;
            margin-top: -0.14rem; // -0.125rem;
        }
    }

    // Prevent double borders when buttons are next to each other
    .btn + .btn,
    .btn + .btn-group,
    .btn-group + .btn,
    .btn-group + .btn-group {
        margin-left: -(bootstrap-variables.$btn-border-width);
    }
}

.btn-group.btn-group-clear-min-width {
    .btn {
        min-width: unset;
    }
}

// Optional: Group multiple button groups together for a toolbar
.btn-toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;

    .input-group {
        width: auto;
    }
}

.btn-group {
    > .btn:first-child {
        margin-left: 0;
    }

    // Reset rounded corners
    > .btn:not(:last-child):not(.dropdown-toggle),
    > .btn-group:not(:last-child) > .btn {
        @include border-radius.border-right-radius(0);
    }

    > .btn:not(:first-child),
    > .btn-group:not(:first-child) > .btn {
        @include border-radius.border-left-radius(0);
    }
}

// Sizing（保留给 radio-group 等仍使用 btn-group-{size} 的场景；thy-button-group 已改为同步子按钮 size）

.btn-group-xs > .btn {
    @extend .btn-xs;
    padding: variables.$btn-group-btn-padding-y-xs variables.$btn-group-btn-padding-x-xs;
    font-size: variables.$font-size-xs;
    &.thy-btn-icon-only {
        @include mixin.btn-icon-only-variant(variables.$btn-group-btn-padding-y-xs, 0.25rem);
    }
}
.btn-group-sm > .btn {
    @extend .btn-sm;
    padding: variables.$btn-group-btn-padding-y-sm variables.$btn-group-btn-padding-x-sm;
    &.thy-btn-icon-only {
        @include mixin.btn-icon-only-variant(variables.$btn-group-btn-padding-y-sm, 0.375rem);
    }
}
.btn-group-md > .btn {
    @extend .btn-md;
    padding: variables.$btn-group-btn-padding-y-md variables.$btn-group-btn-padding-x-md;
    &.thy-btn-icon-only {
        @include mixin.btn-icon-only-variant(variables.$btn-group-btn-padding-y-md, variables.$btn-icon-only-padding-x-md);
    }
}
.btn-group-lg > .btn {
    @extend .btn-lg;
    padding: variables.$btn-group-btn-padding-y-lg variables.$btn-group-btn-padding-x-lg;
    &.thy-btn-icon-only {
        @include mixin.btn-icon-only-variant(variables.$btn-group-btn-padding-y-lg, 0.625rem);
    }
}

// type（thyType 兼容，勿删）

@mixin btn-group-divider {
    position: absolute;
    content: '';
    width: bootstrap-variables.$btn-border-width;
    height: 100%;
    top: 0;
    left: 0px;
    background: rgba(variables.$white, 0.3);
}

@each $color, $value in functions-variables.$theme-colors {
    .btn-group-#{$color} > .btn {
        @extend .btn-#{$color};
        // 按钮中间的分割线
        &:not(:first-child) {
            &::before {
                @include btn-group-divider;
            }
        }
    }

    .btn-group-#{$color} {
        .btn:not(:first-child) {
            &::before {
                @include btn-group-divider;
            }
        }
    }

    .btn-group-outline-#{$color} > .btn {
        @extend .btn-outline-#{$color};
        &:hover {
            @include shadow.box-shadow(none);
        }
    }
}
.btn-group-outline-default > .btn {
    @extend .btn-outline-default;
    &:hover {
        @include shadow.box-shadow(none);
    }
}

// thyAppearance="fill"：实心色按钮组中间分割线
.btn-group-fill {
    @each $color, $value in functions-variables.$theme-colors {
        > .btn-#{$color}:not(:first-child) {
            &::before {
                @include btn-group-divider;
            }
        }
    }
}

// The clickable button for toggling the menu
// Set the same inset shadow as the :active state
.btn-group.show .dropdown-toggle {
    @include shadow.box-shadow(variables.$btn-active-box-shadow);

    // Show no shadow for `.btn-link` since it has no other button styles.
    &.btn-link {
        @include shadow.box-shadow(none);
    }
}

//
// Vertical button groups
//

.btn-group-vertical {
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;

    .btn,
    .btn-group {
        width: 100%;
    }

    > .btn + .btn,
    > .btn + .btn-group,
    > .btn-group + .btn,
    > .btn-group + .btn-group {
        margin-top: -(bootstrap-variables.$btn-border-width);
        margin-left: 0;
    }

    // Reset rounded corners
    > .btn:not(:last-child):not(.dropdown-toggle),
    > .btn-group:not(:last-child) > .btn {
        @include border-radius.border-bottom-radius(0);
    }

    > .btn:not(:first-child),
    > .btn-group:not(:first-child) > .btn {
        @include border-radius.border-top-radius(0);
    }
}

// Checkbox and radio options
//
// In order to support the browser's form validation feedback, powered by the
// `required` attribute, we have to "hide" the inputs via `clip`. We cannot use
// `display: none;` or `visibility: hidden;` as that also hides the popover.
// Simply visually hiding the inputs via `opacity` would leave them clickable in
// certain cases which is prevented by using `clip` and `pointer-events`.
// This way, we ensure a DOM element is visible to position the popover from.
//
// See https://github.com/twbs/bootstrap/pull/12794 and
// https://github.com/twbs/bootstrap/pull/14559 for more information.

.btn-group-toggle {
    > .btn,
    > .btn-group > .btn {
        margin-bottom: 0; // Override default `<label>` value

        input[type='radio'],
        input[type='checkbox'] {
            position: absolute;
            clip: rect(0, 0, 0, 0);
            pointer-events: none;
        }
    }
}
