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

import {
  TAG_SYSTEM_CATEGORY_BRAND_SAFETY,
  TAG_SYSTEM_CATEGORY_BRANDS,
  TAG_SYSTEM_CATEGORY_CUSTOM,
  TAG_SYSTEM_CATEGORY_IAB,
  TAG_SYSTEM_CATEGORY_PEOPLE,
} from '../../constants';

const initialState = {
  data: [],
  isLoading: false,
  // used when user click edit or delete btn
  // for manipulate tag data
  selectedStartTime: null,
  // used for select row
  // when player play or user click to row
  selectedItemTime: null,
  shouldWrapTags: true,
  search: {
    searchText: '',
    startTime: null,
    currentSearchedUniqIndex: null,
  },
  tagInfo: {},
  // filters
  activeTagsFilter: [
    TAG_SYSTEM_CATEGORY_PEOPLE,
    TAG_SYSTEM_CATEGORY_BRANDS,
    TAG_SYSTEM_CATEGORY_BRAND_SAFETY,
    TAG_SYSTEM_CATEGORY_IAB,
    TAG_SYSTEM_CATEGORY_CUSTOM,
  ],
};

export const slice = createSlice({
  name: '@@AIWORKBENCH/TAGLOG',
  initialState,
  reducers: {
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    unmountAction: (state) => {
      state.data = null;
      state.selectedStartTime = null;
      state.selectedItemTime = null;
      state.shouldWrapTags = true;
      state.search = {
        searchText: '',
        startTime: null,
      };
      state.tagInfo = {};
    },
    getDataAction: (state) => state,
    upsertTagsAction: (state) => state,
    updateSummaryTagsAction: (state) => state,
    updateVideoVerificationStateAction: (state) => state,
    getTagInfoAction: (state) => state,
    downloadAction: (state) => state,
  },
});

export const {
  setAction,
  unmountAction,
  getDataAction,
  upsertTagsAction,
  updateSummaryTagsAction,
  updateVideoVerificationStateAction,
  getTagInfoAction,
  downloadAction,
} = slice.actions;
