import {
  httpGetRequest,
  httpPostRequest,
  APIUrlGenerator,
} from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  LandingHeroResponse,
  UpdateLandingHeroRequest,
  LandingHeroData,
} from "./_models";

export const getLandingHero = async () => {
  const response = await httpGetRequest<any>(
    APIUrlGenerator(API_ROUTES.GET_LANDING_HERO)
  );
  const root = response.data;
  const setting = root?.setting ?? root;
  const data: LandingHeroData = setting?.data ??
    root?.data ?? { main: { alt_text: "", url: "", thumbnail: "" }, side: [] };
  return data;
};

export const updateLandingHero = async (data: LandingHeroData) => {
  const payload: UpdateLandingHeroRequest = { data };
  const response = await httpPostRequest<LandingHeroResponse>(
    APIUrlGenerator(API_ROUTES.UPDATE_LANDING_HERO),
    payload
  );
  return response.data.data;
};
