import { analytics, dataPack, languages, pages, sites, social, structuredData } from "@ax/api";
import { analyticsActions } from "@ax/containers/Analytics";
import { appActions } from "@ax/containers/App";
import { integrationsActions } from "@ax/containers/Integrations";
import { menuActions, navigationActions } from "@ax/containers/Navigation";
import { resetPageEditor } from "@ax/containers/PageEditor/actions";
import { dataPacksActions } from "@ax/containers/Settings/DataPacks";
import { socialActions } from "@ax/containers/Settings/Social";
import { structuredDataActions } from "@ax/containers/StructuredData";
import { usersActions } from "@ax/containers/Users";
import { getDefaultTheme, getThemeElements, handleRequest, isReqOk, sortBy } from "@ax/helpers";
import type {
	IGetGlobalPagesParams,
	IGetSitePagesParams,
	IGetSitesParams,
	IPage,
	IQueryValue,
	IRootState,
	ISite,
	ISiteListConfig,
	IThemeElements,
} from "@ax/types";

import type { Dispatch } from "redux";

import {
	DEFAULT_PARAMS,
	SET_ALL_SITE_PAGES,
	SET_ALL_SITES,
	SET_CONFIG,
	SET_CONTENT_FILTERS,
	SET_CURRENT_SEARCH,
	SET_CURRENT_SITE_ERROR_PAGES,
	SET_CURRENT_SITE_INFO,
	SET_CURRENT_SITE_LANGUAGES,
	SET_CURRENT_SITE_PAGES,
	SET_FILTER,
	SET_IS_SITES_LOADING,
	SET_RECENT_SITES,
	SET_SAVED_SITE_INFO,
	SET_SITES,
	SET_SITES_BY_LANG,
	SET_SITES_TOTAL_ITEMS,
	SET_THEME_ELEMENTS,
	SET_TOTAL_ITEMS,
} from "./constants";
import type {
	ISetAllSitePagesAction,
	ISetAllSitesAction,
	ISetConfig,
	ISetContentFilters,
	ISetCurrentSearch,
	ISetCurrentSiteErrorPages,
	ISetCurrentSiteInfoAction,
	ISetCurrentSiteLanguagesAction,
	ISetCurrentSitePagesAction,
	ISetFilter,
	ISetIsSitesLoading,
	ISetRecentSitesAction,
	ISetSavedSiteInfoAction,
	ISetSitesAction,
	ISetSitesByLangAction,
	ISetSitesTotalItems,
	ISetThemeElements,
	ISetTotalItems,
} from "./interfaces";

const { setIsSaving, setLanguage } = appActions;
const { resetDefaultsValues, getDefaults } = navigationActions;
const { resetMenuValues } = menuActions;
const { getAnalytics } = analyticsActions;
const { getIntegrations } = integrationsActions;
const { getUsers, getUserCurrentPermissions, getRoles } = usersActions;

function setSites(sitesList: ISite[]): ISetSitesAction {
	return { type: SET_SITES, payload: { sites: sitesList } };
}

function setRecentSites(recentSites: ISite[]): ISetRecentSitesAction {
	return { type: SET_RECENT_SITES, payload: { recentSites } };
}

function setSitesTotalItems(sitesTotalItems: number): ISetSitesTotalItems {
	return { type: SET_SITES_TOTAL_ITEMS, payload: { sitesTotalItems } };
}

function setSitesByLang(sitesList: ISite[]): ISetSitesByLangAction {
	return { type: SET_SITES_BY_LANG, payload: { sitesByLang: sitesList } };
}

function setAllSites(allSites: ISite[]): ISetAllSitesAction {
	return { type: SET_ALL_SITES, payload: { allSites } };
}

function setCurrentSiteInfo(currentSiteInfo: ISite | null): ISetCurrentSiteInfoAction {
	localStorage.setItem("siteID", JSON.stringify(currentSiteInfo?.id || "global"));
	return { type: SET_CURRENT_SITE_INFO, payload: { currentSiteInfo } };
}

function setCurrentSitePages(currentSitePages: IPage[]): ISetCurrentSitePagesAction {
	return { type: SET_CURRENT_SITE_PAGES, payload: { currentSitePages } };
}

function setAllSitePages(allSitePages: IPage[]): ISetAllSitePagesAction {
	return { type: SET_ALL_SITE_PAGES, payload: { allSitePages } };
}

function setFilter(currentFilter: string | null): ISetFilter {
	return { type: SET_FILTER, payload: { currentFilter } };
}

function setTotalItems(totalItems: number): ISetTotalItems {
	return { type: SET_TOTAL_ITEMS, payload: { totalItems } };
}

