import { UPDATE_PRELOAD, SYNC_SYSTEMINFO } from "@/constants/common";

// 初始数据
const INITIAL_STATE = {
  cityList: [],
  miniappInfo: {},
  orgInfo: {},
  userInfo: {},
  settings: [],
  systemInfo: {},

  // 当前推荐人信息
  recommender: {},
  // 当前推荐置业顾问信息
  consultant: {},
  // 开屏广告 - 如果当前城市已展示过, 那么会把 cityId 放入数组
  firstScreen: [],
};

export default function(state = INITIAL_STATE, action) {
  switch (action.type) {
    case UPDATE_PRELOAD: {
      return {
        ...state,
        ...action.payload
      };
    }
    case SYNC_SYSTEMINFO: {
      return {
        ...state,
        systemInfo: action.payload
      };
    }
    case 'SYNC_RECOMMENDER': {
      return {
        ...state,
        recommender: action.payload
      };
    }
    case 'SYNC_CONSULTANT': {
      return {
        ...state,
        consultant: action.payload
      };
    }
    case 'TOGGLE_FIRSTSCREEN': {
      const { cityId } = action.payload
      let cityState = state.firstScreen.filter(x => x.cityId === cityId)[0]

      if (!cityState) {
        return {
          ...state,
          firstScreen: state.firstScreen.concat({ cityId, showed: true, visible: true })
        };
      } else if (cityState.showed && cityState.visible) {
        const firstScreen = state.firstScreen.map(x => x.cityId === cityId ? { ...x, visible: false } : x)
        return {
          ...state,
          firstScreen,
        };
      }

      return state;
    }

    default:
      return state;
  }
}
