/*
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)
*/

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

$steps-items-focused-outline: none !default;
$steps-details-background-color: cv.getVar("body-background-color") !default;
$steps-maker-default-color: cv.getVar("grey-light") !default;
$steps-marker-default-border: .2em solid #fff !default;
$steps-default-color: cv.getVar("grey-lighter") !default;
$step-previous-background-color: cv.getVar("primary") !default;
$step-previous-color: cv.getVar("primary-invert") !default;
$steps-active-color: cv.getVar("primary") !default;
$steps-divider-height: .2em !default;
$steps-vertical-padding: 1em 0 !default;
$steps-mobile-max-width: iv.$tablet !default;
$steps-colors: dv.$colors !default;

.#{iv.$class-prefix}b-steps  {
    @include cv.register-vars((
        "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-marker-default-border": #{$steps-marker-default-border},
        "steps-default-color": #{$steps-default-color},
        "step-previous-background-color": #{$step-previous-background-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},
        "step-size": cv.getVar("size-normal")
    ));

    &.is-small {
        @include cv.register-vars((
            "step-size": #{cv.getVar('size-small')}
        ));
    }
    &.is-medium {
        @include cv.register-vars((
            "step-size": #{cv.getVar('size-medium')}
        ));
    }
    &.is-large {
        @include cv.register-vars((
            "step-size": #{cv.getVar('size-large')}
        ));
    }

    .step-item {
        @include cv.register-vars((
            "step-horizontal-connector-color": linear-gradient(to left, cv.getVar("steps-default-color") 50%, cv.getVar("steps-active-color") 50%),
            "step-vertical-connector-color": linear-gradient(to top, cv.getVar("steps-default-color") 50%, cv.getVar("steps-active-color") 50%)
        ));
        
        @each $name, $pair in $steps-colors {
            &.is-#{$name} {
                @include cv.register-vars((
                    "step-horizontal-connector-color": linear-gradient(to left, cv.getVar("steps-default-color") 50%, #{cv.getVar($name)} 50%),
                    "step-vertical-connector-color": linear-gradient(to top, cv.getVar("steps-default-color") 50%, cv.getVar("steps-active-color") 50%),
                    "steps-active-color": #{cv.getVar($name)},
                    "step-previous-color": #{cv.getVar($name, "", "-invert")},
                    "step-previous-background-color": #{cv.getVar($name)}
                ));
            }

        }
    }
}