function setCurrentSiteLanguages(currentSiteLanguages: any[]): ISetCurrentSiteLanguagesAction {
	return { type: SET_CURRENT_SITE_LANGUAGES, payload: { currentSiteLanguages } };
}

function setSavedSiteInfo(savedSiteInfo: ISite): ISetSavedSiteInfoAction {
	return { type: SET_SAVED_SITE_INFO, payload: { savedSiteInfo } };
}

function setCurrentSiteErrorPages(currentSiteErrorPages: number[]): ISetCurrentSiteErrorPages {
	return { type: SET_CURRENT_SITE_ERROR_PAGES, payload: { currentSiteErrorPages } };
}

function setContentFilters(contentFilters: Record<string, IQueryValue[]> | null): ISetContentFilters {
	return { type: SET_CONTENT_FILTERS, payload: { contentFilters } };
}

function setConfig(config: ISiteListConfig): ISetConfig {
	return { type: SET_CONFIG, payload: { config } };
}

function setCurrentSearch(currentSearch: string): ISetCurrentSearch {
	return { type: SET_CURRENT_SEARCH, payload: { currentSearch } };
}

function setThemeElements(themeElements: IThemeElements | null): ISetThemeElements {
	return { type: SET_THEME_ELEMENTS, payload: { themeElements } };
}

function setIsSitesLoading(isSitesLoading: boolean): ISetIsSitesLoading {
	return { type: SET_IS_SITES_LOADING, payload: { isSitesLoading } };
}

