@use "sass:list";
@use "../../../bulma/sass/utilities" as bulma-utilities;
@use "../../../bulma/sass/utilities/derived-variables" as bulma-vars;
@use "../../../bulma/sass/form/shared" as bulma-form;

/*
This project is based on
"bulma-steps" (https://github.com/Wikiki/bulma-steps) by
Wikiki (https://github.com/Wikiki) licensed under
MIT (https://github.com/Wikiki/bulma-steps/blob/master/LICENSE)
*/

$steps-size: var(--size-normal) !default;
$steps-height: auto !default;
$steps-background-color: var(--body-background-color) !default;
$steps-items-focused-outline: none !default;
$steps-details-background-color: var(--body-background-color) !default;
$steps-maker-default-color: var(--grey-light) !default;
$steps-maker-color: var(--scheme-main) !default;
$steps-marker-default-border: .2em solid #ffffff !default;
$steps-default-color: var(--grey-lighter) !default;
$steps-previous-color: var(--primary) !default;
$steps-active-color: var(--primary) !default;
$steps-divider-height: .2em !default;
$steps-vertical-padding: 1em 0 !default;
$steps-mobile-max-width: bulma-vars.$tablet !default;
$steps-colors: bulma-form.$form-colors !default;

// Map Local variables
$-css-vars-map: (
    "steps-size": $steps-size,
    "steps-height": $steps-height,
    "steps-background-color": $steps-background-color,
    "steps-items-focused-outline": $steps-items-focused-outline,
    "steps-details-background-color": $steps-details-background-color,
    "steps-maker-default-color": $steps-maker-default-color,
    "steps-maker-color": $steps-maker-color,
    "steps-marker-default-border": $steps-marker-default-border,
    "steps-default-color": $steps-default-color,
    "steps-previous-color": $steps-previous-color,
    "steps-active-color": $steps-active-color,
    "steps-divider-height": $steps-divider-height,
    "steps-vertical-padding": $steps-vertical-padding,
    "steps-mobile-max-width": $steps-mobile-max-width,
);

// Register & Export CSS Variabless
@include bulma-utilities.exportCSSVars($-css-vars-map);
@include bulma-utilities.registerComponentCSSVars("b-steps", $-css-vars-map);

