import { URLS } from "../api/apiConfig";
import { TAxios } from "../api/TAxios";
import { types } from "./actionTypes";
import { changeAppStatus } from "./commonActions";

// On Sale 조회 IF-LGSP-086 (Home)
export const getHomeOnSaleInfo = (props) => (dispatch, getState) => {
  const { categoryIncFlag, homeSaleInfosIncFlag, lgCatCd, saleInfosIncFlag } =
    props;

  const onSuccess = (response) => {
    console.log("getHomeOnSaleInfo onSuccess ", response.data);

    dispatch({
      type: types.GET_HOME_ON_SALE_INFO,
      payload: {
        data: response.data.data,
      },
    });
    dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
  };

  const onFail = (error) => {
    console.error("getHomeOnSaleInfo onFail", error);
    dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_ON_SALE_INFO,
    { categoryIncFlag, homeSaleInfosIncFlag, lgCatCd, saleInfosIncFlag },
    {},
    onSuccess,
    onFail
  );
};

// On Sale 조회 IF-LGSP-086 (OnSale)
export const getOnSaleInfo = (props) => (dispatch, getState) => {
  const { categoryIncFlag, lgCatCd, saleInfosIncFlag } = props;

  dispatch(changeAppStatus({ showLoadingPanel: { show: true, type: "wait" } }));

  const onSuccess = (response) => {
    console.log("getOnSaleInfo onSuccess ", response.data);
    dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
    dispatch({
      type: types.GET_ON_SALE_INFO,
      payload: {
        data: response.data.data,
      },
    });
  };

  const onFail = (error) => {
    console.error("getOnSaleInfo onFail", error);
    dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_ON_SALE_INFO,
    { categoryIncFlag, lgCatCd, saleInfosIncFlag },
    {},
    onSuccess,
    onFail
  );
};

export const copyCategoryInfos = (categoryInfos) => ({
  type: types.COPY_CATEGORY_INFO,
  payload: categoryInfos,
});
