import type { UseFileBrowserAppType } from '../useFileBrowserApp';
import { TaskRunnerModal } from './TaskRunnerModal';

type TaskRunnerDialogProps = {
  fileBrowserManager: UseFileBrowserAppType;
};

export const TaskRunnerDialog = ({ fileBrowserManager }: TaskRunnerDialogProps) => {
  const { runningTask, setRunningTask, refresh, currentPath, getEntryAbsPath } = fileBrowserManager;

  if (!runningTask) return null;

  const entryAbs = getEntryAbsPath(runningTask.entry.name);
  // Parent folder of the entry — the batch target when a multi-selection is run.
  const folderFullPath = entryAbs.replace(/\/[^/]+$/, '');

  return (
    <TaskRunnerModal
      open
      onOpenChange={(open) => {
        if (!open) {
          setRunningTask(null);
          refresh();
        }
      }}
      task={runningTask.task}
      entryName={runningTask.entry.name}
      entryFullPath={entryAbs}
      entryType={runningTask.entry.type}
      cwd={{ root: 'home', path: currentPath.replace(/^\//, '') }}
      selectedNames={runningTask.selectedNames}
      folderFullPath={folderFullPath}
    />
  );
};
