@use "../../styles/abstract/base" as base;
@use "../../styles/abstract/variables" as v;
@use "../../styles/abstract/functions" as f;
@use "../../styles/abstract/mixins" as m;
@use "../../styles/abstract/theme" as t;

$component: "#{base.$prefix}-text-field";

@mixin mirrored {
[dir='rtl'] &,
&.#{$component}--rtl {
@content;
}
}

@keyframes onAutoFillStart {
from {
opacity: 1;
}
to {
opacity: 1;
}
}

@keyframes onAutoFillCancel {
from {
opacity: 1;
}
to {
opacity: 1;
}
}

.#{$component} {
position: relative;
display: inline-flex;
flex-direction: column;
contain: inline-size;
contain-intrinsic-inline-size: v.text-field('width');
flex: 0 0 auto;
&__field {
position: relative;
display: flex;
flex-direction: column;
}

&__label {
@include m.typography("body-large");
user-select: none;
position: absolute;
left: var(--#{$component}-start, 16px);
top: 50%;
transform: translateY(-50%);
transform-origin: left top;
pointer-events: none;
color: t.color("on-surface-variant");
transition:
transform v.motion("duration-short4") v.motion("easing-emphasized"),
color v.motion("duration-short2") v.motion("easing-standard"),
left v.motion("duration-short4") v.motion("easing-emphasized");
}

&__input {
@include m.typography("body-large");
@include m.shape("extra-small");
padding: 13px 16px;
width: 100%;
height: f.density(56px, 40px);
box-sizing: border-box;
color: t.color("on-surface");
caret-color: t.color("primary");
border: 0;
appearance: none;
outline: none;

animation-name: onAutoFillCancel;
animation-duration: 10ms;

transition: background-color 0s ease-out 50000000s;

&::placeholder {
color: t.color("on-surface-variant");
opacity: 1;
}

&:-webkit-autofill {
animation-name: onAutoFillStart;
animation-duration: 10ms;
-webkit-text-fill-color: t.color("on-surface");
transition: background-color 5000s ease-in-out 0s;
}

&:autofill {
animation-name: onAutoFillStart;
animation-duration: 10ms;
color: t.color("on-surface");
}
}

&:has(> .#{$component}__field > .#{$component}__label):not(.#{$component}--focused) .#{$component}__input::placeholder {
color: transparent;
-webkit-text-fill-color: transparent;
}

&--empty:has(> .#{$component}__field > .#{$component}__label):not(.#{$component}--focused):not(:has(.#{$component}__input:is(:not(:placeholder-shown), :autofill, :-webkit-autofill))) {
:is(.#{$component}__prefix, .#{$component}__suffix) {
opacity: 0;
}
}

&__prefix {
@include m.typography("body-large");
position: absolute;
left: var(--#{$component}-start, 16px);
top: 50%;
transform: translateY(-50%);
color: t.color("on-surface-variant");
pointer-events: none;
z-index: 2;
white-space: nowrap;
max-width: 40%;
overflow: hidden;
text-overflow: ellipsis;
transition:
top v.motion("duration-short4") v.motion("easing-emphasized"),
opacity v.motion("duration-short4") v.motion("easing-emphasized");
}

&__suffix {
@include m.typography("body-large");
position: absolute;
right: var(--#{$component}-end, 16px);
top: 50%;
transform: translateY(-50%);
color: t.color("on-surface-variant");
pointer-events: none;
z-index: 2;
white-space: nowrap;
max-width: 40%;
overflow: hidden;
text-overflow: ellipsis;
transition:
top v.motion("duration-short4") v.motion("easing-emphasized"),
opacity v.motion("duration-short4") v.motion("easing-emphasized");
}

&__leading-icon {
position: absolute;
left: 12px;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
pointer-events: none;
color: t.color("on-surface-variant");
z-index: 1;
transition: top v.motion("duration-short4")
v.motion("easing-emphasized");

svg {
width: 24px;
height: 24px;
}
}

&__trailing-icon {
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: t.color("on-surface-variant");
z-index: 1;
transition: top v.motion("duration-short4")
v.motion("easing-emphasized");

svg {
width: 24px;
height: 24px;
}
}

&__leading-icon[hidden],
&__trailing-icon[hidden] {
display: none;
}

&--with-leading-icon {
--#{$component}-start: 52px;
}

&--with-trailing-icon {
--#{$component}-end: 52px;

.#{$component}__input[type="search" i]::-webkit-search-cancel-button {
display: none;
}
}

&--with-prefix {
--#{$component}-prefix-space: calc(var(--#{$component}-prefix-width, 32px) + 2px);
}

&--with-suffix {
--#{$component}-suffix-space: calc(var(--#{$component}-suffix-width, 32px) + 2px);
}

&--error {
.#{$component}__label,
.#{$component}__trailing-icon {
color: t.color("error");
}

.#{$component}__input {
caret-color: t.color("error");
}
}

