//----------------------------------------------
// Mixins
// ---------------------------------------------
//
//   Category             ||  Description
//   ===========================================
//   Misc                 ||  General helper @mixins
//   Deprecated           ||  Not used anymore
//   ===========================================

//----------------------------------------------
// Misc
// ---------------------------------------------
@use 'sass:math';
@use 'sass:list';
@import 'feature-flags';
@import 'vars';
@import 'css--reset';
@import 'typography';
@import '@carbon/import-once/scss/import-once';

@mixin text-overflow($width: false) {
  display: block;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  // apply a width if width parameter exists
  @if ($width) {
    width: $width;
  }
}

@mixin placeholder-colors {
  color: var(--text-02);
}

@mixin input-base {
  height: $input-height;
  min-width: 10rem;
  padding: 0 $spacing-xs;
  color: var(--text-01);
  appearance: none;
  background-color: var(--field-01);
  order: 2;
  border: $input-border;
  border-radius: $input-border-radius;
  box-shadow: none;

  @include print {
    background: var(--ui-01) !important;
    border-color: var(--ui-06);
    padding: 0 $spacing-2xs;
    height: $input-height * 0.7;
  }
  &::placeholder {
    @include placeholder-colors;
  }

  &:focus {
    @include focus-outline('border');
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  &:disabled:hover {
    border: $input-border;
  }

  &:read-only {
    @include read-only('border');
  }
}

@mixin input-error {
  &:invalid,
  &.#{$prefix}--input--invalid {
    border-color: var(--support-01);
    // outline: 1px solid var(--support-01);
  }
}

@mixin box-shadow($size: 'small') {
  // Large - For dropdowns
  @if ($size == 'large') {
    box-shadow: 6px 6px 6px 0 $box-shadow;
  }

  @if ($size == 'small') {
    box-shadow: 0px 3px 3px 0 $box-shadow;
  }
}

@mixin read-only($type: 'border') {
  @if ($type == 'border') {
    border-color: var(--ui-02);
    &:hover,
    &:focus {
      border-color: var(--ui-02);
      outline: none;
      box-shadow: none;
    }
  }
}

@mixin focus-outline($type: 'border') {
  @if ($type == 'border') {
    border-color: var(--interactive-01);
    outline: 1px solid var(--interactive-01);
    box-shadow: 0px 2px 8px 0 var(--focus-01);
  }

  @if ($type == 'border-small') {
    outline: 2px solid var(--interactive-02);
    outline-offset: 2px;
  }

  @if ($type == 'border-important') {
    outline: 2px solid var(--interactive-01) !important;
    outline-offset: -1px;
  }

  @if ($type == 'blurred') {
    box-shadow: 0 0 0 3px var(--focus-01);
    outline: 1px solid transparent;
  }

  @if ($type == 'outline') {
    outline: 2px solid var(--interactive-01);
    outline-offset: -2px;
  }
}

@mixin rotate($deg, $speed, $origin: center) {
  transform: rotate($deg);
  transition: transform $speed;
  transform-origin: $origin;
}

@mixin hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
  visibility: visible;
  white-space: nowrap;
}

@mixin button-reset($width: true) {
  @include reset;
  @include font-family;
  @include font-smoothing;
  @include letter-spacing;
  display: inline-block;
  background: none;
  appearance: none;
  border: 0;
  padding: 0;
  cursor: pointer;

  @if ($width == true) {
    width: 100%;
  }

  &::-moz-focus-inner {
    border: 0;
  }
}

@mixin column-span($span, $columns: false) {
  flex: 0 0 math.percentage(calc($span / 12));
  max-width: math.percentage(calc($span / 12));

  @if ($columns) {
    flex: 0 0 math.percentage((calc($span / $columns)));
    max-width: math.percentage((calc($span / $columns)));
  }
}

/**
 * Generic `deprecate` mixin that is being used to indicate that a component is
 * no longer going to be present in the next major release of Carbon.
 */
@mixin deprecate($reason) {
  $deprecations--entry: false !default;

  @if ($deprecations--entry == true) {
    $deprecations--reasons: append($deprecations--reasons, $reason) /* !global */;
    @content;
  } @else {
    @warn 'Deprecated code was found, this code will be removed before the next release of Carbon. REASON: #{$reason}';
    @content;
  }
}

// 💀 Skeleton loading animation

@mixin skeleton {
  position: relative;
  border: none;
  padding: 0;
  box-shadow: none;
  pointer-events: none;
  background: $skeleton;

  &:hover,
  &:focus,
  &:active {
    border: none;
    outline: none;
    cursor: default;
  }

  &:before {
    content: '';
    width: 0%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0.3;
    background: $skeleton;
    animation: 3000ms ease-in-out skeleton infinite;
  }
}

@include exports('skeleton') {
  @keyframes skeleton {
    0% {
      width: 0%;
      left: 0;
      right: auto;
      opacity: 0.3;
    }
    20% {
      width: 100%;
      left: 0;
      right: auto;
      opacity: 1;
    }
    28% {
      width: 100%;
      left: auto;
      right: 0;
    }
    51% {
      width: 0%;
      left: auto;
      right: 0;
    }
    58% {
      width: 0%;
      left: auto;
      right: 0;
    }
    82% {
      width: 100%;
      left: auto;
      right: 0;
    }
    83% {
      width: 100%;
      left: 0;
      right: auto;
    }
    96% {
      width: 0%;
      left: 0;
      right: auto;
    }
    100% {
      width: 0%;
      left: 0;
      right: auto;
      opacity: 0.3;
    }
  }
}

//----------------------------------------------
// Deprecated
// ---------------------------------------------

@mixin light-ui {
  .#{$prefix}--global-light-ui & {
    @content;
  }
}
