import { createSlice, type PayloadAction } from '@reduxjs/toolkit';

import { DestinationListStateType, DestinationType, PublishEntryType } from '../../types';

const initialState: DestinationListStateType = {
  destinations: [],
  totalDestinations: 0,

  publishers: [],

  search: '',
  page: 1,
  pageSize: 15,
  sortBy: 'id',
  sortOrder: 'DESC',
  platform: null,
  status: null,
  publisherId: null,

  publishEntries: [],
};

export const slice = createSlice({
  name: '@@DESTINATION/LIST',
  initialState,

  reducers: {
    getPublishersAction: (state, action) => ({
      ...state,
      ...action.payload,
    }),
    addPublishEntriesAction: (state, action: PayloadAction<{ destinations: DestinationType[]; videoId: string }>) => {
      if (action.payload) {
        return state;
      }
      return state;
    },
    getPublishEntriesAction: (state, action: PayloadAction<string>) => {
      if (action.payload) {
        return state;
      }
      return state;
    },
    deletePublishEntriesAction: (state, action: PayloadAction<{ entries: PublishEntryType[]; videoId: string }>) => {
      if (action.payload) {
        return state;
      }
      return state;
    },
    updatePublishEntryAction: (state, action: PayloadAction<{ videoId: string; postText?: string }>) => {
      if (action.payload) {
        return state;
      }
      return state;
    },
    getDestinationsAction: (state, action) => ({
      ...state,
      ...action.payload,
    }),
    setDestinationsAction: (state, action) => {
      state.destinations = action.payload;
    },
    setTotalDestinationsAction: (state, action) => {
      state.totalDestinations = action.payload;
    },
    setPublishersAction: (state, action) => {
      state.publishers = action.payload;
    },
    setPublishEntriesAction: (state, action) => {
      state.publishEntries = action.payload;
    },
    setFilterParamAction: (state, action) => ({
      ...state,
      ...action.payload,
    }),
    clearFilterParamsAction: () => ({
      ...initialState,
    }),
  },
});

export const {
  addPublishEntriesAction,
  clearFilterParamsAction,
  deletePublishEntriesAction,
  getDestinationsAction,
  getPublishEntriesAction,
  getPublishersAction,
  setDestinationsAction,
  setFilterParamAction,
  setPublishEntriesAction,
  setPublishersAction,
  setTotalDestinationsAction,
  updatePublishEntryAction,
} = slice.actions;

export const nameSpace = slice.name;

export default slice.reducer;
