/* stylelint-disable */
#stacks-internals() {
    #load-static-config() {
        #stacks-config-static();
    }

    //  ===========================================================================
    //  --  RESPONSIVE (modern)
    //      #responsify renders a plain class as well as its responsive counterparts.
    //
    //      Usage example:
    //
    //      #stacks-internals #responsify('.d-none', { display: none !important; });
    //
    //  ---------------------------------------------------------------------------

    #responsify(@class, @rules) {
        @nodot-s: replace(@class, "^\.", "");
        @nodot: ~"@{nodot-s}";

        .@{nodot} {
            @rules();
        }

        #stacks-internals-collect-large() {
            .lg\:@{nodot} {
                @rules();
            };
        }
        #stacks-internals-collect-medium() {
            .md\:@{nodot} {
                @rules();
            };
        }
        #stacks-internals-collect-small() {
            .sm\:@{nodot} {
                @rules();
            };
        }
    }

    //  ===========================================================================
    //  --  RESPONSIVE
    //      The three #screen-XX mixins wrap the given rules in the appropriate
    //      media queries and (if applicable) selector prefixes for all breakpoint
    //      groups that have been defined in the static configuration section.
    //
    //      Usage example:
    //
    //          .lg\:comic-sans {
    //              #stacks-internals #screen-lg({
    //                  font-family: "Comic Sans MS";
    //              });
    //          }
    //
    //      If @force-selector is `true`, the @rules will be evaluated inside a `html { ... }`
    //      block if a breakpoint group doesn't have a selector. You should only use this if you
    //      need to apply CSS to the root element itself:
    //
    //          #stacks-internals #screen-lg({
    //              &, body {
    //                  font-size: 77px;
    //              }
    //          }, @force-selector: true);
    //
    //      The above sets the font size on <html> and <body> at the appropriate widths, regardless
    //      of whether responsiveness was configured with or without selectors.
    //  ---------------------------------------------------------------------------

    #screen-lg(@rules, @force-selector: false) {
        #stacks-internals #load-static-config();
        #stacks-internals #responsive-stuff(0, @rules, @breakpoints, 1, @force-selector);
    }
    #screen-md(@rules, @force-selector: false) {
        #stacks-internals #load-static-config();
        #stacks-internals #responsive-stuff(1, @rules, @breakpoints, 1, @force-selector);
    }
    #screen-sm(@rules, @force-selector: false) {
        #stacks-internals #load-static-config();
        #stacks-internals #responsive-stuff(2, @rules, @breakpoints, 1, @force-selector);
    }

    // @bp-index is 0 (large), 1 (medium), or 2 (small). @start-index is the index into the @breakpoints config option
    // which is increased recursively to exhaust the whole list.
    #responsive-stuff(@bp-index, @rules, @breakpoints, @start-index, @force-selector: false) when (@start-index < length(@breakpoints)) {

        // If the first list element is a string, it's a selector that is prepended to the responsive class
        & when (isstring(extract(@breakpoints, @start-index))) {

            // the actual breakpoint; "+1" to skip the (selector) string
            @bp: extract(@breakpoints, @start-index + @bp-index + 1);

            // convert the selector string into an identifier (i.e. remove the quotes)
            @sel-s: extract(@breakpoints, @start-index);
            @sel: ~"@{sel-s}";

            @media (max-width: (@bp)) {
                @{sel} & {
                    @rules();
                }
            }

            // recursively descend to handle the next breakpoint group ("+4" for the selector string and the three breakpoint values)
            #stacks-internals #responsive-stuff(@bp-index, @rules, @breakpoints, @start-index + 4, @force-selector)
        }

        // If the first list element is a number (i.e. pixel value), there is no selector string
        & when (isnumber(extract(@breakpoints, @start-index))) {

            // the actual breakpoint
            @bp: extract(@breakpoints, @start-index + @bp-index);

            & when not (@force-selector) {
                @media (max-width: (@bp)) {
                    @rules();
                }
            }

            & when (@force-selector) {
                @media (max-width: (@bp)) {
                    html & {
                        @rules();
                    }
                }
            }

            // recursively descend to handle the next breakpoint group ("+3" for the three breakpoint values)
            #stacks-internals #responsive-stuff(@bp-index, @rules, @breakpoints, @start-index + 3, @force-selector)
        }
    }

    //  ===========================================================================
    //  --  AUTO-GENERATING
    //      #build-classes is a tool for auto-generating a bunch of related, usually atomic,
    //      classes.
    //      The first argument is optional; if given, it must be `responsive`, in which case
    //      the corresponding responsive classes will be generated as well.
    //      The next argument, @class-prefix, is the prefix of the generated class names,
    //      including the dot.
    //      The @rules argument is a detached ruleset that must contain a mixin called .template()
    //      which takes a single argument.
    //      The @values argument is a list of values that will be passed to the template.
    //
    //      The final generated class name consists of
    //      1) the given prefix,
    //      2a) if the value is a negative number, the letter "n",
    //      2b) if the value is not a number, a dash "-"
    //      3) the actual value, stripped of any unit (if applicable).
    //
    //      Usage example:
    //
    //          #stacks-internals #build-classes(
    //              '.ml',
    //              { .template(@value) { margin-left: @value; } },
    //              -50%, auto, 20px
    //          )
    //
    //      will generate:
    //
    //          .mln50 { margin-left: -50%; }
    //          .ml-auto { margin-left: auto; }
    //          .ml20 { margin-left: 20px; }
    //
    //  ---------------------------------------------------------------------------


    #build-classes(@class-prefix, @rules, @values, @responsive: false) {
        #stacks-internals #build-classes-with-start-index(@class-prefix, @rules, 1, @responsive, @values);
    }

    #build-classes-with-start-index(@class-prefix, @rules, @start-index, @responsive, @values) when (@start-index <= length(@values)) {
        @val: extract(@values, @start-index);
        & when (isnumber(@val)) {
            @unitless: unit(@val);
            & when (@unitless < 0) {
                @abs: abs(@unitless);
                @class-name: ~"@{class-prefix}n@{abs}";

                & when not (@responsive) {
                    @{class-name} { @rules(); .template(@val); }
                }

                & when (@responsive) {
                    #stacks-internals #responsify(@class-name, { @rules(); .template(@val); });
                }

            }
            & when (@unitless >= 0) {
                @class-name: ~"@{class-prefix}@{unitless}";
                & when not (@responsive) {
                    @{class-name} { @rules(); .template(@val); }
                }

                & when (@responsive) {
                    #stacks-internals #responsify(@class-name, { @rules(); .template(@val); });
                }
            }
        }
        & when not (isnumber(@val)) {
            @class-name: ~"@{class-prefix}-@{val}";
                & when not (@responsive) {
                    @{class-name} { @rules(); .template(@val); }
                }

                & when (@responsive) {
                    #stacks-internals #responsify(@class-name, { @rules(); .template(@val); });
                }
        }

        #stacks-internals #build-classes-with-start-index(@class-prefix, @rules, @start-index + 1, @responsive, @values);
    }

    #bullet-arrow(@col) {
        @image: '<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" viewBox="0 0 7 10" xmlns="http://www.w3.org/2000/svg"><path d="m0.72153 0.68446 4.1336 4.3077-4.1336 4.3077" fill="none" stroke="@{col}" stroke-width="2"/></svg>';
        @encoded-image: escape(@image);
        background-image: url("data:image/svg+xml;,@{encoded-image}");
    }
}
