import {
  combineReducers,
  configureStore,
  createAction,
} from "@reduxjs/toolkit";
import token, { actions as tokenActions, selectToken } from "./token";
import userId, { actions as userIdActions, selectUserId } from "./userId";
import session, { actions as sessionActions, selectSession } from "./session";
import utm, { actions as utmActions, selectUTM } from "./utm";
import questionnaire, {
  actions as questionnaireActions,
  selectQuestionnaire,
} from "./questionnaire";
import palmistryV1Answers, {
  actions as palmistryV1AnswersActions,
  selectPalmistryV1Answers,
} from "./palmistryV1Answers";
import compatibilityV2Answers, {
  actions as compatibilityV2AnswersActions,
  selectCompatibilityV2Answers,
} from "./compatibilityV2Answers";
import compatibilityV3Answers, {
  actions as compatibilityV3AnswersActions,
  selectCompatibilityV3Answers,
} from "./compatibilityV3Answers";
import compatibilityV4Answers, {
  actions as compatibilityV4AnswersActions,
  selectCompatibilityV4Answers,
} from "./compatibilityV4Answers";
import user, { actions as userActions, selectUser } from "./user";
import form, {
  actions as formActions,
  selectors as formSelectors,
} from "./form";
import aura, { actions as auraActions } from "./aura";
import paywalls, { actions as paywallsActions } from "./paywalls";
import siteConfig, {
  selectDarkTheme,
  selectHome,
  selectOpenAiToken,
  selectTrialChoiceArrowOptions,
  actions as siteConfigActions,
} from "./siteConfig";
import onboardingConfig, {
  selectOnboarding,
  selectOnboardingBreath,
  selectOnboardingCompatibility,
  selectOnboardingHome,
  selectOnboardingNavbarFooter,
  actions as onboardingConfigActions,
} from "./onboarding";
import payment, {
  actions as paymentActions,
  selectActiveProduct,
} from "./payment";
import status, { actions as userStatusActions, selectStatus } from "./status";
import compatibility, {
  actions as compatibilityActions,
  selectSelfName,
} from "./compatibility";
import userConfig, {
  actions as userConfigActions,
  selectUserDeviceType,
  selectIsShowTryApp,
  selectFeature,
  selectAuthCode,
  selectIsForceShortPath,
  selectDateOfPaymentChatMike,
  selectCurrency,
} from "./userConfig";
import compatibilities, {
  actions as compatibilitiesActions,
  selectCompatibilities,
} from "./compatibilities";
import userCallbacks, {
  actions as userCallbacksActions,
} from "./userCallbacks";
import { loadStore, backupStore } from "./storageHelper";
import { selectAuraCoordinates } from "./aura";
import { selectSelectedPrice } from "./payment";
import { selectRightUser, selectCategoryId } from "./compatibility";
import {
  selectUserCallbacksDescription,
  selectUserCallbacksPrevStat,
} from "./userCallbacks";
import palmistry, {
  actions as palmistryActions,
  selectPalmistryFingers,
  selectPalmistryFromRedesign,
  selectPalmistryLines,
  selectPalmistryPhoto,
} from "./palmistry";
import compatibilityV2, {
  actions as compatibilityV2Actions,
  selectCompatibilityV2Fingers,
  selectCompatibilityV2FromRedesign,
  selectCompatibilityV2Lines,
  selectCompatibilityV2Photo,
} from "./compatibilityV2";
import retainingFunnel, {
  actions as retainingFunnelActions,
  selectRetainingFunnel,
  selectCancellingSubscriptionId,
} from "./retainingFunnel";
import compatibilityV3, {
  actions as compatibilityV3Actions,
  selectCompatibilityV3Fingers,
  selectCompatibilityV3FromRedesign,
  selectCompatibilityV3Lines,
  selectCompatibilityV3Photo,
} from "./compatibilityV3";
import compatibilityV4, {
  actions as compatibilityV4Actions,
  selectCompatibilityV4Fingers,
  selectCompatibilityV4FromRedesign,
  selectCompatibilityV4Lines,
  selectCompatibilityV4Photo,
} from "./compatibilityV4";
import { selectPaywallsIsMustUpdate, selectPaywalls } from "./paywalls";
import privacyPolicy, {
  actions as privacyPolicyActions,
  selectPrivacyPolicy,
} from "./privacyPolicy";
import personalVideo, {
  actions as personalVideoActions,
  selectPersonalVideo,
} from "./personalVideo";
import chats, {
  actions as chatsActions,
  selectAnswers,
  selectCurrentStep,
} from "./chats";
import chat, {
  actions as chatActions,
  selectCurrentAssistant,
  selectCurrentChatId,
  selectDateEndChatting,
  selectIsAutoRefill,
  selectIsPayedFirstPurchase,
} from "./chat";

