'use client';
import React, {useState} from 'react';
import {WorldProps} from '@/lib/types/world';
import InputField from '@/components/form/InputField';
import TextInput from '@/components/form/TextInput';
import {Globe, Plus} from 'lucide-react';
import EmptyState from '@/components/ui/EmptyState';
import EntityListItem from '@/components/ui/EntityListItem';
import AvatarIcon from '@/components/ui/AvatarIcon';
import {useTranslations} from '@/lib/i18n';

interface WorldEditorListProps {
    worlds: WorldProps[];
    onWorldClick: (world: WorldProps) => void;
    onAddWorld: () => void;
}

/**
 * WorldEditorList - Liste des mondes pour ComposerRightBar
 * Version compacte avec liste cliquable (même pattern que CharacterEditorList)
 * PAS de scroll interne (géré par parent ComposerRightBar)
 */
export default function WorldEditorList({
                                            worlds,
                                            onWorldClick,
                                            onAddWorld,
                                        }: WorldEditorListProps): React.JSX.Element {
    const t = useTranslations();
    const [searchQuery, setSearchQuery] = useState<string>('');
    
    function getFilteredWorlds(): WorldProps[] {
        return worlds.filter(function (world: WorldProps): boolean {
            return world.name.toLowerCase().includes(searchQuery.toLowerCase());
        });
    }
    
    const filteredWorlds: WorldProps[] = getFilteredWorlds();
    
    return (
        <div className="space-y-3">
            <div className="px-2">
                <InputField
                    input={
                        <TextInput
                            value={searchQuery}
                            setValue={function (e: React.ChangeEvent<HTMLInputElement>): void {
                                setSearchQuery(e.target.value);
                            }}
                            placeholder={t('worldSetting.search')}
                        />
                    }
                    actionIcon={Plus}
                    actionLabel={t('worldSetting.addWorldLabel')}
                    addButtonCallBack={async function (): Promise<void> {
                        onAddWorld();
                    }}
                />
            </div>
            
            <div className="px-2 space-y-2">
                {filteredWorlds.length === 0 ? (
                    <EmptyState icon={Globe} title={t('worldSetting.noWorldAvailable')}
                                description={t('worldSetting.noWorldDescription')}/>
                ) : (
                    filteredWorlds.map(function (world: WorldProps): React.JSX.Element {
                        return (
                            <EntityListItem
                                key={world.id}
                                size="sm"
                                onClick={function (): void {
                                    onWorldClick(world);
                                }}
                                avatar={<AvatarIcon size="sm" icon={Globe}/>}
                                title={world.name}
                                subtitle={world.history ? world.history.substring(0, 50) + (world.history.length > 50 ? '...' : '') : null}
                            />
                        );
                    })
                )}
            </div>
        </div>
    );
}
