@use 'sass:math';

// Query to kick us into "mobile" mode with larger drag handles/bars.
// See: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/pointer
$mobile-media-query: '(pointer: coarse)' !default;

// SASS variables for normal drag handle and bar size.
// Override in your scss file by setting these variables FIRST, then including this file.
$drag-handle-width: 10px !default;
$drag-handle-height: 10px !default;
$drag-bar-size: 6px !default;

// Mobile handle/bar sizes.  Override as above.
$drag-handle-mobile-width: 24px !default;
$drag-handle-mobile-height: 24px !default;

// Handle color/border.
$drag-handle-background-colour: rgba(0, 0, 0, 0.2) !default;
$drag-handle-border: 1px solid rgba(255, 255, 255, 0.7) !default;
$drag-handle-active-border-color: blue !default;
$drag-handle-active-bg-color: #2dbfff !default;

$half-drag-handle-height: math.div($drag-handle-height, 2);
$half-drag-handle-width: math.div($drag-handle-width, 2);
$half-drag-bar-size: math.div($drag-bar-size, 2);

.ReactCrop {
  $root: &;

  position: relative;
  display: inline-block;
  cursor: crosshair;
  overflow: hidden;
  max-width: 100%;

  & *,
  & *::before,
  & *::after {
    box-sizing: border-box;
  }

  &--disabled,
  &--locked {
    cursor: inherit;
  }

  &__child-wrapper {
    max-height: inherit;

    & > img,
    & > video {
      display: block;
      max-width: 100%;
      max-height: inherit;
    }
  }

  &:not(#{$root}--disabled) {
    #{$root}__child-wrapper {
      & > img,
      & > video {
        touch-action: none;
      }
    }
    #{$root}__crop-selection {
      touch-action: none;
    }
  }

  &__crop-selection {
    position: absolute;
    top: 0;
    left: 0;
    transform: translate3d(0, 0, 0);
    cursor: move;
    box-shadow: 0 0 0 9999em rgba(0, 0, 0, 0.5);

    .ReactCrop--disabled & {
      cursor: inherit;
    }

    .ReactCrop--circular-crop & {
      border-radius: 50%;
    }

    .ReactCrop--no-animate & {
      // border: 1px dashed white;
      outline: 1px dashed white;
    }
    &:not(.ReactCrop--no-animate &) {
      $antWidth: 10px;
      $doubleAntWidth: 10px * 2;

      @keyframes marching-ants {
        0% {
          background-position: 0 0, 0 100%, 0 0, 100% 0;
        }
        100% {
          background-position: $doubleAntWidth 0, (-$doubleAntWidth) 100%, 0 (-$doubleAntWidth), 100% $doubleAntWidth;
        }
      }

      animation: marching-ants 1s;
      background-image: linear-gradient(to right, #fff 50%, #444 50%), linear-gradient(to right, #fff 50%, #444 50%),
        linear-gradient(to bottom, #fff 50%, #444 50%), linear-gradient(to bottom, #fff 50%, #444 50%);
      background-size: $antWidth 1px, $antWidth 1px, 1px $antWidth, 1px $antWidth;
      background-position: 0 0, 0 100%, 0 0, 100% 0;
      background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
      color: #fff;
      animation-play-state: running;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }

    &:focus {
      outline: none;
      border-color: $drag-handle-active-border-color;
      border-style: solid;
    }
  }
  &--invisible-crop &__crop-selection {
    display: none;
  }

  &__rule-of-thirds-vt::before,
  &__rule-of-thirds-vt::after,
  &__rule-of-thirds-hz::before,
  &__rule-of-thirds-hz::after {
    content: '';
    display: block;
    position: absolute;
    background-color: rgba(255, 255, 255, 0.4);
  }

  &__rule-of-thirds-vt {
    &::before,
    &::after {
      width: 1px;
      height: 100%;
    }

    &::before {
      left: 33.3333%;
      left: calc(100% / 3);
    }

    &::after {
      left: 66.6666%;
      left: calc(100% / 3 * 2);
    }
  }

  &__rule-of-thirds-hz {
    &::before,
    &::after {
      width: 100%;
      height: 1px;
    }

    &::before {
      top: 33.3333%;
      top: calc(100% / 3);
    }

    &::after {
      top: 66.6666%;
      top: calc(100% / 3 * 2);
    }
  }

  &__drag-handle {
    position: absolute;

    &::after {
      position: absolute;
      content: '';
      display: block;
      width: $drag-handle-width;
      height: $drag-handle-height;
      background-color: $drag-handle-background-colour;
      border: $drag-handle-border;

      // This stops the borders disappearing when keyboard
      // nudging.
      outline: 1px solid transparent;
    }

    &:focus {
      &::after {
        border-color: $drag-handle-active-border-color;
        background: $drag-handle-active-bg-color;
      }
    }
  }

  .ord-nw {
    top: 0;
    left: 0;
    margin-top: -$half-drag-handle-height;
    margin-left: -$half-drag-handle-width;
    cursor: nw-resize;

    &::after {
      top: 0;
      left: 0;
    }
  }
  .ord-n {
    top: 0;
    left: 50%;
    margin-top: -$half-drag-handle-height;
    margin-left: -$half-drag-handle-width;
    cursor: n-resize;

    &::after {
      top: 0;
    }
  }
  .ord-ne {
    top: 0;
    right: 0;
    margin-top: -$half-drag-handle-height;
    margin-right: -$half-drag-handle-width;
    cursor: ne-resize;

    &::after {
      top: 0;
      right: 0;
    }
  }
  .ord-e {
    top: 50%;
    right: 0;
    margin-top: -$half-drag-handle-height;
    margin-right: -$half-drag-handle-width;
    cursor: e-resize;

    &::after {
      right: 0;
    }
  }
  .ord-se {
    bottom: 0;
    right: 0;
    margin-bottom: -$half-drag-handle-height;
    margin-right: -$half-drag-handle-width;
    cursor: se-resize;

    &::after {
      bottom: 0;
      right: 0;
    }
  }
  .ord-s {
    bottom: 0;
    left: 50%;
    margin-bottom: -$half-drag-handle-height;
    margin-left: -$half-drag-handle-width;
    cursor: s-resize;

    &::after {
      bottom: 0;
    }
  }
  .ord-sw {
    bottom: 0;
    left: 0;
    margin-bottom: -$half-drag-handle-height;
    margin-left: -$half-drag-handle-width;
    cursor: sw-resize;

    &::after {
      bottom: 0;
      left: 0;
    }
  }
  .ord-w {
    top: 50%;
    left: 0;
    margin-top: -$half-drag-handle-height;
    margin-left: -$half-drag-handle-width;
    cursor: w-resize;

    &::after {
      left: 0;
    }
  }

  // Use the same specificity as the ords above but just
  // come after.
  &__disabled &__drag-handle {
    cursor: inherit;
  }

  &__drag-bar {
    position: absolute;

    &.ord-n {
      top: 0;
      left: 0;
      width: 100%;
      height: $drag-bar-size;
      margin-top: -$half-drag-bar-size;
    }
    &.ord-e {
      right: 0;
      top: 0;
      width: $drag-bar-size;
      height: 100%;
      margin-right: -$half-drag-bar-size;
    }
    &.ord-s {
      bottom: 0;
      left: 0;
      width: 100%;
      height: $drag-bar-size;
      margin-bottom: -$half-drag-bar-size;
    }
    &.ord-w {
      top: 0;
      left: 0;
      width: $drag-bar-size;
      height: 100%;
      margin-left: -$half-drag-bar-size;
    }
  }

  &--new-crop &__drag-bar,
  &--new-crop &__drag-handle,
  &--fixed-aspect &__drag-bar {
    display: none;
  }

  &--fixed-aspect &__drag-handle.ord-n,
  &--fixed-aspect &__drag-handle.ord-e,
  &--fixed-aspect &__drag-handle.ord-s,
  &--fixed-aspect &__drag-handle.ord-w {
    display: none;
  }

  @media #{$mobile-media-query} {
    .ord-n,
    .ord-e,
    .ord-s,
    .ord-w {
      display: none;
    }

    &__drag-handle {
      width: $drag-handle-mobile-width;
      height: $drag-handle-mobile-height;
    }
  }
}
