import QueryString from "qs";
import { axiosGet } from "./actions";
import { apiReviews } from "./constants";

export const getReviews = async (props:any) => {
    const {locale = 'es',showOnLocations=false,showOnHome=false,showOnAboutUs=false,projectSlug="",otherFilterQs={}} = props
    try {
        const showOnHomeQs = showOnHome
        ? {
            showOnHome: true
          }
        : {};
          const showOnAboutUsQs = showOnAboutUs
        ? {
            showOnAboutUs: true
          }
        : {};
          const showOnLocationsQs = showOnLocations
        ? {
            showOnLocations: true
          }
        : {};
        const projectQs = projectSlug
        ? {
            project:{
              slug: projectSlug
            }
          }
        : {};
      const query = QueryString.stringify(
        {
          locale: locale,
          filters:{
            ...showOnHomeQs,
            ...showOnAboutUsQs, 
            ...projectQs,
            ...showOnLocationsQs,
            ...otherFilterQs,
          },
          pagination: {
            limit: -1
          },
          populate: {
            project: {
              fields: ['id', 'name', 'slug']
            },
            image: {
                fields: ['url', 'width', 'height']
              },
          }
        },
        {
          encodeValuesOnly: true
        }
      );
      let data = await axiosGet(apiReviews, query);

      return data || [];
    } catch (error) {
      console.log('error ', error);
      return [];
    }
  };