@use 'sass:map';
@use 'sass:math';
@use 'sass:string';
@use '../tools' as *;

// =============================================================================
// Variables
// =============================================================================

$size-home-button: 3.5em;
$position: 2em;
$box-shadow-props:
  0 0 1px 0 rgba(107, 214, 230, 1),
  0 1px 10px 0 rgba(107, 214, 230, 0.7);
$max-items: 12;
$userbar-radius: 6px;
$userbar-z-index: 9999;

// Possible positions for the userbar to exist in. These are set through the
// {% wagtailuserbar 'bottom-left' %} template tag.
$positions: (
  'top-left': (
    'vertical': 'top',
    'horizontal': 'inline-start',
    'horizontal-inv': 'inline-end',
  ),
  'top-right': (
    'vertical': 'top',
    'horizontal': 'inline-end',
    'horizontal-inv': 'inline-start',
  ),
  'bottom-left': (
    'vertical': 'bottom',
    'horizontal': 'inline-start',
    'horizontal-inv': 'inline-end',
  ),
  'bottom-right': (
    'vertical': 'bottom',
    'horizontal': 'inline-end',
    'horizontal-inv': 'inline-start',
  ),
);

// =============================================================================
// Wagtail userbar proper
// =============================================================================
.w-userbar {
  position: fixed;
  z-index: $userbar-z-index;
  font-size: initial;
  line-height: initial;
  margin: 0;
  padding: 0;
  // Stop hiding the userbar once stylesheets are loaded.
  // stylelint-disable-next-line declaration-no-important
  display: block !important;
  border: 0;
  width: auto;
  height: auto;

  &-icon {
    @include svg-icon(2em);
  }
}

@media print {
  .w-userbar {
    display: none;
  }
}

.w-userbar-trigger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: $size-home-button;
  height: $size-home-button;
  margin: 0;
  background-color: theme('colors.white.DEFAULT');
  border: 2px solid transparent;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  box-shadow: $box-shadow-props;
  transition: all 0.2s ease-in-out;
  font-size: 1rem;
  text-decoration: none;
  position: relative;

  .w-userbar-axe-count {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: theme('colors.critical.200');
    border-radius: theme('borderRadius.full');
    color: theme('colors.text-button');
    font-size: theme('fontSize.14');
    line-height: theme('lineHeight.none');
    height: theme('spacing.5');
    width: theme('spacing.5');
    position: absolute;
    inset-inline-end: -5px;
    top: -5px;

    @media (forced-colors: active) {
      border: theme('spacing.px') solid ButtonText;
    }
  }

  &:focus {
    outline: theme('colors.focus') solid 3px;
  }
}

.w-userbar-items {
  display: block;
  list-style: none;
  position: absolute;
  margin: 0;
  min-width: 210px;
  visibility: hidden;
  font-family: theme('fontFamily.sans');
  font-size: 0.875rem;
  padding-inline-start: 0;
  text-decoration: none;

  .w-userbar--active & {
    visibility: visible;
  }
}

.w-userbar-nav {
  background: transparent;
  padding: 0;
  margin: 0;
  display: block;

  .w-action {
    background: transparent;
  }
}

.w-userbar__item {
  margin: 0;
  background-color: theme('colors.surface-menus');
  opacity: 0;
  overflow: hidden;
  transition-duration: 0.125s;
  transition-timing-function: cubic-bezier(0.55, 0, 0.1, 1);
  font-family: theme('fontFamily.sans');
  font-size: 1rem;
  text-decoration: none;

  @media (prefers-reduced-motion: reduce) {
    transition: none;

    // Force disable transitions for all items
    transition-delay: 0s;
  }

  &:first-child {
    border-start-start-radius: $userbar-radius;
    border-start-end-radius: $userbar-radius;
  }

  &:last-child {
    border-end-end-radius: $userbar-radius;
    border-end-start-radius: $userbar-radius;
  }

  a,
  .w-action,
  button {
    color: theme('colors.text-label-menus-default');
    display: block;
    text-decoration: none;
    transform: none;
    transition: none;
    margin: 0;
    font-size: 0.875rem;

    &:hover,
    &:focus {
      color: theme('colors.text-label-menus-default');
      background-color: theme('colors.surface-menu-item-active');
    }

    &:focus {
      outline: theme('colors.focus') solid 3px;
    }

    &-icon {
      @include svg-icon(1em, middle);
      margin-inline-end: 0.5em;
      fill: currentColor;
      opacity: 0.4;
    }

    .w-a11y-result__count {
      margin-inline-end: theme('spacing.2');
    }
  }

  a,
  button {
    font-size: 0.875rem;
    text-align: start;
    padding: theme('spacing.[3.5]');
  }

  button {
    border: 0;
    width: 100%;
    background-color: transparent;
    outline: none;
    display: flex;
    align-items: center;
  }
}

