/*!
 * Copyright 2024 Adobe. All rights reserved.
 *
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at <http://www.apache.org/licenses/LICENSE-2.0>
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

@import "@spectrum-css/commons/basebutton.css";
@import "./themes/spectrum-two.css";

@media (forced-colors: active) {
	.spectrum-ActionButton {
		/* force a more visible focus indicator color */
		--highcontrast-actionbutton-focus-indicator-color: ButtonText;
		--highcontrast-actionbutton-content-color: ButtonText;

		&::after {
			/* make sure focus indicator renders */
			forced-color-adjust: none;
		}

		&.is-selected {
			--highcontrast-actionbutton-background-color: Highlight;
			--highcontrast-actionbutton-border-color: HighlightText;
			--highcontrast-actionbutton-content-color: HighlightText;

			.spectrum-ActionButton-icon,
			.spectrum-ActionButton-hold,
			.spectrum-ActionButton-label {
				/* ensure custom text colors from above get applied */
				/* it seems like this shouldn't have to be done, but colors are wrong without it */
				forced-color-adjust: none;
			}
		}

		&:disabled,
		&.is-disabled {
			--highcontrast-actionbutton-content-color: GrayText;
			--highcontrast-actionbutton-border-color: GrayText;
			--highcontrast-actionbutton-background-color: ButtonFace;
		}
	}
}

