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

const initialState = {
  translationFiles: null,
  selectedTranslateFile: null,
  ccSegments: null,
  languages: [],
  translateLanguages: [],
  subtitle: null,
  published: false,
  processing: false,
  currentProcessingJobType: null,
  processingPercentage: null,
  isLoading: false,
  shouldOpenGenerateByAiConfirm: false,

  isEditMode: false,
  // shape
  // { uid: { tokensId: token } }
  editData: {},

  // toolbox
  isAutoscroll: true,
  isHighlightSpeech: false,
};

export const slice = createSlice({
  name: '@@AIWORKBENCH/TRANSLATES',
  initialState,
  reducers: {
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    getTranslationFilesAction: (state) => state,
    removeTranslationFileAction: (state) => state,
    getSubtitleAction: (state, action) => {
      state.selectedTranslateFile = action.payload;
    },
    uploadAction: (state) => state,
    addTranslationFileAction: (state) => state,
    toggleConfirmForGenerateByAiConfirmAction: (state, action) => {
      state.shouldOpenGenerateByAiConfirm = action.payload;
    },
    generateByAiAction: (state) => state,
    generateTranscriptByAiAction: (state) => state,
    setEditDataAction: (state, action) => {
      const { uid, startTime, endTime, token } = action.payload;

      if (!state.editData[uid]) {
        state.editData[uid] = {
          startTime,
          endTime,
          tokens: {},
        };
      }

      state.editData[uid].tokens[token.startTime] = token;
    },
    setSubtitleAction: (state) => state,
    unmountAction: (state) => {
      state.translationFiles = null;
      state.selectedTranslateFile = null;
      state.subtitle = null;
      state.ccSegments = null;
      state.isEditMode = false;
      state.editData = {};
    },
  },
});

export const {
  setAction,
  getTranslationFilesAction,
  removeTranslationFileAction,
  getSubtitleAction,
  uploadAction,
  addTranslationFileAction,
  toggleConfirmForGenerateByAiConfirmAction,
  generateByAiAction,
  generateTranscriptByAiAction,
  setEditDataAction,
  setSubtitleAction,
  unmountAction,
} = slice.actions;
