@use '../../styles/bootstrap/mixins/border-radius';
@use '../../styles/bootstrap/variables' as bootstrap-variables;
@use '../../styles/variables';
@use '../../form/styles/forms.scss';
// stylelint-disable selector-no-qualifying-type

//
// Base styles
//

.thy-input-group {
    position: relative;
    display: flex;
    flex-wrap: wrap; // For form validation feedback
    align-items: stretch;
    width: 100%;

    > .form-control,
    > .custom-select,
    > .custom-file {
        position: relative; // For focus state's z-index
        flex: 1 1 auto;
        // Add width 1% and flex-basis auto to ensure that button will not wrap out
        // the column. Applies to IE Edge+ and Firefox. Chrome does not require this.
        width: 1%;
        margin-bottom: 0;

        // Bring the "active" form control to the top of surrounding elements
        &:focus {
            z-index: 3;
        }

        + .form-control,
        + .custom-select,
        + .custom-file {
            margin-left: -(bootstrap-variables.$input-border-width);
        }
    }

    > .form-control,
    > .custom-select {
        &:not(:last-child) {
            @include border-radius.border-right-radius(0);
        }
        &:not(:first-child) {
            @include border-radius.border-left-radius(0);
        }
    }

    &-with-textarea-suffix {
        > .form-control {
            min-height: 34px;

            // bottom 6px + group-suffix height 22px
            padding-bottom: 28px;

            // The suffix of textarea uses absolute position, so the right side of textarea needs border-radius
            &:not(:last-child) {
                @include border-radius.border-right-radius(bootstrap-variables.$input-border-radius);
            }
        }

        .input-group-suffix {
            position: absolute;
            right: 0;
            bottom: 6px;
            //  Solve that the input count disappears when focus textarea
            z-index: 3;
        }

        &:not(.disabled) {
            .input-group-suffix {
                background-color: variables.$bg-default;
            }
        }
    }
    &-with-scroll-bar {
        .input-group-suffix {
            right: 14px;
        }
    }

    // Custom file inputs have more complex markup, thus requiring different
    // border-radius overrides.
    > .custom-file {
        display: flex;
        align-items: center;

        &:not(:last-child) .custom-file-label,
        &:not(:last-child) .custom-file-label::before {
            @include border-radius.border-right-radius(0);
        }
        &:not(:first-child) .custom-file-label,
        &:not(:first-child) .custom-file-label::before {
            @include border-radius.border-left-radius(0);
        }
    }
}

// Prepend and append
//
// While it requires one extra layer of HTML for each, dedicated prepend and
// append elements allow us to 1) be less clever, 2) simplify our selectors, and
// 3) support HTML5 form validation.

.input-group-prepend,
.input-group-append {
    display: flex;

    // Ensure buttons are always above inputs for more visually pleasing borders.
    // This isn't needed for `.input-group-text` since it shares the same border-color
    // as our inputs.
    .btn {
        position: relative;
        z-index: 2;
    }

    .btn + .btn,
    .btn + .input-group-text,
    .input-group-text + .input-group-text,
    .input-group-text + .btn {
        margin-left: -(bootstrap-variables.$input-border-width);
    }
}

.input-group-prepend {
    margin-right: -(bootstrap-variables.$input-border-width);
}

.input-group-append {
    margin-left: -(bootstrap-variables.$input-border-width);
}

// Textual addons
//
// Serves as a catch-all element for any text or radio/checkbox input you wish
// to prepend or append to an input.

.input-group-text {
    display: flex;
    align-items: center;
    padding: variables.$input-padding-y variables.$input-padding-x;
    margin-bottom: 0; // Allow use of <label> elements by overriding our default margin-bottom
    font-size: variables.$font-size-base; // Match inputs
    font-weight: bootstrap-variables.$font-weight-normal;
    line-height: bootstrap-variables.$input-line-height;
    color: variables.$input-group-addon-color;
    text-align: center;
    white-space: nowrap;
    background-color: variables.$input-group-addon-bg;
    border: bootstrap-variables.$input-border-width solid bootstrap-variables.$input-group-addon-border-color;
    @include border-radius.border-radius(bootstrap-variables.$input-border-radius);

    // Nuke default margins from checkboxes and radios to vertically center within.
    input[type='radio'],
    input[type='checkbox'] {
        margin-top: 0;
    }

    i {
        line-height: normal;
    }
}

// Sizing
//
// Remix the default form control sizing classes into new ones for easier
// manipulation.

.thy-input-group-lg > .form-control,
.thy-input-group-lg > .input-group-prepend > .input-group-text,
.thy-input-group-lg > .input-group-append > .input-group-text,
.thy-input-group-lg > .input-group-prepend > .btn,
.thy-input-group-lg > .input-group-append > .btn {
    @extend .form-control-lg;
}

.thy-input-group-md > .form-control,
.thy-input-group-md > .input-group-prepend > .input-group-text,
.thy-input-group-md > .input-group-append > .input-group-text,
.thy-input-group-md > .input-group-prepend > .btn,
.thy-input-group-md > .input-group-append > .btn {
    @extend .form-control-md;
}

.thy-input-group-sm > .form-control,
.thy-input-group-sm > .input-group-prepend > .input-group-text,
.thy-input-group-sm > .input-group-append > .input-group-text,
.thy-input-group-sm > .input-group-prepend > .btn,
.thy-input-group-sm > .input-group-append > .btn {
    @extend .form-control-sm;
}

// Prepend and append rounded corners
//
// These rulesets must come after the sizing ones to properly override sm and lg
// border-radius values when extending. They're more specific than we'd like
// with the `.input-group >` part, but without it, we cannot override the sizing.

.thy-input-group > .input-group-prepend > .btn,
.thy-input-group > .input-group-prepend > .input-group-text,
.thy-input-group > .input-group-append:not(:last-child) > .btn,
.thy-input-group > .input-group-append:not(:last-child) > .input-group-text,
.thy-input-group > .input-group-append:last-child > .btn:not(:last-child):not(.dropdown-toggle),
.thy-input-group > .input-group-append:last-child > .input-group-text:not(:last-child) {
    @include border-radius.border-right-radius(0);
}

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

.thy-input-group-with-prefix,
.thy-input-group-with-suffix {
    position: relative;
    width: 100%;
    padding: 0 !important;
    display: flex !important;
    align-items: center;
    .form-control {
        flex: 1;
        border: 0 !important;
    }

    .input-group-prefix {
        margin-left: variables.$input-padding-x-fixed-lg;
        display: inline-flex;
        align-items: center;
    }

    .input-group-suffix {
        margin-right: variables.$input-padding-x-fixed-lg;
    }

    &.input-group-sm,
    &.input-group-md {
        .input-group-prefix {
            margin-left: variables.$input-padding-x-fixed;
        }
        .input-group-suffix {
            margin-right: variables.$input-padding-x-fixed;
        }
    }
}

.thy-input-group-with-prefix,
.thy-input-group-with-suffix {
    .form-control {
        padding-right: variables.$input-padding-x-fixed;
        padding-left: variables.$input-padding-x-fixed;
    }
}

.thy-input-group-with-prefix:not(.input-group-sm):not(.input-group-md) {
    .form-control {
        &:last-child {
            padding-right: variables.$input-padding-x-fixed-lg;
        }
    }
}

.thy-input-group-with-suffix:not(.input-group-sm):not(.input-group-md) {
    .form-control {
        &:first-child:not(.search-input-field) {
            padding-left: variables.$input-padding-x-fixed-lg;
        }
    }
}
