//
// 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/@carbon/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 {
    background-color: $disabled-01;
    color: $disabled-02;
  }

  .#{$prefix}--snippet--disabled .#{$prefix}--snippet-btn--expand:hover,
  .#{$prefix}--snippet--disabled .#{$prefix}--copy-btn:hover {
    background-color: $disabled-01;
    color: $disabled-02;
    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;
    border: 2px solid transparent;
    background-color: $field-01;
    border-radius: 4px;
    color: $text-01;
    cursor: pointer;

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

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

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

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

      display: none;
    }

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

      display: none;
      overflow: visible;
      box-sizing: content-box;
      margin: auto;
      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;
    height: $carbon--spacing-08;
    align-items: center;
    padding-right: $carbon--spacing-08;
  }

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

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

  .#{$prefix}--snippet--single .#{$prefix}--snippet-container {
    position: relative;
    display: flex;
    height: 100%;
    align-items: center;
    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;
    padding: $carbon--spacing-05;
  }

  //collapsed snippet container
  .#{$prefix}--snippet--multi .#{$prefix}--snippet-container {
    position: relative;
    min-height: 100%;
    max-height: 100%;
    order: 1;
    overflow-y: auto;
    transition: max-height $duration--moderate-01 motion(standard, productive);
  }

  // expanded snippet container
  .#{$prefix}--snippet--multi.#{$prefix}--snippet--expand
    .#{$prefix}--snippet-container {
    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;
  }

  // collapsed 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);
    fill: $icon-01;
    transition: all $duration--fast-01 motion(standard, productive);
  }

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

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

    &: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');

    z-index: z('overlay');
    top: 0.75rem;
    right: 1.25rem;
    left: inherit;
    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;
    z-index: 10;
    right: 0;
    bottom: 0;
    display: inline-flex;
    align-items: center;
    padding: $spacing-03 $spacing-05;
    border: 0;
    background-color: $field-01;
    color: $text-01;
  }

  .#{$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;
    fill: $icon-primary;
    transform: rotate(0deg);
    transition: $duration--moderate-01 motion(standard, productive);
  }

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

  .#{$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.#{$prefix}--skeleton .#{$prefix}--snippet-container {
    width: 100%;
    height: 100%;
  }

  .#{$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 {
    z-index: 10;
    top: $carbon--spacing-03;
    right: $carbon--spacing-03;
    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;
    width: $carbon--spacing-05;
    flex: 1 0 auto;
  }

  .#{$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--no-copy
    .#{$prefix}--snippet__overflow-indicator--right {
    right: 0;
  }

  .#{$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;
  }

  // Windows HCM fix
  /* stylelint-disable */
  .#{$prefix}--snippet__icon {
    @include high-contrast-mode('icon-fill');
  }

  .#{$prefix}--snippet--inline:focus {
    @include high-contrast-mode('focus');
  }

  .#{$prefix}--snippet--single,
  .#{$prefix}--snippet--multi {
    @include high-contrast-mode('outline');
  }
  /* stylelint-enable */
}

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