import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  chapters: null,
  published: false,
  draft: false,
  processing: false,

  processingPercentage: null,

  isLoading: false,

  editableId: null,
  createId: null,

  shouldOpenGenerateByAiConfirm: false,

  isDirty: false,

  isGeneratingAvailable: false,
  causes: [],

  // multiselect
  isMultiselectModeActive: false,
  selectedChapters: [], // used time to indicate what are selected

  // create video
  shouldOpenCreateVideoForm: false,
  videos: [],
};

export const slice = createSlice({
  name: '@@AIWORKBENCH/CHAPTERS',
  initialState,
  reducers: {
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    getChaptersAction: (state) => state,
    setChaptersAction: (state) => state,
    publishUnpublishAction: (state) => state,
    toggleConfirmForGenerateByAiConfirmAction: (state, action) => {
      state.shouldOpenGenerateByAiConfirm = action.payload;
    },
    generateByAiAction: (state) => state,
    unmountAction: (state) => {
      state.isDirty = false;
      state.editableId = false;
      state.createId = false;
      state.chapters = null;
      state.isMultiselectModeActive = false;
      state.selectedChapters = [];
      state.shouldOpenCreateVideoForm = false;
      state.videos = [];
    },
    createVideoAction: (state) => state,
    getListOfCreatedVideoAction: (state) => state,
  },
});

export const {
  setAction,
  getChaptersAction,
  setChaptersAction,
  publishUnpublishAction,
  toggleConfirmForGenerateByAiConfirmAction,
  generateByAiAction,
  unmountAction,
  createVideoAction,
  getListOfCreatedVideoAction,
} = slice.actions;