// TODO: hay que controlar que cuando da error la API borrar los sites ya guardados y sacar el error (ver los siguientes FIXME)
function getSites(params: IGetSitesParams = { recentSitesNumber: 7 }): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (data: any) => {
					dispatch(setSites(data.allSites));
					dispatch(setRecentSites(data.recentSites));
					dispatch(setSitesTotalItems(data.totalItems));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};
			const callback = async () => sites.getAllSites(params);

			const setIsLoading = setIsSitesLoading;

			await handleRequest(callback, responseActions, [setIsLoading])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function getSite(siteID: number): (dispatch: Dispatch, getState: () => IRootState) => Promise<void> {
	return async (dispatch, getState) => {
		try {
			const responseActions = {
				handleSuccess: async (data: ISite) => {
					dispatch(setCurrentSiteInfo(data));
					await getDefaults()(dispatch, getState);
					await getIntegrations(data.id)(dispatch);
					await setSiteInfo(data)(dispatch, getState);
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};
			const callback = async () => sites.getSiteInfo(siteID);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function getSitesByLang(params: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (data: any) => {
					dispatch(setSitesByLang(data.allSites));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () => sites.getAllSites(params);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function getAllSites(): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (data: any) => {
					dispatch(setAllSites(data.allSites));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () => sites.getAllSites({ pagination: false });

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function saveSettings(form: ISite): (dispatch: Dispatch, getState: () => IRootState) => Promise<boolean> {
	return async (dispatch, getState) => {
		try {
			const {
				sites: { currentSiteInfo },
			} = getState();

			const isNewSite = !currentSiteInfo;

			const theme = getDefaultTheme();

			const responseActions = {
				handleSuccess: (response: ISite) => {
					setSiteInfo(response)(dispatch, getState);
					const themeElements = getThemeElements(response.theme) || null;
					dispatch(setThemeElements(themeElements));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () =>
				isNewSite ? sites.createSite({ ...form, theme }) : sites.updateSite(currentSiteInfo.id, form);

			return await handleRequest(callback, responseActions, [appActions.setIsSaving])(dispatch);
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
		dispatch(setIsSaving(false));
		return false;
	};
}

function getGlobalPages(params: IGetGlobalPagesParams, filterQuery: string): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (response: any) => {
					const { items, totalItems } = response;
					dispatch(setCurrentSitePages(items));
					dispatch(setTotalItems(totalItems));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () => sites.getGlobalPages(params, filterQuery);

			await handleRequest(callback, responseActions, [appActions.setIsLoading])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

// TODO: refactor
function setSiteInfo(currentSiteInfo: ISite): (dispatch: Dispatch, getState: () => IRootState) => Promise<void> {
	return async (dispatch, getState) => {
		try {
			const {
				structuredData: { structuredData: currentStructuredData },
				structuredData: { categories: currentCategories },
			} = getState();

			dispatch(appActions.setIsLoading(true));

			resetSiteValues(currentSiteInfo.id)(dispatch);
			structuredDataActions.resetStructuredData()(dispatch);
			dispatch(setCurrentSiteInfo(currentSiteInfo));

			await getRoles({ siteId: currentSiteInfo.id })(dispatch);

			const themeElements = getThemeElements(currentSiteInfo.theme) || null;
			dispatch(setThemeElements(themeElements));

			// get site languages
			const response: any = await languages.getSiteLanguages(currentSiteInfo.id);
			if (isReqOk(response.status)) {
				const siteLanguages = response.data.items;
				const defaultLanguage = siteLanguages.find((language: any) => language.isDefault);
				const { locale, id } = defaultLanguage;
				const lang = {
					locale,
					id,
				};

				dispatch(setLanguage(lang));
				dispatch(setCurrentSiteLanguages(siteLanguages));
			} else {
				console.log("Error en sites getSiteLanguages");
			}

			// get site activated dataPacks
			const getActivatedQuery = "?status=activated";
			const dataPackResponse: any = await dataPack.getSiteDataPacks(currentSiteInfo.id, getActivatedQuery);
			if (isReqOk(dataPackResponse.status)) {
				const data = dataPackResponse.data.items;
				const orderedData = data.sort(sortBy("title", false));
				dispatch(dataPacksActions.setActivated(orderedData));
				dataPacksActions.getSiteModules()(dispatch, getState);
				dataPacksActions.getSiteTemplates()(dispatch, getState);
			} else {
				console.log("Error en sites getSiteDataPacks");
			}

			// get site socials
			const socialResponse: any = await social.getSocial(currentSiteInfo.id);
			if (isReqOk(socialResponse.status)) {
				dispatch(socialActions.setSocial(socialResponse.data));
			} else {
				console.log("Error en sites getSocial");
			}

			// get structuredData
			const structuredDataResponse: any = await structuredData.getData(null, currentSiteInfo.id);
			if (isReqOk(structuredDataResponse.status)) {
				const data = structuredDataResponse.data.items;

				const categories = structuredDataActions.filterStructuredData(data, true, currentSiteInfo.id);

				const structuredDataValues = structuredDataActions.filterStructuredData(data, false, currentSiteInfo.id);
				dispatch(structuredDataActions.setCategories({ global: currentCategories.global, site: categories.site }));

				dispatch(
					structuredDataActions.setStructuredData({ ...currentStructuredData, site: structuredDataValues.site }),
				);
			} else {
				console.log("Error en sites getStructuredData");
			}

			// get analytics
			const analyticsResponse: any = await analytics.getAnalytics(currentSiteInfo.id);
			if (isReqOk(socialResponse.status)) {
				dispatch(analyticsActions.setAnalytics(analyticsResponse.data));
			} else {
				console.log("Error en sites getAnalytics");
			}

			getUserCurrentPermissions()(dispatch, getState);

			await sites.setSiteActivity(currentSiteInfo.id);

			dispatch(appActions.setIsLoading(false));
		} catch (e) {
			console.log(e);
		}
	};
}

function getFilteredContent(filter: any): (dispatch: Dispatch, getState: () => IRootState) => Promise<void> {
	return async (dispatch, getState) => {
		try {
			const { value, fromPage } = filter;
			dispatch(setFilter(value));

			const {
				sites: { currentSiteInfo },
				app: { token },
			} = getState();

			if (!currentSiteInfo) return;

			if (value === "unique-pages" || fromPage) {
				const params = {
					siteID: currentSiteInfo.id,
					...DEFAULT_PARAMS,
				};
				getSitePages(params, value)(dispatch);
			} else {
				const params = {
					dataID: filter,
					token,
					...DEFAULT_PARAMS,
				};
				structuredDataActions.setSelectedStructuredData(value, "site")(dispatch, getState);
				structuredDataActions.getStructuredDataContents(params, currentSiteInfo.id)(dispatch, getState);
			}
		} catch (e) {
			console.log(e);
		}
	};
}

function getSitePages(
	params: IGetSitePagesParams,
	structuredData?: string | null,
	filterQuery?: string,
): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (response: any) => {
					const { items, totalItems } = response;
					dispatch(setCurrentSitePages(items));
					dispatch(setTotalItems(totalItems));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () =>
				structuredData
					? await sites.getStructuredSitePages(params, structuredData, filterQuery)
					: await sites.getSitePages(params, filterQuery);

			await handleRequest(callback, responseActions, [appActions.setIsLoading])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function getAllSitePages(params: IGetSitePagesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (response: any) => {
					const { items } = response;
					dispatch(setAllSitePages(items));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () => sites.getSitePages(params);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function getSiteLanguages(siteID: number): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: (response: any) => {
					const siteLanguages = response.items;

					const defaultLanguage = siteLanguages.find((language: any) => language.isDefault);
					const { locale, id } = defaultLanguage;
					const lang = {
						locale,
						id,
					};

					dispatch(setLanguage(lang));
					dispatch(setCurrentSiteLanguages(siteLanguages));
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};

			const callback = async () => await languages.getSiteLanguages(siteID);

			await handleRequest(callback, responseActions, [appActions.setIsLoading])(dispatch);
		} catch (e) {
			console.log(e);
		}
	};
}

function deleteSite(siteID: number, params?: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: () => {
					getSites(params)(dispatch);
				},
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};
			const callback = async () => sites.deleteSite(siteID);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function publishSite(siteID: number, params?: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const response = await sites.publishSite(siteID);
			if (isReqOk(response.status)) {
				getSites(params)(dispatch);
			}
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function publishSitesBulk(ids: number[], params?: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: () => getSites(params)(dispatch),
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};
			const callback = async () => sites.publishSiteBulk(ids);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function unpublishSite(siteID: number, params?: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const response = await sites.unpublishSite(siteID);
			if (isReqOk(response.status)) {
				getSites(params)(dispatch);
			}
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function unpublishSitesBulk(ids: number[], params?: IGetSitesParams): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const responseActions = {
				handleSuccess: () => getSites(params)(dispatch),
				handleError: (response: any) => appActions.handleError(response)(dispatch),
			};
			const callback = async () => sites.unpublishSiteBulk(ids);

			await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function resetSiteValues(siteID: number): (dispatch: Dispatch) => void {
	return async (dispatch) => {
		dispatch(setFilter("unique-pages"));
		resetDefaultsValues()(dispatch);
		resetPageEditor()(dispatch);
		resetMenuValues(dispatch);
		dispatch(setCurrentSitePages([]));
		dispatch(setTotalItems(0));
		getAnalytics(siteID)(dispatch);
		dispatch(setCurrentSearch(""));
		dispatch(setThemeElements(null));
	};
}

function saveCurrentSiteInfo(): (dispatch: Dispatch, getState: () => IRootState) => Promise<void> {
	return async (dispatch, getState) => {
		try {
			const {
				sites: { currentSiteInfo },
			} = getState();

			if (!currentSiteInfo) return;

			dispatch(setSavedSiteInfo(currentSiteInfo));
			dispatch(setCurrentSiteInfo(null));
		} catch (e) {
			console.log(e);
		}
	};
}

function importPageFromGlobal(
	pageID: number | number[],
): (dispatch: Dispatch, getState: () => IRootState) => Promise<boolean> {
	return async (dispatch, getState) => {
		try {
			const {
				sites: { currentSiteInfo, currentFilter },
			} = getState();

			if (!currentSiteInfo) return false;

			const responseActions = {
				handleSuccess: () => {
					const params = {
						siteID: currentSiteInfo.id,
						...DEFAULT_PARAMS,
					};
					const filter = currentFilter === "unique-pages" ? null : currentFilter;
					getSitePages(params, filter)(dispatch);
				},
				handleError: (response: any) => {
					const {
						data: { message },
					} = response;
					const isMultiple = Array.isArray(message) && message.length > 1;
					const msg = isMultiple ? `The import action failed due to ${message.length} errors.` : undefined;
					appActions.handleError(response, isMultiple, msg)(dispatch);
				},
			};

			const callback = async () =>
				Array.isArray(pageID)
					? sites.importPageBulk(currentSiteInfo.id, pageID)
					: sites.importPage(currentSiteInfo.id, pageID);

			return await handleRequest(callback, responseActions, [appActions.setIsLoading])(dispatch);
		} catch (e) {
			console.log(e);
			return false;
		}
	};
}

function removeUsersBulk(siteId: number, users: number[]): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			const response = await sites.removeUsersBulk(siteId, users);
			if (isReqOk(response.status)) {
				const params = { filterQuery: "?order=dateCreated" };
				getUsers(params, siteId)(dispatch);
			}
		} catch (e) {
			console.log(e); // TODO: capturar error bien
		}
	};
}

function removePageFromSite(
	pageID: number | number[],
	refresh = true,
): (dispatch: Dispatch, getState: () => IRootState) => Promise<boolean> {
	return async (dispatch, getState) => {
		try {
			const {
				sites: { currentSiteInfo },
				structuredData: { currentStructuredData },
			} = getState();

			if (!currentSiteInfo) return false;

			const responseActions = {
				handleSuccess: () => {
					const params = {
						siteID: currentSiteInfo.id,
						...DEFAULT_PARAMS,
					};
					const filter = currentStructuredData ? currentStructuredData.id : null;
					if (refresh) {
						getSitePages(params, filter)(dispatch);
					}
				},
				handleError: (response: any) => {
					const {
						data: { message },
					} = response;
					const isMultiple = Array.isArray(message) && message.length > 1;
					const msg = isMultiple ? `The delete action failed due to ${message.length} errors.` : undefined;
					appActions.handleError(response, isMultiple, msg)(dispatch);
				},
			};

			const callback = async () =>
				Array.isArray(pageID)
					? sites.removePageBulk(currentSiteInfo.id, pageID)
					: sites.removePage(currentSiteInfo.id, pageID);

			return await handleRequest(callback, responseActions, [])(dispatch);
		} catch (e) {
			console.log(e);
			return false;
		}
	};
}

function deleteAndRemoveFromSiteBulk(
	pageIds: number[],
	globalPageIds: number[],
): (dispatch: Dispatch, getState: () => IRootState) => Promise<boolean> {
	return async (dispatch, getState) => {
		try {
			const {
				sites: { currentSiteInfo },
			} = getState();

			if (!currentSiteInfo) return false;

			let responseErrorPages: any = { data: { code: null, message: null } };

			const getMessageErrors = () => {
				const {
					data: { message },
				} = responseErrorPages;

				const isMultiple = Array.isArray(message) && message.length > 1;
				const msg = isMultiple ? `The delete action failed due to ${message.length} errors.` : undefined;
				appActions.handleError(responseErrorPages, isMultiple, msg)(dispatch);
			};

			const responsePageActions = {
				handleSuccess: () => true,
				handleError: (response: any) => {
					responseErrorPages = response;
					getMessageErrors();
				},
			};

			const responseGlobalPageActions = {
				handleSuccess: () => {
					if (responseErrorPages.data.message?.length > 0) {
						getMessageErrors();
					}
				},
				handleError: (response: any) => {
					const errorMessages = responseErrorPages.data?.message ? responseErrorPages.data.message : [];
					responseErrorPages = {
						...responseErrorPages,
						data: {
							code: responseErrorPages.data?.code || response.data?.code,
							message: response.data ? [...errorMessages, ...response.data.message] : errorMessages,
						},
					};

					getMessageErrors();
				},
			};

			const pageNoGlobalIds = pageIds.filter((elemento) => !globalPageIds.includes(elemento));

			const callbackPages = async () => pages.bulkDelete(pageNoGlobalIds);
			const callbackGlobalPages = async () => sites.removePageBulk(currentSiteInfo.id, globalPageIds);

			let errors = 0;

			if (pageNoGlobalIds.length > 0) {
				const result = await handleRequest(callbackPages, responsePageActions, [])(dispatch);
				if (!result) {
					errors = errors + 1;
				}
			}

			if (globalPageIds.length > 0) {
				const result = await handleRequest(callbackGlobalPages, responseGlobalPageActions, [])(dispatch);
				if (!result) {
					errors = errors + 1;
				}
			}

			return !(errors > 0);
		} catch (e) {
			console.log(e);
			return false;
		}
	};
}

function resetCurrentSiteErrorPages(): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			dispatch(setCurrentSiteErrorPages([]));
		} catch (e) {
			console.log(e);
		}
	};
}

function setListConfig(config: ISiteListConfig): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			localStorage.setItem("sitesConfig", JSON.stringify(config));
			dispatch(setConfig(config));
		} catch (e) {
			console.log(e);
		}
	};
}

function updateCurrentSearch(query: string): (dispatch: Dispatch) => Promise<void> {
	return async (dispatch) => {
		try {
			dispatch(setCurrentSearch(query));
		} catch (e) {
			console.log(e);
		}
	};
}

export {
	setCurrentSiteInfo,
	setCurrentSitePages,
	setFilter,
	setTotalItems,
	setCurrentSiteLanguages,
	getSites,
	getSitesByLang,
	setSiteInfo,
	getFilteredContent,
	getSitePages,
	getAllSitePages,
	getSiteLanguages,
	deleteSite,
	publishSite,
	publishSitesBulk,
	unpublishSite,
	unpublishSitesBulk,
	saveSettings,
	getGlobalPages,
	setSavedSiteInfo,
	saveCurrentSiteInfo,
	importPageFromGlobal,
	removePageFromSite,
	removeUsersBulk,
	setCurrentSiteErrorPages,
	resetCurrentSiteErrorPages,
	setContentFilters,
	deleteAndRemoveFromSiteBulk,
	setListConfig,
	getSite,
	updateCurrentSearch,
	setIsSitesLoading,
	getAllSites,
};
