@use "variables";

label.fwe-input-text {
  position: relative;
  display: flex;
  flex-direction: column;
  line-height: 0px;
  padding-bottom: 18px;
  cursor: text;

  .fwe-input-text-label {
    height: 18px;
    order: 1;
    color: variables.$text;
    font-size: variables.$font-size-small;
    line-height: variables.$line-height-base;
    font-weight: variables.$font-weight-bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .fwe-input-text-info {
    display: block;
    position: absolute;
    top: 51px;
    width: 100%;
    font-size: variables.$font-size-small;
    line-height: variables.$line-height-base;
    color: variables.$text-disabled;
  }

  .fwe-input-text-invalid {
    @extend .fwe-input-text-info;
    display: none;
    color: variables.$red;
  }

  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  textarea {
    text-overflow: ellipsis;
    order: 2;
    outline: none;
    border: none;
    border-radius: 0;
    background: none;
    border-bottom-width: 1px;
    border-bottom-style: solid;
    border-color: variables.$control-border;
    width: 100%;
    padding-top: 4px;
    padding-bottom: 4px;

    &:disabled {
      pointer-events: none;
    }

    &::placeholder {
      color: variables.$text-disabled;
    }

    &:hover:valid:not(:disabled) {
      border-color: variables.$hero;
    }

    &:hover:not(:disabled) {
      color: variables.$hero;
    }

    &:focus {
      outline: none;
      border-color: variables.$hero;
      color: variables.$text !important;
    }

    &:user-invalid {
      border-color: variables.$red;
      & ~ .fwe-input-text-info {
        display: none;
      }
      & ~ .fwe-input-text-invalid {
        display: block;
      }
    }

    &:disabled {
      background: none;
      color: variables.$text-disabled;
      border-color: variables.$control-disabled;

      & ~ .fwe-input-text-label,
      & ~ .fwe-input-text-invalid,
      & ~ .fwe-input-text-info {
        color: variables.$text-disabled;

        &::before {
          color: variables.$text-disabled;
        }
      }
    }
  }

  // replace the calendar icon for webkit
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"] {
    height: 33px;
    position: relative;
    &:before {
      color: transparent;
      background: none;
      display: block;
      width: 16px;
      height: 16px;
      position: absolute;
      top: 6px;
      right: 0px;
      color: variables.$text;
    }
    &::-webkit-calendar-picker-indicator {
      color: transparent;
      background: none;
      z-index: 1;
    }
  }
  input[type="date"],
  input[type="datetime-local"] {
    &:before {
      content: "\ea0d";
    }
  }
  input[type="time"] {
    &:before {
      content: "\ea11";
    }
  }

  textarea {
    resize: none;
    margin-top: 4px;
    padding-left: 8px;
    padding-right: 8px;
    border-width: 1px;
    border-style: solid;

    @for $i from 2 through 10 {
      &.fwe-row-#{$i} {
        // height = rowcount * rowheight + margin-top + margin-bottom + 1px-line
        height: calc(#{$i} * #{variables.$textarea-row-const});
      }
    }

    ~ .fwe-input-text-info,
    ~ .fwe-input-text-invalid {
      top: unset;
      bottom: 0px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }

  // Invalid state: red border + show error text
  &:has(input:user-invalid:not(:disabled)),
  &.fwe-input-text--invalid {
    .fwe-input-text-info {
      display: none;
    }

    .fwe-input-text-invalid {
      display: block;
    }
  }

  // Validation icons: opt-in via .fwe-input-text--with-validation-icons
  // Icons are suppressed when a custom icon is already present (.fwe-input-text-icon)
  &.fwe-input-text--with-validation-icons {
    &:has(input:user-valid:not(:disabled)),
    &:has(input:user-invalid:not(:disabled)),
    &.fwe-input-text--valid,
    &.fwe-input-text--invalid {
      &:not(.fwe-input-text-icon) {
        input[type="text"],
        input[type="email"],
        input[type="password"],
        input[type="number"],
        input[type="date"],
        input[type="time"],
        input[type="datetime-local"] {
          padding-right: 24px;
        }

        &::after {
          content: "";
          position: absolute;
          bottom: 27px;
          right: 0px;
          width: 16px;
          height: 16px;
          background-repeat: no-repeat;
          background-position: center center;
        }
      }
    }

    &:has(input:user-valid:not(:disabled)),
    &.fwe-input-text--valid {
      &:not(.fwe-input-text-icon)::after {
        background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='13' height='10' viewBox='0 0 13 10' fill='none'><path d='M4.207 9.621L0 5.414L1.414 4L4.207 6.793L11 0L12.414 1.414L4.207 9.621Z' fill='%230091DC'/></svg>");
        background-size: 13px 13px;
      }
    }

    &:has(input:user-invalid:not(:disabled)),
    &.fwe-input-text--invalid {
      &:not(.fwe-input-text-icon)::after {
        background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none'><path d='M16.95 8.46405L15.536 7.05005L12 10.586L8.46405 7.05005L7.05005 8.46405L10.586 12L7.05005 15.536L8.46405 16.95L12 13.414L15.536 16.95L16.95 15.536L13.414 12L16.95 8.46405Z' fill='%23D50000'/></svg>");
        background-size: 24px 24px;
      }
    }
  }

  &.fwe-input-text-icon {
    .fwe-svg-icon,
    i {
      line-height: 1;
      position: absolute;
      bottom: 27px;
      right: 0px;
      height: 16px;
      width: 16px;
    }

    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    textarea {
      padding-right: 24px;
    }
  }
}
