@use "bulma/sass/utilities/controls" as controls;
@use "bulma/sass/utilities/initial-variables" as iv;
@use "bulma/sass/utilities/css-variables" as cv;
@use "bulma/sass/utilities/mixins" as mixins;

$floating-in-height: 3.25em !default;

.#{iv.$class-prefix}field {
    @include cv.register-vars(
    (
        "floating-in-height": #{$floating-in-height},
    )
    )
}

.#{iv.$class-prefix}field {
    &.is-grouped {
        .field {
            flex-shrink: 0;
            &:not(:last-child) {
                margin-inline-end: 0.75rem;
            }
            &.is-expanded {
                flex-grow: 1;
                flex-shrink: 1;
            }
        }
    }
    // Nested control addons (for Autocomplete and Datepicker)
    &.has-addons .control {
        &:first-child .control {
            .button,
            .input,
            .select select {
                border-start-start-radius: cv.getVar('input-radius');
                border-end-start-radius: cv.getVar('input-radius');
            }
        }
        &:last-child .control {
            .button,
            .input,
            .select select {
                border-start-end-radius: cv.getVar('input-radius');
                border-end-end-radius: cv.getVar('input-radius');
            }
        }
        .control {
            .button,
            .input,
            .select select {
                border-radius: 0
            }
        }
    }
    // Fix for number input with addons
    &.has-addons {
        .b-numberinput {
            &:not(:first-child) {
                .control:first-child {
                    .button,
                    .input,
                    .select select {
                        border-start-start-radius: 0;
                        border-end-start-radius: 0;
                    }
                }
            }
            &:not(:last-child) {
                .control:last-child {
                    .button,
                    .input,
                    .select select {
                        border-start-end-radius: 0;
                        border-end-end-radius: 0;
                    }
                }
            }
        }
        &.b-numberinput {
            .control {
                margin-inline-end: unset;
            }
        }
    }
}

.#{iv.$class-prefix}field {
    &.is-floating-label,
    &.is-floating-in-label {
        position: relative;

        .label {
            position: absolute;
            inset-inline-start: 1em;
            font-size: calc(#{cv.getVar('size-normal')} * 0.75);
            background-color: transparent;
            z-index: 5;
            white-space: nowrap;
            text-overflow: ellipsis;
            max-width: calc(100% - 2em);
            overflow: hidden;

            &.is-small {
                font-size: calc(#{cv.getVar('size-small')} * 0.75);
            }
            &.is-medium {
                font-size: calc(#{cv.getVar('size-medium')} * 0.75);
            }
            &.is-large {
                font-size: calc(#{cv.getVar('size-large')} * 0.75);
            }
        }
        .taginput .counter {
            float: none;
            text-align: end;
        }

        &.has-addons {
            > .label {
                + .control {
                    .button,
                    .input,
                    .select select {
                        border-start-start-radius: cv.getVar('input-radius');
                        border-end-start-radius: cv.getVar('input-radius');
                    }
                }
            }
        }
    }

    &.is-floating-label {
        .label {
            top: -0.775em;
            padding-inline-start: 0.125em;
            padding-inline-end: 0.125em;
            &:before {
                content: '';
                display: block;
                position: absolute;
                top: 0.775em;
                inset-inline-start: 0;
                inset-inline-end: 0;
                height: 0.375em;
                background-color: cv.getVar('input-background-color');
                z-index: -1;
            }
        }
        .taginput .taginput-container {
            padding-top: 0.475em;
        }
    }
    &.is-floating-in-label {
        > .label {
            top: 0.25em;

            + .control {
                &.datepicker, &.timepicker {
                    .input {
                        padding-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                        padding-bottom: 1px;
                        height: cv.getVar('floating-in-height');
                    }
                }
                &:not(.datepicker):not(.timepicker):not(.taginput) {
                    .input,
                    .textarea,
                    select {
                        padding-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                        padding-bottom: 1px;
                        height: cv.getVar('floating-in-height');
                    }
                    .select:not(multiple) {
                        height: cv.getVar('floating-in-height');
                        &.is-loading {
                            &::after {
                                margin-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                            }
                        }
                        &::after {
                            margin-top: 1px;
                        }
                    }
                }
                &.taginput {
                    .taginput-container {
                        // 0.275em - 1px _taginput.scss
                        padding-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5 + (0.275em - 1px));
                    }
                }
                &:not(.taginput) {
                    .is-left.icon,
                    .is-right.icon {
                        height: cv.getVar('floating-in-height');
                    }
                    .is-left.icon {
                        padding-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                    }
                }
                &.is-loading::after {
                    margin-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                }
            }

            // With addons or grouped
            + .field-body {
                > .is-grouped, > .has-addons {
                    .control {
                        .input,
                        .textarea,
                        select {
                            padding-top: calc(#{cv.getVar('floating-in-height')} * 0.5 - (#{cv.getVar('size-large')} * 0.75) * 0.5);
                            padding-bottom: 1px;
                        }
                        .input,
                        .textarea,
                        select,
                        .button {
                            height: cv.getVar('floating-in-height');
                        }
                    }
                }
            }
        }

        &.has-numberinput {
            .b-numberinput {
                .control {
                    .input,
                    .button {
                        height: cv.getVar('floating-in-height');
                    }
                }
            }
        }
    }
    &.is-floating-label, &.is-floating-in-label {
        &.has-numberinput {
            .label {
                margin-inline-start: calc(#{cv.getVar('size-normal')} * 3);
            }
            &.has-numberinput-is-small {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-small')} * 3);
                }
            }
            &.has-numberinput-is-medium {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-medium')} * 3);
                }
            }
            &.has-numberinput-is-large {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-large')} * 3);
                }
            }
        }
        &.has-numberinput-compact {
            .label {
                margin-inline-start: calc(#{cv.getVar('size-normal')} * 2.25);
            }
            &.has-numberinput-is-small {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-small')} * 2.25);
                }
            }
            &.has-numberinput-is-medium {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-medium')} * 2.25);
                }
            }
            &.has-numberinput-is-large {
                .label {
                    margin-inline-start: calc(#{cv.getVar('size-large')} * 2.25);
                }
            }
        }
    }
    &.is-grouped-right, &.has-addons-right {
        &.is-floating-in-label {
            .label {
                position: relative;
                inset-inline-start: calc(3.25em + 2em);
            }
        }
        &.is-floating-label {
            .label {
                position: relative;
                inset-inline-start: calc(3.25em + 2em);
            }
        }
    }
}

.#{iv.$class-prefix}control {
    .help.counter {
        float: inline-end;
        margin-inline-start: 0.5em;
    }
    .icon {
        &.is-clickable {
            pointer-events: auto;
            cursor: pointer;
        }
    }
    // fix Bulma 0.8.2
    &.is-loading::after {
        top: calc(50% - (1em * 0.5));
        inset-inline-end: calc((2.5em * 0.5) - .5em);
    }
}
