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

import { PUBLIC } from '@/modules/editorial/shareAndAccess/constants';

const initialState = {
  users: [],
  sites: [],
  usersToShare: [],
  message: '',
  accessLevel: PUBLIC,
  accessSites: [],
  accessUsers: [],
  isLoading: false,
  isSitesLoading: false,
  isAccessUsersLoading: false,
  isSharing: false,
  isOpen: false,
  videoId: '',
  sendHubsNotification: false,

  // trim
  isUsedInTrim: false,
  isKeepSharingCheckedInTrim: false,
};

export const slice = createSlice({
  name: '@@shareAndAccess',
  initialState,
  reducers: {
    getUsersByAccountAction: (state) => state,
    setUsersByAccountAction: (state, action) => {
      state.users = action.payload;
    },
    deleteAccessUsersAction: (state) => state,
    getPublishersByIdsAction: (state) => state,
    setPublishersByIdsAction: (state, action) => {
      state.sites = action.payload;
    },
    updateAccessLevelAction: (state) => state,
    changeAccessLevelAction: (state) => state,
    addAccessSitesAction: (state) => state,
    deleteAccessSitesAction: (state) => state,
    shareVideoWithUsersAction: (state) => state,
    updateStateByParamAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    setIsSharingAction: (state, action) => {
      state.isSharing = action.payload;
    },
    setVideoSharedAction: (state) => {
      state.usersToShare = [];
      state.isSharing = false;
    },
    clearFormAction: () => initialState,
    setIsLoadingAction: (state, action) => {
      state.isLoading = action.payload;
    },
    setIsSitesLoadingAction: (state, action) => {
      state.isSitesLoading = action.payload;
    },
    setFormCloseAction: (state, action) => {
      state.isOpen = action.payload;
    },
    copyAccessUsersToTrimedVideoAction: (state) => state,
    getSharedUsersNameAction: (state) => state,
  },
});

export const {
  getUsersByAccountAction,
  setUsersByAccountAction,
  getPublishersByIdsAction,
  setPublishersByIdsAction,
  updateAccessLevelAction,
  changeAccessLevelAction,
  addAccessSitesAction,
  deleteAccessSitesAction,
  shareVideoWithUsersAction,
  setIsSharingAction,
  setVideoSharedAction,
  clearFormAction,
  setIsLoadingAction,
  setIsSitesLoadingAction,
  setFormCloseAction,
  copyAccessUsersToTrimedVideoAction,
  getSharedUsersNameAction,
  deleteAccessUsersAction,
  updateStateByParamAction,
} = slice.actions;
