'use client';

import React from 'react';
import { Form, Typography } from 'antd';
import type { FormItemProps } from 'antd/es/form';
import type { SelectProps } from 'antd/es/select';
import { SelectComParametros } from './SelectComParametros';

interface SelectOption {
    label: string | React.ReactNode;
    value: string | number;
    disabled?: boolean;
}

interface FormSelectComParametrosProps extends Omit<SelectProps, 'name' | 'dropdownRender'> {
    name: string | (string | number)[];
    label?: string;
    required?: boolean;
    help?: React.ReactNode;
    options?: SelectOption[];
    placeholder?: string;
    allowClear?: boolean;
    showSearch?: boolean;
    mode?: 'multiple' | 'tags';
    rules?: FormItemProps['rules'];
    criarLabel: string;
    onCriarClick: () => void;
    dropdownRender?: (menu: React.ReactNode) => React.ReactNode;
    loading?: boolean;
    fetchError?: boolean;
    fetchForbidden?: boolean;
    onRetryFetch?: () => void;
    notFoundContent?: React.ReactNode;
}

export default function FormSelectComParametros({
    name,
    label,
    required = false,
    help,
    options = [],
    placeholder = 'Selecione uma opção',
    allowClear = true,
    showSearch = false,
    mode,
    rules,
    criarLabel,
    onCriarClick,
    dropdownRender,
    loading,
    fetchError,
    fetchForbidden = false,
    onRetryFetch,
    notFoundContent,
    ...selectProps
}: FormSelectComParametrosProps) {
    const formRules = required
        ? [{ required: true, message: `${label || 'Campo'} é obrigatório` }, ...(rules || [])]
        : rules;

    const mergedHelp =
        fetchError ? (
            fetchForbidden ? (
                <span>Sem permissão para consultar este catálogo.</span>
            ) : (
                <span>
                    Não foi possível carregar as opções.
                    {onRetryFetch ? (
                        <>
                            {' '}
                            <Typography.Link onClick={() => onRetryFetch()}>Tentar novamente</Typography.Link>
                        </>
                    ) : null}
                </span>
            )
        ) : (
            help
        );

    return (
        <Form.Item
            name={name}
            label={label}
            rules={formRules}
            help={mergedHelp}
            validateStatus={fetchError ? 'error' : undefined}
        >
            <SelectComParametros
                placeholder={placeholder}
                allowClear={allowClear}
                showSearch={showSearch}
                mode={mode}
                options={options}
                criarLabel={criarLabel}
                onCriarClick={onCriarClick}
                dropdownRender={dropdownRender}
                loading={loading}
                notFoundContent={notFoundContent}
                fetchError={fetchError}
                fetchForbidden={fetchForbidden}
                onRetryFetch={onRetryFetch}
                {...selectProps}
            />
        </Form.Item>
    );
}
