import { Space, TableColumnsType, Tooltip } from "antd";
import { tags } from "../../types/Item";

import { ModalEnum } from "../../enums/Model";
import { MdOutlineEdit } from "react-icons/md";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { useObjectToEdit } from "../../zustand/ObjectToEditState";
import { useModalState } from "../../zustand/Modal";
import { useTranslation } from "react-i18next";
import { canDeleteTags, canEditTags } from "../../utils/hasAbilityFn";

export const useColumns = () => {
  const { setIsOpen } = useModalState((state) => state);

  const { setObjectToEdit } = useObjectToEdit((state) => state);
  const handelDelete = (record: any) => {
    setObjectToEdit(record);
    setIsOpen(ModalEnum?.TAGS_DELETE);
  };
  const [t] = useTranslation();

  const columns: TableColumnsType<tags> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
    },
    {
      title: t("columns.name"),
      dataIndex: "name",
      key: "name",
      align: "center",
    },

    {
      title: "",
      key: "actions",
      align: "end",
      width: "25vw",
      render: (text, record, index) => {
        const handleEdit = (record: any) => {
          // console.log(record,"record");
          setObjectToEdit(record);
          setIsOpen(ModalEnum?.TAGS_EDIT);
        };

        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

        return (
          <Space size="middle" className={className}>
            {canEditTags && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                  <MdOutlineEdit size={22} style={{ color: "#A098AE" }} />
                </span>
              </Tooltip>
            )}

            {canDeleteTags && (
              <RiDeleteBin6Fill
                onClick={() => handelDelete(record)}
                size={22}
                style={{ color: "#C11313" }}
              />
            )}
          </Space>
        );
      },
    },
  ];

  return columns;
};
