$_bmpv_mode_config: false
$_bmpv_is_cascade: true
$_bmpv_variables: ()
$_bmpv_element: null
$_bmpv_block: null
$_bmpv_mods: null


$_bmpv_defaultOrder: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "!" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/" "[" "\\" "]" "^" "_" "{" "|" "}" "~" ":" !default
@function _bmpv_strCompare($a, $b, $order)
  @if type-of($a) == "number" and type-of($b) == "number"
    @return $a < $b
  $a: to-lower-case($a + unquote(""))
  $b: to-lower-case($b + unquote(""))
  @for $i from 1 through min(str-length($a), str-length($b))
    $char-a: str-slice($a, $i, $i)
    $char-b: str-slice($b, $i, $i)
    @if $char-a and $char-b and index($order, $char-a) != index($order, $char-b)
      @return index($order, $char-a) < index($order, $char-b)
  @return str-length($a) < str-length($b)
@function _bmpv_quickSort($list, $order: $_bmpv_defaultOrder)
  $less: ()
  $equal: ()
  $large: ()
  @if length($list) > 1
    $seed: nth($list, ceil(length($list) / 2))
    @each $item in $list
      @if $item == $seed
        $equal: append($equal, $item, list-separator($list))
      @else if _bmpv_strCompare($item, $seed, $order)
        $less: append($less, $item, list-separator($list))
      @else if not _bmpv_strCompare($item, $seed, $order)
        $large: append($large, $item, list-separator($list))
    @return join(join(_bmpv_quickSort($less, $order), $equal), _bmpv_quickSort($large, $order))
  @return $list

@function _bmpv_purge($list)
  $result: ()
  @each $item in $list
    @if $item != null and $item != false and $item != ''
      @if not index($result, $item)
        $result: append($result, $item)
  @if length($result) > 1
    $i: index($result, '[class]')
    @if $i !=null and $i >= 1
      $result: #{set-nth($result, $i, null)}
    @return _bmpv_quickSort($result)
  @return $result

@function _bmpv_selectorMake()
  $elementHas: false
  $sel: ''
  @each $mo in $_bmpv_mods
    @if $mo
      $first: str-slice($mo, 1, 1)
      @if $first == '-'
        $sel: #{$sel + '.' + $mo}
      @else if $first == '\\' and str-slice($mo, 1, 2) == '\\:'
        $sel: #{$sel + str-slice($mo, 2)}
      @else if $first == ':'
        $sel: #{$sel + $mo}
      @else
        $elementHas: true
        $sel: #{$sel + _bmpv_elm() + '--' + $mo}
  @if $sel == ''
    @return _bmpv_elm()
  @if $elementHas
    @return $sel
  @return _bmpv_elm() + $sel

@function _bmpv_elm()
  $_elm: #{'.' + $_bmpv_block}
  @if $_bmpv_element == null or $_bmpv_element == ''
    @return $_elm
  @return #{$_elm + '__' + $_bmpv_element}

@function _bmpv_getSelectorValue($name: null)
  $char: str-slice($name, 1, 1)
  @if $char == '-'
    @if str-slice($name, 1, 2) == '--'
      $block: $_bmpv_block
      @if $block == null
        $block: $b
      @return #{'.' + $block}
    @return _bmpv_elm()
  @if $char == '#'
    @return &
  // ----------- selector self --------------
  $result: ()
  @each $se in nth(selector-parse(&), 1)
    $aa: selector-append(_bmpv_purge(simple-selectors($se))...)
    $result: append($result, $aa)
  @return $result

@function _bmpv_vhas($name, $value: null)
  $selector: _bmpv_getSelectorValue($name)
  $map: map-get($_bmpv_variables, $selector)
  @if $map == null
    @return false
  @if str-slice($name, 1, 1) == '#'
    $name: str-slice($name, 2)
  @return map-get($map, $name) != null

@function _bmpv_vget($name, $value: null)
  $selector: _bmpv_getSelectorValue($name)
  $map: map-get($_bmpv_variables, $selector)
  @if $map == null
    @return $value
  @if str-slice($name, 1, 1) == '#'
    $name: str-slice($name, 2)
  $v: map-get($map, $name)
  @if $v != null
    @return $v
  @return $value