const preloadedState = loadStore();
export const actions = {
  token: tokenActions,
  userId: userIdActions,
  session: sessionActions,
  utm: utmActions,
  user: userActions,
  form: formActions,
  status: userStatusActions,
  aura: auraActions,
  paywalls: paywallsActions,
  siteConfig: siteConfigActions,
  compatibility: compatibilityActions,
  compatibilities: compatibilitiesActions,
  payment: paymentActions,
  userCallbacks: userCallbacksActions,
  onboardingConfig: onboardingConfigActions,
  questionnaire: questionnaireActions,
  palmistryV1Answers: palmistryV1AnswersActions,
  compatibilityV2Answers: compatibilityV2AnswersActions,
  compatibilityV3Answers: compatibilityV3AnswersActions,
  compatibilityV4Answers: compatibilityV4AnswersActions,
  userConfig: userConfigActions,
  palmistry: palmistryActions,
  compatibilityV2: compatibilityV2Actions,
  compatibilityV3: compatibilityV3Actions,
  compatibilityV4: compatibilityV4Actions,
  privacyPolicy: privacyPolicyActions,
  personalVideo: personalVideoActions,
  chats: chatsActions,
  chat: chatActions,
  retainingFunnel: retainingFunnelActions,
  reset: createAction("reset"),
};
export const selectors = {
  selectToken,
  selectUserId,
  selectSession,
  selectUTM,
  selectUser,
  selectStatus,
  selectAuraCoordinates,
  selectRightUser,
  selectSelfName,
  selectCategoryId,
  selectSelectedPrice,
  selectActiveProduct,
  selectUserCallbacksDescription,
  selectUserCallbacksPrevStat,
  selectHome,
  selectOnboarding,
  selectOnboardingHome,
  selectOnboardingCompatibility,
  selectOnboardingBreath,
  selectOnboardingNavbarFooter,
  selectCompatibilities,
  selectQuestionnaire,
  selectPalmistryV1Answers,
  selectUserDeviceType,
  selectIsShowTryApp,
  selectFeature,
  selectAuthCode,
  selectIsForceShortPath,
  selectDateOfPaymentChatMike,
  selectOpenAiToken,
  selectTrialChoiceArrowOptions,
  selectDarkTheme,
  selectPalmistryLines,
  selectPalmistryPhoto,
  selectPalmistryFingers,
  selectPaywalls,
  selectPaywallsIsMustUpdate,
  selectPrivacyPolicy,
  selectPersonalVideo,
  selectCurrency,
  selectPalmistryFromRedesign,
  selectCurrentStep,
  selectAnswers,
  selectCurrentAssistant,
  selectCurrentChatId,
  selectIsAutoRefill,
  selectIsPayedFirstPurchase,
  selectDateEndChatting,
  selectCompatibilityV2Fingers,
  selectCompatibilityV2FromRedesign,
  selectCompatibilityV2Lines,
  selectCompatibilityV2Photo,
  selectCompatibilityV2Answers,
  selectCompatibilityV3Fingers,
  selectCompatibilityV3FromRedesign,
  selectCompatibilityV3Lines,
  selectCompatibilityV3Photo,
  selectCompatibilityV3Answers,
  selectCompatibilityV4Fingers,
  selectCompatibilityV4FromRedesign,
  selectCompatibilityV4Lines,
  selectCompatibilityV4Photo,
  selectCompatibilityV4Answers,
  selectRetainingFunnel,
  selectCancellingSubscriptionId,
  ...formSelectors,
};

export const reducer = combineReducers({
  utm,
  session,
  token,
  userId,
  user,
  form,
  status,
  aura,
  payment,
  compatibility,
  compatibilities,
  userCallbacks,
  siteConfig,
  onboardingConfig,
  questionnaire,
  palmistryV1Answers,
  userConfig,
  palmistry,
  compatibilityV2,
  compatibilityV2Answers,
  compatibilityV3,
  compatibilityV3Answers,
  compatibilityV4,
  compatibilityV4Answers,
  paywalls,
  privacyPolicy,
  personalVideo,
  chats,
  chat,
  retainingFunnel,
});

export type RootState = ReturnType<typeof reducer>;
export const store = configureStore({
  reducer,
  preloadedState,
  devTools: import.meta.env.DEV,
});
export type AppDispatch = typeof store.dispatch;
export type StoreType = typeof store;
export const unsubscribe = backupStore(store);