&--disabled {
.#{$component}__input {
color: t.alpha("on-surface", 0.38);
-webkit-text-fill-color: t.alpha("on-surface", 0.38);
pointer-events: none;
}

.#{$component}__supporting {
color: t.color("on-surface");
opacity: 0.38;
}

.#{$component}__label {
color: t.color("on-surface");
opacity: 0.38;
}

.#{$component}__leading-icon,
.#{$component}__trailing-icon,
.#{$component}__prefix,
.#{$component}__suffix {
color: t.color("on-surface");
opacity: 0.38;
}
}

&:has(.#{$component}__input:disabled) {
.#{$component}__label,
.#{$component}__supporting {
color: t.color("on-surface");
opacity: 0.38;
}
}

&__input:disabled {
color: t.alpha("on-surface", 0.38);
-webkit-text-fill-color: t.alpha("on-surface", 0.38);
pointer-events: none;

& ~ .#{$component}__leading-icon,
& ~ .#{$component}__trailing-icon,
& ~ .#{$component}__prefix,
& ~ .#{$component}__suffix {
color: t.color("on-surface");
opacity: 0.38;
}
}

&__supporting {
@include m.typography("body-small");
display: flex;
gap: 16px;
padding: 4px 16px 0;
color: t.color("on-surface-variant");
}

&__helper {
flex: 1 1 auto;
min-width: 0;

&--error {
color: t.color("error");
}
}

&__counter {
flex: none;
margin-inline-start: auto;
}

&--error .#{$component}__counter {
color: t.color("error");
}

&__required {
color: inherit;
}

@include m.reduced-motion {
&__label,
&__outline-leading,
&__outline-notch,
&__outline-trailing {
transition: none;
}

&__prefix,
&__suffix,
&__leading-icon,
&__trailing-icon {
transition: none;
}
}

&--filled {
.#{$component}__label {
opacity: 1;
}
.#{$component}__input {
background-color: t.color("surface-container-highest");
padding: f.density(24px, 12px, 3px) 16px f.density(7px, 3px, 1px);
border-bottom: 1px solid t.color("on-surface-variant");
border-radius: f.get-shape("extra-small") f.get-shape("extra-small")
0 0;
@include m.motion-transition(background-color, border-color);

&:-webkit-autofill {
border-radius: f.get-shape("extra-small")
f.get-shape("extra-small") 0 0;
}
}

.#{$component}__field::before {
content: "";
position: absolute;
opacity: 0;
bottom: 0;
width: 100%;
height: 2px;
background-color: t.color("primary");
border-radius: 0;
pointer-events: none;
transition: opacity v.motion("duration-short4") v.motion("easing-standard");

@include m.reduced-motion {
transition: none;
}
}

&:not(.#{$component}--empty) .#{$component}__label,
&.#{$component}--focused .#{$component}__label {
transform: translateY(calc(-12px - 37.5%)) scale(0.75);
}

&:has(.#{$component}__input:is(:not(:placeholder-shown), :autofill, :-webkit-autofill)) .#{$component}__label {
transform: translateY(calc(-12px - 37.5%)) scale(0.75);
}

&:has(> .#{$component}__field > .#{$component}__label) {
.#{$component}__prefix,
.#{$component}__suffix {
top: f.density(36px, 24px, 3px);
}
}

&:not(:has(> .#{$component}__field > .#{$component}__label)) .#{$component}__input {
padding-top: f.density(16px, 8px, 2px);
padding-bottom: f.density(15px, 7px, 2px);
}

&.#{$component}--focused {
.#{$component}__label {
color: t.color("primary");
}

.#{$component}__field::before {
opacity: 1;
}
}

