import { JSX } from 'preact/jsx-runtime'
import { Icons } from './icons.tsx'

interface IconButtonProps {
	active?: boolean
	text?: string
	icon?: keyof (typeof Icons)
}

export function IconButton(
	{ icon, children, active, ...props }:
		& IconButtonProps
		& JSX.HTMLAttributes<HTMLButtonElement>,
) {
	const Icon = typeof icon === 'string'
		? Icons[icon as keyof typeof Icons]
		: (() => <></>)
	return (
		<button
			class={'p-2 sm:px-4 flex flex-col sm:flex-row flex-1 sm:flex-none bg-mantle justify-center items-center sm:justify-start text-center sm:text-left rounded' +
				(active ? ' text-primary' : '')}
			{...props}
		>
			{icon !== undefined
				? (
					<div class='sm:mr-2'>
						<Icon />
					</div>
				)
				: ''}
			<div class=''>{children}</div>
		</button>
	)
}
