@use 'sass:map';

@use 'mixins/mixins' as *;
@use 'mixins/var' as *;
@use 'common/var' as *;

@mixin inset-prepend-border($color) {
  box-shadow: 1px 0 0 0 $color inset, 0 1px 0 0 $color inset,
    0 -1px 0 0 $color inset;
}

@mixin inset-append-border($color) {
  box-shadow: 0 1px 0 0 $color inset, 0 -1px 0 0 $color inset,
    -1px 0 0 0 $color inset;
}

@mixin inset-prepend-input-border($color) {
  box-shadow: 1px 0 0 0 $color inset, 1px 0 0 0 $color, 0 1px 0 0 $color inset,
    0 -1px 0 0 $color inset !important;
}

@mixin inset-append-input-border($color) {
  box-shadow: -1px 0 0 0 $color, -1px 0 0 0 $color inset, 0 1px 0 0 $color inset,
    0 -1px 0 0 $color inset !important;
}

@mixin mixed-input-border($color) {
  box-shadow: 0 0 0 1px $color inset;
}

@include b(textarea) {
  @include set-component-css-var('input', $input);
}

@include b(textarea) {
  position: relative;
  display: inline-block;
  width: 100%;
  vertical-align: bottom;
  font-size: var(--el-font-size-base);

  @include e(inner) {
    position: relative;
    display: block;
    resize: vertical;
    padding: 5px 15px;
    line-height: 1.5;
    box-sizing: border-box;
    width: 100%;
    font-size: inherit;
    font-family: inherit;
    color: var(--el-input-text-color, map.get($input, 'text-color'));
    background-color: var(--el-input-bg-color, map.get($input, 'bg-color'));
    background-image: none;
    -webkit-appearance: none;
    @include inset-input-border(
      var(--el-input-border-color, map.get($input, 'border-color'))
    );
    border-radius: var(
      --el-input-border-radius,
      map.get($input, 'border-radius')
    );
    transition: var(--el-transition-box-shadow);
    border: none;

    &::placeholder {
      color: var(
        --el-input-placeholder-color,
        map.get($input, 'placeholder-color')
      );
    }

    &:hover {
      @include inset-input-border(var(--el-input-hover-border-color));
    }

    &:focus {
      outline: none;
      @include inset-input-border(var(--el-input-focus-border-color));
    }
  }

  & .#{$namespace}-input__count {
    color: var(--el-color-info);
    background: getCssVar('fill-color', 'blank');
    position: absolute;
    font-size: 12px;
    line-height: 14px;
    bottom: 5px;
    right: 10px;
  }

  @include when(disabled) {
    .#{$namespace}-textarea__inner {
      background-color: map.get($input-disabled, 'fill');
      border-color: map.get($input-disabled, 'border');
      color: map.get($input-disabled, 'text-color');
      cursor: not-allowed;

      &::placeholder {
        color: map.get($input-disabled, 'placeholder-color');
      }
    }
  }

  @include when(exceed) {
    .#{$namespace}-textarea__inner {
      border-color: var(--el-color-danger);
    }

    .#{$namespace}-input__count {
      color: var(--el-color-danger);
    }
  }
}

@include b(input) {
  @include set-component-css-var('input', $input);

  position: relative;
  font-size: var(--el-font-size-base);
  display: inline-flex;
  width: 100%;
  line-height: map.get($input-height, 'default');
  box-sizing: border-box;
  @include scroll-bar;

  & .#{$namespace}-input__clear,
  & .#{$namespace}-input__password {
    color: var(--el-input-icon-color);
    font-size: map.get($input-font-size, 'default');
    cursor: pointer;

    &:hover {
      color: var(--el-input-clear-hover-color);
    }
  }

  & .#{$namespace}-input__count {
    height: 100%;
    display: inline-flex;
    align-items: center;
    color: var(--el-color-info);
    font-size: 12px;

    .#{$namespace}-input__count-inner {
      background: getCssVar('fill-color', 'blank');
      line-height: initial;
      display: inline-block;
      padding-left: 8px;
    }
  }

  @include e(wrapper) {
    display: inline-flex;
    flex-grow: 1;
    align-items: center;
    justify-content: center;
    padding: 0 map.get($input-padding-horizontal, 'default')-$border-width;
    background-color: var(--el-input-bg-color, map.get($input, 'bg-color'));
    background-image: none;
    border-radius: var(
      --el-input-border-radius,
      map.get($input, 'border-radius')
    );
    transition: var(--el-transition-box-shadow);
    @include inset-input-border(
      var(--el-input-border-color, map.get($input, 'border-color'))
    );

    &:hover {
      @include inset-input-border(var(--el-input-hover-border-color));
    }

    @include when(focus) {
      @include inset-input-border(var(--el-input-focus-border-color));
    }
  }

  @include e(inner) {
    width: 100%;
    flex-grow: 1;
    -webkit-appearance: none;
    color: var(--el-input-text-color, map.get($input, 'text-color'));
    font-size: inherit;
    height: map.get($input-height, 'default');
    line-height: map.get($input-height, 'default');
    padding: 0;
    outline: none;
    border: none;
    background: none;
    box-sizing: border-box;

    &:focus {
      outline: none;
    }

    &::placeholder {
      color: var(
        --el-input-placeholder-color,
        map.get($input, 'placeholder-color')
      );
    }

    // override edge default style
    &[type='password']::-ms-reveal {
      display: none;
    }
  }

  @each $slot in (prefix, suffix) {
    @include e($slot) {
      display: inline-flex;
      white-space: nowrap;
      flex-shrink: 0;
      flex-wrap: nowrap;
      height: 100%;
      text-align: center;
      color: var(--el-input-icon-color, map.get($input, 'icon-color'));
      transition: all var(--el-transition-duration);
      pointer-events: none;
    }

    @include e(#{$slot}-inner) {
      pointer-events: all;
      display: inline-flex;
      align-items: center;
      justify-content: center;

      @if $slot == prefix {
        > :last-child {
          margin-right: 8px;
        }

        > :first-child {
          &,
          &.#{$namespace}-input__icon {
            margin-left: 0;
          }
        }
      } @else {
        > :first-child {
          margin-left: 8px;
        }
      }
    }
  }

  & .#{$namespace}-input__icon {
    height: inherit;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all var(--el-transition-duration);
    margin-left: 8px;
  }

  @include e(validateIcon) {
    pointer-events: none;
  }

  @include when(active) {
    .#{$namespace}-input__wrapper {
      @include mixed-input-border(
        var(--el-input-focus-border, map.get($input, 'focus-color'))
      );
    }
  }

  @include when(disabled) {
    cursor: not-allowed;

    .#{$namespace}-input__wrapper {
      background-color: map.get($input-disabled, 'fill');
      @include mixed-input-border(map.get($input-disabled, 'border'));
    }

    .#{$namespace}-input__inner {
      color: map.get($input-disabled, 'text-color');

      &::placeholder {
        color: map.get($input-disabled, 'placeholder-color');
      }
    }

    .#{$namespace}-input__icon {
      cursor: not-allowed;
    }
  }

  @include when(exceed) {
    .#{$namespace}-input__wrapper {
      @include mixed-input-border(var(--el-color-danger));
    }

    .#{$namespace}-input__suffix {
      .#{$namespace}-input__count {
        color: var(--el-color-danger);
      }
    }
  }

  @each $size in (large, small) {
    @include m($size) {
      font-size: map.get($input-font-size, $size);
      line-height: map.get($input-height, $size) - 2;

      @include e(wrapper) {
        padding: 0 map.get($input-padding-horizontal, $size)-$border-width;
      }

      @include e(inner) {
        height: map.get($input-height, $size);
        line-height: map.get($input-height, $size);
      }

      .#{$namespace}-input__icon {
        line-height: map.get($input-height, $size);
      }
    }
  }
}

