// import React, { useEffect, useState } from 'react';
// import { useQueryClient } from '@tanstack/react-query';
// import { enqueueSnackbar } from 'notistack';
// import DialogProgressBar from 'src/shared/components/dialog/DialogProgressBar';
// import useDialogProgressBar from 'src/shared/hooks/useDialogProgressBar';
// import DialogConfirm from 'src/shared/components/dialog/DialogConfirm';
// import type { GridRowSelectionModel } from '@mui/x-data-grid-premium';
// import useCancelFakturPk from '../../hooks/useCancelFakturPK';

// interface ModalCancelFakturProps {
//   dataSelected?: GridRowSelectionModel;
//   setSelectionModel?: React.Dispatch<React.SetStateAction<GridRowSelectionModel | undefined>>;
//   tableApiRef?: React.MutableRefObject<any>;
//   isOpenDialogCancel: boolean;
//   setIsOpenDialogCancel: (v: boolean) => void;
//   successMessage?: string;
//   onConfirmCancel?: () => Promise<void> | void;
// }

// /**
//  * Normalize various possible shapes of grid selection model into array of ids.
//  * Handles:
//  *  - array of ids: [1, 2, 'a']
//  *  - Set-of-ids: new Set([1,2])
//  *  - object like { ids: Set(...), type: 'include' }
//  *  - fallback: tries to extract keys if it's an object map
//  */
// const normalizeSelection = (sel?: any): (string | number)[] => {
//   if (!sel) return [];
//   if (Array.isArray(sel)) return sel as (string | number)[];
//   if (sel instanceof Set) return Array.from(sel) as (string | number)[];
//   if (typeof sel === 'object') {
//     // common shape from newer MUI: { ids: Set(...), type: 'include' }
//     if (sel.ids instanceof Set) return Array.from(sel.ids) as (string | number)[];
//     // maybe it's a map-like object { '1': true, '2': true }
//     const maybeIds = Object.keys(sel).filter((k) => k !== 'type' && k !== 'size');
//     if (maybeIds.length > 0) {
//       // try to convert numeric-like keys to number where applicable
//       return maybeIds.map((k) => {
//         const n = Number(k);
//         return Number.isNaN(n) ? k : n;
//       });
//     }
//   }
//   return [];
// };

// const ModalReturFakturPM: React.FC<ModalCancelFakturProps> = ({
//   dataSelected,
//   setSelectionModel,
//   tableApiRef,
//   isOpenDialogCancel,
//   setIsOpenDialogCancel,
//   successMessage = 'Data berhasil dibatalkan',
//   onConfirmCancel,
// }) => {
//   const queryClient = useQueryClient();

//   // custom hooks for progress state
//   const {
//     numberOfData,
//     setNumberOfData,
//     numberOfDataFail,
//     numberOfDataProcessed,
//     numberOfDataSuccess,
//     processSuccess,
//     processFail,
//     resetToDefault,
//     status,
//   } = useDialogProgressBar();

//   const [isOpenDialogProgressBar, setIsOpenDialogProgressBar] = useState(false);

//   // React Query mutation for delete
//   const { mutateAsync } = useCancelFakturPk({
//     onSuccess: () => processSuccess(),
//     onError: () => processFail(),
//   });

//   // fungsi multiple delete -- gunakan normalized array of ids
//   const handleMultipleDelete = async () => {
//     const ids = normalizeSelection(dataSelected);
//     return Promise.allSettled(ids.map(async (id) => mutateAsync({ id: String(id) })));
//   };

//   const clearSelection = () => {
//     // clear grid selection via apiRef if available
//     tableApiRef?.current?.setRowSelectionModel?.([]);
//     // also clear local state if setter provided
//     // set to undefined to follow the native hook type (or to empty array cast)
//     // setSelectionModel?.(undefined);
//     setSelectionModel?.(undefined);
//   };

//   const handleCloseModal = () => {
//     setIsOpenDialogCancel(false);
//     resetToDefault();
//   };