&:hover:not(.#{$component}--focused):not(.#{$component}--disabled):not(:has(.#{$component}__input:disabled)) .#{$component}__input {
background-image: linear-gradient(#{t.alpha("on-surface", 0.08)}, #{t.alpha("on-surface", 0.08)});
}

&:hover:not(.#{$component}--focused):not(.#{$component}--disabled):not(.#{$component}--error):not(:has(.#{$component}__input:disabled)) .#{$component}__input {
border-bottom-color: t.color("on-surface");
}

&.#{$component}--error {
.#{$component}__input {
border-bottom-color: t.color("error");
}

.#{$component}__field::before {
background-color: t.color("error");
}

.#{$component}__label {
color: t.color("error");
}

&:hover:not(.#{$component}--focused) {
.#{$component}__input {
border-bottom-color: t.color("on-error-container");
}

.#{$component}__label,
.#{$component}__trailing-icon {
color: t.color("on-error-container");
}
}
}

&.#{$component}--disabled .#{$component}__input,
& .#{$component}__input:disabled {
background-color: t.alpha("on-surface", 0.04);
border-bottom-color: t.alpha("on-surface", 0.38);
}

&.#{$component}--disabled {
pointer-events: none;
}

&.#{$component}--with-leading-icon .#{$component}__leading-icon,
&.#{$component}--with-trailing-icon .#{$component}__trailing-icon {
top: f.density(28px, 20px, 2px);
}
}

@include m.high-contrast {
&--filled.#{$component}--focused .#{$component}__field::before {
opacity: 1;
background-color: transparent;
height: 0;
border-top: 2px solid CanvasText;
}
}

&__outline {
display: none;
position: absolute;
inset: 0;
pointer-events: none;

&-leading,
&-notch,
&-trailing {
border: var(--#{$component}-outline-width, 1px) solid
var(--#{$component}-outline-color, #{t.color("outline")});
@include m.motion-transition(border-color);
}

&-leading {
flex: 0 0 12px;
border-inline-end: 0;
border-radius: f.get-shape("extra-small") 0 0
f.get-shape("extra-small");
}

&-notch {
flex: 0 0 auto;
width: 0;
max-width: calc(100% - 24px);
border-inline: 0;
}

&-trailing {
flex: 1 1 auto;
border-inline-start: 0;
border-radius: 0 f.get-shape("extra-small")
f.get-shape("extra-small") 0;
}

&--notched .#{$component}__outline-notch {
border-top-color: transparent;
}

@include m.high-contrast {
&--notched .#{$component}__outline-notch {
border-top-style: none;
}

.#{$component}__input:not(:placeholder-shown) ~ & .#{$component}__outline-notch,
.#{$component}__input:-webkit-autofill ~ & .#{$component}__outline-notch,
.#{$component}__input:autofill ~ & .#{$component}__outline-notch {
border-top-style: none;
}
}
}

&--outlined {
--#{$component}-outline-color: #{t.color("outline")};
--#{$component}-outline-width: 1px;
border-radius: f.get-shape("extra-small");

.#{$component}__outline {
display: flex;
}

.#{$component}__input:not(:placeholder-shown)
~ .#{$component}__outline
.#{$component}__outline-notch,
.#{$component}__input:-webkit-autofill
~ .#{$component}__outline
.#{$component}__outline-notch,
.#{$component}__input:autofill
~ .#{$component}__outline
.#{$component}__outline-notch {
border-top-color: transparent;
}

.#{$component}__input {
background-color: transparent;
padding: f.density(15px, 7px, 2px) 16px;
border: solid transparent;
border-width: 1px 0;
@include m.motion-transition(padding);

&:-webkit-autofill {
border-radius: f.get-shape("extra-small");
}
}

.#{$component}__label {
top: 50%;
}

&:not(.#{$component}--empty) .#{$component}__label,
&.#{$component}--focused .#{$component}__label {
transform: translateY(calc(#{f.density-offset(28px, 20px, 2px)} - 37.5%)) scale(0.75);
--#{$component}-start: 16px;
}

&:has(.#{$component}__input:is(:not(:placeholder-shown), :autofill, :-webkit-autofill)) .#{$component}__label {
transform: translateY(calc(#{f.density-offset(28px, 20px, 2px)} - 37.5%)) scale(0.75);
--#{$component}-start: 16px;
}

&:hover:not(.#{$component}--focused):not(.#{$component}--disabled):not(.#{$component}--error):not(:has(.#{$component}__input:disabled)) {
--#{$component}-outline-color: #{t.color("on-surface")};

.#{$component}__label {
color: t.color("on-surface");
}
}

&.#{$component}--focused {
--#{$component}-outline-color: #{t.color("primary")};
--#{$component}-outline-width: 2px;

.#{$component}__label {
color: t.color("primary");
}
}

&.#{$component}--error {
--#{$component}-outline-color: #{t.color("error")};

&:hover:not(.#{$component}--focused) {
--#{$component}-outline-color: #{t.color("on-error-container")};

.#{$component}__label,
.#{$component}__trailing-icon {
color: t.color("on-error-container");
}
}