@include b(input-group) {
  display: inline-flex;
  width: 100%;
  align-items: stretch;

  @include e((append, prepend)) {
    background-color: getCssVar('fill-color', 'light');
    color: var(--el-color-info);
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    border-radius: var(--el-input-border-radius);
    padding: 0 20px;
    white-space: nowrap;

    &:focus {
      outline: none;
    }

    .#{$namespace}-select,
    .#{$namespace}-button {
      display: inline-block;
      margin: 0 -20px;
    }

    button.#{$namespace}-button,
    button.#{$namespace}-button:hover,
    div.#{$namespace}-select .#{$namespace}-input__wrapper,
    div.#{$namespace}-select:hover .#{$namespace}-input__wrapper {
      border-color: transparent;
      background-color: transparent;
      color: inherit;
    }

    .#{$namespace}-button,
    .#{$namespace}-input {
      font-size: inherit;
    }
  }

  @include e(prepend) {
    border-right: 0;
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    @include inset-prepend-border(var(--el-input-border-color));
  }

  @include e(append) {
    border-left: 0;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    @include inset-append-border(var(--el-input-border-color));
  }

  @include m(prepend) {
    > .#{$namespace}-input__wrapper {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }

    @include e(prepend) {
      .#{$namespace}-select {
        .#{$namespace}-input {
          .#{$namespace}-input__inner {
            box-shadow: none !important;
          }
          .#{$namespace}-input__wrapper {
            border-top-right-radius: 0;
            border-bottom-right-radius: 0;
            @include inset-prepend-border(var(--el-input-border-color));
          }
          &.is-focus {
            .#{$namespace}-input__inner {
              box-shadow: none !important;
            }
            .#{$namespace}-input__wrapper {
              @include inset-prepend-input-border(
                var(--el-input-focus-border-color)
              );
              z-index: 2;
              &:focus {
                outline: none;
                z-index: 2;
                @include inset-prepend-input-border(
                  var(--el-input-focus-border-color)
                );
              }
            }
          }
        }
        &:hover {
          .#{$namespace}-input__inner {
            box-shadow: none !important;
          }
          .#{$namespace}-input__wrapper {
            z-index: 1;
            @include inset-prepend-input-border(
              var(--el-input-hover-border-color)
            );
          }
        }
      }
    }
  }

  @include m(append) {
    > .#{$namespace}-input__wrapper {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    @include e(append) {
      .#{$namespace}-select {
        .#{$namespace}-input {
          .#{$namespace}-input__inner {
            box-shadow: none !important;
          }
          .#{$namespace}-input__wrapper {
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
            @include inset-append-border(var(--el-input-border-color));
          }
          &.is-focus {
            .#{$namespace}-input__inner {
              box-shadow: none !important;
            }
            .#{$namespace}-input__wrapper {
              z-index: 2;
              @include inset-append-input-border(
                var(--el-input-focus-border-color)
              );
            }
          }
        }
        &:hover {
          .#{$namespace}-input__inner {
            box-shadow: none !important;
          }
          .#{$namespace}-input__wrapper {
            z-index: 1;
            @include inset-append-input-border(
              var(--el-input-hover-border-color)
            );
          }
        }
      }
    }
  }
}