.spectrum-ActionButton {
	--spectrum-actionbutton-background-color: var(--highcontrast-actionbutton-background-color, var(--mod-actionbutton-background-color-default, var(--spectrum-actionbutton-background-color-default)));
	--spectrum-actionbutton-border-color: var(--highcontrast-actionbutton-border-color, var(--mod-actionbutton-border-color-default, var(--spectrum-actionbutton-border-color-default)));
	--spectrum-actionbutton-content-color: var(--highcontrast-actionbutton-content-color, var(--mod-actionbutton-content-color-default, var(--spectrum-neutral-content-color-default)));

	--spectrum-actionbutton-border-radius: var(--mod-actionbutton-border-radius, var(--spectrum-actionbutton-border-radius-default));
	--spectrum-actionbutton-border-width: var(--mod-actionbutton-border-width, var(--spectrum-border-width-100));

	--spectrum-actionbutton-focus-indicator-gap: var(--mod-actionbutton-focus-indicator-gap, var(--spectrum-focus-indicator-gap));
	--spectrum-actionbutton-focus-indicator-thickness: var(--mod-actionbutton-focus-indicator-thickness, var(--spectrum-focus-indicator-thickness));
	--spectrum-actionbutton-focus-indicator-color: var(--highcontrast-actionbutton-focus-indicator-color, var(--mod-actionbutton-focus-indicator-color, var(--spectrum-focus-indicator-color)));

	&:dir(rtl) {
		--spectrum-logical-rotation: matrix(-1, 0, 0, 1, 0, 0);
	}

	&.spectrum-ActionButton--quiet {
		--spectrum-actionbutton-border-color: transparent;
	}

	/* Emphasized styles are only applied to the "selected" state of the button and are not applied in static color modes */
	&.spectrum-ActionButton--emphasized:not(.spectrum-ActionButton--staticBlack, .spectrum-ActionButton--staticWhite) {
		--mod-actionbutton-background-color-default-selected: var(--mod-actionbutton-background-color-default-selected-emphasized, var(--spectrum-accent-background-color-default));
		--mod-actionbutton-background-color-hover-selected: var(--mod-actionbutton-background-color-hover-selected-emphasized, var(--spectrum-accent-background-color-hover));
		--mod-actionbutton-background-color-down-selected: var(--mod-actionbutton-background-color-down-selected-emphasized, var(--spectrum-accent-background-color-down));
		--mod-actionbutton-background-color-focus-selected: var(--mod-actionbutton-background-color-focus-selected-emphasized, var(--spectrum-accent-background-color-key-focus));

		--mod-actionbutton-content-color-default-selected: var(--mod-actionbutton-content-color-default-selected-emphasized, var(--spectrum-white));
		--mod-actionbutton-content-color-hover-selected: var(--mod-actionbutton-content-color-hover-selected-emphasized, var(--spectrum-white));
		--mod-actionbutton-content-color-down-selected: var(--mod-actionbutton-content-color-down-selected-emphasized, var(--spectrum-white));
		--mod-actionbutton-content-color-focus-selected: var(--mod-actionbutton-content-color-focus-selected-emphasized, var(--spectrum-white));
	}

	&.spectrum-ActionButton--staticBlack {
		--mod-actionbutton-background-color-default-selected: var(--spectrum-transparent-black-800);
		--mod-actionbutton-background-color-hover-selected: var(--spectrum-transparent-black-900);
		--mod-actionbutton-background-color-down-selected: var(--spectrum-transparent-black-900);
		--mod-actionbutton-background-color-focus-selected: var(--spectrum-transparent-black-900);

		--mod-actionbutton-content-color-default: var(--spectrum-black);
		--mod-actionbutton-content-color-hover: var(--spectrum-black);
		--mod-actionbutton-content-color-down: var(--spectrum-black);
		--mod-actionbutton-content-color-focus: var(--spectrum-black);
		--mod-actionbutton-content-color-disabled: var(--spectrum-disabled-static-black-content-color);

		--mod-actionbutton-content-color-default-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-white));
		--mod-actionbutton-content-color-hover-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-white));
		--mod-actionbutton-content-color-down-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-white));
		--mod-actionbutton-content-color-focus-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-white));

		--mod-actionbutton-focus-indicator-color: var(--spectrum-static-black-focus-indicator-color);
	}

	&.spectrum-ActionButton--staticWhite {
		/* background color _not_ selected is coming from the system theme layer */
		--mod-actionbutton-background-color-default-selected: var(--spectrum-transparent-white-800);
		--mod-actionbutton-background-color-hover-selected: var(--spectrum-transparent-white-900);
		--mod-actionbutton-background-color-down-selected: var(--spectrum-transparent-white-900);
		--mod-actionbutton-background-color-focus-selected: var(--spectrum-transparent-white-900);

		--mod-actionbutton-content-color-default: var(--spectrum-white);
		--mod-actionbutton-content-color-hover: var(--spectrum-white);
		--mod-actionbutton-content-color-down: var(--spectrum-white);
		--mod-actionbutton-content-color-focus: var(--spectrum-white);
		--mod-actionbutton-content-color-disabled: var(--spectrum-disabled-static-white-content-color);

		--mod-actionbutton-content-color-default-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-black));
		--mod-actionbutton-content-color-hover-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-black));
		--mod-actionbutton-content-color-down-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-black));
		--mod-actionbutton-content-color-focus-selected: var(--mod-actionbutton-static-content-color, var(--spectrum-black));

		--mod-actionbutton-focus-indicator-color: var(--spectrum-static-white-focus-indicator-color);
	}

	&.is-selected {
		--mod-actionbutton-background-color-default: var(--mod-actionbutton-background-color-default-selected, var(--spectrum-actionbutton-background-color-selected));
		--mod-actionbutton-background-color-hover: var(--mod-actionbutton-background-color-hover-selected, var(--spectrum-actionbutton-background-color-selected-hover));
		--mod-actionbutton-background-color-down: var(--mod-actionbutton-background-color-down-selected, var(--spectrum-actionbutton-background-color-selected-down));
		--mod-actionbutton-background-color-focus: var(--mod-actionbutton-background-color-focus-selected, var(--spectrum-actionbutton-background-color-selected-focus));
		--mod-actionbutton-background-color-disabled: var(--spectrum-actionbutton-background-color-selected-disabled);

		--mod-actionbutton-border-color-default: transparent;
		--mod-actionbutton-border-color-hover: transparent;
		--mod-actionbutton-border-color-down: transparent;
		--mod-actionbutton-border-color-focus: transparent;
		--mod-actionbutton-border-color-disabled: transparent;

		--mod-actionbutton-content-color-default: var(--mod-actionbutton-content-color-default-selected, var(--spectrum-actionbutton-content-color-selected));
		--mod-actionbutton-content-color-hover: var(--mod-actionbutton-content-color-hover-selected, var(--spectrum-actionbutton-content-color-selected));
		--mod-actionbutton-content-color-down: var(--mod-actionbutton-content-color-down-selected, var(--spectrum-actionbutton-content-color-selected));
		--mod-actionbutton-content-color-focus: var(--mod-actionbutton-content-color-focus-selected, var(--spectrum-actionbutton-content-color-selected));
	}

	&:hover {
		--mod-actionbutton-background-color-default: var(--mod-actionbutton-background-color-hover, var(--spectrum-actionbutton-background-color-hover));
		--mod-actionbutton-border-color-default: var(--mod-actionbutton-border-color-hover, var(--spectrum-actionbutton-border-color-hover));
		--mod-actionbutton-content-color-default: var(--mod-actionbutton-content-color-hover, var(--spectrum-neutral-content-color-hover));
	}

	&:focus-visible {
		--mod-actionbutton-background-color-default: var(--mod-actionbutton-background-color-focus, var(--spectrum-actionbutton-background-color-focus));
		--mod-actionbutton-border-color-default: var(--mod-actionbutton-border-color-focus, var(--spectrum-actionbutton-border-color-focus));
		--mod-actionbutton-content-color-default: var(--mod-actionbutton-content-color-focus, var(--spectrum-neutral-content-color-key-focus));
	}

	&:active {
		--mod-actionbutton-background-color-default: var(--mod-actionbutton-background-color-down, var(--spectrum-actionbutton-background-color-down));
		--mod-actionbutton-border-color-default: var(--mod-actionbutton-border-color-down, var(--spectrum-actionbutton-border-color-down));
		--mod-actionbutton-content-color-default: var(--mod-actionbutton-content-color-down, var(--spectrum-neutral-content-color-down));
	}

	&:disabled,
	&.is-disabled {
		--mod-actionbutton-background-color-default: var(--mod-actionbutton-background-color-disabled, var(--spectrum-actionbutton-background-color-disabled));
		--mod-actionbutton-border-color-default: var(--mod-actionbutton-border-color-disabled, var(--spectrum-actionbutton-border-color-disabled));
		--mod-actionbutton-content-color-default: var(--mod-actionbutton-content-color-disabled, var(--spectrum-disabled-content-color));
	}
}

