@use "../../../scss/shared";

:host {
  display: inline-block;
  cursor: pointer;
}

:host([disabled]) {
  cursor: wait;
}

.animated-button {
  @include shared.button;
  position: relative;
  overflow: hidden;
  min-width: 150px;
  min-height: 40px;
  gap: 8px;
  transition:
    transform var(--zn-transition-fast) ease,
    opacity var(--zn-transition-fast) ease;
  border: none;
  font-size: 1rem;
  font-family: inherit;
  cursor: pointer;

  &:hover:not(:disabled) {
    opacity: 0.9;
  }

  &:active:not(:disabled) {
    transform: scale(0.98);
  }

  &:disabled {
    cursor: wait;
  }

  &:focus-visible {
    outline: 2px solid rgb(var(--zn-primary));
    outline-offset: 2px;
  }
}

.animated-button__content {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  z-index: 1;
}

.animated-button__text {
  font-weight: 600;
  line-height: 1;
}

.animated-button__icon {
  display: inline-flex;
  flex-shrink: 0;
}

.animated-button__spinner {
  display: inline-flex;
  flex-shrink: 0;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

// Shimmer animation for processing state
.animated-button__shimmer {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.3) 50%,
    rgba(255, 255, 255, 0) 100%
  );
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

// State: Idle (default blue)
.animated-button--idle {
  background-color: rgb(var(--zn-primary)) !important;
}

// State: Processing (blue with shimmer)
.animated-button--processing {
  background-color: rgb(var(--zn-primary)) !important;
  cursor: wait;
}

// State: Success (green with fill animation)
.animated-button--success {
  background-color: rgb(var(--zn-primary)) !important;
  cursor: default;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgb(var(--zn-color-success));
    animation: successFill var(--zn-transition-slow) ease-out forwards;
    z-index: 0;
  }

  .animated-button__content {
    z-index: 1;
  }

  .animated-button__icon--success {
    opacity: 0;
    animation: iconFadeIn 0.3s ease-out 0.4s forwards;
  }

  .animated-button__tick {
    stroke-dasharray: 30;
    stroke-dashoffset: 30;
    animation: tickSwoosh 0.5s ease-out 0.4s forwards;
  }
}

@keyframes successFill {
  0% {
    transform: translateY(100%);
  }
  100% {
    transform: translateY(0);
  }
}

@keyframes iconFadeIn {
  to {
    opacity: 1;
  }
}

@keyframes tickSwoosh {
  0% {
    stroke-dashoffset: 30;
  }
  100% {
    stroke-dashoffset: 0;
  }
}

// State: Failure (red)
.animated-button--failure {
  background-color: rgb(var(--zn-color-error)) !important;
  cursor: not-allowed;

  .animated-button__icon--failure {
    animation: failureShake var(--zn-transition-medium) ease-out;
  }
}

@keyframes failureShake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

// Respect user's motion preferences
@media (prefers-reduced-motion: reduce) {
  .animated-button {
    transition: none;
  }

  .animated-button--success::before {
    animation: none;
    transform: translateY(0);
  }

  .animated-button__shimmer {
    animation: none;
    display: none;
  }

  .animated-button__spinner {
    animation: none;
  }

  .animated-button__icon--success {
    animation: none;
    opacity: 1;
  }

  .animated-button__tick {
    animation: none;
    stroke-dashoffset: 0;
  }

  .animated-button__icon--failure {
    animation: none;
  }
}