.w-dialog--userbar {
  // Display off to the side of the page rather than in the middle.
  font-family: theme('fontFamily.sans');
  z-index: $userbar-z-index;

  .w-dialog__close-button {
    border-radius: theme('borderRadius.full');
    border: 2px solid theme('colors.icon-primary');
    background: theme('colors.surface-page');
    z-index: calc($userbar-z-index + 1);
  }

  .w-dialog__close-icon {
    color: theme('colors.text-context');
  }

  .w-dialog__content {
    padding: 0;
    min-height: unset;
    max-height: 60vh;
    font-size: theme('fontSize.14');
  }

  .w-dialog__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .w-dialog__body {
    padding: 0 theme('spacing.[7.5]') theme('spacing.[7.5]');
    display: flex;
    flex-direction: column;
    gap: theme('spacing.[2.5]');
  }

  .w-dialog__title {
    @apply w-h3;
    color: theme('colors.text-context');
    padding: theme('spacing.[7.5]');
    margin-bottom: 0;
  }

  .w-dialog__subtitle {
    @apply w-body-text;
    padding-inline-end: theme('spacing.[7.5]');
    display: flex;
    align-items: center;
    gap: theme('spacing.2');
    margin-bottom: 0;
  }
}

//Media for Windows High Contrast

@media (forced-colors: active) {
  .w-userbar-icon {
    fill: LinkText;
  }

  .w-userbar__item {
    border: 1px solid ButtonText;
  }
}

// =============================================================================
// Userbar positional classes (tl, tr, bl, br)
// =============================================================================

@each $pos, $attrs in $positions {
  $vertical: map.get($attrs, vertical);
  $horizontal: map.get($attrs, horizontal);
  $horizontal-inv: map.get($attrs, horizontal-inv);

  .w-userbar--#{$pos} {
    #{$vertical}: $position;
    inset-#{$horizontal}: $position;

    .w-userbar-items {
      #{$vertical}: 100%;
      inset-#{$horizontal}: 0;
      padding-#{$vertical}: theme('spacing.2');
    }

    .w-userbar__item {
      @if $vertical == 'bottom' {
        transform: translateY(1em);
      } @else {
        transform: translateY(-1em);
      }
    }

    &.w-userbar--active .w-userbar__item {
      @for $i from 1 through $max-items {
        @if $vertical == 'bottom' {
          &:nth-last-child(#{$i}) {
            transition-delay: 0.05s * $i;
          }
        }

        @if $vertical == 'top' {
          &:nth-child(#{$i}) {
            transition-delay: 0.05s * $i;
          }
        }
      }
    }

    .w-dialog--userbar {
      inset-#{$horizontal-inv}: auto;
      padding-#{$horizontal}: theme('spacing.8');

      .w-dialog__close-button {
        $size: theme('spacing.6');
        width: $size;
        height: $size;
        inset-#{$horizontal}: calc(-1 * $size / 2);
        top: calc(-1 * $size / 2);
      }
    }
  }
}

// =============================================================================
// States
// =============================================================================

// Active state for the list items comes last.
.w-userbar--active .w-userbar__item {
  transform: translateY(0);
  opacity: 1;
}
