import { createSelector } from '@ngrx/store';
import { selectBranchState } from '../branch.selectors';
import { DisplayInfoDTO } from '@swagger/eis';
import { fromDisplayInfoToTask } from './mappings';
import { parseStringDate } from '../../../modules/task-calendar/utils';
import { fromDisplayInfoToCalendarItem } from './mappings/calendar-item-mapper';
import { taskCalendarAdapter } from './task-calendar.state';

export const selectTaskCalendarState = createSelector(selectBranchState, (s) => s.taskCalendar);

const { selectAll } = taskCalendarAdapter.getSelectors(selectTaskCalendarState);

export const selectFetching = createSelector(selectTaskCalendarState, (s) => s.fetching);

export const selectTasks = createSelector(selectAll, (s) => s);

export const selectFilters = createSelector(selectTaskCalendarState, (s) => s.filter);

export const selectTaskById = createSelector(selectTasks, (s: DisplayInfoDTO[], id: number) => s.find((task) => task.id === id));

export const selectMappedTasks = createSelector(selectTasks, (s) => s.map((tasks) => fromDisplayInfoToTask(tasks)));

export const selectCalendarItems = createSelector(selectTasks, (s) => s.map((task) => fromDisplayInfoToCalendarItem(task)));

export const selectMinMaxDates = createSelector(selectFilters, (s) => ({
  minDate: parseStringDate(s.start),
  maxDate: parseStringDate(s.stop),
}));
