import React, { useMemo } from "react";
import { MRT_ColumnDef } from "mantine-react-table";
import { BaseProjectSchema, ProjectSchema } from "../../../../../../../client";
import { Radio, TextInput } from "@mantine/core";


type Props = {
    editingProjects: Map<number, ProjectSchema | BaseProjectSchema>;
    selectedProject: ProjectSchema | null;
    setSelectedProject: React.Dispatch<React.SetStateAction<ProjectSchema | null>>;
}

const useProjectsTableColumns = ({ editingProjects, selectedProject, setSelectedProject }: Props) => {
    return useMemo<MRT_ColumnDef<ProjectSchema>[]>(
        () => [
            {
                header: " ",
                Cell: ({ row }) => (
                    <Radio
                        checked={row.original.id === selectedProject?.id}
                        onChange={() =>  setSelectedProject(row.original)}
                    />
                ),
                size: 10,
            },
            {
                header: "Название",
                accessorKey: "name",
                Cell: ({ row }) => {
                    if (editingProjects.has(row.original.id)) {
                        return (
                            <TextInput
                                variant={"default"}
                                value={editingProjects.get(row.original.id)?.name}
                                onChange={e => {
                                    const project = editingProjects.get(row.original.id);
                                    if (!project) return;
                                    project.name = e.target.value;
                                    editingProjects.set(row.original.id, project);
                                }}
                            />
                        );
                    }
                    return row.original.name;
                },
                size: 25,
            },
            {
                header: "Кол-во досок",
                accessorKey: "boardsCount",
                size: 10,
            },
        ],
        [selectedProject],
    );
};

export default useProjectsTableColumns;
