@use "sass:color";
@use "../../../vendor/twbs/bootstrap/scss/bootstrap" as *;
@use "../../../assets/styles/variables" as variables;
@use "data2ui_mini-bootstrap";

@function str-replace($string, $search, $replace: "") {
  $index: str-index($string, $search);
  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace +
      str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }
  @return $string;
}

@function escape-svg($svg) {
  // Encode les caractères problématiques dans les data-URI
  $map: (
          "%": "%25",
          "#": "%23",
          "<": "%3C",
          ">": "%3E",
          '"': "%22",
          "'": "%27",
          " ": "%20"
  );
  @each $k, $v in $map { $svg: str-replace($svg, $k, $v); }
  @return unquote("url('data:image/svg+xml,#{$svg}')");
}

$fill: if(type-of(variables.$primary) == 'color', #{variables.$primary}, variables.$primary);


/* -------------------------------------------------------
   CSS custom properties
-------------------------------------------------------- */
:root {
  --s2-bg: #fff;
  --s2-text: #292929;
  --s2-brand: #{variables.$primary};
  --s2-brand-2: #{variables.$secondary};
  --s2-brand-3: #{variables.$tertiary};
  --s2-chip-bg: #{variables.$data2ui-bg-form-element};
  --s2-border: #ced4da;
  --s2-radius: #{$input-border-radius};
  --s2-ring: 0 0 0 .25rem rgba(#{red(variables.$primary)}, #{green(variables.$primary)}, #{blue(variables.$primary)}, .25);
}

/* -------------------------------------------------------
   Base container (default + bootstrap-5)
-------------------------------------------------------- */
.select2-container,
.select2-container--default,
.select2-container--bootstrap-5 {
  //width: 100%;
  z-index: 1;
  background: transparent !important;
  padding-left: 0 !important;

  &.select2-container--focus .select2-selection,
  &.select2-container--open .select2-selection {
    border-color: var(--s2-brand);
    @include box-shadow(var(--s2-ring));
    outline: 0;
  }

  .select2-selection {
    position: relative;
    display: flex;
    align-items: center;
    min-height: 2.25rem; /* 38px bootstrap */
    border: 1px solid var(--s2-border);
    border-radius: var(--s2-radius);
    background-color: transparent;

    &.secondary {
      background-color: var(--s2-brand-2);

      .select2-selection__placeholder { color: #fff; }
    }
    &.tertiary {
      background-color: var(--s2-brand-3);

      .select2-selection__placeholder { color: #fff; }
    }
  }

  /* ---------- single ---------- */
  .select2-selection--single {
    padding: 0 .75rem;
    line-height: 1.5;
    display: flex !important;
    align-items: center;

    .select2-selection__rendered {
      flex: 1 1 auto;
      color: var(--s2-text);
      display: flex !important;
      align-items: center;

      .select2-selection__placeholder {
        font-weight: 600;
        position: absolute;
        top: 0;
        left: 15px;
      }
    }

    .select2-selection__arrow {
      position: absolute;
      right: .75rem;
      top: 50%;
      transform: translateY(-50%);
      height: 100%;
      pointer-events: none;
      display: flex;
      align-items: center;

      b {
        border-color: #aaa transparent transparent transparent;
        border-style: solid;
        border-width: 5px 4px 0 4px;
        display: block;
        height: 0;
        width: 0;
      }
    }
  }

  /* ---------- multiple ---------- */
  .select2-selection--multiple {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: .25rem;
    padding: .375rem .5rem;
    min-height: 55px;          /* ta valeur de base */
    overflow-y: auto;
    resize: vertical;

    .select2-selection__clear { right: 1.5rem; } /* laisse place au scroll */

    .select2-selection__rendered {
      width: auto;
      display: flex;
      flex-wrap: wrap;
      gap: .25rem;

      li.select2-selection__choice {
        display: inline-flex;
        align-items: center;
        gap: .375rem;
        line-height: 1.1;
        background: var(--s2-chip-bg);
        border: 1px solid color.scale(variables.$data2ui-bg-form-element, $lightness: -5%);
        color: #fff;
        border-radius: 9999px;
        padding: .2rem .75rem .2rem .5rem;
        position: relative;

        .select2-selection__choice__remove {
          position: relative;
          width: 1rem; height: 1rem;
          cursor: pointer;
          font-size: 0; /* cache le caractère */
          background: transparent;
          background-image: escape-svg(
                          "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$fill}'><path d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/></svg>"
          );
          &:hover {
            filter: brightness(.9);
          }
        }
      }
    }

    .select2-search { flex: 1 1 auto; display: flex; }
  }

  /* ---------- dropdown & results ---------- */
  .select2-dropdown {
    background: var(--s2-bg);
    border: 1px solid #dedede;
    border-radius: var(--s2-radius);
    overflow: hidden;
    box-sizing: border-box;
    max-width: calc(100vw - 16px);

    .select2-search {
      padding: .5rem;

      .select2-search__field {
        width: 100%;
        padding: .375rem .5rem;
        border: 1px solid var(--s2-border);
        border-radius: var(--s2-radius);
        font-weight: 600;
        color: var(--s2-text);
      }
    }

    .select2-results {
      background: var(--s2-bg);
      max-height: min(300px, 50vh);
      overflow: hidden;

      .select2-results__options {
        max-height: calc(min(300px, 50vh));
        overflow-y: auto;
        color: var(--s2-text);

        /* groupes */
        .select2-results__option[role="group"] {
          .select2-results__group {
            background: var(--s2-brand);
            color: #fff;
            display: block;
            width: 100%;
            font-size: 1.05rem;
            font-weight: 700;
            padding: .25rem .5rem;
          }
          .select2-results__options--nested {
            max-height: none;
            overflow-y: visible;
          }
        }

        /* items */
        .select2-results__option--selectable {
          padding: .5rem .75rem;

          &:hover,
          &.select2-results__option--highlighted[aria-selected] {
            background-color: color.scale(variables.$secondary, $lightness: -20%);
            color: #fff;
          }
        }

        .select2-results__option--highlighted[aria-selected] {
          background-color: color.scale(variables.$secondary, $lightness: -20%);
          color: #fff;
        }
      }
    }
  }
}

/* -------------------------------------------------------
   États : focus visible, valid/invalid (Bootstrap)
-------------------------------------------------------- */
.select2-container .select2-selection:focus-visible {
  outline: 0;
  @include box-shadow(var(--s2-ring));
  border-color: var(--s2-brand);
}

.select2-selection.is-invalid {
  border-color: $form-feedback-invalid-color;
  @include box-shadow(0 0 0 .25rem rgba(variables.$danger, .25));
}
.select2-selection.is-valid {
  border-color: $form-feedback-valid-color;
  @include box-shadow(0 0 0 .25rem rgba(variables.$success, .25));
}

/* -------------------------------------------------------
   Contexte spécifique
-------------------------------------------------------- */
.stations-with-map {
  .select2-container,
  .select2-container--default,
  .select2-container--bootstrap-5 { height: 100%; }

  .select2-selection { height: 100%; }
}

.search-block {
  position: relative;
  padding: 5px;

  i {
    color: var(--s2-brand);
    position: absolute;
    left: 10px;
    top: 10px;
    font-size: 2em;
  }

  .select2-container .select2-selection {
    background: transparent;
    border: none;
  }
}

/* -------------------------------------------------------
   Bootstrap 4 (legacy)
-------------------------------------------------------- */
.select2-container--bootstrap4 {
  height: 100%;

  .select2-selection--single {
    padding: 1em;
    border: 0;
    text-align: left;
    line-height: 1rem;
    background: transparent;

    .select2-selection__rendered {
      color: #292929;
      padding: .5rem 0;
    }
    .select2-selection__placeholder {
      font-weight: 300;
      font-style: italic;
      color: #292929;
    }

    .select2-selection__arrow b {
      margin-left: -2rem;
      border-color: #000 transparent transparent transparent;
      border-style: solid;
      border-width: .5rem .5rem 0 .5rem;
      position: absolute;
      top: 45%;
      right: 2rem;
    }
  }

  &.select2-container--open .select2-selection .select2-selection__arrow b {
    border-color: transparent transparent var(--s2-brand-3) transparent;
    border-width: 0 .5rem .5rem .5rem;
  }

  .select2-search--dropdown .select2-search__field {
    font-weight: bold;
    color: #292929;
  }

  .select2-dropdown {
    border: 0;
    background: rgba(255, 255, 255, 1);

    .select2-search { font-weight: normal; }
  }
}

/* -------------------------------------------------------
   améliorations
-------------------------------------------------------- */
.select2-container--open .select2-selection__arrow b {
  transform: rotate(180deg);
  transition: transform .15s ease-in-out;
}

.select2-dropdown,
.select2-results,
.select2-results__options {
  overflow-x: hidden;
}