@function _bmpv_vset($name, $value: null)
  $selector: _bmpv_getSelectorValue($name)
  $map: map-get($_bmpv_variables, $selector)
  @if str-slice($name, 1, 1) == '#'
    $name: str-slice($name, 2)
  @if $map == null
    $map: ($name: $value)
  @else
    $map: map-merge($map, ($name: $value))
  $_bmpv_variables: map-merge($_bmpv_variables, ($selector: $map)) !global
  @return $value

@function _bmpv_vgetset($name, $value: null)
  $selector: _bmpv_getSelectorValue($name)
  $map: map-get($_bmpv_variables, $selector)
  @if str-slice($name, 1, 1) == '#'
    $name: str-slice($name, 2)
  @if $map == null
    $map: ()
  $v: map-get($map, $name)
  @if $v != null
    @return $v
  @if $value != null and length($value) > 0
    $map: map-merge($map, ($name: $value))
    $_bmpv_variables: map-merge($_bmpv_variables, ($selector: $map)) !global
  @return $value


@function v($name, $value...)
  @if str-slice($name, 1, 2) == '--'
    $value: _bmpv_vgetset($name, $value)
    @if $value != null
      $fallback: ""
      @if inspect($value) != "()" and $value != null
        $fallback: ", #{$value}"
      @return #{"var(--#{$_bmpv_block}#{$name}#{$fallback})"}
    @return #{'var(--' + $_bmpv_block + $name + ')'}
  @else
    @return _bmpv_vgetset($name, $value)

@function p($name, $value...)
  @return _bmpv_vgetset($name, $value)

@function b($elem: '')
  $block: $_bmpv_block
  @if $block == null
    $block: $b
  @if $elem != ''
    @return #{$block}__#{$elem}
  @return #{$block}


=block($name: null, $mods: null)
  +b($name, $mods)
    @content

=b($name: null, $mods: null)
  $_stack_element: $_bmpv_element
  $_stack_block: $_bmpv_block
  $_stack_mods: $_bmpv_mods

  @if $name == ''
    $name: null
  $_bmpv_element: $name !global
  @if $_bmpv_is_cascade
    $_bmpv_mods: null !global
  @if $_bmpv_block == null
    $_bmpv_block: $b !global
  @if $mods != null
    $_bmpv_mods: _bmpv_purge(join($_bmpv_mods, $mods)) !global

  @if $_bmpv_is_cascade
    #{_bmpv_selectorMake()}
      @content
  @else
    @at-root #{_bmpv_selectorMake()}
      @content
  $_bmpv_element: $_stack_element !global
  $_bmpv_block: $_stack_block !global
  $_bmpv_mods: $_stack_mods !global

=e($name: null, $mods: null)
  $_stack_is_cascade: $_bmpv_is_cascade
  $_bmpv_is_cascade: false !global
  +b($name, $mods)
    $_bmpv_is_cascade: $_stack_is_cascade !global
    @content

=m($mods: null)
  $_stack_mods: $_bmpv_mods
  $_bmpv_mods: _bmpv_purge(join($_bmpv_mods, $mods)) !global
  @if $_bmpv_element or $_bmpv_block
    @at-root
      #{_bmpv_selectorMake()}
        @content
  @else
    @content
  $_bmpv_mods: $_stack_mods !global

=p($name, $value...)
  $res: _bmpv_vgetset($name, $value)
  $value: if(inspect($res) == "()", "", #{$res})
  @if not $_bmpv_mode_config
    @if $value != null and $value != ''
      @if str-slice($name, 1, 1) == '#'
        $name: str-slice($name, 2)
      #{$name}: $value

=v($name, $value...)
  $_stack_block: $_bmpv_block
  @if $_bmpv_block == null
    $_bmpv_block: $b !global
  $res: _bmpv_vgetset($name, $value)
  $value: if(inspect($res) == "()", "", #{$res})
  $_bmpv_block: $_stack_block !global

=bmpv-config($block: null, $is_cascade: false)
  @if $block == null
    $block: $b
  $_stack: $_bmpv_mode_config
  $_bmpv_mode_config: true !global
  +bmpv($block, $is_cascade)
    @content
  $_bmpv_mode_config: $_stack !global


=bmpv($block: null, $is_cascade: false)
  $_stack_is_cascade: $_bmpv_is_cascade
  $_stack_block: $_bmpv_block
  $_bmpv_is_cascade: $is_cascade !global
  $_bmpv_block: $block !global
  @at-root #{_bmpv_selectorMake()}
    @content
  $_bmpv_is_cascade: $_stack_is_cascade !global
  $_bmpv_block: $_stack_block !global
