@use '../../styles/tools'
@use './variables' as *

$bar-transition: width $progress-linear-transition, left $progress-linear-transition, right $progress-linear-transition

@property --v-progress-indeterminate-long-left
  syntax: "<percentage>"
  inherits: true
  initial-value: 0%
@property --v-progress-indeterminate-long-right
  syntax: "<percentage>"
  inherits: true
  initial-value: 0%
@property --v-progress-indeterminate-short-left
  syntax: "<percentage>"
  inherits: true
  initial-value: 0%
@property --v-progress-indeterminate-short-right
  syntax: "<percentage>"
  inherits: true
  initial-value: 0%
@property --v-progress-linear-wave-phase
  syntax: "<number>"
  inherits: true
  initial-value: 0
@property --v-progress-linear-long-tail
  syntax: "<number>"
  inherits: true
  initial-value: 0
@property --v-progress-linear-long-head
  syntax: "<number>"
  inherits: true
  initial-value: 0
@property --v-progress-linear-short-tail
  syntax: "<number>"
  inherits: true
  initial-value: 0
@property --v-progress-linear-short-head
  syntax: "<number>"
  inherits: true
  initial-value: 0

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

    background: transparent
    overflow: hidden
    position: relative
    transition: $progress-linear-transition, mask-size 0s
    width: 100%

    &--rounded
      @include tools.rounded($progress-linear-border-radius)

    &--variant-split.v-progress-linear--rounded
      .v-progress-linear__determinate,
      .v-progress-linear__background,
      .v-progress-linear__buffer
        @include tools.rounded($progress-linear-border-radius)

    @media (forced-colors: active)
      border: thin solid buttontext

      // a real border shrinks the svg below its viewBox height, which scales the whole wave down
      &.v-progress-linear--shape-wavy
        border: none

        &::after
          border: thin solid buttontext
          border-radius: inherit
          content: ''
          inset: 0
          pointer-events: none
          position: absolute

  // Elements
  .v-progress-linear__background,
  .v-progress-linear__buffer
    background: $progress-linear-background
    bottom: 0
    left: 0
    opacity: $progress-linear-background-opacity
    position: absolute
    top: 0
    width: 100%
    transition: $bar-transition

  // isReversed already folds RTL in, so this sits at the physical end like the bars
  .v-progress-linear > .v-progress-linear__stop
    --v-progress-linear-stop-inset: max(0px, (var(--v-progress-linear-height) - #{$progress-linear-stop-size}) / 2)

    border-radius: 50%
    height: min(#{$progress-linear-stop-size}, var(--v-progress-linear-height))
    position: absolute
    right: var(--v-progress-linear-stop-inset)
    top: 50%
    translate: 0 -50%
    width: min(#{$progress-linear-stop-size}, var(--v-progress-linear-height))

  .v-progress-linear--reverse > .v-progress-linear__stop
    left: var(--v-progress-linear-stop-inset)
    right: auto

  @include tools.layer('trumps')
    @media (forced-colors: active)
      .v-progress-linear__buffer
        background-color: highlight
        opacity: .5

      .v-progress-linear__wave
        stroke: highlight

      .v-progress-linear__stop
        background-color: highlight
        fill: highlight

  .v-progress-linear__content
    align-items: center
    display: flex
    height: 100%
    justify-content: center
    left: 0
    position: absolute
    top: 0
    width: 100%

  .v-progress-linear--clickable
    .v-progress-linear__content
      pointer-events: none

  .v-progress-linear__determinate,
  .v-progress-linear__indeterminate
    background: $progress-linear-background

    @include tools.layer('trumps')
      @media (forced-colors: active)
        background-color: highlight

  .v-progress-linear__determinate
    height: inherit
    left: 0
    position: absolute
    transition: $bar-transition

  .v-progress-linear__indeterminate
    animation-play-state: paused
    animation-duration: $progress-linear-indeterminate-animation-duration
    animation-iteration-count: infinite
    animation-name: indeterminate-ltr

    .long, .short
      bottom: 0
      height: inherit
      position: absolute
      top: 0
      width: auto
      animation-name: none // avoid glitch on Firefox

    .long
      left: var(--v-progress-indeterminate-long-left)
      right: var(--v-progress-indeterminate-long-right)

    .short
      left: var(--v-progress-indeterminate-short-left)
      right: var(--v-progress-indeterminate-short-right)

    > .v-progress-linear__background
      height: 100%
      border-radius: inherit
      transition: none

      &:nth-child(1)
        left: 0
        width: clamp(0%, calc(var(--v-progress-indeterminate-long-left) - var(--v-progress-chunk-gap)), 100%)

      &:nth-child(2)
        left: clamp(0%, calc(100% - var(--v-progress-indeterminate-long-right) + var(--v-progress-chunk-gap)), 100%)
        width: clamp(0%, calc(var(--v-progress-indeterminate-short-left) - var(--v-progress-chunk-gap)), 100%)

      &:nth-child(3)
        right: 0
        left: unset
        width: clamp(0%, calc(var(--v-progress-indeterminate-short-right) - var(--v-progress-chunk-gap)), 100%)

  .v-progress-linear__stream
    animation: $progress-linear-stream-animation
    animation-play-state: paused
    bottom: 0
    left: auto
    opacity: $progress-linear-stream-opacity
    pointer-events: none
    position: absolute
    transition: $bar-transition

  // Dash lengths are in percent of the track: paths use pathLength and --from/--to are unitless
  .v-progress-linear__wavy
    display: block
    height: 100%
    width: 100%

    > .v-progress-linear__track,
    > .v-progress-linear__wave
      --v-progress-linear-length: max(0, var(--v-progress-linear-to) - var(--v-progress-linear-from))
      fill: none
      stroke: currentColor
      stroke-linecap: round
      // a zero-length dash still draws its round caps
      stroke-width: clamp(0px, var(--v-progress-linear-length) * 1000px, var(--v-progress-linear-stroke))

    > .v-progress-linear__track
      --v-progress-linear-from: calc(var(--v-progress-linear-value) + min(var(--v-progress-linear-value), var(--v-progress-linear-wave-gap)))
      --v-progress-linear-to: 100
      opacity: $progress-linear-background-opacity
      stroke-dasharray: calc(var(--v-progress-linear-length) * 1px) 200px
      stroke-dashoffset: calc(var(--v-progress-linear-from) * -1px)

    > .v-progress-linear__wave
      --v-progress-linear-from: 0
      --v-progress-linear-to: var(--v-progress-linear-value)

    > .v-progress-linear__stop
      fill: currentColor
      r: calc(min(#{$progress-linear-stop-size}, var(--v-progress-linear-stroke)) / 2)

  .v-progress-linear__wavy--indeterminate
    animation: progress-linear-wave 1s linear infinite, progress-linear-wavy $progress-linear-wavy-animation-duration linear infinite
    animation-play-state: paused

    // Gaps shrink to nothing within 1% of either end, so a segment parked at an edge leaves no notch
    > .v-progress-linear__track
      &:nth-child(1)
        --v-progress-linear-from: 0
        --v-progress-linear-to: calc(var(--v-progress-linear-long-tail) - var(--v-progress-linear-wave-gap) * clamp(0, 100 - var(--v-progress-linear-long-tail), 1))

      &:nth-child(2)
        --v-progress-linear-from: calc(var(--v-progress-linear-long-head) + var(--v-progress-linear-wave-gap) * clamp(0, var(--v-progress-linear-long-head), 1))
        --v-progress-linear-to: calc(var(--v-progress-linear-short-tail) - var(--v-progress-linear-wave-gap) * clamp(0, 100 - var(--v-progress-linear-short-tail), 1))

      &:nth-child(3)
        --v-progress-linear-from: calc(var(--v-progress-linear-short-head) + var(--v-progress-linear-wave-gap) * clamp(0, var(--v-progress-linear-short-head), 1))

    > .v-progress-linear__wave
      stroke-dasharray: calc(var(--v-progress-linear-length) * 1px) calc((100 + var(--v-progress-linear-wave-offset)) * 1px)
      stroke-dashoffset: calc((var(--v-progress-linear-from) + var(--v-progress-linear-wave-phase) * var(--v-progress-linear-wave-offset)) * -1px)
      transform: translateX(calc((1 - var(--v-progress-linear-wave-phase)) * var(--v-progress-linear-wavelength)))

      &.long
        --v-progress-linear-from: var(--v-progress-linear-long-tail)
        --v-progress-linear-to: var(--v-progress-linear-long-head)

      &.short
        --v-progress-linear-from: var(--v-progress-linear-short-tail)
        --v-progress-linear-to: var(--v-progress-linear-short-head)

  @media (prefers-reduced-motion: reduce)
    .v-progress-linear__wavy--indeterminate
      animation-name: none, progress-linear-wavy

  // Modifiers
  .v-progress-linear--reverse
    .v-progress-linear__background,
    .v-progress-linear__buffer,
    .v-progress-linear__determinate,
    .v-progress-linear__content
      left: auto
      right: 0

    .v-progress-linear__wavy
      transform: scaleX(-1)

    .v-progress-linear__indeterminate
      animation-name: indeterminate-rtl

      > .v-progress-linear__background
        &:nth-child(1)
          left: unset
          right: 0
          width: clamp(0%, calc(var(--v-progress-indeterminate-long-right) - var(--v-progress-chunk-gap)), 100%)

        &:nth-child(2)
          left: unset
          right: clamp(0%, calc(100% - var(--v-progress-indeterminate-long-left) + var(--v-progress-chunk-gap)), 100%)
          width: clamp(0%, calc(var(--v-progress-indeterminate-short-right) - var(--v-progress-chunk-gap)), 100%)

        &:nth-child(3)
          right: unset
          left: 0
          width: clamp(0%, calc(var(--v-progress-indeterminate-short-left) - var(--v-progress-chunk-gap)), 100%)

    .v-progress-linear__stream
      right: auto

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

    .v-progress-linear__determinate,
    .v-progress-linear__track,
    .v-progress-linear__wave
      transition-duration: var(--v-progress-reveal-duration)

  .v-progress-linear--no-transition
    --v-progress-linear-transition-duration: 0s

  .v-progress-linear--absolute,
  .v-progress-linear--fixed
    left: 0
    z-index: 1

  .v-progress-linear--absolute
    position: absolute

  .v-progress-linear--fixed
    position: fixed

  .v-progress-linear--striped
    .v-progress-linear__determinate
      animation: $progress-linear-striped-animation
      background-image: $progress-linear-stripe-gradient
      background-repeat: repeat
      background-size: $progress-linear-striped-size

  .v-progress-linear--active
    .v-progress-linear__indeterminate
      animation-play-state: running

    .v-progress-linear__stream
      animation-play-state: running

    .v-progress-linear__wavy
      animation-play-state: running

  .v-progress-linear--rounded-bar
    .v-progress-linear__determinate,
    .v-progress-linear__indeterminate,
    .v-progress-linear__buffer,
    .v-progress-linear__stream + .v-progress-linear__background
      @include tools.rounded($progress-linear-border-radius)

    .v-progress-linear__determinate,
    .v-progress-linear__buffer
      border-radius: 0 $progress-linear-border-radius $progress-linear-border-radius 0

    &.v-progress-linear--reverse
      .v-progress-linear__determinate,
      .v-progress-linear__buffer
        border-radius: $progress-linear-border-radius 0 0 $progress-linear-border-radius

  .v-progress-linear--rounded.v-progress-linear--rounded-bar
    .v-progress-linear__determinate,
    .v-progress-linear__indeterminate,
    .v-progress-linear__buffer
      border-radius: inherit

  // Keyframes
  @keyframes indeterminate-ltr
    0%
      --v-progress-indeterminate-long-left: -90%
      --v-progress-indeterminate-long-right: 107%
      --v-progress-indeterminate-short-left: -200%
      --v-progress-indeterminate-short-right: 107%
    60%
      --v-progress-indeterminate-long-left: -90%
      --v-progress-indeterminate-long-right: 107%
      --v-progress-indeterminate-short-left: 107%
      --v-progress-indeterminate-short-right: -8%
    100%
      --v-progress-indeterminate-long-left: 107%
      --v-progress-indeterminate-long-right: -35%
      --v-progress-indeterminate-short-left: 107%
      --v-progress-indeterminate-short-right: -8%

  @keyframes indeterminate-rtl
    0%
      --v-progress-indeterminate-long-left: 107%
      --v-progress-indeterminate-long-right: -90%
      --v-progress-indeterminate-short-left: 107%
      --v-progress-indeterminate-short-right: -200%
    60%
      --v-progress-indeterminate-long-left: 107%
      --v-progress-indeterminate-long-right: -90%
      --v-progress-indeterminate-short-left: -8%
      --v-progress-indeterminate-short-right: 107%
    100%
      --v-progress-indeterminate-long-left: -35%
      --v-progress-indeterminate-long-right: 107%
      --v-progress-indeterminate-short-left: -8%
      --v-progress-indeterminate-short-right: 107%

  // a keyframe's timing function applies only to the properties it sets, so each segment end starts in its own keyframe
  @keyframes progress-linear-wavy
    0%
      --v-progress-linear-short-head: 0
      animation-timing-function: cubic-bezier(.1, 0, .45, 1)
    18.5%
      --v-progress-linear-short-tail: 0
      animation-timing-function: cubic-bezier(0, 0, .65, 1)
    41.67%
      --v-progress-linear-short-head: 100
    55.56%
      --v-progress-linear-long-head: 0
      animation-timing-function: cubic-bezier(.4, 0, 1, 1)
    65.72%
      --v-progress-linear-short-tail: 100
    70.39%
      --v-progress-linear-long-tail: 0
      animation-timing-function: cubic-bezier(.2, 0, .8, 1)
    87.06%
      --v-progress-linear-long-head: 100
    100%
      --v-progress-linear-long-tail: 100
      --v-progress-linear-long-head: 100
      --v-progress-linear-short-tail: 100
      --v-progress-linear-short-head: 100

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

  @keyframes stream
    to
      transform: translateX(var(--v-progress-linear-stream-to))

  @keyframes progress-linear-stripes
    0%
      background-position-x: $progress-linear-striped-size
