import { TABLE_REDUX_FIELD_NAME } from '../../constants';
import { SORT_ASC, SORT_DESC } from '@/modules/@common/constants/sort';
import { STATUSES_ALL } from '@/modules/xRay/creatives/List/constants';

import type { FeedsDataRecordsType } from '../../types';

import { slice } from '../slices';
import createTableSelector from '@/modules/@common/Table/redux/selectors';

import type { RootState } from '@/modules/@common/store/store';

type OrderType = typeof SORT_ASC | typeof SORT_DESC;

const nameSpace = slice.name;
const tableSelectors = createTableSelector(TABLE_REDUX_FIELD_NAME, nameSpace);

// table
export const dataSelector = (state: RootState) => tableSelectors.dataSelector(state) as FeedsDataRecordsType;
export const pageSelector = (state: RootState) => tableSelectors.pageSelector(state) as number;
export const pageSizeSelector = (state: RootState) => tableSelectors.pageSizeSelector(state) as number;
export const totalCountSelector = (state: RootState) => tableSelectors.totalCountSelector(state) as number;
export const sortBySelector = (state: RootState) => tableSelectors.sortBySelector(state) as string;
export const sortOrderSelector = (state: RootState) => tableSelectors.sortOrderSelector(state) as OrderType;
export const isLoadingSelector = (state: RootState) => tableSelectors.isLoadingSelector(state) as boolean;
export const selectedSelector = (state: RootState) => tableSelectors.selectedSelector(state) as number[];

// filters
export const searchTextSelector = (state: RootState) => state[nameSpace].searchText;
export const statusSelector = (state: RootState) => state[nameSpace].status;

// emptyState
export const shouldShowEmptySelector = (state: RootState) => {
  const data = dataSelector(state);
  const page = pageSelector(state);
  const search = searchTextSelector(state);
  const status = statusSelector(state);
  const isLoading = isLoadingSelector(state);

  return !isLoading && Array.isArray(data) && !data.length && page === 1 && !search && status === STATUSES_ALL;
};