.spectrum-ActionButton,
.spectrum-ActionButton--sizeM {
	--spectrum-actionbutton-sized-height: var(--spectrum-component-height-100);
	--spectrum-actionbutton-sized-icon-size: var(--spectrum-workflow-icon-size-100);
	--spectrum-actionbutton-sized-font-size: var(--spectrum-font-size-100);
	--spectrum-actionbutton-sized-text-to-visual: var(--spectrum-text-to-visual-100);
	--spectrum-actionbutton-sized-edge-to-hold-icon: var(--spectrum-action-button-edge-to-hold-icon-medium);
	--spectrum-actionbutton-sized-edge-to-visual: var(--spectrum-component-edge-to-visual-100);
	--spectrum-actionbutton-sized-edge-to-text: var(--spectrum-component-edge-to-text-100);
	--spectrum-actionbutton-sized-edge-to-visual-only: var(--spectrum-component-edge-to-visual-only-100);
}

.spectrum-ActionButton--sizeXS {
	--spectrum-actionbutton-sized-height: var(--spectrum-component-height-50);
	--spectrum-actionbutton-sized-icon-size: var(--spectrum-workflow-icon-size-50);
	--spectrum-actionbutton-sized-font-size: var(--spectrum-font-size-50);
	--spectrum-actionbutton-sized-text-to-visual: var(--spectrum-text-to-visual-50);
	--spectrum-actionbutton-sized-edge-to-hold-icon: var(--spectrum-action-button-edge-to-hold-icon-extra-small);
	--spectrum-actionbutton-sized-edge-to-visual: var(--spectrum-component-edge-to-visual-50);
	--spectrum-actionbutton-sized-edge-to-text: var(--spectrum-component-edge-to-text-50);
	--spectrum-actionbutton-sized-edge-to-visual-only: var(--spectrum-component-edge-to-visual-only-50);
}

