"use client";

import { FC, useEffect, useState } from "react";
import ObjectSelect, {
    ObjectSelectProps,
} from "@/components/selects/ObjectSelect/ObjectSelect";
import useStatusesList from "@/hooks/lists/useStatusesList";
import { StatusSchema } from "@/lib/client";

type Props = Omit<
    ObjectSelectProps<StatusSchema | null>,
    "data" | "getLabelFn" | "getValueFn"
> & {
    boardId?: number;
    clearOnBoardChange?: boolean;
};

const StatusSelect: FC<Props> = ({ boardId, clearOnBoardChange, ...props }) => {
    const [isInitial, setIsInitial] = useState<boolean>(true);
    const onClear = () => props.onChange(null);

    const { statuses } = useStatusesList({ boardId });

    useEffect(() => {
        if (!clearOnBoardChange) return;
        if (isInitial) {
            setIsInitial(false);
        } else {
            onClear();
        }
    }, [boardId]);

    return (
        <ObjectSelect
            data={statuses}
            disabled={!boardId}
            searchable
            placeholder={"Выберите статус"}
            onClear={onClear}
            {...props}
        />
    );
};

export default StatusSelect;
