import ObjectSelect, { ObjectSelectProps } from "../ObjectSelect/ObjectSelect.tsx";
import { FC, useEffect, useState } from "react";
import { BoardSchema, StatusSchema } from "../../client";

type OtherProps = {
    board: BoardSchema | null;
}

type SelectProps = Omit<ObjectSelectProps<StatusSchema | null>, "data" | "getLabelFn" | "getValueFn">;

type Props = OtherProps & SelectProps;

const CardStatusSelect: FC<Props> = ({ board, ...props}) => {
    const [isInitial, setIsInitial] = useState<boolean>(true);

    const filteredData = board?.statuses.filter(
        status => !status.isDeleted,
    ) ?? [];

    const onClear = () => props.onChange(null);

    useEffect(() => {
        if (isInitial) {
            setIsInitial(false);
        } else {
            onClear();
        }
    }, [board?.id]);

    return (
        <ObjectSelect
            data={filteredData}
            searchable
            placeholder={"Выберите статус"}
            onClear={onClear}
            {...props}
        />
    );
};
export default CardStatusSelect;
