@use "../base/color";
@use "../base/var";

// LINEAR
// See: https://github.com/material-components/material-web/blob/main/progress/internal/_linear-progress.scss

// note, transition settings match MDC
// see https://github.com/material-components/material-components-web/blob/main/packages/mdc-linear-progress/_linear-progress.scss#L79
$_determinate-duration: 250ms;
$_determinate-easing: cubic-bezier(0.4, 0, 0.6, 1);
// see https://github.com/material-components/material-components-web/blob/main/packages/mdc-linear-progress/_linear-progress.scss#L218
$_indeterminate-duration: 2s;

:host {
  --_track-height: 4px;
  --_track-shape: 2px;
  --_active-indicator-height: 2px;
  --_active-indicator-color: #{color.token(--wy-primary)};
  --_warning-color: #{color.token(--wy-warning)};
  --_error-color: #{color.token(--wy-error)};
  --_track-color: #{color.token(--wy-surface-variant)};
  display: contents;
  position: relative;
}

[part~="wy-progress-linear"] {
  border-radius: var(--_track-shape);
  display: flex;
  position: relative;
  // note, this matches the `meter` element and is just done so
  // there's a default width.
  min-width: 80px;
  height: var(--_track-height);
  content-visibility: auto;
  contain: strict;
  flex: 1 0 auto;

  // Animations need to be in LTR. We support RTL by flipping the indicator
  // with scale(-1).
  direction: ltr;
  inset: 0;
  border-radius: inherit;
  overflow: hidden;
  display: flex;
  align-items: center;
}

[part~="wy-progress-linear"][part~="wy-progress-padded"] {
  margin: var.wy-rem(1) var.wy-rem(2);
}

[part~="wy-inactive-track"],
[part~="wy-bar"],
[part~="wy-bar-inner"] {
  position: absolute;
}

[part~="wy-bar"] {
  animation: none;
  // position is offset for indeterminate animation, so we lock the inline size here.
  width: 100%;
  height: var(--_active-indicator-height);
  transform-origin: left center;
  transition: transform $_determinate-duration $_determinate-easing;
}

[part~="wy-secondary-bar"] {
  display: none;
}

[part~="wy-bar-inner"] {
  inset: 0;
  animation: none;
  background: var(--_active-indicator-color);
}

[part~="wy-inactive-track"] {
  background: var(--_track-color);
  inset: 0;
  transition: transform $_determinate-duration $_determinate-easing;
  transform-origin: left center;
}

// indeterminate
[part~="wy-progress-indeterminate"] [part~="wy-bar"] {
  transition: none;
}

// note, the numbers here come directly from the mdc implementation.
// see https://github.com/material-components/material-components-web/blob/main/packages/mdc-linear-progress/_linear-progress.scss#L208.
[part~="wy-progress-indeterminate"] [part~="wy-primary-bar"] {
  inset-inline-start: -145.167%;
}

[part~="wy-progress-indeterminate"] [part~="wy-secondary-bar"] {
  inset-inline-start: -54.8889%;
  // this is display none by default.
  display: block;
}

[part~="wy-progress-indeterminate"] [part~="wy-primary-bar"] {
  animation: linear infinite $_indeterminate-duration;
  animation-name: primary-indeterminate-translate;
}

[part~="wy-progress-indeterminate"] [part~="wy-primary-bar"] > [part~="wy-bar-inner"] {
  animation: linear infinite $_indeterminate-duration primary-indeterminate-scale;
}

[part~="wy-progress-indeterminate"] [part~="wy-secondary-bar"] {
  animation: linear infinite $_indeterminate-duration;
  animation-name: secondary-indeterminate-translate;
}

[part~="wy-progress-indeterminate"] [part~="wy-secondary-bar"] > [part~="wy-bar-inner"] {
  animation: linear infinite $_indeterminate-duration secondary-indeterminate-scale;
}

@keyframes primary-indeterminate-scale {
  0% {
    transform: scaleX(0.08);
  }

  36.65% {
    animation-timing-function: cubic-bezier(0.334731, 0.12482, 0.785844, 1);
    transform: scaleX(0.08);
  }

  69.15% {
    animation-timing-function: cubic-bezier(0.06, 0.11, 0.6, 1);
    transform: scaleX(0.661479);
  }

  100% {
    transform: scaleX(0.08);
  }
}

@keyframes secondary-indeterminate-scale {
  0% {
    animation-timing-function: cubic-bezier(0.205028, 0.057051, 0.57661, 0.453971);
    transform: scaleX(0.08);
  }

  19.15% {
    animation-timing-function: cubic-bezier(0.152313, 0.196432, 0.648374, 1.00432);
    transform: scaleX(0.457104);
  }

  44.15% {
    animation-timing-function: cubic-bezier(0.257759, -0.003163, 0.211762, 1.38179);
    transform: scaleX(0.72796);
  }

  100% {
    transform: scaleX(0.08);
  }
}

@keyframes primary-indeterminate-translate {
  0% {
    transform: translateX(0px);
  }

  20% {
    animation-timing-function: cubic-bezier(0.5, 0, 0.701732, 0.495819);
    transform: translateX(0px);
  }

  59.15% {
    animation-timing-function: cubic-bezier(0.302435, 0.381352, 0.55, 0.956352);
    transform: translateX(83.6714%);
  }

  100% {
    transform: translateX(200.611%);
  }
}

@keyframes secondary-indeterminate-translate {
  0% {
    animation-timing-function: cubic-bezier(0.15, 0, 0.515058, 0.409685);
    transform: translateX(0px);
  }

  25% {
    animation-timing-function: cubic-bezier(0.31033, 0.284058, 0.8, 0.733712);
    transform: translateX(37.6519%);
  }

  48.35% {
    animation-timing-function: cubic-bezier(0.4, 0.627035, 0.6, 0.902026);
    transform: translateX(84.3862%);
  }

  100% {
    transform: translateX(160.278%);
  }
}

// Warning, Error

[part~="wy-progress-warning"], [part~="wy-progress-error"] {
  [part~="wy-primary-bar"], [part~="wy-secondary-bar"], [part~="wy-bar-inner"] {
    animation: none !important;
  }

  [part~="wy-secondary-bar"] {
    display: none !important;
  }

  [part~="wy-primary-bar"] {
    transform: scaleX(1) !important;
  }
}

[part~="wy-progress-warning"] [part~="wy-bar-inner"] {
  background: var(--_warning-color);
}

[part~="wy-progress-error"] [part~="wy-bar-inner"] {
  background: var(--_error-color);
}