<script lang="ts">
	import { untrack } from 'svelte'
	import { twMerge } from 'tailwind-merge'
	import Popover from '$lib/components/Popover.svelte'
	import DropdownV2 from '$lib/components/DropdownV2.svelte'
	import ToggleButton from '$lib/components/common/toggleButton-v2/ToggleButton.svelte'
	import Tooltip from '$lib/components/meltComponents/Tooltip.svelte'

	type TogglableItem = {
		label: string
		value: string
		tooltip?: string
	}

	interface Props {
		disabled?: boolean
		small?: boolean
		id?: string | undefined
		item?: any | undefined
		selected?: string | undefined
		togglableItems: TogglableItem[]
		btnText?: string
		class?: string
		hideSelectedOption?: boolean
	}

	let {
		disabled = false,
		small = false,
		id = undefined,
		item = undefined,
		selected = $bindable(undefined),
		togglableItems,
		btnText,
		class: className = '',
		hideSelectedOption = false
	}: Props = $props()

	let items = untrack(() => togglableItems).map((i) => ({
		displayName: i.label,
		action: () => (selected = i.value),
		tooltip: i.tooltip
	}))

	function isAnOptionSelected(selected: string | undefined) {
		return togglableItems.some((i) => i.value === selected)
	}
</script>

<Popover
	disablePopup={true}
	notClickable
	class={twMerge('flex', disabled ? 'cursor-not-allowed' : 'cursor-pointer', className)}
	disappearTimeout={0}
>
	<div {id} class="flex">
		{#if isAnOptionSelected(selected) && !hideSelectedOption}
			{@const tooltip = togglableItems.find((i) => i.value === selected)?.tooltip}
			<ToggleButton
				{disabled}
				value={selected ?? ''}
				{item}
				{small}
				{id}
				label={togglableItems.find((i) => i.value === selected)?.label}
				{tooltip}
				showTooltipIcon={!!tooltip}
			/>
		{/if}
		<div class="flex items-center">
			<!-- The tooltip fixes a bug where the other tooltips won't disappear -->
			<Tooltip disablePopup>
				<DropdownV2 {btnText} enableFlyTransition {items} size={small ? 'sm' : 'md'} />
			</Tooltip>
		</div>
	</div>
</Popover>
