/* stylelint-disable max-nesting-depth */

@use 'sass:map';
@use '../../global/scss/tools' as nsw;

.nsw-form {
  margin-top: nsw.rem(48px);

  &:first-child {
    margin-top: 0;
  }

  &__group {
    margin-top: nsw.rem(16px);

    &:first-child {
      margin-top: 0;
    }
  }

  &__helper {
    display: block;
    margin-bottom: nsw.rem(4px);

    @include nsw.font-size('xs');

    &--error,
    &--valid {
      margin-top: nsw.rem(8px);
      padding: nsw.rem(8px);
      font-weight: var(--nsw-font-bold);
      color: var(--nsw-text-dark);
      background-repeat: no-repeat;
      background-position: left nsw.rem(8px) top nsw.rem(8px);
      background-size: 1rem auto;
      display: flex;
      align-items: center;

      .nsw-material-icons {
        font-size: nsw.rem(map.get(nsw.$nsw-icon-sizes, 20));
        margin-right: nsw.rem(4px);
      }
    }

    &--error {
      background-color: var(--nsw-status-error-bg);

      .nsw-material-icons {
        color: var(--nsw-status-error);
      }
    }

    &--valid {
      background-color: var(--nsw-status-success-bg);

      .nsw-material-icons {
        color: var(--nsw-status-success);
      }
    }
  }

  &__label {
    font-weight: var(--nsw-font-bold);
    display: block;

    @include nsw.font-size('sm');

    &--small {
      font-weight: var(--nsw-font-normal);
    }

    + select {
      cursor: pointer;
    }

    + input,
    + textarea,
    + select,
    + .nsw-form__input-group {
      margin-top: nsw.rem(4px);
    }
  }

  &__required {
    &::after {
      content: ' *';
      color: var(--nsw-status-error);
    }
  }

  &__input {
    padding: nsw.rem(11px) nsw.rem(16px);
    border-radius: var(--nsw-border-radius);
    background-color: var(--nsw-white);
    color: var(--nsw-text-dark);
    border: 1px solid var(--nsw-grey-01);
    vertical-align: middle;
    appearance: none;
    -webkit-appearance: none; /* stylelint-disable-line property-no-vendor-prefix */
    width: 100%;

    @include nsw.font-size('sm');

    &:focus {
      @include nsw.nsw-focus;

      .nsw-section--invert & {    
        @include nsw.nsw-focus($color: var(--nsw-focus-light));
      }
    }

    &:disabled,
    &[readonly='true'] {
      background: var(--nsw-off-white);
      border-color: var(--nsw-grey-03);
      color: var(--nsw-grey-01);
      cursor: not-allowed;
      opacity: 1;

      &:hover {
        background-image: none;
      }
    }

    &[aria-invalid='true'],
    &.has-error {
      border-color: var(--nsw-status-error);
      border-width: 2px;
    }

    &:hover {
      background-image: linear-gradient(var(--nsw-hover), var(--nsw-hover));
    }
  
    &[aria-invalid='true'],
    &.has-error {
      &:hover {
        background: var(--nsw-status-error-bg);
      }
    }
  }

  &__input-group {
    display: flex;

    .nsw-form__input {
      border-right-width: 0;
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
      
      &:focus {
        position: relative;
      }
    }

    .nsw-button {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 nsw.rem(24px);
      margin: 0;
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
      border-width: 1px;
      white-space: nowrap;
      border-color: var(--nsw-grey-01);

      .nsw-section--invert & {    
        color: var(--nsw-text-light);
        background-color: var(--nsw-brand-dark);
        border-color: var(--nsw-white);
      }

      &:focus {
        @include nsw.nsw-focus();

        .nsw-section--invert & {    
          @include nsw.nsw-focus($color: var(--nsw-focus-light));
        }
      }
    }

    .nsw-material-icons {
      display: block;
      font-size: nsw.rem(map.get(nsw.$nsw-icon-sizes, 36));
      position: static;

      .nsw-section--invert & {    
        color: var(--nsw-text-light);

        &:hover {
          color: var(--nsw-text-light);
        }
      }
    }

    &--icon {
      .nsw-button {
        padding: 0;
        min-width: nsw.rem(60px);
        border-color: var(--nsw-grey-01);
        border-left-width: 0;

        .nsw-material-icons:first-child {
          margin-right: 0;
        }

        .nsw-section--invert & {  
          color: var(--nsw-white);
          border-color: var(--nsw-white);

          &:hover {
            @include nsw.nsw-hover-light;
          }

          .nsw-material-icons {
            color: var(--nsw-text-light);
          }
        }
      }

      .nsw-button--white {
        .nsw-material-icons {
          color: var(--nsw-brand-dark);

          .nsw-section--invert & {    
            color: var(--nsw-text-light);
          }
        }
      }
    }

    &--large {
      .nsw-form__input {
        padding: nsw.rem(16px);
      }
    }
  }

  &__predictive {
    position: relative;
  }

  &__predictive-list {
    @include nsw.z-index;
    border-radius: var(--nsw-border-radius);
    border-top-left-radius: 0;
    border-top-right-radius: 0;
    background-color: var(--nsw-white);
    box-shadow: 0 4px 12px 0 rgba(var(--nsw-black-rgb), 0.15);
    list-style-type: none;
    padding: 0;
    margin: 0;
    position: absolute;
    top: nsw.rem(48px);
    right: 0;
    left: 0;
    overflow: hidden;

    &--large {
      top: nsw.rem(58px);
    }

    li {
      padding: 0;
      margin: 0;
    }

    a {
      display: block;
      padding: nsw.rem(10px);
      color: var(--nsw-text-dark);
      font-weight: var(--nsw-font-normal);

      &:hover {
        @include nsw.nsw-hover;
        outline-width: 0;
      }

      &:focus {
        outline-width: 3px;
        outline-offset: -3px;
      }

      span {
        font-weight: var(--nsw-font-bold);
      }

      .nsw-section--invert & {    
        color: var(--nsw-text-dark);

        &:hover {
          @include nsw.nsw-hover;
        }

        &:focus {
          outline-color: var(--nsw-focus-light);
        }
      }
    }
  }

  &__fieldset {
    margin: 0;
    padding: 0;
    border: 0;
    /* autoprefixer: ignore next */
    display: contents;

    legend {
      padding: 0;
      margin: 0 0 nsw.rem(16px) 0;
      display: block;
    }
  }

  &__legend {
    font-weight: var(--nsw-font-bold);
    width: 100%;
    display: block;

    @include nsw.font-size('sm');
  }

  &__checkbox-input,
  &__radio-input {
    position: absolute;
    opacity: 0;

    &:first-child + .nsw-form__checkbox-label,
    &:first-child + .nsw-form__radio-label {
      margin-top: 0;
    }

    &:hover + .nsw-form__checkbox-label::before,
    &:hover + .nsw-form__radio-label::before {
      background-image: linear-gradient(var(--nsw-hover), var(--nsw-hover));
    }

    &:checked + .nsw-form__checkbox-label::after {
      background-image: nsw.svg-url(nsw.$nsw-form-tick);
      background-color: var(--nsw-brand-dark);
    }

    &:checked + .nsw-form__radio-label::after {
      background-color: var(--nsw-brand-dark);
    }

    &:disabled,
    &[readonly='true'] {
      + .nsw-form__checkbox-label,
      + .nsw-form__radio-label {
        cursor: not-allowed;
      }

      + .nsw-form__checkbox-label::before,
      + .nsw-form__radio-label::before {
        border-color: var(--nsw-grey-03);
        background-color: var(--nsw-off-white);
      }

      &:checked + .nsw-form__checkbox-label::after,
      &:checked + .nsw-form__radio-label::after {
        background-color: var(--nsw-brand-dark);
        opacity: 0.2;
      }

      &:hover + .nsw-form__checkbox-label::before,
      &:hover + .nsw-form__radio-label::before {
        background-image: none;
      }
    }

    &:focus + .nsw-form__checkbox-label::before {
      @include nsw.nsw-focus;

      .nsw-section--invert & {   
        @include nsw.nsw-focus($color: var(--nsw-focus-light));
      }
    }

    &:focus + .nsw-form__radio-label::before {
      box-shadow: 0 0 0 3px var(--nsw-text-light), 0 0 0 6px var(--nsw-focus);

      .nsw-section--invert & {
        box-shadow: 0 0 0 3px var(--nsw-text-dark), 0 0 0 6px var(--nsw-focus-light);
      }
    }

    &[aria-invalid='true'] + .nsw-form__checkbox-label,
    &.has-error + .nsw-form__checkbox-label {
      &:hover::before {
        background-image: linear-gradient(var(--nsw-status-error-bg), var(--nsw-status-error-bg));
      }

      &::before {
        border-width: 2px;
        border-color: var(--nsw-status-error);
      }
    }
  }

  &__checkbox-label,
  &__radio-label {
    margin: nsw.rem(16px) 0 0 0;
    padding: nsw.rem(4px) 0 nsw.rem(4px) nsw.rem(48px);
    min-height: nsw.rem(32px);
    display: block;
    position: relative;
    cursor: pointer;
    
    @include nsw.font-size('sm');

    &::before {
      content: '';
      width: nsw.rem(32px);
      height: nsw.rem(32px);
      margin-right: nsw.rem(16px);
      border: 1px var(--nsw-grey-01) solid;
      top: 0;
      left: 0;
      display: inline-block;
      vertical-align: middle;
      background-color: var(--nsw-white);
      position: absolute;
    }

    &::after {
      content: '';
      position: absolute;
      width: nsw.rem(22px);
      height: nsw.rem(22px);
      background-repeat: no-repeat;
      background-position: center center;
      background-size: nsw.rem(16px) auto;
      left: nsw.rem(5px);
      top: nsw.rem(5px);
    }
  }

  &__checkbox-label {
    &::before {
      border-radius: var(--nsw-border-radius);
    }
  }

  &__radio-label {
    &::before {
      border-radius: 50%;
    }

    &::after {
      border-radius: 50%;
    }
  }
}
