//------------------------------------------------------------------------------
// HTML Builder Common
//------------------------------------------------------------------------------

// Base snippet rules
%o-we-background-layer-parent {
    &, & > * {
        // Allow background layers to be placed accordingly and snippet content
        // to be displayed on top. Note: we cannot just position the layers
        // with z-index: -1, otherwise it would go under the snippet own
        // background. Adding a z-index: 0 on the snippet to create its own
        // stacking context won't solve that either as, in that case, any BS
        // component inside would be using that stacking context (e.g. a
        // dropdown inside snippet 1 of the page would go under snippet 2
        // when opened since the dropdown z-index would be confined into
        // snippet 1's stacking context.
        position: relative;
    }
}
%o-we-background-layer {
    @include o-position-absolute(0, 0, 0, 0);
    position: absolute !important;
    display: block;
    overflow: hidden;
    background-repeat: no-repeat;
    pointer-events: none;
    // For border with all 4 edges of equal value the max function is useless,
    // but in case the border is uneven it approximates the correct roundness
    // See BORDER_VARIABLES_RULES_EXTENSION
    //
    // --box-border-XXX-(radius|width) : editor Round Corners user input
    // --box-border-(radius|width) : rounded-XXX classes
    // --border-(radius|width) : theme (+ default bootstrap)
    border-radius:
        calc(var(--box-border-top-left-radius, 0px) - max(var(--box-border-top-width, 0px), var(--box-border-left-width, 0px)))
        calc(var(--box-border-top-right-radius, 0px) - max(var(--box-border-top-width, 0px), var(--box-border-right-width, 0px)))
        calc(var(--box-border-bottom-right-radius, 0px) - max(var(--box-border-bottom-width, 0px), var(--box-border-right-width, 0px)))
        calc(var(--box-border-bottom-left-radius, 0px) - max(var(--box-border-bottom-width, 0px), var(--box-border-left-width, 0px)))
}

section, .oe_img_bg, [data-oe-shape-data] {
    @extend %o-we-background-layer-parent;
}
.o_we_bg_filter {
    @extend %o-we-background-layer;
}

// Background shapes
@function compute-shape-url-params($colors, $color-to-cc-bg-map) {
    $url-params: '';
    @each $i in $colors {
        $mapped-color: map-get($color-to-cc-bg-map, $i);
        $color: encode-color(#{o-color($mapped-color)});
        $url-params: '#{$url-params}&c#{$i}=#{$color}';
    }
    @return $url-params;
}

.o_we_shape {
    @extend %o-we-background-layer;

    &.o_we_animated {
        will-change: transform;
    }

    // Default map to use to map shape file colors to color combination
    // background colors.
    $default-color-to-cc-bg-map: (
        1: 4,
        2: 3,
        3: 2,
        4: 1,
        5: 5,
    );
    @each $module, $shapes in $o-bg-shapes {

        // Compatibility
        $current-module: $module;
        @if $current-module == "web_editor" {
            $current-module: "html_builder";
        }

        @each $shape, $style in $shapes {
            $colors: map-get($style, 'colors');
            $color-to-cc-bg-map: map-merge($default-color-to-cc-bg-map, map-get($style, 'color-to-cc-bg-map') or ());
            $url-params: compute-shape-url-params($colors, $color-to-cc-bg-map);
            $extra-mappings: map-get($style, 'extra-mappings') or ();

            // eg: o_website_shape_bg_1
            &.o_#{$module}_#{str-replace($shape, '/', '_')} {
                // When the shape is not customized, this URL, built in SCSS,
                // allows for the shape to respond to palette changes.
                // Mainly useful for default pages built by the configurator.
                background-image: url("/html_editor/shape/#{$current-module}/#{$shape}.svg?#{str-slice($url-params, 2)}");
                background-position: map-get($style, 'position');
                background-size: map-get($style, 'size');
                background-repeat:
                    if(map-get($style, 'repeat-x'), repeat, no-repeat)
                    if(map-get($style, 'repeat-y'), repeat, no-repeat);

                @each $mapping-name, $mapping in $extra-mappings {
                    $color-to-cc-bg-map: map-merge($default-color-to-cc-bg-map, $mapping or ());
                    $url-params: compute-shape-url-params($colors, $color-to-cc-bg-map);

                    &.o_#{$mapping-name}_extra_shape_mapping {
                        background-image: url("/html_editor/shape/#{$current-module}/#{$shape}.svg?#{str-slice($url-params, 2)}");
                    }
                }
            }
        }
    }
}
