//
// 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.
//

//-----------------------------
// Text
//-----------------------------
@import '../../globals/scss/vars';
@import '../../globals/scss/css--reset';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/typography';
@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../../globals/scss/tooltip';
@import '../form/form';

/// Text input styles
/// @access private
/// @group text-input
@mixin text-input {
  .#{$prefix}--text-input {
    @include reset;
    @include type-style('body-short-01');
    @include focus-outline('reset');

    width: 100%;
    height: rem(40px);
    padding: 0 $carbon--spacing-05;
    color: $text-01;
    background-color: $field-01;
    border: none;
    border-bottom: 1px solid $ui-04;
    transition: background-color $duration--fast-01 motion(standard, productive),
      outline $duration--fast-01 motion(standard, productive);

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

    &-wrapper svg[hidden] {
      display: none;
    }
  }

  .#{$prefix}--text-input--xl {
    height: rem(48px);
  }

  .#{$prefix}--text-input--sm {
    height: rem(32px);
  }

  .#{$prefix}--password-input {
    padding-right: $carbon--spacing-08;
  }

  .#{$prefix}--text-input::placeholder {
    @include placeholder-colors;
  }

  .#{$prefix}--text-input--light {
    background-color: $field-02;
  }

  //-----------------------------
  // Disabled & Error icon spacing
  //-----------------------------
  .#{$prefix}--text-input__field-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;

    .#{$prefix}--text-input__invalid-icon {
      position: absolute;
      // top/transform used to center invalid icon in IE11
      top: 50%;
      right: $carbon--spacing-05;
      transform: translateY(-50%);
      fill: $support-01;
    }

    .#{$prefix}--text-input__invalid-icon--warning {
      fill: $support-03;

      path[data-icon-path='inner-path'] {
        opacity: 1;
        fill: $carbon__black-100;
      }
    }

    // TODO: deprecate this style block
    .#{$prefix}--text-input--password__visibility {
      @include tooltip--trigger('icon', 'bottom');
      @include tooltip--placement('icon', 'bottom', 'center');
    }

    .#{$prefix}--text-input--password__visibility,
    // TODO: remove selector above
    .#{$prefix}--text-input--password__visibility__toggle.#{$prefix}--tooltip__trigger {
      position: absolute;
      right: $carbon--spacing-05;
      width: rem(16px);
      height: rem(16px);
      padding: 0;
      background: none;
      border: 0;
      cursor: pointer;

      svg {
        fill: $icon-02;
      }
    }

    .#{$prefix}--text-input--invalid {
      padding-right: $carbon--spacing-08;
    }

    .#{$prefix}--text-input--invalid.#{$prefix}--password-input {
      padding-right: rem(64px);
    }

    .#{$prefix}--text-input--invalid
      + .#{$prefix}--text-input--password__visibility,
    // TODO: remove selector above
    .#{$prefix}--text-input--invalid
      + .#{$prefix}--text-input--password__visibility__toggle {
      right: $carbon--spacing-05;
    }
  }

  .#{$prefix}--password-input-wrapper .#{$prefix}--text-input__invalid-icon {
    right: $carbon--spacing-08;
  }

  .#{$prefix}--text-input:disabled
    + .#{$prefix}--text-input--password__visibility
    svg,
  // TODO: remove selector above
  .#{$prefix}--text-input:disabled
    + .#{$prefix}--text-input--password__visibility__toggle
    svg {
    cursor: not-allowed;
    opacity: 0.5;
  }

  //-----------------------------
  // Disabled
  //-----------------------------
  .#{$prefix}--text-input:disabled {
    @include focus-outline('reset');

    color: $disabled-02;
    background-color: $disabled-01;
    border-bottom: 1px solid transparent;
    // Needed to fix disabled text in Safari #6673
    -webkit-text-fill-color: currentColor;
    cursor: not-allowed;
  }

  .#{$prefix}--text-input--light:disabled {
    background-color: $field-02;
  }

  .#{$prefix}--text-input:disabled::placeholder {
    color: $disabled-02;
    opacity: 1;
  }

  //-----------------------------
  // Error
  //-----------------------------
  .#{$prefix}--text-input--invalid {
    @include focus-outline('invalid');

    box-shadow: none;

    .#{$prefix}--text-input--password__visibility,
    // TODO: remove selector above
    .#{$prefix}--text-input--password__visibility__toggle {
      right: $carbon--spacing-08;
    }
  }

  //-----------------------------
  // Fluid Text Input
  //-----------------------------
  .#{$prefix}--form--fluid .#{$prefix}--text-input-wrapper {
    position: relative;
    background: $field-01;
    transition: background-color $duration--fast-01 motion(standard, productive),
      outline $duration--fast-01 motion(standard, productive);
  }

  .#{$prefix}--form--fluid .#{$prefix}--label {
    position: absolute;
    top: rem(13px);
    left: $carbon--spacing-05;
    z-index: 1;
    margin: 0;
  }

  .#{$prefix}--form--fluid .#{$prefix}--form__helper-text {
    display: none;
  }

  .#{$prefix}--form--fluid .#{$prefix}--text-input {
    min-height: rem(64px);
    padding: rem(32px) $carbon--spacing-05 rem(13px);
  }

  .#{$prefix}--text-input__divider,
  .#{$prefix}--form--fluid .#{$prefix}--text-input__divider {
    display: none;
  }

  .#{$prefix}--form--fluid .#{$prefix}--text-input--invalid,
  .#{$prefix}--form--fluid .#{$prefix}--text-input--warn {
    border-bottom: none;
  }

  .#{$prefix}--form--fluid
    .#{$prefix}--text-input--invalid
    + .#{$prefix}--text-input__divider,
  .#{$prefix}--form--fluid
    .#{$prefix}--text-input--warn
    + .#{$prefix}--text-input__divider {
    display: block;
    margin: 0 1rem;
    border-color: $ui-03;
    border-style: solid;
    border-bottom: none;
  }

  .#{$prefix}--form--fluid .#{$prefix}--text-input__invalid-icon {
    top: rem(80px);
  }

  .#{$prefix}--form--fluid .#{$prefix}--text-input-wrapper--light {
    background: $field-02;
  }

  .#{$prefix}--form--fluid
    .#{$prefix}--text-input__field-wrapper[data-invalid]
    > .#{$prefix}--text-input--invalid {
    @include focus-outline('reset');
  }

  .#{$prefix}--form--fluid
    .#{$prefix}--text-input__field-wrapper[data-invalid]:not(:focus) {
    @include focus-outline('invalid');
  }

  .#{$prefix}--form--fluid
    .#{$prefix}--text-input__field-wrapper[data-invalid]
    > .#{$prefix}--text-input--invalid:focus {
    @include focus-outline('outline');
  }

  //-----------------------------
  // Inline Text Input
  //-----------------------------

  .#{$prefix}--text-input-wrapper--inline {
    flex-flow: row wrap;
  }

  .#{$prefix}--label--inline {
    flex: 1;
    margin: rem(13px) 0 0 0;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  .#{$prefix}--label--inline--sm {
    margin-top: rem(9px);
  }

  .#{$prefix}--label--inline--xl {
    margin-top: rem(17px);
  }

  .#{$prefix}--text-input__label-helper-wrapper {
    flex: 2;
    flex-direction: column;
    max-width: rem(128px);
    margin-right: rem(24px);
    overflow-wrap: break-word;
  }

  .#{$prefix}--form__helper-text--inline {
    margin-top: rem(2px);
  }

  .#{$prefix}--text-input__field-outer-wrapper {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }

  .#{$prefix}--text-input__field-outer-wrapper--inline {
    flex: 8;
    flex-direction: column;
  }
}

@include exports('text-input') {
  @include text-input;
}