.spectrum-ActionButton--sizeS {
	--spectrum-actionbutton-sized-height: var(--spectrum-component-height-75);
	--spectrum-actionbutton-sized-icon-size: var(--spectrum-workflow-icon-size-75);
	--spectrum-actionbutton-sized-font-size: var(--spectrum-font-size-75);
	--spectrum-actionbutton-sized-text-to-visual: var(--spectrum-text-to-visual-75);
	--spectrum-actionbutton-sized-edge-to-hold-icon: var(--spectrum-action-button-edge-to-hold-icon-small);
	--spectrum-actionbutton-sized-edge-to-visual: var(--spectrum-component-edge-to-visual-75);
	--spectrum-actionbutton-sized-edge-to-text: var(--spectrum-component-edge-to-text-75);
	--spectrum-actionbutton-sized-edge-to-visual-only: var(--spectrum-component-edge-to-visual-only-75);
}

.spectrum-ActionButton--sizeL {
	--spectrum-actionbutton-sized-height: var(--spectrum-component-height-200);
	--spectrum-actionbutton-sized-icon-size: var(--spectrum-workflow-icon-size-200);
	--spectrum-actionbutton-sized-font-size: var(--spectrum-font-size-200);
	--spectrum-actionbutton-sized-text-to-visual: var(--spectrum-text-to-visual-200);
	--spectrum-actionbutton-sized-edge-to-hold-icon: var(--spectrum-action-button-edge-to-hold-icon-large);
	--spectrum-actionbutton-sized-edge-to-visual: var(--spectrum-component-edge-to-visual-200);
	--spectrum-actionbutton-sized-edge-to-text: var(--spectrum-component-edge-to-text-200);
	--spectrum-actionbutton-sized-edge-to-visual-only: var(--spectrum-component-edge-to-visual-only-200);
}

.spectrum-ActionButton--sizeXL {
	--spectrum-actionbutton-sized-height: var(--spectrum-component-height-300);
	--spectrum-actionbutton-sized-icon-size: var(--spectrum-workflow-icon-size-300);
	--spectrum-actionbutton-sized-font-size: var(--spectrum-font-size-300);
	--spectrum-actionbutton-sized-text-to-visual: var(--spectrum-text-to-visual-300);
	--spectrum-actionbutton-sized-edge-to-hold-icon: var(--spectrum-action-button-edge-to-hold-icon-extra-large);
	--spectrum-actionbutton-sized-edge-to-visual: var(--spectrum-component-edge-to-visual-300);
	--spectrum-actionbutton-sized-edge-to-text: var(--spectrum-component-edge-to-text-300);
	--spectrum-actionbutton-sized-edge-to-visual-only: var(--spectrum-component-edge-to-visual-only-300);
}

