@use "@wordpress/base-styles/colors" as *;
@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/variables" as *;
@use "@wordpress/base-styles/z-index" as *;

$arrow-triangle-base-size: 14px;

$shadow-popover-border-default: 0 0 0 var(--wpds-border-width-xs) var(--wpds-color-stroke-surface-neutral);
$shadow-popover-border-default-alternate: 0 0 0 $border-width $gray-900;
$shadow-popover-border-top-only: 0 calc(var(--wpds-border-width-xs) * -1) 0 0 var(--wpds-color-stroke-surface-neutral);
$shadow-popover-border-top-only-alternate: 0 #{-$border-width} 0 $gray-900;

.components-popover {
	@include reset;
	z-index: z-index(".components-popover");

	&.is-expanded {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: z-index(".components-popover") !important;
	}
}

.components-popover__content {
	background: var(--wpds-color-background-surface-neutral-strong);
	// The stroke stays on the box-shadow, since consumers override it there.
	box-shadow: $shadow-popover-border-default, $elevation-medium;
	border-radius: var(--wpds-border-radius-md);
	box-sizing: border-box;
	width: min-content;

	// Alternate treatment for popovers that put them at elevation zero with high contrast and smaller radius.
	.is-alternate & {
		box-shadow: $shadow-popover-border-default-alternate;
		border-radius: $radius-small;
	}

	// A style that gives the popover no visible ui.
	.is-unstyled & {
		background: none;
		border-radius: 0;
		box-shadow: none;
	}

	.components-popover.is-expanded & {
		position: static;
		height: calc(100% - #{ $panel-header-height });
		overflow-y: visible;
		width: auto;
		box-shadow: $shadow-popover-border-top-only;
	}
	.components-popover.is-expanded.is-alternate & {
		box-shadow: $shadow-popover-border-top-only-alternate;
	}

	// Forced colors removes box-shadow. The transparent outline is drawn as the system text color instead.
	// Exclusions stay in :where() so a consumer outline, such as the caption toolbar's `outline: none`, still wins.
	@media (forced-colors: active) {
		.components-popover:where(:not(.is-unstyled):not(.is-expanded)) & {
			outline: var(--wpds-border-width-xs) solid transparent;
		}
	}
}

.components-popover__header {
	align-items: center;
	background: var(--wpds-color-background-surface-neutral-strong);
	display: flex;
	height: $panel-header-height;
	justify-content: space-between;
	padding: 0 8px 0 $grid-unit-20;
}

.components-popover__header-title {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	width: 100%;
}

.components-popover__arrow {
	position: absolute;
	width: $arrow-triangle-base-size;
	height: $arrow-triangle-base-size;
	pointer-events: none;
	display: flex;

	// Thin line that helps to make sure that the underlying
	// popover__content's outline is fully overlapped by the
	// arrow
	&::before {
		content: "";
		position: absolute;
		top: calc(var(--wpds-border-width-xs) * -1);
		left: 1px;
		height: calc(var(--wpds-border-width-xs) * 2);
		right: 1px;
		background-color: var(--wpds-color-background-surface-neutral-strong);

		@media (forced-colors: active) {
			background-color: Canvas;
		}
	}

	// Position and rotate the arrow depending on the popover's placement.
	// The `!important' is necessary to override the inline styles.
	&.is-top {
		bottom: -1 * $arrow-triangle-base-size !important;
		transform: rotate(0);
	}
	&.is-right {
		/*rtl:begin:ignore*/
		left: -1 * $arrow-triangle-base-size !important;
		transform: rotate(90deg);
	}
	&.is-bottom {
		top: -1 * $arrow-triangle-base-size !important;
		transform: rotate(180deg);
	}
	&.is-left {
		/*rtl:begin:ignore*/
		right: -1 * $arrow-triangle-base-size !important;
		transform: rotate(-90deg);
		/*rtl:end:ignore*/
	}
}

.components-popover__triangle {
	display: block;
	flex: 1;
}

.components-popover__triangle-bg {
	// Fill color is the same as the .components-popover__content's background
	fill: var(--wpds-color-background-surface-neutral-strong);

	@media (forced-colors: active) {
		fill: Canvas;
	}
}

.components-popover__triangle-border {
	// Stroke colors are the same as the .components-popover__content's outline
	fill: transparent;
	stroke-width: var(--wpds-border-width-xs);
	stroke: var(--wpds-color-stroke-surface-neutral);

	.is-alternate & {
		stroke: $gray-900;
	}

	@media (forced-colors: active) {
		.components-popover:not(.is-unstyled) & {
			stroke: CanvasText;
		}
	}
}
