import { types } from "../actions/actionTypes";

const initialState = {
  brandInfoData: {
    data: {},
  },

  brandLayoutInfoData: {
    data: {},
  },

  brandLiveChannelInfoData: {
    data: {},
  },

  brandTsvInfoData: {
    data: {},
  },

  brandRecommendedShowInfoData: {
    data: {},
  },

  brandCreatorsInfoData: {
    data: {},
  },

  brandSeriesInfoData: {
    data: {},
  },

  brandCategoryInfoData: {
    data: {},
  },

  brandBestSellerData: {
    data: {},
  },

  brandShowroomData: {
    data: {},
  },

  brandRecentlyAiredData: {
    data: {},
  },
};

export const brandReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_BRAND_LIST:
      return {
        ...state,
        brandInfoData: action.payload,
      };

    case types.GET_BRAND_LAYOUT_INFO:
      return {
        ...state,
        brandLayoutInfoData: action.payload,
      };

    case types.GET_BRAND_LIVE_CHANNEL_INFO:
      return {
        ...state,
        brandLiveChannelInfoData: action.payload,
      };

    case types.GET_BRAND_CHAN_INFO: {
      const brandChanInfo =
        action.payload && action.payload.data?.brandChanInfo;
      return {
        ...state,
        brandLiveChannelInfoData: {
          ...state.brandLiveChannelInfoData,
          data: {
            ...state.brandLiveChannelInfoData.data,
            brandChanInfo,
          },
        },
      };
    }

    case types.GET_BRAND_TSV_INFO:
      return {
        ...state,
        brandTsvInfoData: action.payload,
      };

    case types.GET_BRAND_RECOMMENDED_SHOW_INFO: {
      return {
        ...state,
        brandRecommendedShowInfoData: action.payload,
      };
    }

    case types.GET_BRAND_CREATORS_INFO: {
      const barndCreatorsShowInfo =
        action.payload &&
        action.payload.data.barndCreatorsShowInfo?.slice(0, 10);
      const data = { ...action.payload.data, barndCreatorsShowInfo };
      return {
        ...state,
        brandCreatorsInfoData: {
          ...action.payload,
          data,
        },
      };
    }

    case types.GET_BRAND_SERIES_INFO: {
      return {
        ...state,
        brandSeriesInfoData: action.payload,
      };
    }

    case types.GET_BRAND_CATEGORY_INFO: {
      return {
        ...state,
        brandCategoryInfoData: action.payload,
      };
    }

    case types.GET_BRAND_CATEGORY_PRODUCT_INFO: {
      const brandCategoryProductInfo =
        action.payload && action.payload.data.brandCategoryProductInfo;
      return {
        ...state,
        brandCategoryInfoData: {
          ...state.brandCategoryInfoData,
          data: {
            ...state.brandCategoryInfoData.data,
            brandCategoryProductInfo,
          },
        },
      };
    }

    case types.GET_BRAND_BEST_SELLER:
      return {
        ...state,
        brandBestSellerData: action.payload,
      };

    case types.GET_BRAND_SHOWROOM:
      return {
        ...state,
        brandShowroomData: action.payload,
      };

    case types.GET_BRAND_RECENTLY_AIRED:
      return {
        ...state,
        brandRecentlyAiredData: action.payload,
      };

    case types.SET_BRAND_LIVE_CHANNEL_UPCOMING:
      return {
        ...state,
        brandLiveChannelInfoData: action.payload,
      };

    case types.SET_BRAND_CHAN_INFO:
      return {
        ...state,
        brandLiveChannelInfoData: action.payload,
      };

    case types.RESET_BRAND_STATE_EXCEPT_BRAND_INFO: {
      const { brandInfoData, ...otherInitialState } = initialState;
      return {
        ...state,
        ...otherInitialState,
      };
    }

    case types.RESET_BRAND_LAYOUT_INFO: {
      return {
        ...state,
        brandLayoutInfoData: {
          data: {},
        },
      };
    }

    default:
      return state;
  }
};
