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

@import '../../globals/scss/vars';
@import '../../globals/scss/typography';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/vendor/@rocketsoftware/elements/scss/import-once/import-once';
@import '../../globals/scss/css--reset';
@import '../../globals/scss/theme-tokens';
@import '../../globals/scss/tooltip';
@import '../../globals/scss/keyframes';
@import 'mixins';

/// Code snippet styles
/// @access private
/// @group code-snippet
@mixin snippet {
  .#{$prefix}--snippet {
    @include reset;
  }

  .#{$prefix}--snippet--disabled,
  .#{$prefix}--snippet--disabled
    .#{$prefix}--btn.#{$prefix}--snippet-btn--expand {
    color: $disabled-02;
    background-color: $disabled-01;
  }

  .#{$prefix}--snippet--disabled .#{$prefix}--snippet-btn--expand:hover,
  .#{$prefix}--snippet--disabled .#{$prefix}--copy-btn:hover {
    color: $disabled-02;
    background-color: $disabled-01;
    cursor: not-allowed;
  }

  .#{$prefix}--snippet--disabled .#{$prefix}--snippet__icon,
  .#{$prefix}--snippet--disabled
    .#{$prefix}--snippet-btn--expand
    .#{$prefix}--icon-chevron--down {
    fill: $disabled-02;
  }

  .#{$prefix}--snippet code {
    @include type-style('code-01');
  }

  // Inline Code Snippet
  .#{$prefix}--snippet--inline {
    @include reset;

    position: relative;
    display: inline;
    padding: 0;
    color: $text-01;
    background-color: $field-01;
    border: 2px solid transparent;
    border-radius: 4px;
    cursor: pointer;

    &:hover {
      background-color: $ui-03;
    }

    &:active {
      background-color: $copy-active;
    }

    &:focus {
      border: 2px solid $focus;
      outline: none;

      // Firefox HCM fix
      @media screen and (prefers-contrast) {
        border-style: dotted;
      }
    }

    &::before {
      @include tooltip--caret;

      display: none;
    }

    .#{$prefix}--copy-btn__feedback {
      @include tooltip--content('icon');

      display: none;
      box-sizing: content-box;
      margin: auto;
      overflow: visible;
      clip: auto;
    }

    @include tooltip--placement('icon', 'bottom', 'center');
  }

  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating::before,
  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating
    .#{$prefix}--copy-btn__feedback {
    display: block;
  }

  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating.#{$prefix}--copy-btn--fade-out::before,
  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating.#{$prefix}--copy-btn--fade-out
    .#{$prefix}--copy-btn__feedback {
    animation: $duration--fast-02 motion(standard, productive) hide-feedback;
  }

  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating.#{$prefix}--copy-btn--fade-in::before,
  .#{$prefix}--snippet--inline.#{$prefix}--copy-btn--animating.#{$prefix}--copy-btn--fade-in
    .#{$prefix}--copy-btn__feedback {
    animation: $duration--fast-02 motion(standard, productive) show-feedback;
  }

  .#{$prefix}--snippet--inline code {
    padding: 0 $spacing-03;
  }

  .#{$prefix}--snippet--inline.#{$prefix}--snippet--no-copy {
    display: inline-block;

    &:hover {
      background-color: $field-01;
      cursor: auto;
    }
  }

  .#{$prefix}--snippet--light.#{$prefix}--snippet--inline.#{$prefix}--snippet--no-copy:hover {
    background-color: $field-02;
    cursor: auto;
  }

  // Single Line Snippet
  .#{$prefix}--snippet--single {
    @include bx--snippet;

    display: flex;
    align-items: center;
    max-width: rem(760px);
    height: $carbon--spacing-08;
    padding-right: $carbon--spacing-08;

    // Windows, Firefox HCM Fix
    @media screen and (-ms-high-contrast: active),
      screen and (prefers-contrast) {
      outline: 2px solid transparent;
    }
  }

  .#{$prefix}--snippet--single.#{$prefix}--snippet--no-copy {
    padding: 0;

    &::after {
      right: $carbon--spacing-05;
    }
  }

  .#{$prefix}--snippet--single .#{$prefix}--snippet-container {
    position: relative;
    display: flex;
    align-items: center;
    height: 100%;
    padding-left: $carbon--spacing-05;
    overflow-x: auto;

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

  .#{$prefix}--snippet--single pre {
    @include type-style('code-01');

    padding-right: $spacing-03;
  }

  // Preserve whitespace, but don't allow wrapping for inline and single
  .#{$prefix}--snippet--single pre,
  .#{$prefix}--snippet--inline code {
    white-space: pre;
  }

  // Multi Line Snippet
  .#{$prefix}--snippet--multi {
    @include bx--snippet;

    display: flex;
    max-width: 100%;
    padding: $carbon--spacing-05;

    // Windows, Firefox HCM Fix
    @media screen and (-ms-high-contrast: active),
      screen and (prefers-contrast) {
      outline: 2px solid transparent;
    }
  }

  //closed snippet container
  .#{$prefix}--snippet--multi .#{$prefix}--snippet-container {
    position: relative;
    order: 1;
    min-height: rem(56px);
    max-height: rem(238px);
    overflow: hidden;
    transition: max-height $duration--moderate-01 motion(standard, productive);
  }

  // expanded snippet container
  .#{$prefix}--snippet--multi.#{$prefix}--snippet--expand
    .#{$prefix}--snippet-container {
    max-height: 100%;
    padding-bottom: $spacing-05;
    transition: max-height $duration--moderate-01 motion(standard, productive);
  }

  .#{$prefix}--snippet--multi.#{$prefix}--snippet--wraptext pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  // closed pre
  .#{$prefix}--snippet--multi .#{$prefix}--snippet-container pre {
    padding-right: $carbon--spacing-08;
    padding-bottom: rem(24px);
    overflow-x: auto;
  }

  .#{$prefix}--snippet--multi.#{$prefix}--snippet--no-copy
    .#{$prefix}--snippet-container
    pre {
    padding-right: 0;
  }

  // expanded pre
  .#{$prefix}--snippet--multi.#{$prefix}--snippet--expand
    .#{$prefix}--snippet-container
    pre {
    overflow-x: auto;
  }

  .#{$prefix}--snippet--multi .#{$prefix}--snippet-container pre::after {
    position: absolute;
    top: 0;
    right: 0;
    width: rem(16px);
    height: 100%;
    // Safari interprets `transparent` differently, so make color token value transparent instead:
    background-image: linear-gradient(to right, rgba($field-01, 0), $field-01);
    content: '';
  }

  .#{$prefix}--snippet--multi .#{$prefix}--snippet-container pre code {
    overflow: hidden;
  }

  //Copy Button
  .#{$prefix}--snippet__icon {
    width: rem(16px);
    height: rem(16px);
    transition: all $duration--fast-01 motion(standard, productive);
    fill: $icon-01;

    // Windows, Firefox HCM Fix
    @media screen and (-ms-high-contrast: active),
      screen and (prefers-contrast) {
      // `ButtonText` is a CSS2 system color to help improve colors in HCM
      fill: ButtonText;
    }
  }

  .#{$prefix}--snippet-button {
    @include reset;

    position: absolute;
    top: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: $carbon--spacing-08;
    height: $carbon--spacing-08;
    padding: 0;
    overflow: visible;
    background-color: $field-01;
    border: none;
    outline: none;
    cursor: pointer;

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

      outline-color: $focus;
    }
  }

  .#{$prefix}--snippet--multi .#{$prefix}--snippet-button {
    top: $spacing-03;
    right: $spacing-03;
    width: $carbon--spacing-07;
    height: $carbon--spacing-07;
  }

  .#{$prefix}--snippet-button:hover {
    background: $hover-ui;
  }

  .#{$prefix}--snippet-button:active {
    background-color: $copy-active;
  }

  .#{$prefix}--btn--copy__feedback {
    @include type-style('body-short-01');
    // Override one in code snippet
    @include carbon--font-family('sans');

    top: 0.75rem;
    right: 1.25rem;
    left: inherit;
    z-index: z('overlay');
    font-weight: 400;
  }

  .#{$prefix}--btn--copy__feedback::before,
  .#{$prefix}--btn--copy__feedback::after {
    background: $copy-btn-feedback;
  }

  .#{$prefix}--btn--copy__feedback::after {
    border: none;
  }

  // TODO: remove copy button styles above
  .#{$prefix}--snippet .#{$prefix}--copy-btn {
    position: absolute;
    top: 0;
    right: 0;

    // Override inherited rule in code snippet
    @include carbon--font-family('sans');
  }

  // Show more / less button
  .#{$prefix}--snippet-btn--expand {
    @include type-style('body-short-01');
    @include carbon--font-family('sans');

    position: absolute;
    right: 0;
    bottom: 0;
    z-index: 10;
    display: inline-flex;
    align-items: center;
    padding: $spacing-03 $spacing-05;
    color: $text-01;
    background-color: $field-01;
    border: 0;
  }

  .#{$prefix}--snippet-btn--expand .#{$prefix}--snippet-btn--text {
    position: relative;
    top: rem(-1px);
  }

  .#{$prefix}--snippet-btn--expand--hide.#{$prefix}--snippet-btn--expand {
    display: none;
  }

  .#{$prefix}--snippet-btn--expand .#{$prefix}--icon-chevron--down {
    margin-left: $spacing-03;
    transform: rotate(0deg);
    transition: $duration--moderate-01 motion(standard, productive);
    fill: $text-01;
  }

  .#{$prefix}--snippet-btn--expand:hover {
    color: $text-01;
    background: $hover-ui;
  }

  .#{$prefix}--snippet-btn--expand:active {
    background-color: $copy-active;
  }

  .#{$prefix}--snippet-btn--expand:focus {
    @include focus-outline('outline');

    border-color: transparent;
  }

  .#{$prefix}--snippet--expand
    .#{$prefix}--snippet-btn--expand
    .#{$prefix}--icon-chevron--down {
    transform: rotate(180deg);
    transition: transform $transition--expansion;
  }

  // Light version
  .#{$prefix}--snippet--light,
  .#{$prefix}--snippet--light .#{$prefix}--snippet-button,
  .#{$prefix}--snippet--light .#{$prefix}--btn.#{$prefix}--snippet-btn--expand,
  .#{$prefix}--snippet--light .#{$prefix}--copy-btn {
    background-color: $field-02;
  }

  .#{$prefix}--snippet--light.#{$prefix}--snippet--inline:hover,
  .#{$prefix}--snippet--light .#{$prefix}--snippet-button:hover,
  .#{$prefix}--snippet--light
    .#{$prefix}--btn.#{$prefix}--snippet-btn--expand:hover,
  .#{$prefix}--snippet--light .#{$prefix}--copy-btn:hover {
    background-color: $hover-light-ui;
  }

  .#{$prefix}--snippet--light.#{$prefix}--snippet--inline:active,
  .#{$prefix}--snippet--light .#{$prefix}--snippet-button:active,
  .#{$prefix}--snippet--light
    .#{$prefix}--btn.#{$prefix}--snippet-btn--expand:active,
  .#{$prefix}--snippet--light .#{$prefix}--copy-btn:active {
    background-color: $active-light-ui;
  }

  .#{$prefix}--snippet--light.#{$prefix}--snippet--single::after,
  .#{$prefix}--snippet--light.#{$prefix}--snippet--multi
    .#{$prefix}--snippet-container
    pre::after {
    // Safari interprets `transparent` differently, so make color token value transparent instead:
    background-image: linear-gradient(to right, rgba($field-02, 0), $field-02);
  }

  // Skeleton State
  .#{$prefix}--snippet--code.#{$prefix}--skeleton {
    height: rem(98px);
  }

  .#{$prefix}--snippet--terminal.#{$prefix}--skeleton {
    height: rem(56px);
  }

  .#{$prefix}--snippet.#{$prefix}--skeleton .#{$prefix}--snippet-container {
    height: 100%;
  }

  .#{$prefix}--snippet.#{$prefix}--skeleton code {
    @include skeleton;

    display: block;
    width: 100%;
    height: 1rem;
  }

  .#{$prefix}--snippet-button .#{$prefix}--btn--copy__feedback {
    // (The height of button) + (The height of the tooltip's triangle) + 4px
    top: rem(50.8px);
    right: auto;
    left: 50%;

    &::before {
      top: 0;
    }

    &::after {
      top: rem(-4px);
    }
  }

  .#{$prefix}--snippet--multi .#{$prefix}--copy-btn {
    top: $carbon--spacing-03;
    right: $carbon--spacing-03;
    z-index: 10;
    width: $carbon--spacing-07;
    height: $carbon--spacing-07;
  }

  .#{$prefix}--snippet--multi
    .#{$prefix}--snippet-button
    .#{$prefix}--btn--copy__feedback {
    // (The height of button) + (The height of the tooltip's triangle) + 4px
    top: rem(42.8px);
  }

  .#{$prefix}--snippet--inline .#{$prefix}--btn--copy__feedback {
    // TODO: use updated global tooltip mixins under the hood
    // since all of the positioning values for the copy button tooltip are arbitrary hard coded rem values, we need this arbitrary 4px offset to keep the proper tooltip spacing according to the spec
    top: calc(100% - #{rem(4px)});
    right: auto;
    left: 50%;
  }

  // overflow indicator
  .#{$prefix}--snippet__overflow-indicator--left,
  .#{$prefix}--snippet__overflow-indicator--right {
    z-index: 1;
    flex: 1 0 auto;
    width: $carbon--spacing-05;
  }

  .#{$prefix}--snippet__overflow-indicator--left {
    order: 0;
    margin-right: -$carbon--spacing-05;
    background-image: linear-gradient(to left, transparent, $field-01);
  }

  .#{$prefix}--snippet__overflow-indicator--right {
    order: 2;
    margin-left: -$carbon--spacing-05;
    background-image: linear-gradient(to right, transparent, $field-01);
  }

  .#{$prefix}--snippet--single .#{$prefix}--snippet__overflow-indicator--right,
  .#{$prefix}--snippet--single .#{$prefix}--snippet__overflow-indicator--left {
    position: absolute;
    width: $carbon--spacing-07;
    height: calc(100% - #{$carbon--spacing-02});
  }

  .#{$prefix}--snippet--single .#{$prefix}--snippet__overflow-indicator--right {
    right: $carbon--spacing-08;
  }

  .#{$prefix}--snippet--single
    .#{$prefix}--snippet-container:focus
    ~ .#{$prefix}--snippet__overflow-indicator--right {
    right: calc(#{$carbon--spacing-08} + #{rem(2px)});
  }

  .#{$prefix}--snippet--single
    .#{$prefix}--snippet-container:focus
    + .#{$prefix}--snippet__overflow-indicator--left {
    left: rem(2px);
  }

  .#{$prefix}--snippet--light .#{$prefix}--snippet__overflow-indicator--left {
    background-image: linear-gradient(to left, transparent, $field-02);
  }

  .#{$prefix}--snippet--light .#{$prefix}--snippet__overflow-indicator--right {
    background-image: linear-gradient(to right, transparent, $field-02);
  }

  // Safari-only media query
  // since fades won't appear correctly with CSS custom properties
  // see: tabs, code snippet, and modal overflow indicators
  @media not all and (min-resolution: 0.001dpcm) {
    @supports (-webkit-appearance: none) and (stroke-color: transparent) {
      .#{$prefix}--snippet__overflow-indicator--left {
        background-image: linear-gradient(
          to left,
          rgba($field-01, 0),
          $field-01
        );
      }

      .#{$prefix}--snippet__overflow-indicator--right {
        background-image: linear-gradient(
          to right,
          rgba($field-01, 0),
          $field-01
        );
      }
    }
  }

  #{$prefix}--snippet--multi.#{$prefix}--skeleton {
    height: rem(98px);
  }

  .#{$prefix}--snippet--single.#{$prefix}--skeleton {
    height: rem(56px);
  }

  .#{$prefix}--snippet.#{$prefix}--skeleton span {
    @include skeleton;

    display: block;
    width: 100%;
    height: 1rem;
    margin-top: 0.5rem;

    &:first-child {
      margin: 0;
    }

    &:nth-child(2) {
      width: 85%;
    }

    &:nth-child(3) {
      width: 95%;
    }
  }

  .#{$prefix}--snippet--single.#{$prefix}--skeleton
    .#{$prefix}--snippet-container {
    padding-bottom: 0;
  }
}

@include exports('snippet') {
  @include snippet;
}
