@use './../functions/tokens';
@use './../tokens/components';
@use './../mixins/utilities';
@use './../mixins/forms' as forms-mx;
@use './../mixins/icons' as icons-mx;
@use './../tokens/helpers';

tokens.$default-map: components.$post-text-input;

@mixin time-icon-position(
  $inset-inline-end: tokens.get('input-validation-icon-position-inline-end')
) {
  position: absolute;
  inset-inline-end: $inset-inline-end;
  inset-block-start: 50%;
  transform: translateY(-50%);
  width: tokens.get('input-sizing-icon');
  height: tokens.get('input-sizing-icon');
}

input.form-control {
  display: block;
  width: 100%;
  font-family: inherit;
  appearance: none; // Fix appearance for date inputs in Safari
  background-clip: padding-box;
  border: tokens.get('input-border-width') tokens.get('input-border-style-default')
    tokens.get('input-color-enabled-border');
  position: relative;
  border-radius: tokens.get('input-border-radius-surface');
  background-color: tokens.get('input-color-enabled-bg');
  color: tokens.get('input-color-enabled-fg');
  padding-inline-start: tokens.get('input-padding-inline-start');
  padding-inline-end: tokens.get('input-padding-inline-end');
  padding-block: tokens.get('input-standalone-padding-block');
  height: tokens.get('input-sizing-height');
  font-size: tokens.get('input-font-size');

  &.form-control-sm {
    height: tokens.get('input-small-sizing-height');
    padding-block: tokens.get('input-non-floating-small-padding-block');
    font-size: tokens.get('input-non-floating-small-font-size');
  }

  &:focus {
    border-color: tokens.get('input-color-selected-border');
    background-color: tokens.get('input-color-selected-bg');
    color: tokens.get('input-color-selected-fg');

    @include utilities.focus-style;

    @include utilities.high-contrast-mode {
      border-color: FieldText !important; // On blink browser valid and invalid border colors are converted to Highlight color
    }
  }

  &:not(:disabled) {
    &:hover,
    &:focus {
      @include utilities.high-contrast-mode {
        border-color: FieldText;
      }
    }

    &:hover {
      border-color: tokens.get('input-color-hover-border');
      background-color: tokens.get('input-color-hover-bg');

      @include utilities.high-contrast-mode {
        border-color: Highlight;
      }
    }
  }

  &[readonly]:not(:disabled) {
    border-color: tokens.get('input-color-enabled-border');
    background-color: tokens.get('input-color-enabled-bg');
  }

  &[type='file'] {
    overflow: hidden;
    position: relative;

    &:disabled {
      @include utilities.high-contrast-mode {
        color: GrayText; // Needed for the button on blink browser, even if we define it on .form-control:disabled
        border-color: GrayText;
      }
    }

    // File input buttons theming
    &::file-selector-button {
      padding-block: tokens.get('input-standalone-padding-block');
      padding-inline-start: tokens.get('input-padding-inline-start');
      padding-inline-end: tokens.get('input-padding-inline-end');
      margin-block: calc(#{tokens.get('input-standalone-padding-block')} * -1);
      color: tokens.get('input-color-enabled-fg');
      pointer-events: none;
      border-color: inherit;
      border-style: solid;
      border-width: 0;
      border-inline-end-width: tokens.get('input-border-width');
      border-radius: 0;
      display: block;
      position: absolute;
      inset-block-start: 0;
      inset-block-end: 0;
      inset-inline-end: 0;
      margin-inline: 0;
      height: auto;
      background-color: inherit;
      border-right: 0 none;
      border-left: inherit;
    }

    // Fix Safari cutoff when change the initial position of the selector button.
    /* stylelint-disable-next-line */
    @supports (background: -webkit-named-image(i)) {
      &::file-selector-button {
        display: initial;
        position: initial;
        margin-inline: 0;
        padding-inline: 0;
        width: 0;
        border: 0 none;
      }

      &::after {
        display: flex;
        align-items: center;
        content: 'Choose File';
        position: absolute;
        inset-block-start: 0;
        inset-block-end: 0;
        inset-inline-end: 0;
        z-index: 2;
        padding-inline: tokens.get('input-padding-inline-start');
        background-color: inherit;
        border-inline-start: inherit;
        text-align: center;

        [lang='de'] & {
          content: 'Datei auswählen';
        }

        [lang='fr'] & {
          content: 'Choisir un fichier';
        }

        [lang='it'] & {
          content: 'Scegli file';
        }
      }

      &[multiple] {
        &::after {
          content: 'Choose Files';

          [lang='de'] & {
            content: 'Dateien auswählen';
          }

          [lang='fr'] & {
            content: 'Choisir des fichiers';
          }
        }
      }
    }

    &.is-valid,
    &.is-invalid {
      &::file-selector-button {
        padding-inline-end: calc(
          #{tokens.get('input-padding-inline-end')} +
          #{tokens.get('input-validation-icon-position-inline-end')} +
          #{tokens.get('input-sizing-icon')}
        );
      }
    }

    &:not(:disabled):not([readonly]) {
      cursor: pointer;
    }
  }

  &[type='time'] {
    padding-inline-end: calc(
      #{tokens.get('input-padding-inline-end')} +
      #{tokens.get('input-validation-icon-position-inline-end')} +
      #{tokens.get('input-sizing-icon')}
    );

    &::-webkit-date-and-time-value {
      text-align: start; // force iOS to align the text on the left
    }

    &::-webkit-calendar-picker-indicator {
      @include time-icon-position;
      background: transparent;
      cursor: pointer;
      z-index: 1;
      padding: 0;
      margin: 0;

      &:focus-within {
        border-radius: 4px;
        outline-offset: tokens.get('focus-outline-offset', helpers.$post-focus) !important;
        outline: tokens.get('focus-outline-color', helpers.$post-focus)
          tokens.get('post-focus-border-style', helpers.$post-focus)
          tokens.get('focus-outline-width', helpers.$post-focus) !important;
      }
    }

    // Custom time icon overlay that forwards clicks to the native picker (pointer-events: none).
    &::after {
      @include time-icon-position;
      content: '';
      display: block;
      pointer-events: none;
      background-color: tokens.get('input-color-enabled-fg');
      z-index: 2;

      @include icons-mx.mask-image('time');
    }

    &:disabled::after {
      display: none;
    }

    &.is-valid,
    &.is-invalid {
      &:not(:disabled) {
        padding-inline-end: calc(
          #{tokens.get('input-validation-icon-position-inline-end')} +
          #{tokens.get('post-datepicker-input-icons-gap-inline', components.$post-datepicker)} +
          #{tokens.get('input-sizing-icon')} * 2 + #{tokens.get('input-padding-inline-end')}
        );
        background-position: top 50% right
          calc(
            #{tokens.get('input-validation-icon-position-inline-end')} +
            #{tokens.get('post-datepicker-input-icons-gap-inline', components.$post-datepicker)} +
            #{tokens.get('input-sizing-icon')}
          );
      }
    }
  }

  &::placeholder {
    color: tokens.get('input-color-enabled-fg');
    opacity: 1;
  }

  // Safari-specific: Override yellow placeholder text in date/time inputs
  // Note: :not(:focus) prevents black text on dark blue selection background in
  // Chrome/Edge (Windows), a combination that produces insufficient contrast ratios.
  &[type='date'],
  &[type='datetime-local'],
  &[type='month'],
  &[type='week'],
  &[type='time'] {
    &::-webkit-datetime-edit-day-field:not(:focus),
    &::-webkit-datetime-edit-month-field:not(:focus),
    &::-webkit-datetime-edit-year-field:not(:focus),
    &::-webkit-datetime-edit-hour-field:not(:focus),
    &::-webkit-datetime-edit-minute-field:not(:focus),
    &::-webkit-datetime-edit-second-field:not(:focus),
    &::-webkit-datetime-edit-ampm-field:not(:focus),
    &::-webkit-datetime-edit-meridiem-field:not(:focus) {
      -webkit-text-fill-color: tokens.get('input-color-enabled-fg') !important;
    }

    // Safari-specific: Override default selection styles where background and text colors
    // produce insufficient contrast ratios.
    &::-webkit-datetime-edit-day-field:focus,
    &::-webkit-datetime-edit-fields-wrapper:focus,
    &::-webkit-datetime-edit-month-field:focus,
    &::-webkit-datetime-edit-year-field:focus,
    &::-webkit-datetime-edit-hour-field:focus,
    &::-webkit-datetime-edit-minute-field:focus,
    &::-webkit-datetime-edit-second-field:focus,
    &::-webkit-datetime-edit-ampm-field:focus,
    &::-webkit-datetime-edit-meridiem-field:focus {
      background-color: #b4d5fe !important;
      -webkit-text-fill-color: #000 !important;
    }
  }

  &:disabled {
    color: tokens.get('input-color-disabled-fg');
    background-color: tokens.get('input-color-disabled-bg');
    border-color: tokens.get('input-color-disabled-border');
    border-style: tokens.get('input-border-style-disabled');
    opacity: 1;

    &::placeholder {
      color: tokens.get('input-color-disabled-fg');
      opacity: 1;
    }
  }

  &:hover:not(:disabled):not([readonly])::file-selector-button {
    background-color: tokens.get('input-color-hover-bg');
  }

  &.is-valid,
  &.is-invalid {
    &:not(:disabled) {
      background-size: tokens.get('input-sizing-icon') tokens.get('input-sizing-icon');
      background-position: top 50% right tokens.get('input-validation-icon-position-inline-end');
      background-repeat: no-repeat;
      padding-inline-end: calc(
        #{tokens.get('input-padding-inline-end')} +
        #{tokens.get('input-validation-icon-position-inline-end')} +
        #{tokens.get('input-sizing-icon')}
      );

      &.form-control-sm {
        background-size: tokens.get('input-small-sizing-icon') tokens.get('input-small-sizing-icon');
        padding-inline-end: calc(
          #{tokens.get('input-padding-inline-end')} +
          #{tokens.get('input-validation-icon-position-inline-end')} +
          #{tokens.get('input-small-sizing-icon')}
        );
      }
    }
  }
}

// Only adds validation icons as background-image on none floating elements
*:not(.form-floating) > input.form-control {
  @include forms-mx.validation-icons;
}

.form-label:has(+ input.form-control[disabled]) {
  color: tokens.get('input-color-disabled-fg');
}

.form-label:has(+ input.form-control-sm) {
  display: block;
  padding-inline: tokens.get('input-non-floating-small-label-padding-inline');
  padding-block: tokens.get('input-non-floating-small-label-padding-block');
  font-weight: tokens.get('input-non-floating-small-label-font-weight');
  font-size: tokens.get('input-non-floating-small-label-font-size');
}

.form-floating {
  position: relative;

  &::after {
    width: tokens.get('input-sizing-icon');
    height: tokens.get('input-sizing-icon');
  }

  &:has(> input.form-control.is-valid:not(:disabled))::after,
  &:has(> input.form-control:-webkit-autofill.is-valid)::after {
    content: '';
    position: absolute;
    inset-block-start: var(
      --post-form-floating-validation-icon-block-start,
      calc(#{tokens.get('input-sizing-height')} / 2)
    );
    transform: translateY(-50%);
    inset-inline-end: var(
      --post-form-floating-validation-icon-inline-end,
      #{tokens.get('input-validation-icon-position-inline-end')}
    );
    z-index: 2;
    pointer-events: none;
    background-image: var(--post-signal-icon-success);
  }

  &:has(> input.form-control.is-invalid:not(:disabled))::after,
  &:has(> input.form-control:-webkit-autofill.is-invalid)::after {
    content: '';
    position: absolute;
    inset-block-start: var(
      --post-form-floating-validation-icon-block-start,
      calc(#{tokens.get('input-sizing-height')} / 2)
    );
    transform: translateY(-50%);
    inset-inline-end: var(
      --post-form-floating-validation-icon-inline-end,
      #{tokens.get('input-validation-icon-position-inline-end')}
    );
    z-index: 1;
    pointer-events: none;
    background-image: var(--post-signal-icon-error);
  }

  &:has(> input.form-control[type='time'].is-valid:not(:disabled))::after,
  &:has(> input.form-control[type='time']:-webkit-autofill.is-valid)::after,
  &:has(> input.form-control[type='time'].is-invalid:not(:disabled))::after,
  &:has(> input.form-control[type='time']:-webkit-autofill.is-invalid)::after {
    inset-inline-end: calc(
      #{tokens.get('input-validation-icon-position-inline-end')} +
      #{tokens.get('post-datepicker-input-icons-gap-inline', components.$post-datepicker)} +
      #{tokens.get('input-sizing-icon')}
    );
  }

  > input.form-control ~ label {
    position: absolute;
    z-index: 1;
    text-align: start;
    pointer-events: none;
    display: block;
    inset-block-start: tokens.get('input-border-width');
    inset-inline-start: tokens.get('input-border-width');
    margin: 0;
    padding-inline-start: tokens.get('input-padding-inline-start');
    padding-inline-end: tokens.get('input-label-empty-padding-inline-end');
    padding-block-start: tokens.get('input-label-empty-padding-block-start');
    border: 0;
    color: tokens.get('input-color-enabled-fg');
    font-size: tokens.get('input-font-size');
    width: auto;
    height: auto;
    max-width: calc(100% - (#{tokens.get('input-border-width')} * 2));
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition:
      padding-block-start 0.2s ease-in-out,
      font-size 0.2s ease-in-out;
  }

  > input.form-control {
    min-height: tokens.get('input-sizing-height');
    padding-block-start: tokens.get('input-padding-block-start');
    padding-block-end: tokens.get('input-padding-block-end');

    &::placeholder {
      color: transparent;
      opacity: 0;

      /*
       * Does not work in firefox because it's non-standard behaviour.
       * Implemented as progressive enhancement for the browsers that support it.
       * https://stackoverflow.com/questions/16381895/firefox-transition-placeholder-opacity
      */
      transition: opacity 0.25s linear;
    }

    &:focus,
    &:not(:placeholder-shown) {
      ~ label {
        color: tokens.get('input-color-selected-fg');
        font-size: tokens.get('input-label-filled-font-size');
        padding-block-start: tokens.get('input-label-filled-padding-block-start');
      }

      &:not(:hover):not(:disabled) {
        ~ label {
          color: tokens.get('input-color-selected-fg');
        }
      }
    }

    &:focus::placeholder {
      color: tokens.get('input-color-helptext-fg');
      opacity: 1;
      transition: opacity 0.35s 0.15s linear;
    }

    &:disabled ~ label {
      color: tokens.get('input-color-disabled-fg');
      background-color: tokens.get('input-color-disabled-bg');
    }

    &:not(:disabled):hover ~ label {
      color: tokens.get('input-color-hover-fg');
    }

    &[type='file'] {
      &:focus,
      &:not(:placeholder-shown) {
        padding-inline-start: tokens.get('input-label-empty-padding-inline-start');
        padding-inline-end: tokens.get('input-label-empty-padding-inline-end');
      }
    }
  }

  > input.form-control:-webkit-autofill {
    padding-block-start: tokens.get('input-padding-block-start');
    padding-block-end: tokens.get('input-padding-block-end');

    ~ label {
      color: tokens.get('input-color-selected-fg');
      font-size: tokens.get('input-label-filled-font-size');
      background-color: tokens.get('input-color-selected-bg');
    }
  }

  @include utilities.high-contrast-mode() {
    > input::placeholder {
      opacity: 0;
    }
  }
}
