@use 'sass:map';
@use '../../base/token';
@use '../../abstract';
@use '../button/button';

$input-size: (
  'xs': (
    pd: 7px 12px,
    font: abstract.rem(token.$font-size-1),
    border-radius: 6px,
  ),
  'md': (
    pd: 7px 12px 8px,
    font: abstract.rem(token.$font-size-2),
    border-radius: 8px,
  ),
  'lg': (
    pd: 9px 14px 10px,
    font: abstract.rem(token.$font-size-2),
    border-radius: 8px,
  ),
);

@mixin cds-input-unit-style($has-padding: true) {
  @if $has-padding {
    padding: 8px 12px;
  }

  display: flex;
  align-items: center;
  background-color: var(--base-white);
  border: 1px solid var(--gray-200);
}

// ==============================================================================
// common
// ==============================================================================
.cds-input {
  $this: &;

  display: inline-block;
  font-size: abstract.rem(token.$font-size-1);
  line-height: normal;
  vertical-align: top;

  & > *:last-child {
    margin-bottom: 0;
  }

  &__label {
    display: flex;
    gap: 4px;
    align-items: center;
    margin-bottom: 8px;
    color: var(--gray-600);
    font-weight: abstract.font-weight-to-number(token.$font-weights-commerce-sans-1);

    :where(&.is-required) {
      &::before {
        content: '*';
        color: var(--primary-red-500);
      }
    }
  } // label

  &__content {
    margin-bottom: 6px;
    box-shadow: var(--shadow-xs);
    border-radius: 8px;
  } // content

  &:where(:not([class*='textarea'])) &__content {
    display: flex;
  } // input height

  &__field {
    @include cds-input-unit-style(false);
    @include abstract.relative();

    // padding: 8px 12px;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    line-height: 1;
  } // field

  &:focus-within :where(#{$this}__field) {
    border-color: var(--gray-400);
    box-shadow: var(--shadow-xs-focused-4px-primary-gray-100);
    z-index: 1;
  }

  input,
  textarea {
    width: 100%;
    padding: 0;
    border: 0;
    color: var(--gray-700);
    font-size: token.$font-size-2;
    line-height: 1rem;
    background-color: var(--base-white);

    &::-webkit-input-placeholder {
      color: var(--gray-300);
    }

    &::-moz-placeholder {
      color: var(--gray-300);
    }

    &:-ms-input-placeholder {
      color: var(--gray-300);
    }

    &::-ms-input-placeholder {
      color: var(--gray-300);
    }

    &::placeholder {
      transform: translateY(1px);
      color: var(--gray-300);
    }

    &:focus {
      outline: none;
    }
  } // input

  @each $size, $info in $input-size {
    &--#{$size} {
      padding: map.get($info, 'pd');
      border-radius: map.get($info, 'border-radius');
      input {
        font-size: map.get($info, 'font');
      }
    }
  }

  input {
    &:-webkit-autofill,
    &:-webkit-autofill:hover,
    &:-webkit-autofill:focus,
    &:-webkit-autofill:active {
      -webkit-box-shadow: 0 0 0 30px var(--base-white) inset !important;
    }
  }

  &__destructive-ico {
    margin-left: 10px;
    stroke: var(--primary-red-600);
  }

  &__hint-text {
    color: var(--gray-400);
  } // help text

  // size
  // default: sm, md
  // ==============================================================================
  @each $type in ('sm', 'md') {
    :where(&.#{$type}) #{$this}__content {
      height: if($type == 'sm', 40px, 44px);
    }
  }

  // destructive, is-disabled, full-width
  // input, editor, textarea
  // ==============================================================================

  &.destructive {
    // field
    #{$this}__field {
      border-color: var(--primary-red-600);
    }

    &:where(:focus-within) #{$this}__field {
      box-shadow: var(--shadow-xs-focused-4px-error-100);
    }

    #{$this}__field-inner {
      #{$this}__destructive-ico {
        z-index: 5;
      }
    } // field

    #{$this}__hint-text {
      color: var(--primary-red-600);
    } // helptext
  } // destructive

  // disabled
  // ==============================================================================
  &.is-disabled {
    #{$this}__content > :not(button) {
      background-color: var(--gray-50);
      border-color: var(--gray-200);
    }

    #{$this}__field {
      overflow: hidden;

      &::after {
        @include abstract.absolute($top: 0, $left: 0);

        content: '';
        width: 100%;
        height: 100%;
        background: inherit;
        opacity: 0.5;
      }
    }

    input {
      color: var(--gray-300);
      background-color: inherit;
    }

    #{$this}__destructive-ico {
      display: none;
    }

    #{$this}__button {
      color: var(--gray-200);
    }

    #{$this}__hint-text {
      color: var(--gray-400);
    }
  } // disabled

  &.full-width {
    width: 100%;
  }
}
// password input
// ==============================================================================
.cds-input--password-input {
  .cds-input__pw-icon {
    margin-block-start: 2px;
    margin-inline-end: 8px;
  }

  .cds-btn {
    padding: 0 0 0 8px;
    height: auto;
  } // view pw

  .cds-input__pw-eye-icon {
    color: var(--gray-300);
  }
}