.#{iv.$class-prefix}b-steps {
    .steps {
        .step-items {
            display: flex;
            flex-wrap: wrap;

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

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

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

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

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

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

                //             &::before, &::after {
                //                 background-position: left bottom;
                //             }
                //         }
                //         &.is-previous {
                //             .step-marker {
                //                 color: $color-invert;
                //                 background-color: $color;
                //             }

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

                .step-marker{
                    color: cv.getVar("scheme-main");
                }

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

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

                    &::before {
                        // This will contain the divider
                        content: " ";
                        display: block;
                        position: absolute;
                        width: 100%;
                        bottom: 0;
                        left: -50%;
                    }
                }
                &:only-child {
                    &::after {
                        // This will contain the divider
                        content: " ";
                        display: block;
                        position: absolute;
                        height: cv.getVar("steps-divider-height");
                        bottom: 0;
                    }
                    &::before, &::after {
                        width: 25%;
                        left: 50%;
                    }
                    &::before {
                        right: 50%;
                        left: auto;
                    }
                }

                &::before, &::after {
                    background: cv.getVar("step-horizontal-connector-color");
                    background-size: 200% 100%;
                    background-position: right bottom;
                }

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

                    .step-marker {
                        background-color: cv.getVar("scheme-main");
                        border-color: cv.getVar("steps-active-color");
                        color: cv.getVar("steps-active-color");
                    }

                    &::before, &::after {
                        background-position: left bottom;
                    }
                }
                &.is-previous {
                    .step-marker {
                        color: cv.getVar("step-previous-color");
                        background-color: cv.getVar("step-previous-background-color");
                    }

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

        + .step-content {
            position: relative;
            overflow: visible;
            display: flex;
            flex-direction: column;
            padding: 1rem;
            .step-item {
                flex-shrink: 0;
                flex-basis: auto;

                &:focus {
                    outline: cv.getVar("steps-items-focused-outline");
                }
            }
            &.is-transitioning {
                overflow: hidden;
            }
        }

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

        &.is-animated {
            .step-item {
                &:not(:first-child), &:only-child {
                    &::before {
                        transition: background cv.getVar("speed-slow") cv.getVar("easing");
                    }
                }
            }
        }

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

                        > .step-details {
                            background-color: cv.getVar("steps-details-background-color");
                            padding: .2em;
                        }
                    }
                }
            }
        }
        &.has-label-left {
            .step-items {
                .step-item {
                    .step-link {
                        flex-direction: row-reverse;
                    }
                }
            }
        }
    }

    .steps {
        font-size: cv.getVar("step-size");
        min-height: calc(cv.getVar("step-size") * 2);

        .step-items {
            .step-item {
                .step-marker {
                    height: calc(cv.getVar("step-size") * 2);
                    width: calc(cv.getVar("step-size") * 2);

                    .icon {
                *, *:before {
                            font-size: cv.getVar("step-size");
                        }
                    }
                }

                .step-details {
                    .step-title {
                        font-size: calc(cv.getVar("step-size") * 1.2);
                        font-weight: cv.getVar("weight-semibold");
                        line-height: cv.getVar("step-size");
                    }
                }

                &:not(:first-child), &:only-child {
                    &::before {
                        height: cv.getVar("steps-divider-height");
                        top: #{cv.getVar("step-size")};
                    }
                }
                &:only-child {
                    &::after {
                        top: #{cv.getVar("step-size")};
                    }
                }

                @include mixins.until(cv.getVar("steps-mobile-max-width")) {
                    &::before, &::after, &:not(:first-child)::before {
                        top: #{cv.getVar("step-size")};
                    }
                }
            }
        }
    }

    &.is-vertical {
        > .steps {
            &.has-label-right {
                .step-items {
                    .step-item {
                        &:not(:first-child), &:only-child {
                            &::before, &::after {
                                left: calc(#{cv.getVar("step-size")} - #{cv.getVar("steps-divider-height")} * 0.5);
                            }
                        }
                    }
                }
            }
            &.has-label-left {
                .step-items {
                    .step-item {
                        &:not(:first-child), &:only-child {
                            &::before, &::after {
                                left: auto;
                                right: calc(#{cv.getVar("step-size")} - #{cv.getVar("$steps-divider-height")} * 0.5);
                            }
                        }
                    }
                }
            }
        }

        &.is-right {
            > .steps {
                &.has-label-right {
                    .step-items {
                        .step-item {
                            &:not(:first-child), &:only-child {
                                &::before, &::after {
                                    left: calc(#{cv.getVar("step-size")} - #{cv.getVar("$steps-divider-height")} * 0.5);
                                }
                            }
                        }
                    }
                }
                &.has-label-left {
                    .step-items {
                        .step-item {
                            &:not(:first-child), &:only-child {
                                &::before, &::after {
                                    left: auto;
                                    right: calc(#{cv.getVar("step-size")} - #{cv.getVar("$steps-divider-height")} * 0.5);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

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

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

                    &::before, &::after {
                        background: cv.getVar("step-vertical-connector-color");
                        background-size: 100% 200%;
                        background-position: left bottom;
                    }

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

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

                    &:only-child {
                        &::before {
                            top: 50%;
                        }
                        &::after {
                            width: cv.getVar("steps-divider-height");
                            top: auto;
                            bottom: 50%;
                        }
                        &::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):not(.has-label-left) {
                .step-items {
                    .step-item {
                        .step-link {
                            > .step-details {
                                background-color: cv.getVar("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 mixins.until(cv.getVar("steps-mobile-max-width")) {
                &.mobile-minimalist {
                    .step-items {
                        .step-item {
                            &:not(.is-active) {
                                display: none;
                            }
                            &::before, &::after, &:not(:first-child)::before {
                                // This will contain the divider
                                content: " ";
                                display: block;
                                position: absolute;
                                height: cv.getVar("steps-divider-height");
                                width: 25%;
                                bottom: 0;
                                left: 50%;
                            }
                            &::before, &:not(:first-child)::before {
                                right: 50%;
                                left: auto;
                            }
                        }
                    }
                }
                &.mobile-compact {
                    .step-items {
                        .step-item {
                            &:not(.is-active) {
                                .step-details {
                                    display: none;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
