import type { ComponentProps } from 'react';

import { Spinner } from '@/components/ui/spinner';
import { cn } from '@/lib/utils';

type LoadingIndicatorProps = ComponentProps<'span'> & {
    label?: string;
    showSpinner?: boolean;
    spinnerClassName?: string;
};

function LoadingDots({ className }: { className?: string }) {
    return (
        <span aria-hidden="true" className={cn('app-loading-dots', className)}>
            <span className="app-loading-dot">.</span>
            <span className="app-loading-dot">.</span>
            <span className="app-loading-dot">.</span>
        </span>
    );
}

function LoadingIndicator({
    className,
    label = 'Memuat',
    showSpinner = true,
    spinnerClassName,
    ...props
}: LoadingIndicatorProps) {
    return (
        <span
            aria-live="polite"
            className={cn('app-loading-indicator', className)}
            role="status"
            {...props}
        >
            {showSpinner && (
                <Spinner
                    aria-hidden="true"
                    className={cn('app-loading-spinner', spinnerClassName)}
                    role="presentation"
                />
            )}
            {label && <span>{label}</span>}
            {!showSpinner && <LoadingDots />}
        </span>
    );
}

export { LoadingDots, LoadingIndicator };