@mixin steps-size {
    .steps {
        min-height: calc(var(--steps-size) * 2);
        font-size: var(--steps-size);
        background-color: var(--steps-background-color);

        .step-items {
            // height: var(--steps-height);
            // align-items: center;

            .step-item {
                .step-marker {
                    width: calc(var(--steps-size) * 2);
                    height: calc(var(--steps-size) * 2);

                    .icon {
                        *, *::before {
                            font-size: var(--steps-size);
                        }
                    }
                }

                .step-details {
                    background-color: var(--steps-background-color);

                    .step-title {
                        font-size: calc(var(--steps-size) * 1.2);
                        font-weight: var(--weight-semibold);
                        line-height: var(--steps-size);
                    }
                }

                &:not(:first-child), &:only-child {
                    &::before {
                        top: var(--steps-size);
                        height: var(--steps-divider-height);
                    }
                }

                &:only-child {
                    &::after {
                        top: var(--steps-size);
                    }
                }

                @include bulma-utilities.until($steps-mobile-max-width) {
                    &::before, &::after, &:not(:first-child)::before {
                        top: var(--steps-size);
                    }
                }
            }
        }
    }

    &.is-vertical {
        --steps-divider-indent-height: calc(var(--steps-size) - (var(--steps-divider-height) * 0.5));

        > .steps {
            &.has-label-right {
                .step-items {
                    .step-item {
                        &:not(:first-child), &:only-child {
                            &::before, &::after {
                                left: var(--steps-divider-indent-height);
                            }
                        }
                    }
                }
            }

            &.has-label-left {
                .step-items {
                    .step-item {
                        &:not(:first-child), &:only-child {
                            &::before, &::after {
                                right: var(--steps-divider-indent-height);
                                left: auto;
                            }
                        }
                    }
                }
            }
        }

        &.is-right {
            > .steps {
                &.has-label-right {
                    .step-items {
                        .step-item {
                            &:not(:first-child), &:only-child {
                                &::before, &::after {
                                    left: var(--steps-divider-indent-height);
                                }
                            }
                        }
                    }
                }

                &.has-label-left {
                    .step-items {
                        .step-item {
                            &:not(:first-child), &:only-child {
                                &::before, &::after {
                                    right: var(--steps-divider-indent-height);
                                    left: auto;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

.b-steps {
    .steps {
        .step-items {
            display: flex;
            flex-wrap: wrap;

            .step-item {
                position: relative;
                flex-grow: 1;
                flex-basis: 1em;
                margin-top: 0;

                .step-link {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    color: var(--body-color);

                    &:not(.is-clickable) {
                        cursor: not-allowed;
                    }
                }

                .step-marker {
                    z-index: 1;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    overflow: hidden;
                    font-weight: var(--weight-bold);
                    color: var(--steps-maker-color);
                    background: var(--steps-maker-default-color);
                    border: var(--steps-marker-default-border);
                    border-radius: var(--radius);
                }

                // Override marker color per step
                @each $name, $pair in $steps-colors {
                    $color: list.nth($pair, 1);
                    $color-invert: list.nth($pair, 2);

                    &.is-#{$name} {
                        &::before, &::after {
                            background: linear-gradient(to left, var(--steps-default-color) 50%, var(--#{$name}) 50%);
                            background-position: right bottom;
                            background-size: 200% 100%;
                        }

                        &.is-active {
                            .step-marker {
                                color: var(--#{$name});
                                background-color: var(--steps-maker-color);
                                border-color: var(--#{$name});
                            }

                            &::before, &::after {
                                background-position: left bottom;
                            }
                        }

                        &.is-previous {
                            .step-marker {
                                color: var(--#{$name}-invert);
                                background-color: var(--#{$name});
                            }

                            &::before, &::after {
                                background-position: left bottom;
                            }
                        }
                    }
                }

                .step-marker{
                    color: var(--steps-maker-color);
                }

                .step-details {
                    z-index: 1;
                    text-align: center;
                }

                &:not(:first-child), &:only-child {
                    flex-shrink: 1;

                    &::before {
                        position: absolute;
                        bottom: 0;
                        left: -50%;
                        display: block;
                        width: 100%;

                        // This will contain the divider
                        content: " ";
                    }
                }

                &:only-child {
                    &::after {
                        position: absolute;
                        bottom: 0;
                        display: block;
                        height: var(--steps-divider-height);

                        // This will contain the divider
                        content: " ";
                    }

                    &::before, &::after {
                        left: 50%;
                        width: 25%;
                    }

                    &::before {
                        right: 50%;
                        left: auto;
                    }
                }

                &::before, &::after {
                    background: linear-gradient(to left, var(--steps-default-color) 50%, var(--steps-active-color) 50%);
                    background-position: right bottom;
                    background-size: 200% 100%;
                }

                &.is-active {
                    .step-link {
                        cursor: default;
                    }

                    .step-marker {
                        color: var(--steps-active-color);
                        background-color: var(--steps-maker-color);
                        border-color: var(--steps-active-color);
                    }

                    &::before, &::after {
                        background-position: left bottom;
                    }
                }

                &.is-previous {
                    .step-marker {
                        color: var(--steps-maker-color);
                        background-color: var(--steps-previous-color);
                    }

                    &::before, &::after {
                        background-position: left bottom;
                    }
                }
            }
        }

        + .step-content {
            position: relative;
            display: flex;
            flex-direction: column;
            padding: 1rem;
            overflow: visible;

            .step-item {
                flex-shrink: 0;
                flex-basis: auto;

                &:focus {
                    outline: var(--steps-items-focused-outline);
                }
            }

            &.is-transitioning {
                overflow: hidden;
            }
        }

        &.is-rounded {
            .step-item {
                .step-marker {
                    border-radius: var(--radius-rounded);
                }
            }
        }

        &.is-animated {
            .step-item {
                &:not(:first-child), &:only-child {
                    &::before {
                        transition: background calc(var(--speed) * 2) var(--easing);
                    }
                }
            }
        }

        &.has-label-right, &.has-label-left {
            .step-items {
                .step-item {
                    .step-link {
                        flex-direction: row;

                        > .step-details {
                            padding: .2em;
                            background-color: var(--steps-details-background-color);
                        }
                    }
                }
            }
        }

        &.has-label-left {
            .step-items {
                .step-item {
                    .step-link {
                        flex-direction: row-reverse;
                    }
                }
            }
        }
    }

    @include steps-size;
    @include bulma-utilities.control-sizes(steps-size);

    &.is-vertical {
        display: flex;
        flex-flow: row wrap;

        > .steps {
            .step-items {
                flex-direction: column;
                height: 100%;
                border-bottom-color: transparent;

                .step-item {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 100%;
                    padding: var(--steps-vertical-padding);

                    &::before, &::after {
                        background: linear-gradient(to top, var(--steps-default-color) 50%, var(--steps-active-color) 50%);
                        background-position: left bottom;
                        background-size: 100% 200%;
                    }

                    // Override marker connector color per step
                    @each $name, $pair in $steps-colors {
                        $color: list.nth($pair, 1);

                        &.is-#{$name} {
                            &::before, &::after {
                                background: linear-gradient(to top, var(--steps-default-color) 50%, var(--#{$name}) 50%);
                                background-position: left bottom;
                                background-size: 100% 200%;
                            }
                        }
                    }

                    &:not(:first-child), &:only-child {
                        &::before {
                            top: -50%;
                            left: calc(50% - var(--steps-divider-height) * 0.5);
                            width: var(--steps-divider-height);
                            height: 100%;
                        }
                    }

                    &.is-active, &.is-previous {
                        &::before, &::after {
                            background-position: right top;
                        }
                    }

                    &:only-child {
                        &::before {
                            top: 50%;
                        }

                        &::after {
                            top: auto;
                            bottom: 50%;
                            width: var(--steps-divider-height);
                        }

                        &::before, &::after {
                            height: 25%;
                        }
                    }
                }
            }

            &.has-label-right {
                .step-items {
                    .step-item {
                        justify-content: flex-start;
                    }
                }
            }

            &.has-label-left {
                .step-items {
                    .step-item {
                        justify-content: flex-end;
                    }
                }
            }

            &:not(.has-label-right, .has-label-left) {
                .step-items {
                    .step-item {
                        .step-link {
                            > .step-details {
                                background-color: var(--steps-details-background-color);
                            }
                        }
                    }
                }
            }
        }

        > .step-content {
            flex-grow: 1;
        }

        > .step-navigation {
            flex-basis: 100%;
        }

        &.is-right {
            flex-direction: row-reverse;
        }
    }

    &:not(.is-vertical) {
        .steps {
            @include bulma-utilities.until($steps-mobile-max-width) {
                &.mobile-minimalist {
                    .step-items {
                        .step-item {
                            &:not(.is-active) {
                                display: none;
                            }

                            &::before, &::after, &:not(:first-child)::before {
                                position: absolute;
                                bottom: 0;
                                left: 50%;
                                display: block;
                                width: 25%;
                                height: var(--steps-divider-height);

                                // This will contain the divider
                                content: " ";
                            }

                            &::before, &:not(:first-child)::before {
                                right: 50%;
                                left: auto;
                            }
                        }
                    }
                }

                &.mobile-compact {
                    .step-items {
                        .step-item {
                            &:not(.is-active) {
                                .step-details {
                                    display: none;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
