@import 'helpers/simple-menus';


@mixin select-dropdown-arrow-svg-bg_($fill-hex-number: 000000, $opacity: .54) {
  background-image: url(data:image/svg+xml,%3Csvg%20width%3D%2210px%22%20height%3D%225px%22%20viewBox%3D%227%2010%2010%205%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%20%20%3Cpolygon%20id%3D%22Shape%22%20stroke%3D%22none%22%20fill%3D%22%23#{$fill-hex-number}%22%20fill-rule%3D%22evenodd%22%20opacity%3D%22#{$opacity}%22%20points%3D%227%2010%2012%2015%2017%2010%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E);
}

.menu-anchor {
  position: relative;
  overflow: visible;
}

.select {
    @include typography-subheading-2();
    @include select-dropdown-arrow-svg-bg_();

    align-items: center;
    appearance: none;
    background-color: transparent;
    // background-position: right center;
    background-position: calc(100% - 2px) center;
    background-repeat: no-repeat;
    border: none;
    border-bottom: 1px solid rgba(0,0,0,0.42);
    border-radius: 0;
    box-sizing: content-box;
    color: $fancy-select-primary-text-color;
    cursor: pointer;
    display: inline-flex;
    font-family: Roboto, sans-serif;
    height: 32px;
    justify-content: flex-start;
    letter-spacing: 0.002rem;
    line-height: 32px;
    max-width: calc(100% - #{$dropdown-arrow-padding});
    padding-left: 2px;
    padding-right: $dropdown-arrow-padding;
    position: relative;

    -webkit-appearance: none;
       -moz-appearance: none;
            appearance: none;

    [dir="rtl"] &,
    &[dir="rtl"] {
        padding-left: $dropdown-arrow-padding;
        padding-right: 0;
    }

    transition:
      border-bottom-color 150ms 0ms cubic-bezier(0.4, 0, 0.6, 1),
      background-color 150ms 0ms cubic-bezier(0.4, 0, 0.6, 1);

    &::-ms-expand {
        display: none;
    }

    &:hover {
      border-bottom-color: rgba(0,0,0,0.87);
    }

    &::after {
        background-color: $form-focus-color;
        bottom: -1px;
        content: '';
        height: 2px;
        left: 50%;
        position: absolute;
        transition:
            left $form-transition-speed ease,
            width $form-transition-speed ease;
        width: 0;
        z-index: 2;
    }

    &:focus {
        background-color: rgba($form-focus-color, 0.3);
        border-top-left-radius: 2px;
        border-top-right-radius: 2px;
        outline: none;


        &::after {
            left: 0;
            width: 100%;
        }
    }

    [dir="rtl"] &,
    &[dir="rtl"] {
        // background-position: left center;
        background-position: 2px center;
    }

    &--theme-dark,
    .theme--dark & {
        @include select-dropdown-arrow-svg-bg_(FFFFFF);

        border-bottom: 1px solid rgba(white, .12);
        color: $fancy-select-primary-text-color;

        &:focus {
            background-color: rgba(white, .09);
            border-bottom-color: $fancy-select-primary-color;
        }
    }

    &__menu {
        left: 0;
        margin: -8px 0 0 -14px;
        max-height: 100%;
        position: fixed;
        top: 0;
        transform-origin: center center;
        z-index: 48; // Should pop up above everything else. temporary-drawer is next highest at 3.
    }

    &__selected-text {
        overflow: hidden;
        transition:
          opacity 125ms 0 cubic-bezier(0.4, 0, 0.6, 1),
          transform 125ms 0 cubic-bezier(0.4, 0, 0.6, 1);
        white-space: nowrap;
    }

    & ~ .control-label {
        color: rgba(0,0,0,0.54);
        font-size: 12px;
        left: 0px;
        top: 26px;
    }

    & ~ .help-block {
        position: relative;
    }
}

.select--open {
    .select__selected-text {
        opacity: 0;
        transform: translateY(8px);
        transition:
          opacity 125ms 125ms cubic-bezier(0, 0, 0.2, 1),
          transform 125ms 125ms cubic-bezier(0, 0, 0.2, 1);
    }
}

.select--disabled,
.select[disabled] {
    @include select-dropdown-arrow-svg-bg_(000000, 0.38);

    border-bottom-style: dotted;
    color: $fancy-select-text-disabled-color;  // was text-disabled-on-light.
    cursor: default;
    pointer-events: none;
    // Imitate native disabled functionality
    user-select: none;
}

@each $sel in ("select--disabled", "select[disabled]") {
    .#{$sel} {
        .select--theme-dark#{&},
        .theme--dark &  {
            @include select-dropdown-arrow-svg-bg_(ffffff, 0.38);

            border-bottom: 1px dotted rgba(white, .38);
            color: $fancy-select-text-disabled-color;  // was text-disabled-on-dark.
        }
    }
}


.select__menu {
    .list-item {
        @include typography-subheading-2();

        color: $fancy-select-text-choices-color;  // was text-secondary-on-light.
        letter-spacing: 0.002rem;

        &[aria-selected="true"] {
            color: $form-focus-color;  // was text-primary-on-light.
        }

        .select--theme-dark &,
        .theme--dark & {
            color: $fancy-select-text-choices-color;  // was text-secondary-on-dark.

            &[aria-selected="true"] {
                color: $form-focus-color;  // was text-primary-on-dark.
            }
        }
    }

    .list-group,
    .list-group > .list-item:first-child {
        margin-top: 12px;
    }

    .list-group {
        color: $fancy-select-text-hint-color;  // was text-hint-on-light.
        font-weight: normal;

        .list-item {
            color: $fancy-select-primary-text-color;  // was text-primary-on-light.
        }
    }

    .select--theme-dark &,
    .theme--dark & {
        .list-group {
            color: $fancy-select-text-hint-color;  // was text-hint-on-dark.

            .list-item {
                color: $fancy-select-primary-text-color;  // was text-primary-on-dark.
            }
        }
    }
}

.multi-select {
    appearance: none;
    border-color: $fancy-select-text-hint-color;  // was text-hint-on-light.
    border-style: solid;
    border-width: 1px;
    outline: none;
    padding: 0;
    width: 250px;

    &--theme-dark,
    .theme--dark & {
        border-color: $fancy-select-text-hint-color;  // was text-hint-on-dark.
    }

    .list-group {
        color: $fancy-select-text-hint-color;  // was text-hint-on-light.
        font-weight: normal;
        margin: 16px 0 0;
        padding: 0 0 0 16px;

        &--theme-dark,
        .theme--dark & {
            color: $fancy-select-text-hint-color;  // was text-hint-on-dark.
        }

        &:last-child {
            margin-bottom: 16px;
        }

        .list-divider {
            margin-left: -16px;
        }
    }

    .list-item {
        color: $fancy-select-primary-text-color;  // was text-primary-on-light.
        margin: 0 0 0 -16px;
        padding: 0 16px;

        &--theme-dark,
        .theme--dark & {
            color: $fancy-select-primary-text-color;  // was text-primary-on-dark.
        }

        &:first-child {
            margin-top: 12px;
        }

        &:last-child {
            margin-bottom: 8px;
        }
    }

    .list-item:checked {
        // was background.
        background-color: $fancy-select-background-color;  // was background.

        &--theme-dark,
        .theme--dark & {
            background-color: $fancy-select-primary-text-color;  // was text-primary-on-dark.
        }
    }

    .list-divider {
        font-size: 0;
        margin-bottom: 8px;
        padding-top: 8px;
    }
}

.multi-select:focus .list-item:checked {
    background-color: $fancy-select-primary-color;  // was primary.

    &--theme-dark,
    .theme--dark & {
        background-color: $fancy-select-primary-text-color;  // was text-primary-on-dark.
    }
}

.has-error {
    &> select:not([role="listbox"]) {
        border-bottom-color: #F44336;
    }

    &> .select[role="listbox"] {
        &::after {
            background: #F44336;
            left: 0;
            width: 100%;
        }
    }
}