.#{$component}__label {
color: t.color("error");
}
}

&.#{$component}--disabled {
--#{$component}-outline-color: #{t.alpha("on-surface", 0.12)};
pointer-events: none;
}

}

&--multiline {
.#{$component}__input {
min-height: f.density(v.text-field('height-multiline'), 84px);
height: auto;
resize: vertical;
padding-top: f.density(v.text-field('padding-top-multiline'), 8px, 2px);
}

&.#{$component}--filled:has(> .#{$component}__field > .#{$component}__label) .#{$component}__input {
padding-top: f.density(v.text-field('padding-top-multiline-label'), 12px, 3px);
}

&.#{$component}--outlined .#{$component}__input {
padding-top: calc(#{f.density(v.text-field('padding-top-multiline'), 8px, 2px)} - #{v.text-field('border-width')});
}

.#{$component}__prefix,
.#{$component}__suffix {
top: f.density(28px, 20px, 2px);
}

.#{$component}__label {
top: f.density(28px, 20px, 2px);
}
}

&__input[type="multiline"] {
min-height: f.density(v.text-field('height-multiline'), 84px);
height: auto;
max-height: none;
flex-shrink: 1;

& ~ .#{$component}__leading-icon,
& ~ .#{$component}__trailing-icon,
& ~ .#{$component}__prefix,
& ~ .#{$component}__suffix {
top: 20px;
transform: none;
}
}

&--density-compact {
--#{base.$prefix}-density: -4;
}

.#{$component}__field .#{$component}__input {
padding-left: calc(var(--#{$component}-start, 16px) + var(--#{$component}-prefix-space, 0px));
padding-right: calc(var(--#{$component}-end, 16px) + var(--#{$component}-suffix-space, 0px));
}

@include mirrored {
.#{$component}__label {
left: auto;
right: var(--#{$component}-start, 16px);
transform-origin: right top;
}

.#{$component}__prefix {
left: auto;
right: var(--#{$component}-start, 16px);
}

.#{$component}__suffix {
right: auto;
left: var(--#{$component}-end, 16px);
}

.#{$component}__leading-icon {
left: auto;
right: 12px;
}

.#{$component}__trailing-icon {
right: auto;
left: 12px;
}

.#{$component}__field .#{$component}__input {
padding-left: calc(var(--#{$component}-end, 16px) + var(--#{$component}-suffix-space, 0px));
padding-right: calc(var(--#{$component}-start, 16px) + var(--#{$component}-prefix-space, 0px));
}

.#{$component}__outline-leading {
border-radius: 0 f.get-shape("extra-small")
f.get-shape("extra-small") 0;
}

.#{$component}__outline-trailing {
border-radius: f.get-shape("extra-small") 0 0
f.get-shape("extra-small");
}
}

&.#{$component}--disabled.#{$component}--error,
&:has(.#{$component}__input:disabled) {
.#{$component}__helper--error,
.#{$component}__counter {
color: t.color("on-surface");
}
}
}

.#{$component}__trailing-icon.#{$component}__trailing-icon--button {
right: 4px;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 50%;
background: transparent;
font: inherit;
cursor: pointer;
@include m.touch-target(48px);
position: absolute;

&::before {
content: "";
position: absolute;
inset: 0;
border-radius: inherit;
background-color: currentColor;
opacity: 0;
pointer-events: none;
}

&:hover:not(:disabled)::before {
opacity: 0.08;
}

&:focus-visible {
outline: 2px solid t.color("secondary");
outline-offset: 2px;
}

&:focus-visible::before,
&:active:not(:disabled)::before {
opacity: 0.1;
}

&:disabled {
cursor: default;
}

[dir='rtl'] &,
.#{$component}--rtl & {
right: auto;
left: 4px;
}
}
