@use "sass:math";
@use "../../core/settings" as *;
@use "../../core/tools" as *;
@use "../../core/helpers" as *;
@forward "../error-message";
@forward "../fieldset";
@forward "../hint";
@forward "../label";

////
/// Radios component
///
/// @group components/radios
////

$nhsuk-radios-size: nhsuk-px-to-rem(40px);
$nhsuk-radios-small-size: nhsuk-px-to-rem(24px);

$nhsuk-radios-label-padding-left-right: nhsuk-px-to-rem(12px);

// When the default focus width is used on a curved edge it looks visually
// smaller. So for the circular radios we bump the default to make it look
// visually consistent.
$nhsuk-radios-focus-width: nhsuk-px-to-rem($nhsuk-focus-width + 1px);

// Offset 2px due to 40px form control with 44px clickable area
// https://nhsuk.github.io/nhsuk-frontend/examples/form-alignment/
$nhsuk-radios-offset: $nhsuk-border-width-form-element;

@include nhsuk-exports("nhsuk/components/radios") {
  $nhsuk-touch-target-gutter: nhsuk-px-to-rem(4px);
  $nhsuk-touch-target-size: $nhsuk-radios-size + $nhsuk-touch-target-gutter;

  .nhsuk-radios {
    margin: -$nhsuk-radios-offset nhsuk-px-to-rem(-$nhsuk-radios-offset);
    margin-right: 0;
  }

  .nhsuk-radios__item {
    display: flex;
    position: relative;
    flex-wrap: wrap;
    margin-top: nhsuk-spacing(2) - $nhsuk-radios-offset;

    .nhsuk-radios__conditional:not(.nhsuk-radios__conditional--hidden) + & {
      @include nhsuk-responsive-margin(4, "top", $adjustment: - $nhsuk-radios-offset);
    }
  }

  .nhsuk-radios__item:first-child {
    margin-top: 0;
  }

  .nhsuk-radios__input {
    // Allow the input to sit above the label, enabling its proper detection
    // when exploring by touch or using automation tools like Selenium
    z-index: 1;

    width: $nhsuk-touch-target-size;
    height: $nhsuk-touch-target-size;
    margin: 0;

    opacity: 0;

    cursor: pointer;
  }

  .nhsuk-radios__label {
    box-sizing: border-box;

    align-self: center;

    // Ensure that the width of the label is never more than the width of the
    // container minus the input. This prevents the label from wrapping due to
    // __item using flex-wrap because we want hints on a separate line
    max-width: calc(100% - $nhsuk-touch-target-size);
    margin-bottom: 0;
    padding: (nhsuk-spacing(1) + $nhsuk-border-width-form-element) $nhsuk-radios-label-padding-left-right;

    cursor: pointer;
    // remove 300ms pause on mobile
    touch-action: manipulation;
  }

  // ( ) Radio ring
  .nhsuk-radios__label::before {
    content: "";

    box-sizing: border-box;

    position: absolute;
    top: math.div($nhsuk-touch-target-gutter, 2);
    left: math.div($nhsuk-touch-target-gutter, 2);

    width: $nhsuk-radios-size;
    height: $nhsuk-radios-size;

    border: $nhsuk-border-width-form-element solid $nhsuk-input-border-colour;
    border-radius: 50%;

    background: $nhsuk-input-background-colour;
  }

  //  •  Radio button
  //
  // We create the 'button' entirely out of 'border' so that they remain
  // 'filled' even when colours are overridden in the browser.
  .nhsuk-radios__label::after {
    $radio-button-size: nhsuk-px-to-rem(10px);

    content: "";

    position: absolute;

    // Positioned by getting half the touch target, so we have the centre of the
    // input, and then moving back by the button's border width, thus positioning
    // the centre of the button in the centre of the input.
    top: math.div($nhsuk-touch-target-size, 2) - $radio-button-size;
    left: math.div($nhsuk-touch-target-size, 2) - $radio-button-size;

    width: 0;
    height: 0;

    border: $radio-button-size solid currentcolor;
    border-radius: 50%;

    opacity: 0;

    background: currentcolor;
  }

  .nhsuk-radios__hint {
    display: block;
    width: 100%;
    margin-top: nhsuk-spacing(-1);
    padding-right: $nhsuk-radios-label-padding-left-right;
    padding-left: $nhsuk-radios-label-padding-left-right + $nhsuk-touch-target-size;
  }

  // This is to bypass nhsuk-hint's specificity on hints following labels having
  // a margin bottom of 8px (nhsuk-spacing(2)). Because radios are flexbox,
  // the margin doesn't collapse so we have to do this manually.
  .nhsuk-label:not(.nhsuk-label--m):not(.nhsuk-label--l):not(.nhsuk-label--xl) + .nhsuk-radios__hint {
    margin-bottom: 0;
  }

  // Focused state
  .nhsuk-radios__input:focus + .nhsuk-radios__label::before {
    @include nhsuk-focused-radio($nhsuk-radios-focus-width);
  }

  // Selected state
  .nhsuk-radios__input:checked + .nhsuk-radios__label::after {
    opacity: 1;
  }

  // Disabled state
  .nhsuk-radios__input:disabled,
  .nhsuk-radios__input:disabled + .nhsuk-radios__label {
    cursor: not-allowed;
  }

  .nhsuk-radios__input:disabled + .nhsuk-radios__label,
  .nhsuk-radios__input:disabled ~ .nhsuk-hint {
    opacity: 0.5;
  }

  // =========================================================
  // Inline radios
  // =========================================================

  .nhsuk-radios--inline {
    @include nhsuk-media-query($from: tablet) {
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
      row-gap: nhsuk-spacing(2) - $nhsuk-radios-offset;

      &.nhsuk-radios--small {
        row-gap: 0;
      }

      .nhsuk-radios__item {
        max-width: calc(50% - $nhsuk-radios-label-padding-left-right - nhsuk-spacing(4));
        margin-top: 0;
        margin-right: nhsuk-spacing(4);
      }
    }
  }

  // =========================================================
  // Dividers ('or')
  // =========================================================

  .nhsuk-radios__divider {
    width: $nhsuk-radios-size;
    margin: nhsuk-spacing(2) - $nhsuk-radios-offset nhsuk-px-to-rem($nhsuk-radios-offset);
    text-align: center;
    @include nhsuk-text-colour;
    @include nhsuk-font($size: 19);
  }

  // =========================================================
  // Conditional reveals
  // =========================================================

  // Calculate the amount of padding needed to keep the border centered against
  // the radios.
  $conditional-border-padding: math.div($nhsuk-touch-target-size, 2) - nhsuk-px-to-rem($nhsuk-border-width-form-element);
  // Move the border centered with the radios
  $conditional-margin-left: $conditional-border-padding;
  // Move the contents of the conditional inline with the label
  $conditional-padding-left: $conditional-border-padding + $nhsuk-radios-label-padding-left-right;

  .nhsuk-radios__conditional {
    margin-top: nhsuk-spacing(2) - $nhsuk-radios-offset;
    margin-bottom: $nhsuk-radios-offset;
    margin-left: $conditional-margin-left;
    padding-left: $conditional-padding-left;
    border-left: nhsuk-px-to-rem($nhsuk-border-width) solid nhsuk-colour("grey-3");

    .nhsuk-frontend-supported &--hidden {
      display: none;
    }
  }

  // =========================================================
  // Small radios
  // =========================================================

  .nhsuk-radios--small {
    $input-offset: math.div($nhsuk-touch-target-size - $nhsuk-radios-small-size, 2);

    .nhsuk-radios__item {
      margin-top: 0;
    }

    // Shift the touch target into the left margin so that the visible edge of
    // the control is aligned
    //
    //   ┆Which colour is your favourite?
    //  ┌┆───┐
    //  │┆() │ Purple
    //  └┆▲──┘
    //  ▲┆└─ Radio pseudo element, aligned with margin
    //  └─── Touch target (invisible input), shifted into the margin
    .nhsuk-radios__input {
      margin-left: $input-offset * -1;
    }

    .nhsuk-radios__label {
      max-width: calc(100% - $nhsuk-touch-target-size + $input-offset);

      // Create a tiny space between the small radio hover state so that it
      // doesn't clash with the label
      padding-left: $nhsuk-radios-label-padding-left-right - $input-offset;
    }

    // ( ) Radio ring
    //
    // Reduce the size of the control [1], vertically centering it within the
    // touch target [2]
    .nhsuk-radios__label::before {
      top: $input-offset; // 2
      left: nhsuk-px-to-rem($nhsuk-radios-offset);
      width: $nhsuk-radios-small-size; // 1
      height: $nhsuk-radios-small-size; // 1
    }

    //  •  Radio button
    //
    // Reduce the size of the 'button' and center it within the ring
    .nhsuk-radios__label::after {
      $radio-button-size: nhsuk-px-to-rem(5px);

      // The same calculation as normal radio buttons but reduce the border width
      top: math.div($nhsuk-touch-target-size, 2) - $radio-button-size;
      left: (math.div($nhsuk-touch-target-size, 2) - $radio-button-size) - $input-offset +
        nhsuk-px-to-rem($nhsuk-radios-offset);
      border-width: $radio-button-size;
    }

    // Fix position of hint with small radios
    .nhsuk-radios__hint {
      padding-left: $nhsuk-touch-target-size + $nhsuk-radios-label-padding-left-right - ($input-offset * 2);
    }

    // Align conditional reveals with small radios
    .nhsuk-radios__conditional {
      margin-left: $conditional-border-padding - $input-offset + nhsuk-px-to-rem($nhsuk-radios-offset);
      padding-left: $conditional-border-padding + $nhsuk-radios-label-padding-left-right -
        $input-offset - nhsuk-px-to-rem($nhsuk-radios-offset);
    }

    .nhsuk-radios__divider {
      width: $nhsuk-radios-small-size;
      margin: nhsuk-spacing(1) - $nhsuk-radios-offset nhsuk-px-to-rem($nhsuk-radios-offset);
    }

    // Hover state for small radios.
    //
    // We use a hover state for small radios because the touch target size
    // is so much larger than their visible size, and so we need to provide
    // feedback to the user as to which radio they will select when their
    // cursor is outside of the visible area.
    .nhsuk-radios__input:not(:disabled):hover + .nhsuk-radios__label::before,
    .nhsuk-radios__input:not(:disabled) + .nhsuk-radios__label:hover::before {
      // Forced colours modes tend to ignore box-shadow.
      // Apply an outline for those modes to use instead.
      outline: $nhsuk-radios-focus-width dashed transparent;
      outline-offset: 1px;
      box-shadow: 0 0 0 $nhsuk-hover-width $nhsuk-hover-colour;
    }

    // Because we've overridden the border-shadow provided by the focus state,
    // we need to redefine that too.
    //
    // We use two box shadows, one that restores the original focus state [1]
    // and another that then applies the hover state [2].
    .nhsuk-radios__input:focus:hover + .nhsuk-radios__label::before,
    .nhsuk-radios__input:focus + .nhsuk-radios__label:hover::before {
      // prettier-ignore
      box-shadow:
        0 0 0 $nhsuk-radios-focus-width $nhsuk-focus-colour, // 1
        0 0 0 $nhsuk-hover-width $nhsuk-hover-colour; // 2

      // Set different HCM colour when we have both hover/focus applied at once
      @media screen and (forced-colors: active), (-ms-high-contrast: active) {
        outline-color: Highlight;
      }
    }

    // For devices that explicitly don't support hover, don't provide a hover
    // state (e.g. on touch devices like iOS).
    //
    // We can't use `@media (hover: hover)` because we wouldn't get the hover
    // state in browsers that don't support `@media (hover)` (like Internet
    // Explorer) – so we have to 'undo' the hover state instead.
    @media (hover: none), (pointer: coarse) {
      .nhsuk-radios__input:not(:disabled):hover + .nhsuk-radios__label::before,
      .nhsuk-radios__input:not(:disabled) + .nhsuk-radios__label:hover::before {
        box-shadow: initial;
      }

      .nhsuk-radios__input:focus:hover + .nhsuk-radios__label::before,
      .nhsuk-radios__input:focus + .nhsuk-radios__label:hover::before {
        box-shadow: 0 0 0 $nhsuk-radios-focus-width $nhsuk-focus-colour;
      }
    }
  }
}
