import React, { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { TypeDashboardPaginationState } from '../Dashboard/types';

export const usePathName = (): string => {
  const { pathname } = useLocation();
  const [path, setPath] = useState(pathname);
  useEffect(() => setPath(pathname), [pathname]);
  return path;
};

export const preparePathName = (
  pathName: string,
  id: number,
  paramsId?: string,
): string => {
  if (paramsId) return pathName.replace(paramsId, String(id));
  return `${pathName}/${id}`;
};

export const onSelectOnceRow = (
  selected: boolean,
  selectedRows: any,
  record: any,
  onSetSelectedRowItem: React.Dispatch<React.SetStateAction<number[]>>,
  getRowId: (row: any) => number = (row: any) => row.id,
): void => {
  if (selected) {
    onSetSelectedRowItem((prevState: number[]) => [
      ...prevState,
      getRowId(selectedRows[selectedRows.length - 1]),
    ]);
  } else {
    onSetSelectedRowItem((prevState: number[]) =>
      prevState.filter((item) => item !== getRowId(record)),
    );
  }
};

export const onSelectAllRow = (
  selected: boolean,
  selectedRows: any[],
  changeRows: any[],
  rowSelection: {
    selectedRowKeys: number[];
    onSetSelectedRowItem: React.Dispatch<React.SetStateAction<number[]>>;
  },
  getRowId: (row: any) => number = (row: any) => row.id,
): void => {
  if (selected) {
    const newArrayForSelectedRowKeys: number[] = [];
    for (let i = 0; i < selectedRows.length; i++) {
      if (selectedRows[i]) {
        newArrayForSelectedRowKeys.push(getRowId(selectedRows[i]));
      } else {
        selectedRows
          .filter((item) => item !== undefined)
          .forEach((item) => {
            newArrayForSelectedRowKeys.push(getRowId(item));
          });
      }
    }

    // Уникальные id при выборе чекбокса
    const uniqueSelectedItemId = [
      ...new Set([
        ...rowSelection.selectedRowKeys,
        ...newArrayForSelectedRowKeys,
      ]),
    ];
    rowSelection.onSetSelectedRowItem([...uniqueSelectedItemId]);
  } else {
    const selectedItemId = changeRows.map((row) => getRowId(row));
    const newArraySelectedRowsId = rowSelection.selectedRowKeys.filter(
      (x) => !selectedItemId.includes(x),
    );
    rowSelection.onSetSelectedRowItem(newArraySelectedRowsId);
  }
};

export const currentPageForLoadingAfterDeleteAction = (
  dashboardStatePagination: {
    current: number;
    pageSize: number;
    total: number;
  },
  dashboardListLength: number,
  selectedItems: number[],
  setDashboardState?: React.Dispatch<
    React.SetStateAction<TypeDashboardPaginationState>
  >,
): number => {
  const { current } = dashboardStatePagination;

  if (dashboardListLength === selectedItems.length && current > 1) {
    if (setDashboardState) {
      setDashboardState((prevState: TypeDashboardPaginationState) => {
        return {
          ...prevState,
          current: current - 1,
        };
      });
    }
    return current - 1;
  } else {
    return current;
  }
};
