@use 'sass:list'
@use '../../styles/settings'
@use '../../styles/tools'
@use './variables' as *

// ease-in starves retargeted transitions, so frequent value updates would lag far behind
$split-transition: var(--v-progress-circular-transition-duration) settings.$decelerated-easing

@property --v-progress-circular-wave-phase
  syntax: "<number>"
  inherits: true
  initial-value: 0
@property --v-progress-circular-head
  syntax: "<number>"
  inherits: true
  initial-value: 0

@include tools.layer('components')
  // Elements
  .v-progress-circular
    --v-progress-circular-transition-duration: #{$progress-circular-transition-duration}

    align-items: center
    display: inline-flex
    justify-content: center
    overflow: hidden
    position: relative
    vertical-align: middle

    > svg
      width: 100%
      height: 100%
      margin: auto
      position: absolute
      top: 0
      bottom: 0
      left: 0
      right: 0
      z-index: 0

  .v-progress-circular__content
    align-items: center
    display: flex
    justify-content: center

  .v-progress-circular__underlay
    color: $progress-circular-underlay-color
    stroke: currentColor
    z-index: 1

  .v-progress-circular__overlay
    stroke: currentColor
    transition: $progress-circular-overlay-transition
    z-index: 2

  // Dash lengths are in percent of one turn: paths use pathLength and --from/--to are unitless
  .v-progress-circular__split
    > .v-progress-circular__track,
    > .v-progress-circular__wave
      --v-progress-circular-length: max(0, var(--v-progress-circular-to) - var(--v-progress-circular-from))
      fill: none
      stroke-dasharray: calc(var(--v-progress-circular-length) * 1px) 400px
      // a zero-length dash still draws its round caps
      stroke-width: clamp(0px, var(--v-progress-circular-length) * 1000px, var(--v-progress-circular-stroke))

    > .v-progress-circular__track
      --v-progress-circular-from: calc(var(--v-progress-circular-value) + min(var(--v-progress-circular-value), var(--v-progress-circular-gap)))
      --v-progress-circular-to: calc(100 - min(var(--v-progress-circular-value), var(--v-progress-circular-gap)))
      stroke-dashoffset: calc(var(--v-progress-circular-from) * -1px)
      transition: stroke-dasharray $split-transition, stroke-dashoffset $split-transition

    > .v-progress-circular__wave
      --v-progress-circular-from: 0
      --v-progress-circular-to: var(--v-progress-circular-value)
      stroke: currentColor
      stroke-dashoffset: calc((var(--v-progress-circular-from) + var(--v-progress-circular-wave-phase) * 100 / var(--v-progress-circular-waves)) * -1px)
      transform: rotate(calc(var(--v-progress-circular-wave-phase) * -360deg / var(--v-progress-circular-waves)))
      transform-origin: center center
      transition: stroke-dasharray $split-transition, d $split-transition

  .v-progress-circular--shape-wavy .v-progress-circular__wave
    animation: progress-circular-wave 1s linear infinite

  @media (prefers-reduced-motion: reduce)
    .v-progress-circular--shape-wavy .v-progress-circular__wave
      animation: none

  @include tools.layer('trumps')
    @media (forced-colors: active)
      .v-progress-circular__wave
        stroke: highlight

  .v-progress-circular--revealing
    --v-progress-reveal-duration: #{$progress-circular-reveal-duration}

    .v-progress-circular__overlay
      transition-duration: var(--v-progress-reveal-duration), 0s

    .v-progress-circular__track,
    .v-progress-circular__wave
      transition-duration: var(--v-progress-reveal-duration)

  // Modifiers
  .v-progress-circular--no-transition
    --v-progress-circular-transition-duration: 0s

  .v-progress-circular
    @each $name, $multiplier in $progress-circular-sizes
      $size: $progress-circular-size + (settings.$size-scale * $multiplier)

      &--size-#{$name}
        height: $size
        width: $size

  .v-progress-circular--indeterminate
    > svg
      animation: $progress-circular-rotate-animation
      transform-origin: center center
      transition: $progress-circular-intermediate-svg-transition

    &.v-progress-circular--variant-split > svg,
    &.v-progress-circular--shape-wavy > svg
      animation: progress-circular-split-spin $progress-circular-split-animation-duration linear infinite

    .v-progress-circular__overlay
      animation: $progress-circular-rotate-dash, $progress-circular-rotate-animation
      stroke-dasharray: 25, 200
      stroke-dashoffset: 0
      stroke-linecap: round
      transform-origin: center center
      transform: $progress-circular-overlay-transform

    .v-progress-circular__split
      animation: progress-circular-split-sweep $progress-circular-split-animation-duration linear infinite, progress-circular-split-step $progress-circular-split-animation-duration linear infinite
      transform-origin: center center

      > .v-progress-circular__track
        --v-progress-circular-from: calc(var(--v-progress-circular-head) + var(--v-progress-circular-gap))
        --v-progress-circular-to: calc(100 - var(--v-progress-circular-gap))
        transition: none

      > .v-progress-circular__wave
        --v-progress-circular-to: var(--v-progress-circular-head)
        transition: none

  .v-progress-circular--disable-shrink
    > svg
      animation-duration: list.nth($progress-circular-rotate-animation, 2) * .5

    .v-progress-circular__overlay
      animation: none

    .v-progress-circular__split
      --v-progress-circular-head: 20

    .v-progress-circular__split,
    .v-progress-circular__wave
      animation: none

  .v-progress-circular--indeterminate:not(.v-progress-circular--visible)
    > svg,
    .v-progress-circular__overlay
      animation-play-state: paused

  .v-progress-circular:not(.v-progress-circular--visible)
    .v-progress-circular__split,
    .v-progress-circular__wave
      animation-play-state: paused

  @keyframes progress-circular-dash
    0%
      stroke-dasharray: 1, 200
      stroke-dashoffset: 0px

    50%
      stroke-dasharray: 100, 200
      stroke-dashoffset: -15px

    100%
      stroke-dasharray: 100, 200
      stroke-dashoffset: -124px

  // only the shrink back from 50% is eased
  @keyframes progress-circular-split-sweep
    0%
      --v-progress-circular-head: 10

    50%
      --v-progress-circular-head: 87
      animation-timing-function: cubic-bezier(.2, 0, 0, 1)

    100%
      --v-progress-circular-head: 10

  @keyframes progress-circular-split-spin
    to
      rotate: 1080deg

  @keyframes progress-circular-split-step
    0%
      transform: rotate(0)

    5%, 25%
      transform: rotate(90deg)

    30%, 50%
      transform: rotate(180deg)

    55%, 75%
      transform: rotate(270deg)

    80%, 100%
      transform: rotate(360deg)

  @keyframes progress-circular-wave
    to
      --v-progress-circular-wave-phase: 1

  @keyframes progress-circular-rotate
    100%
      transform: rotate(270deg)
