@use "sass:string";
@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}-button.#{base.$prefix}-tab";
$container: "#{base.$prefix}-tabs";

.#{$container} {
position: relative;
display: flex;
flex-direction: column;
width: 100%;
box-sizing: border-box;

&--primary {
min-height: 48px;

&:has(.#{$component}--icon-and-text) {
min-height: 64px;
}

.#{$container}__indicator {
border-radius: 3px 3px 0 0;
background-color: t.color("primary");
}
}

&--secondary {
min-height: 48px;
.#{$container}__indicator {
border-radius: 0;
background-color: t.color("primary");
}
}

.#{$component}::before {
content: none;
}

&:not(&--scrollable) {
flex-direction: row;
}

&:not(&--scrollable) > .#{$component} {
flex: 1;
}

&--scrollable {
overflow: hidden;

.#{$container}__scroll {
display: flex;
padding-inline: 52px;
overflow-x: auto;
scroll-behavior: smooth;
scrollbar-width: none;
-ms-overflow-style: none;

&::-webkit-scrollbar {
display: none;
}
}
}

&__divider {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 1px;
background-color: t.color(
"outline-variant"
);
}

&__indicator {
position: absolute;
bottom: 0;
left: 0;
transition: m.spring(transform, default-spatial), m.spring(width, default-spatial);
z-index: 1;
}

@supports (#{string.unquote("anchor-name: --a")}) {
anchor-scope: string.unquote("--#{base.$prefix}-tabs-indicator");
$anchor: string.unquote("--#{base.$prefix}-tabs-indicator");

&__indicator {
position-anchor: $anchor;
}

.#{base.$prefix}-tab--active {
anchor-name: $anchor;
}

&--primary:not(:has(#{&}__indicator--fixed)):not(:has(#{&}__indicator--dynamic)) {
.#{base.$prefix}-tab--active:has(.#{base.$prefix}-button__text) {
anchor-name: none;
}

.#{base.$prefix}-tab--active .#{base.$prefix}-button__text {
anchor-name: $anchor;
}
}

&--primary &__indicator,
&--secondary &__indicator {
transition: m.spring(left, default-spatial), m.spring(width, default-spatial);
}

&--primary &__indicator {
width: string.unquote("max(24px, calc(anchor-size(width) - 4px))");
left: string.unquote("calc(anchor(center) - 0.5 * max(24px, anchor-size(width) - 4px))");
}

&--secondary &__indicator {
left: anchor(left);
width: anchor-size(width);
min-width: 0;
}

&--primary:has(.#{base.$prefix}-tab--active:not(:has(.#{base.$prefix}-button__text))) &__indicator:not(#{&}__indicator--fixed):not(#{&}__indicator--content),
&--primary &__indicator--dynamic,
&--secondary &__indicator--dynamic {
width: string.unquote("max(30px, calc(anchor-size(width) / 2))");
left: string.unquote("calc(anchor(center) - 0.5 * max(30px, calc(anchor-size(width) / 2)))");
}

&--primary &__indicator--content,
&--secondary &__indicator--content {
width: string.unquote("max(30px, anchor-size(width))");
left: string.unquote("calc(anchor(center) - 0.5 * max(30px, anchor-size(width)))");
}

&:has(.#{base.$prefix}-tab--active:not(:has(.#{base.$prefix}-button__text))) &__indicator--content,
&--primary &__indicator--fixed,
&--secondary &__indicator--fixed {
width: var(--#{base.$prefix}-tabs-indicator-width, 40px);
left: string.unquote("calc(anchor(center) - 0.5 * var(--#{base.$prefix}-tabs-indicator-width, 40px))");
}
}
}

.#{$component} {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 90px;
max-width: 360px;
padding: 0 16px;
border: none;
border-radius: 0;
background-color: transparent;
color: t.color("on-surface-variant");
cursor: pointer;
user-select: none;
flex-shrink: 0;

@include m.typography("title-small");

transition:
background-color 0.2s cubic-bezier(0.4, 0, 0.2, 1),
color 0.2s cubic-bezier(0.4, 0, 0.2, 1);

&--text-only {
height: 48px;
}

&--icon-only {
height: 48px;
}

&--icon-and-text {
height: 64px;
flex-direction: column;
}

&.#{base.$prefix}-button--icon {
padding: 0 16px;
}

