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

const initialState = {
  productCouponInfoData: {},
  productCouponSearchData: {},
  couponDownloadSuccess: null,
};

export const couponReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_PRODUCT_COUPON_INFO: {
      return {
        ...state,
        productCouponInfoData: action.payload.couponInfo,
      };
    }

    case types.GET_PRODUCT_COUPON_SEARCH: {
      if (action.payload && Array.isArray(action.payload.couponInfo)) {
        const partnerCoupon = action.payload.couponInfo.filter(
          (item) => item.cpnTpNm === "Partner"
        );

        const shoptimeCoupon = action.payload.couponInfo.filter(
          (item) => item.cpnTpNm === "ShopTime"
        );

        return {
          ...state,
          productCouponSearchData: {
            partnerCoupon,
            shoptimeCoupon: shoptimeCoupon,
          },
        };
      }
    }
    case types.CLEAR_COUPON_INFO: {
      return {
        ...state,
        productCouponSearchData: {},
      };
    }
    case types.GET_PRODUCT_COUPON_DOWNLOAD: {
      return {
        ...state,
        couponDownloadSuccess: action.retCode,
      };
    }
    case types.CLEAR_GET_PRODUCT_COUPON_DOWNLOAD: {
      return {
        ...state,
        couponDownloadSuccess: null,
      };
    }

    default:
      return state;
  }
};