// leading text
// ==============================================================================
.cds-input--leading-text {
  .cds-input__leading-text {
    @include cds-input-unit-style();

    border-radius: 8px 0 0 8px;
    color: var(--gray-400);
    font-size: token.$font-size-2;
    border-width: 1px 0 1px 1px;

    & + .cds-input__field {
      border-radius: 0 8px 8px 0;
    }
  } // leading text
}

// file
// ==============================================================================
.cds-input--choose-file {
  $secondary-gray-info: map.get(button.$btn, 'secondary-gray');
  @include abstract.relative();

  display: block;

  .cds-input__content {
    display: block;
    box-shadow: none;
  }

  [type='file'] {
    @include abstract.absolute();

    width: 1px;
    height: 1px;
    clip: rect(0, 0, 0, 0);
  }

  .cds-input__hint-text {
    margin-top: 6px;
  }

  .cds-input__hint-list {
    margin-top: 6px;
    margin-left: 10px;
    color: var(--gray-400);
    font-size: token.$font-size-0;
  }

  .cds-input__hint-item {
    @include abstract.relative();
    padding-left: 10px;

    &::before {
      @include abstract.absolute($left: 0, $top: 5px);

      content: '';
      width: 4px;
      height: 4px;
      border-radius: 50%;
      background-color: currentColor;
    }
  }

  .cds-input__tag-wrap {
    margin-top: 6px;
    padding: 12px 8px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    background-color: var(--gray-50);

    .cds-tag {
      &:nth-child(n + 2) {
        margin-top: 6px;
      }
    }
  }

  :where(.cds-btn.is-disabled),
  .is-disabled.cds-btn:hover {
    $color: map.get($secondary-gray-info, 'disable');

    background-color: map.get($color, 'bg');
    border-color: map.get($color, 'border');
    color: map.get($color, 'color');
    cursor: not-allowed;
  }

  &:focus-within :not(.is-disabled).cds-btn {
    $shadow: map.get(map.get($secondary-gray-info, 'focus'), 'shadow');
    box-shadow: $shadow;
  }
}

// trailing-button
.cds-input--trailing-button {
  .cds-input__button {
    @include cds-input-unit-style();

    border-radius: 0 8px 8px 0;
    border-left-width: 0;
    color: var(--gray-700);
    font-weight: abstract.font-weight-to-number(token.$font-weights-commerce-sans-2);

    &:not(:disabled) {
      cursor: pointer;
    }
  } // cds-input__button

  .cds-input__field {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }
}

// textarea
// ==============================================================================
.cds-input--textarea {
  .cds-input__content {
    flex-direction: column;
    align-items: flex-start;
    height: 126px;
  }

  .cds-input__field {
    padding: 12px;
    height: 100%;
    border-radius: 8px;
  }

  textarea {
    width: 100%;
    height: 100%;
    word-wrap: break-word;

    &:disabled {
      white-space: pre-wrap;
    }
  }
}

.cds-input__text-count {
  color: var(--gray-400);
  font-size: abstract.rem(token.$font-size-1);

  @at-root .cds-input.destructive & {
    color: var(--primary-red-600);
  }
}
