import { types } from "../actions/actionTypes";
import { CATEGORY_DATA_MAX_RESULTS_LIMIT } from "../utils/Config";

const initialState = {
  subCategoryData: {},
  top20ShowData: {},
  productData: null,
  fullVideolgCatCd: "",
  fullVideoData: {},
  showDetailInfo: [],
  showNowInfo: null,
  featuredShowsInfos: {},
  categoryParams: {
    lgCatCd: null,
    patnrIdList: null,
    tabType: null,
    filterType: null,
    pageSize: CATEGORY_DATA_MAX_RESULTS_LIMIT,
  },
};

export const mainReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_SUB_CATEGORY: {
      const dataType = action.payload.categoryShowInfos
        ? "categoryShowInfos"
        : action.payload.categoryItemInfos
          ? "categoryItemInfos"
          : null;

      if (!dataType) {
        return {
          ...state,
          subCategoryData: {
            ...state.subCategoryData,
            categoryShowInfos: { subCatShowList: [], total: 0 },
            categoryItemInfos: { subCatItemList: [], total: 0 },
          },
          categoryFilterCd: action.payload.categoryFilterCd,
          topShowInfo: action.payload.topShowInfo,
          partnerInfos: action.payload.partnerInfos,
          categoryParams: action.categoryParams,
        };
      }

      const subKey = action.payload[dataType]?.subCatShowList
        ? "subCatShowList"
        : action.payload[dataType]?.subCatItemList
          ? "subCatItemList"
          : null;

      if (!subKey) {
        return {
          ...state,
          subCategoryData: {
            ...state.subCategoryData,
            [dataType]: {
              subCatShowList: [],
              subCatItemList: [],
              total: 0,
            },
          },
          categoryFilterCd: action.payload.categoryFilterCd,
          topShowInfo: action.payload.topShowInfo,
          partnerInfos: action.payload.partnerInfos,
          categoryParams: action.categoryParams,
        };
      }

      const newResults = action.payload[dataType][subKey] || [];
      const total = action.payload[dataType].total || 0;

      if (action.append) {
        const existingData = state.subCategoryData[dataType] || {
          [subKey]: [],
          total: 0,
        };
        const existingSubData = existingData[subKey] || [];

        for (let i = 0; i < newResults.length; i++) {
          existingSubData[action.startIndex + i] = newResults[i];
        }
        return {
          ...state,
          subCategoryData: {
            ...state.subCategoryData,
            [dataType]: {
              ...existingData,
              [subKey]: existingSubData,
              total,
            },
          },
          categoryFilterCd: action.payload.categoryFilterCd,
          topShowInfo: action.payload.topShowInfo,
          partnerInfos: action.payload.partnerInfos,
        };
      } else {
        return {
          ...state,
          subCategoryData: {
            ...state.subCategoryData,
            [dataType]: {
              ...action.payload[dataType],
              [subKey]: newResults,
            },
          },
          categoryFilterCd: action.payload.categoryFilterCd,
          topShowInfo: action.payload.topShowInfo,
          partnerInfos: action.payload.partnerInfos,
          categoryParams: action.categoryParams,
        };
      }
    }

    case types.GET_TOP_20_SHOW:
      return {
        ...state,
        top20ShowData: action.payload,
      };
    case types.GET_PRODUCT_DETAIL: {
      const data = action.payload.product[0];
      const productGroup = data?.grPrdtFlag;
      const images = data.imgUrls600;
      const media = data.prdtMediaUrl;
      return {
        ...state,
        productData: data,
        productGroupYn: productGroup,
        productImages: { images, media },
      };
    }
    case types.CLEAR_PRODUCT_DETAIL: {
      return {
        ...state,
        productData: null,
      };
    }
    case types.GET_YOUMAYLIKE: {
      const data = action.payload;
      return {
        ...state,
        youmaylikeData: data.youmaylike,
      };
    }
    case types.GET_HOME_FULL_VIDEO_INFO: {
      const { data, lgCatCd } = action.payload;
      return {
        ...state,
        fullVideolgCatCd: lgCatCd,
        youmaylikeInfos: data.youmaylikeInfos,
        liveChannelInfos: data.liveChannelInfos,
        featuredShowsInfos: data.featuredShowsInfos,
        fullVideoData: action.payload,
      };
    }

    case types.GET_MAIN_LIVE_SHOW_NOW_PRODUCT: {
      const data = action.payload;

      return {
        ...state,
        showNowInfo: action.payload,
      };
    }

    case types.GET_MAIN_CATEGORY_SHOW_DETAIL: {
      return {
        ...state,
        showDetailInfo: action.payload.showInfos,
      };
    }

    case types.GET_MAIN_LIVE_SHOW: {
      return {
        ...state,
        liveShowInfos: action.payload.showInfos,
      };
    }
    case types.CLEAR_SHOPNOW_INFO:
      return {
        ...state,
        fullVideolgCatCd: "",
        showDetailInfo: [],
        showNowInfo: null,
        fullVideoData: null,
        featuredShowsInfos: null,
        youmaylikeInfos: null,
      };

    case types.CLEAR_SUB_CATEGORY:
      return {
        ...state,
        subCategoryData: {},
      };

    default:
      return state;
  }
};
