// Adapted select-with-search component from govuk_publishing_components:
// https://github.com/alphagov/govuk_publishing_components/blob/d1840a1fcf5f8367dd10ad7aede9ce78184da019/app/assets/stylesheets/govuk_publishing_components/components/_select-with-search.scss
// Choices.js provides the base styles and progressive enhancement; the .gem-c-select-with-search layer
// restyles it to match the GOV.UK Design System.

@use "govuk/base" as govuk;

@use "node_modules/choices.js/src/styles/choices" with (
    $choices-font-size-lg: 19px,
    $choices-font-size-md: 19px,
    $choices-font-size-sm: 19px,
    $choices-border-radius: 0,
    $choices-border-radius-item: 0,
    $choices-bg-color: #ffffff,
    $choices-bg-color-dropdown: #ffffff,
    $choices-z-index: 2
);

.gem-c-select-with-search {
    .choices * {
        box-sizing: border-box;
        font-family: govuk.$govuk-font-family;
    }

    .choices[data-type="select-one"]
        .choices__list--dropdown
        .choices__list
        .choices__placeholder {
        opacity: 1;
    }

    .choices__placeholder {
        opacity: 0.6;
    }

    .choices[data-type*="select-one"]::after {
        display: inline-block;
        width: 0;
        height: 0;
        border-style: solid;
        border-color: transparent;
        clip-path: polygon(0% 0%, 50% 100%, 100% 0%);
        border-width: 8.66px 5px 0 5px;
        border-top-color: inherit;
        margin: 0;
        transform: translateY(-50%);
    }

    .choices.is-open[data-type*="select-one"]::after {
        margin: 0;
        bottom: 0.0526315789em;
        transform: translateY(-50%) rotate(180deg);
    }

    .choices[data-type*="select-multiple"] .choices__button,
    .choices[data-type*="text"] .choices__button {
        border-color: govuk.$govuk-border-colour;
        border-right: 1px solid govuk.$govuk-border-colour;
        padding: 10px 20px 10px 10px;
        margin-right: 0;

        &:hover {
            background-color: govuk.$govuk-border-colour;
            border-color: govuk.$govuk-secondary-text-colour;
            box-shadow: 0 2px govuk.$govuk-secondary-text-colour;
        }

        &:focus {
            background-color: govuk.$govuk-focus-colour;
            box-shadow: 0 2px govuk.$govuk-text-colour;
        }
    }

    .choices.is-disabled .choices__item[data-deletable] {
        background-color: govuk.govuk-colour("white");
    }

    .choices.is-disabled .choices__button {
        display: none;
    }

    .choices__input {
        display: block;
        margin-bottom: 0;
    }

    .choices__inner {
        padding: 5px;
        border: 2px solid govuk.$govuk-input-border-colour;
    }

    &.govuk-form-group--error
        .choices:not(.is-active):not(.is-focused):not(.is-open)
        .choices__inner {
        border-color: govuk.$govuk-error-colour;
    }

    .choices.is-focused,
    .choices.is-open {
        overflow: visible;
    }

    .choices.is-flipped .choices__list {
        border-radius: 0;
        border-width: 2px;
    }

    .choices.is-focused .choices__inner,
    .choices.is-open .choices__inner {
        outline: 3px solid govuk.$govuk-focus-colour;
        outline-offset: 0;
        box-shadow: inset 0 0 0 2px;
    }

    .choices__list--multiple {
        display: block;

        &:not(:empty) {
            margin-block-start: 6px;
            border-block-start: 1px solid govuk.$govuk-border-colour;
            padding-block-end: 5px;
        }

        .choices__item {
            display: inline-flex;
            align-items: center;
            border: 0;
            padding: 0 0 0 10px;
            margin: 10px 10px 0 0;
            background-color: govuk.govuk-colour("light-grey");
            box-shadow: 0 2px govuk.$govuk-border-colour;
            line-height: 1;
            color: govuk.$govuk-text-colour;
        }
    }

    .is-disabled .choices__list--multiple .choices__item {
        opacity: 0.5;
    }

    .choices__list--dropdown,
    .choices__list[aria-expanded] {
        border: 2px solid govuk.$govuk-input-border-colour;
        border-top-width: 0;
    }

    .is-flipped .choices__list--dropdown,
    .is-flipped .choices__list[aria-expanded] {
        border-top-width: 2px;
        border-bottom-width: 0;
    }

    .choices__list--dropdown .choices__item,
    .choices__list[aria-expanded] .choices__item {
        position: relative;
        border-bottom: 1px solid govuk.$govuk-border-colour;

        &:last-child {
            border-bottom: 0;
        }
    }

    .choices__list--dropdown .choices__item--selectable.is-highlighted,
    .choices__list[aria-expanded] .choices__item--selectable.is-highlighted {
        background-color: govuk.govuk-colour("blue");
        border-color: govuk.govuk-colour("blue");
        color: govuk.govuk-colour("white");
        outline: none;
    }

    .choices__heading {
        font-weight: 700;
        color: govuk.$govuk-text-colour;
        padding: 30px 10px 10px;
        border-bottom: 1px solid govuk.$govuk-border-colour;
        cursor: default;
    }

    // Our extension to the vendored component: optional hint text below each option in the dropdown,
    // provided via a `data-hint` attribute on the corresponding <option>.
    .app-select-with-search__option-hint {
        display: block;
        color: govuk.$govuk-secondary-text-colour;
        @include govuk.govuk-font($size: 16);
    }

    .choices__item.is-highlighted .app-select-with-search__option-hint {
        color: inherit;
    }
}