.spectrum-ActionButton {
	/* Variables leveraging t-shirt sizing */
	--spectrum-actionbutton-height: var(--mod-actionbutton-height, var(--spectrum-actionbutton-sized-height));
	--spectrum-actionbutton-icon-size: var(--mod-actionbutton-icon-size, var(--spectrum-actionbutton-sized-icon-size));
	--spectrum-actionbutton-font-size: var(--mod-actionbutton-font-size, var(--spectrum-actionbutton-sized-font-size));
	--spectrum-actionbutton-text-to-visual: var(--mod-actionbutton-text-to-visual, var(--spectrum-actionbutton-sized-text-to-visual));
	--spectrum-actionbutton-edge-to-hold-icon: var(--mod-actionbutton-edge-to-hold-icon, var(--spectrum-actionbutton-sized-edge-to-hold-icon));

	--spectrum-actionbutton-edge-to-visual: var(--mod-actionbutton-edge-to-visual, calc(var(--spectrum-actionbutton-sized-edge-to-visual) - var(--spectrum-actionbutton-border-width)));
	--spectrum-actionbutton-edge-to-text: var(--mod-actionbutton-edge-to-text, calc(var(--spectrum-actionbutton-sized-edge-to-text) - var(--spectrum-actionbutton-border-width)));
	--spectrum-actionbutton-edge-to-visual-only: var(--mod-actionbutton-edge-to-visual-only, calc(var(--spectrum-actionbutton-sized-edge-to-visual-only) - var(--spectrum-actionbutton-border-width)));

	@extend %spectrum-BaseButton;

	position: relative;

	min-inline-size: var(--mod-actionbutton-min-width, calc(var(--mod-actionbutton-edge-to-visual-only, var(--spectrum-actionbutton-sized-edge-to-visual-only)) * 2 + var(--spectrum-actionbutton-icon-size)));
	block-size: var(--spectrum-actionbutton-height);

	border-radius: var(--spectrum-actionbutton-border-radius);
	border-width: var(--spectrum-actionbutton-border-width);

	gap: calc(var(--spectrum-actionbutton-text-to-visual) + var(--spectrum-actionbutton-edge-to-text) - var(--spectrum-actionbutton-edge-to-visual-only));

	/* Start with text-only padding */
	padding-inline: var(--spectrum-actionbutton-edge-to-text);

	background-color: var(--spectrum-actionbutton-background-color);
	border-color: var(--spectrum-actionbutton-border-color);
	color: var(--spectrum-actionbutton-content-color);

	transition: border-color var(--mod-actionbutton-animation-duration, var(--spectrum-animation-duration-100)) ease-in-out;

	&::after {
		position: absolute;
		inset: 0;

		margin: calc((var(--spectrum-actionbutton-focus-indicator-gap) + var(--spectrum-actionbutton-border-width)) * -1);

		border-radius: var(--mod-actionbutton-focus-indicator-border-radius, calc(var(--spectrum-actionbutton-border-radius) + var(--spectrum-actionbutton-focus-indicator-gap)));

		transition: box-shadow var(--mod-actionbutton-animation-duration, var(--spectrum-animation-duration-100)) ease-in-out;

		pointer-events: none;
		content: "";
	}

	&:focus-visible {
		/* kill the default ring */
		box-shadow: none;
		outline: none;

		&::after {
			box-shadow: 0 0 0 var(--spectrum-actionbutton-focus-indicator-thickness) var(--spectrum-actionbutton-focus-indicator-color);
		}
	}
}

a.spectrum-ActionButton {
	@extend %spectrum-AnchorButton;
}

/* Nested sub-component: Icon */
.spectrum-ActionButton-icon {
	@extend %spectrum-ButtonIcon;

	inline-size: var(--spectrum-actionbutton-icon-size);
	block-size: var(--spectrum-actionbutton-icon-size);

	/* adjust icon positioning to match UI kit */
	margin-inline-start: calc(var(--spectrum-actionbutton-edge-to-visual) - var(--spectrum-actionbutton-edge-to-text));
	margin-inline-end: calc(var(--spectrum-actionbutton-edge-to-visual-only) - var(--spectrum-actionbutton-edge-to-text));

	color: inherit;

	/* Augment the margin correction for the icon only scenario */
	.spectrum-ActionButton-hold + &,
	&:only-child {
		margin-inline-start: calc(var(--spectrum-actionbutton-edge-to-visual-only) - var(--spectrum-actionbutton-edge-to-text));
	}
}

.spectrum-ActionButton-label {
	@extend %spectrum-ButtonLabel;
	pointer-events: none;
	line-height: var(--spectrum-actionbutton-height);

	font-size: var(--spectrum-actionbutton-font-size);
	white-space: nowrap;
	color: var(--mod-actionbutton-label-color, inherit);

	text-overflow: ellipsis;
	overflow: hidden;
}

.spectrum-ActionButton-hold {
	position: absolute;
	inset-inline-end: calc(var(--spectrum-actionbutton-edge-to-hold-icon) - var(--spectrum-actionbutton-border-width));
	inset-block-end: calc(var(--spectrum-actionbutton-edge-to-hold-icon) - var(--spectrum-actionbutton-border-width));

	color: inherit;

	transform: var(--spectrum-logical-rotation,);
}
