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

import {initialState} from './index.js';
import {StorageKeys} from "../../globalConstants";

/**
 * 获取store中的指定片段
 * @param state
 */
const selectDomain = (state) => state?.app || initialState;

/**
 * 主页标签
 */
export const selectTabs = createSelector(
  [selectDomain],
  appState => appState.tabs
);

/**
 * 当前路由
 */
export const selectPathname = createSelector(
  [selectDomain],
  appState => appState.pathname
);

/**
 * 切割出一个登录中的状态，登录事件触发时使用
 */
export const selectLoginLoading = createSelector(
  [selectDomain],
  appState => appState.loginLoading
);

/**
 * 切割出一个用户登录信息，登录事件触发时使用
 */
export const selectCurrentUser = createSelector(
  [selectDomain],
  appState => appState.loggedInUser || JSON.parse(localStorage.getItem(StorageKeys.LoggedInUser))
);

/**
 * 切割出一个导游的名称
 */
export const selectTourName = createSelector(
  [selectDomain],
  appState => appState.tourName
);

/**
 * 切割出一个导游的执行状态
 */
export const selectTouring = createSelector(
  [selectDomain],
  appState => appState.touring
);
