@use 'sass:map';
@use '../utils/constants.scss';
@use '../utils/tokens.scss';

@import '@viasat/beam-tokens/components/Toast';

$toastBaseClass: '#{constants.$prefix}toast';
$toastCompBaseClass: '#{constants.$prefix}comp-toast';

@keyframes enterFirstAnimation {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
    z-index: 9999;
  }
}

@keyframes enterAnimation {
  0% {
    position: absolute;
    max-height: 0px;
    opacity: 0;
    z-index: 0;
  }

  30% {
    position: relative;
  }

  50% {
    max-height: 300px;
    opacity: 0;
  }

  100% {
    max-height: 300px;
    opacity: 1;
    z-index: 9999;
  }
}

@keyframes exitAnimation {
  0% {
    max-height: 300px;
    opacity: 1;
  }

  30% {
    max-height: 300px;
    opacity: 0;
  }

  100% {
    max-height: 0;
    opacity: 0;
  }
}

* {
  --#{$toastCompBaseClass}-animation-duration: 500ms;
}

$toast-animation-duration: var(--#{$toastCompBaseClass}-animation-duration);

$appearances: (
  'information': (
    'background': tokens.$bm-comp-toast-color-info-bg,
    'body': tokens.$bm-comp-toast-color-info-body,
    'border': tokens.$bm-comp-toast-color-info-border,
    'heading': tokens.$bm-comp-toast-color-info-heading,
    'highlight': tokens.$bm-comp-toast-color-info-highlight,
    'icon': tokens.$bm-comp-toast-color-info-icon,
  ),
  'negative': (
    'background': tokens.$bm-comp-toast-color-negative-bg,
    'body': tokens.$bm-comp-toast-color-negative-body,
    'border': tokens.$bm-comp-toast-color-negative-border,
    'heading': tokens.$bm-comp-toast-color-negative-heading,
    'highlight': tokens.$bm-comp-toast-color-negative-highlight,
    'icon': tokens.$bm-comp-toast-color-negative-icon,
  ),
  'positive': (
    'background': tokens.$bm-comp-toast-color-positive-bg,
    'body': tokens.$bm-comp-toast-color-positive-body,
    'border': tokens.$bm-comp-toast-color-positive-border,
    'heading': tokens.$bm-comp-toast-color-positive-heading,
    'highlight': tokens.$bm-comp-toast-color-positive-highlight,
    'icon': tokens.$bm-comp-toast-color-positive-icon,
  ),
  'warning': (
    'background': tokens.$bm-comp-toast-color-warning-bg,
    'body': tokens.$bm-comp-toast-color-warning-body,
    'border': tokens.$bm-comp-toast-color-warning-border,
    'heading': tokens.$bm-comp-toast-color-warning-heading,
    'highlight': tokens.$bm-comp-toast-color-warning-highlight,
    'icon': tokens.$bm-comp-toast-color-warning-icon,
  ),
);

.#{$toastBaseClass} {
  position: relative;
  display: flex;
  align-items: flex-start;
  border-radius: tokens.$bm-comp-toast-radius;
  border-width: tokens.$bm-comp-toast-border-width;
  box-shadow: tokens.$bm-comp-toast-shadow;
  gap: tokens.$bm-comp-toast-space-gap;
  padding: tokens.$bm-comp-toast-space-y tokens.$bm-comp-toast-space-x;
  width: tokens.$bm-comp-toast-size-width;
  z-index: 9999;

  &:focus,
  &:focus-visible,
  &:active {
    outline: none;
  }

  &__lil-diss-button {
    width: tokens.$bm-sem-size-icon-md;
    height: tokens.$bm-sem-size-icon-md;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &__content {
    flex-direction: column;
    display: flex;
    flex: 1;
    gap: tokens.$bm-comp-toast-space-text-gap;

    &--lite {
      gap: tokens.$bm-comp-toast-space-gap;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
    }
  }

  &__heading {
    font: tokens.$bm-comp-toast-typo-heading;
  }

  &__inner-content {
    display: flex;
    flex-direction: column;
    gap: tokens.$bm-comp-toast-space-content-gap;

    .#{$toastBaseClass}__content--lite & {
      flex-direction: row;
    }
  }

  &__body {
    display: flex;
    align-items: center;
    flex: 1;
    font: tokens.$bm-comp-toast-typo-body;
  }

  &__actions {
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
    white-space: nowrap;
    align-items: center;
    gap: tokens.$bm-comp-toast-space-actions-gap;
  }

  &--lite &__actions {
    align-items: flex-start;
  }

  &__icon {
    display: flex;
    width: tokens.$bm-comp-toast-size-icon;
    height: tokens.$bm-comp-toast-size-icon;

    ::slotted([slot='icon']) {
      width: tokens.$bm-comp-toast-size-icon;
      height: tokens.$bm-comp-toast-size-icon;
    }
  }

  &--lite {
    padding: tokens.$bm-comp-toast-space-lite-y tokens.$bm-comp-toast-space-x;
    width: auto;

    &::before {
      content: none;
      display: none;
    }
  }

  &--anim-entering {
    animation: enterAnimation 1s ease-in-out forwards;
  }

  &--anim-exiting {
    animation: exitAnimation 0.8s ease-out forwards;
  }

  &--anim-first-entering {
    animation: enterAnimation 0.4s ease-out forwards;
  }

  @each $state, $colors in $appearances {
    &--#{$state} {
      background-color: map.get($colors, 'background');
      outline: tokens.$bm-comp-toast-border-width solid map.get($colors, 'border');
      border-inline-start: tokens.$bm-comp-toast-size-highlight-width
        solid
        map.get($colors, 'highlight');

      .#{$toastBaseClass}__body {
        color: map.get($colors, 'body');
      }

      .#{$toastBaseClass}__heading {
        color: map.get($colors, 'heading');
      }

      .#{$toastBaseClass}__icon {
        color: map.get($colors, 'icon');
      }

      &.#{$toastBaseClass}--lite {
        border-inline-start: none;
      }
    }
  }
}
