//
// Copyright IBM Corp. 2016, 2023
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@use 'vars' as *;
@use 'functions' as *;
@use 'animation';
@use '../../config' as *;
@use '../../layer' as *;
@use '../../motion' as *;
@use '../../spacing' as *;
@use '../../theme' as *;
@use '../../utilities/component-reset';
@use '../../utilities/convert';
@use '../../utilities/z-index' as *;

/// Loading styles
/// @access private
/// @group loading
@mixin loading {
  .#{$prefix}--loading {
    @include component-reset.reset;
    @include animation.spin;

    block-size: $loading-size;

    inline-size: $loading-size;
  }

  // Animation (Spin by default)
  .#{$prefix}--loading__svg {
    fill: transparent;
  }

  .#{$prefix}--loading__svg circle {
    stroke-dasharray: $circumference $circumference;
    stroke-linecap: butt;
    stroke-width: 10;
  }

  .#{$prefix}--loading__stroke {
    stroke: $interactive;
    stroke-dashoffset: loading-progress($circumference, 81);
  }

  .#{$prefix}--loading--small .#{$prefix}--loading__stroke {
    stroke-dashoffset: loading-progress($circumference, 48);
  }

  .#{$prefix}--loading--stop {
    @include animation.stop;
  }

  .#{$prefix}--loading--small {
    block-size: convert.to-rem(16px);
    inline-size: convert.to-rem(16px);

    //See https://github.com/carbon-design-system/carbon/issues/13121
    line-height: 1rem;

    circle {
      stroke-width: 16;
    }
  }

  .#{$prefix}--loading--small .#{$prefix}--loading__svg {
    stroke: $interactive;
  }

  .#{$prefix}--loading__background {
    stroke: $layer-accent;
    stroke-dashoffset: -22;
  }

  // Negative values for `stroke-dashoffset` are not supported in Safari
  @supports (hanging-punctuation: first) and (font: -apple-system-body) and
    (-webkit-appearance: none) {
    circle.#{$prefix}--loading__background {
      stroke-dasharray: 265;
      stroke-dashoffset: 0;
    }
  }

  .#{$prefix}--loading-overlay {
    position: fixed;
    z-index: z('overlay');
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: $overlay;
    block-size: 100%;
    inline-size: 100%;
    inset-block-start: 0;
    inset-inline-start: 0;
    transition: background-color $duration-slow-02 motion(standard, expressive);
  }

  .#{$prefix}--loading-overlay--stop {
    display: none;
  }

  @keyframes #{$prefix}--rotate {
    0% {
      transform: rotate(0deg);
    }

    100% {
      transform: rotate(360deg);
    }
  }

  @keyframes #{$prefix}--rotate-end-p1 {
    100% {
      transform: rotate(360deg);
    }
  }

  @keyframes #{$prefix}--rotate-end-p2 {
    100% {
      transform: rotate(-360deg);
    }
  }

  /* Stroke animations */
  @keyframes #{$prefix}--init-stroke {
    0% {
      stroke-dashoffset: loading-progress($circumference, 0);
    }

    100% {
      stroke-dashoffset: loading-progress($circumference, 81);
    }
  }

  @keyframes #{$prefix}--stroke-end {
    0% {
      stroke-dashoffset: loading-progress($circumference, 81);
    }

    100% {
      stroke-dashoffset: loading-progress($circumference, 0);
    }
  }
}