//   const onSubmit = async () => {
//     try {
//       setIsOpenDialogProgressBar(true);
//       await handleMultipleDelete();
//       enqueueSnackbar(successMessage, { variant: 'success' });

//       await onConfirmCancel?.();

//       handleCloseModal();
//       clearSelection();
//     } catch (error: any) {
//       enqueueSnackbar(error?.message || 'Gagal membantalkan data', { variant: 'error' });
//     } finally {
//       // sesuaikan queryKey jika perlu; tetap panggil invalidasi
//       queryClient.invalidateQueries({ queryKey: ['faktur', 'pk'] });
//     }
//   };

//   useEffect(() => {
//     setNumberOfData(normalizeSelection(dataSelected).length);
//   }, [isOpenDialogCancel, dataSelected, setNumberOfData]);

//   return (
//     <>
//       <DialogConfirm
//         fullWidth
//         maxWidth="xs"
//         title="Apakah Anda yakin akan melakukan Retur Pajak Masukan?"
//         description=""
//         actionTitle="Iya"
//         isOpen={isOpenDialogCancel}
//         isLoadingBtnSubmit={false}
//         handleClose={handleCloseModal}
//         handleSubmit={onSubmit}
//       />

//       <DialogProgressBar
//         isOpen={isOpenDialogProgressBar}
//         handleClose={() => {
//           handleCloseModal();
//           setIsOpenDialogProgressBar(false);
//         }}
//         numberOfData={numberOfData}
//         numberOfDataProcessed={numberOfDataProcessed}
//         numberOfDataFail={numberOfDataFail}
//         numberOfDataSuccess={numberOfDataSuccess}
//         status={status}
//       />
//     </>
//   );
// };

// export default ModalReturFakturPM;

import React, { useMemo } from 'react';
import DialogConfirm from 'src/shared/components/dialog/DialogConfirm';
import type { GridRowSelectionModel } from '@mui/x-data-grid-premium';
import { useNavigate } from 'react-router';

interface ModalReturFakturProps {
  dataSelected?: GridRowSelectionModel;
  setSelectionModel?: React.Dispatch<React.SetStateAction<GridRowSelectionModel | undefined>>;
  tableApiRef?: React.MutableRefObject<any>;
  isOpenDialogCancel: boolean;
  setIsOpenDialogCancel: (v: boolean) => void;
  type?: 'retur' | 'ubah';
}

/**
 * Ambil single ID dari berbagai bentuk rowSelectionModel
 */
const getSingleId = (sel?: any): string | null => {
  if (!sel) return null;
  if (Array.isArray(sel)) return sel[0]?.toString() ?? null;
  if (sel instanceof Set) return Array.from(sel)[0]?.toString() ?? null;
  if (sel?.ids instanceof Set) return Array.from(sel.ids)[0]?.toString() ?? null;
  return null;
};

const ModalReturFakturPM: React.FC<ModalReturFakturProps> = ({
  dataSelected,
  setSelectionModel,
  tableApiRef,
  isOpenDialogCancel,
  setIsOpenDialogCancel,
  type = 'retur',
}) => {
  const navigate = useNavigate();

  const selectedId = useMemo(() => getSingleId(dataSelected), [dataSelected]);

  const handleCloseModal = () => {
    setIsOpenDialogCancel(false);
  };

  const clearSelection = () => {
    tableApiRef?.current?.setRowSelectionModel?.([]);
    setSelectionModel?.(undefined);
  };

  const handleSubmit = () => {
    if (!selectedId) return;

    handleCloseModal();
    clearSelection();

    navigate(`/faktur/retur-pm/${selectedId}/${type}`);
  };

  return (
    <DialogConfirm
      fullWidth
      maxWidth="xs"
      title="Apakah Anda yakin akan melakukan Retur Pajak Masukan?"
      description=""
      actionTitle="Iya"
      isOpen={isOpenDialogCancel}
      isLoadingBtnSubmit={false}
      handleClose={handleCloseModal}
      handleSubmit={handleSubmit}
    />
  );
};

export default ModalReturFakturPM;
