import { TableColumnsType } from "antd";
import { ModalEnum } from "../../enums/Model";
import { useObjectToEdit } from "../../zustand/ObjectToEditState";
import { useModalState } from "../../zustand/Modal";
import { useTranslation } from "react-i18next";
import { canDeletePackage,canEditPackage } from "../../utils/hasAbilityFn";
import ActionButtons from "../../Components/Table/ActionButtons";
import { useNavigate } from "react-router-dom";

export const useColumns = () => {
  const [t] = useTranslation();

  const { setIsOpen } = useModalState((state) => state);
  const navigate = useNavigate()
  const { setObjectToEdit } = useObjectToEdit((state) => state);
  const handelDelete = (record: any) => {
    setObjectToEdit(record);
    setIsOpen(ModalEnum?.Package_DELETE);
  };
  const handleEdit = (record: any) => {
    setObjectToEdit(record);
    setIsOpen(ModalEnum?.Package_EDIT);
    navigate(`${record?.id}`)

  };

  const columns: TableColumnsType<any> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
    },
    {
      title: t("columns.name"),
      dataIndex: "name",
      key: "name",
      align: "center",
    },
    {
      title: t("columns.price"),
      dataIndex: "price",
      key: "price",
      align: "center",
    },
    {
      title: "",
      key: "actions",
      align: "end",
      width: "25vw",
      render: (_text, record, index) => {
        return (
          <ActionButtons
            canDelete={canEditPackage}
            canEdit={canDeletePackage}
            index={index}
            onDelete={() => handelDelete(record)}
            onEdit={() => handleEdit(record)}
          />
        );
      },
    },
  ];

  return columns;
};
