@use '../utils/constants.scss';
@use '../utils/tokens.scss' as tokens;
@use './icon.vars.scss' as iconVars;

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

$dialogBaseClass: '#{constants.$prefix}dialog';

// Fluent 2 dialog motion duration (durationGentle).
$dialog-motion-duration: 0.25s;

$sizes: (
  'sm': tokens.$bm-comp-dialog-size-sm,
  'md': tokens.$bm-comp-dialog-size-md,
  'lg': tokens.$bm-comp-dialog-size-lg,
  'xl': tokens.$bm-comp-dialog-size-xl,
);

$appearances: (
  'information': tokens.$bm-comp-dialog-color-icon-info,
  'positive': tokens.$bm-comp-dialog-color-icon-positive,
  'negative': tokens.$bm-comp-dialog-color-icon-negative,
  'warning': tokens.$bm-comp-dialog-color-icon-warning,
);

.#{$dialogBaseClass} {
  position: relative;
  outline: none;
  background: tokens.$bm-comp-dialog-color-bg;
  border-color: tokens.$bm-comp-dialog-color-border;
  border-radius: tokens.$bm-comp-dialog-radius-default;
  border-width: tokens.$bm-comp-dialog-border-width;
  box-shadow: tokens.$bm-comp-dialog-shadow;

  display: flex;
  flex-direction: column;
  max-height: 100%;

  // Fluent 2 surface motion: fade + scale, driven by FloatingUI's data-status.
  // Enter uses a decelerate curve, exit an accelerate curve (durationGentle = 250ms).
  // The backdrop scrim fades separately on the overlay (see floatingui.module.scss).
  opacity: 0;
  transform: scale(0.85);
  transition:
    opacity $dialog-motion-duration cubic-bezier(0.8, 0, 0.78, 1),
    transform $dialog-motion-duration cubic-bezier(0.8, 0, 0.78, 1);

  &[data-status='open'] {
    opacity: 1;
    transform: scale(1);
    transition-timing-function: cubic-bezier(0, 0, 0, 1);
  }

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

  @each $size, $sizeValue in $sizes {
    &--#{$size} {
      width: $sizeValue;
      max-width: $sizeValue;
    }
  }

  // One-off structural variant: fills the overlay's padded, centered area so it keeps
  // the standard viewport-offset gap and never touches the screen edge. Relies on the
  // overlay's existing flex centering + padding (no overlay changes needed). Can't use
  // the $sizes width map.
  &--full {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
  }

  &--full &__body {
    flex: 1;
  }
}

.#{$dialogBaseClass}__header {
  font: tokens.$bm-comp-dialog-typo-heading;
  color: tokens.$bm-comp-dialog-color-heading;
  padding-block: tokens.$bm-comp-dialog-space-heading-top 0;
  padding-inline: tokens.$bm-comp-dialog-space-x;
  display: flex;
  flex-direction: column;
  gap: tokens.$bm-comp-dialog-space-heading-gap-y;
  flex-shrink: 0;

  &__row {
    display: flex;
    gap: tokens.$bm-comp-dialog-space-heading-gap-x;
    justify-content: flex-start;
    width: 100%;
  }

  &__icon {
    height: 1lh;
    color: tokens.$bm-comp-dialog-color-icon;

    display: flex;
    align-items: center;

    @each $appearance, $color in $appearances {
      &--#{$appearance} {
        color: $color;
        #{iconVars.$iconColor}: $color;
      }
    }
  }

  &__icon svg {
    height: tokens.$bm-comp-dialog-size-icon;
    width: tokens.$bm-comp-dialog-size-icon;

    #{iconVars.$iconSize}: tokens.$bm-comp-dialog-size-icon;
    #{iconVars.$iconColor}: tokens.$bm-comp-dialog-color-icon;
  }

  &__heading {
    margin: 0;
    font: inherit;
  }

  &__close-trigger {
    height: 1lh;
    width: 1lh;
    margin-left: auto;
  }

  &__description {
    margin: 0;
  }

  &__description,
  &__children {
    font: tokens.$bm-comp-dialog-typo-description;
    color: tokens.$bm-comp-dialog-color-description;
  }

  &__divider {
    border: none;
    border-top: tokens.$bm-sem-border-width-md solid tokens.$bm-sem-color-border-01;

    margin-block: tokens.$bm-comp-dialog-size-divider-wrapper 0;
    margin-inline: calc(tokens.$bm-comp-dialog-space-x * -1);
  }
}

.#{$dialogBaseClass}__body {
  font: tokens.$bm-comp-dialog-typo-body;
  color: tokens.$bm-comp-dialog-color-body;
  padding-block: tokens.$bm-comp-dialog-space-body-y;
  padding-inline: tokens.$bm-comp-dialog-space-x;

  display: flex;
  flex-direction: column;
  gap: tokens.$bm-comp-dialog-space-body-gap;

  overflow: auto;
  overscroll-behavior: contain;

  &:focus {
    outline: var(--bm-sem-border-width-focus) auto var(--bm-sem-color-border-focus);
  }
}

.#{$dialogBaseClass}__footer {
  flex-shrink: 0;
  padding-inline: tokens.$bm-comp-dialog-space-x;

  &__divider {
    border-top: tokens.$bm-sem-border-width-md solid tokens.$bm-sem-color-border-01;
    display: block;
  }

  padding-block: tokens.$bm-comp-dialog-space-footer-y;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
  justify-content: space-between;
  gap: tokens.$bm-comp-dialog-space-footer-gap;

  &__actions {
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    gap: tokens.$bm-comp-dialog-space-footer-gap;
  }

  &__tertiaryActions {
    display: flex;
  }
}

/**
 * Mobile Styles
 */
.#{$dialogBaseClass} {
  @media all and (max-width: 512px) {
    &__header,
    &__body,
    &__footer {
      padding-inline: tokens.$bm-comp-dialog-space-sm-screen-x;
    }

    &__header {
      &__divider {
        margin-inline: calc(tokens.$bm-comp-dialog-space-sm-screen-x * -1);
      }
    }

    &__footer {
      flex-direction: column;
      gap: tokens.$bm-comp-dialog-space-footer-sm-screen-gap;

      &__actions,
      &__tertiaryActions {
        width: 100%;
        align-self: stretch;
        justify-content: center;

        > *,
        > [class*='bm-'] {
          width: 100%;
        }
      }

      &__actions {
        flex-direction: column;
        gap: tokens.$bm-comp-dialog-space-footer-sm-screen-gap;
      }
    }
  }
}