&:focus {
outline: none;
}

&:focus-visible {
outline: 3px solid t.color("secondary");
outline-offset: -3px;
z-index: 1;
}

&--disabled,
&:disabled {
pointer-events: none;
opacity: 0.38;
}

.#{base.$prefix}-button__icon {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;

svg {
width: 100%;
height: 100%;
fill: currentColor;
}
}

&--icon-and-text {
.#{base.$prefix}-button__icon {
margin-bottom: 4px;
}
}

.#{base.$prefix}-button__text {
@include m.truncate;
max-width: 100%;
text-align: center;
line-height: 1.2;
}

&--icon-only .#{base.$prefix}-button__text {
@include m.visually-hidden;
}

&--icon-only .#{base.$prefix}-badge__wrapper .#{base.$prefix}-badge--top-right,
&--icon-and-text .#{base.$prefix}-badge__wrapper .#{base.$prefix}-badge--top-right {
top: 0;
right: auto;
left: calc(100% - 6px);

@include m.rtl {
right: calc(100% - 6px);
left: auto;
}
}

&--text-only .#{base.$prefix}-badge__wrapper .#{base.$prefix}-badge--top-right {
top: 50%;
right: auto;
left: calc(100% + 4px);
transform: translateY(-50%);

@include m.rtl {
right: calc(100% + 4px);
left: auto;
}
}

.#{$container}--primary & {
&--active {
color: t.color("primary");
border-radius: 0;
.#{base.$prefix}-button__icon {
color: t.color("primary");
}

&:hover {
background-color: t.alpha("primary", v.state("hover-state-layer-opacity"));
}

&:focus-visible {
background-color: t.alpha("primary", v.state("focus-state-layer-opacity"));
}

&:active:not(:has(> .#{base.$prefix}-ripple)) {
background-color: t.alpha("primary", v.state("pressed-state-layer-opacity"));
}
}

&:not(.#{$component}--active):not(.#{$component}--disabled):not(
:disabled
) {
color: t.color("on-surface-variant");

&:hover {
color: t.color("on-surface");
background-color: t.alpha("on-surface", v.state("hover-state-layer-opacity"));
}

&:focus-visible {
color: t.color("on-surface");
background-color: t.alpha("on-surface", v.state("focus-state-layer-opacity"));
}

&:active {
color: t.color("on-surface");
}

&:active:not(:has(> .#{base.$prefix}-ripple)) {
background-color: t.alpha("primary", v.state("pressed-state-layer-opacity"));
}

.#{base.$prefix}-ripple-wave {
background-color: t.color("primary");
}
}
}

.#{$container}--secondary & {
&--active {
color: t.color("on-surface");
border-radius: 0;
.#{base.$prefix}-button__icon {
color: t.color("on-surface");
}

&:hover {
background-color: t.alpha("on-surface", v.state("hover-state-layer-opacity"));
}

&:focus-visible {
background-color: t.alpha("on-surface", v.state("focus-state-layer-opacity"));
}

&:active:not(:has(> .#{base.$prefix}-ripple)) {
background-color: t.alpha("on-surface", v.state("pressed-state-layer-opacity"));
}
}

&:not(.#{$component}--active):not(.#{$component}--disabled):not(
:disabled
) {
color: t.color("on-surface-variant");

&:hover {
color: t.color("on-surface");
background-color: t.alpha("on-surface", v.state("hover-state-layer-opacity"));
}

&:focus-visible {
color: t.color("on-surface");
background-color: t.alpha("on-surface", v.state("focus-state-layer-opacity"));
}

&:active {
color: t.color("on-surface");
}

&:active:not(:has(> .#{base.$prefix}-ripple)) {
background-color: t.alpha("on-surface", v.state("pressed-state-layer-opacity"));
}
}
}

}

.#{base.$prefix}-tab-panel {
padding: 16px;

&[hidden] {
display: none;
}
}

.#{$container}--responsive-small {
.#{$component} {
padding: 0 12px;
min-width: 72px;
}
}
