import { useMode } from "../../../stores/useMode.js";
import React, { useMemo, useState } from "react";
import { Button } from "antd";
import { BiTrash } from "react-icons/all.js";
import { deletePoint } from "../../../api.js";
import { useUpdateLayerCounter } from "../../../stores/useUpdateLayerCounter.js";

const MATCHING_STATUS = {
  New: {
    name: 'Новая',
    color: 'import_status_new'
  },
  Error: {
    name: 'Ошибка геокодирования',
    color: 'import_status_error'
  },
  Matched: {
    name: 'Совпадение',
    color: 'import_status_matched'
  },

}

export const usePendingTableFields = () => {
  const { isImportMode } = useMode();
  const { toggleUpdateCounter } = useUpdateLayerCounter();
  const [isDeleting, setIsDeleting] = useState(false);
  const deleteRow = async (e, id) => {
    e.stopPropagation();
    setIsDeleting(true);
    try {
      await deletePoint(id);
      toggleUpdateCounter();
    } catch (e) {
      //
    } finally {
      setIsDeleting(false);
    }
  };

  const fields = useMemo(() => {
    return isImportMode ? [
      {
        title: "Статус импорта",
        dataIndex: "matching_status",
        key: "matching_status",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
        render: (_, record) => {
          if (!record.matching_status) return;
          const name = MATCHING_STATUS[record.matching_status].name;
          const color = MATCHING_STATUS[record.matching_status].color;
          return (
            <div className={`bg-opacity-25 rounded-md px-2 py-1 text-center border-solid border-[2px] ${color}`}>
              {name}
            </div>
          );
        },
      },
      {
        title: "Удалить",
        key: "del",
        width: "60px",
        ellipsis: true,
        render: (_, record) => {
          if (!record.id) return;
          return (
            <Button type="text" onClick={(event) => deleteRow(event, record.id)}>
              <BiTrash />
            </Button>
          );
        },
      }
    ] : [];
  }, [isImportMode]);
  return { fields, isDeleting };
}