import {
    CREATE_ORDER_URL,
    ORDERS_URL,
    PRODUCTS_AND_CATEGORIES_URL,
    RESTAURANT_DETAILS_URL,
} from "utils/constants";

import menuParser from "pages/menu/helper";
import { RestaurantDetails } from "utils/types/appTypes";
import { baseApi } from "./apiSlice";

export const branchApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getRestaurantDetails: builder.query<RestaurantDetails, string | void>({
      query: (restaurantId: string) =>
        `${RESTAURANT_DETAILS_URL}${restaurantId}`,
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    getMenu: builder.query<any, string | void>({
      query: (restaurantId: string) =>
        `${PRODUCTS_AND_CATEGORIES_URL}${restaurantId}`,
      transformResponse: (response: any) => {
        return menuParser(response);
      },
    }),
    getOrders: builder.query<any, void>({
      query: () => ({
        url: ORDERS_URL,
        method: "POST",
      }),
      transformResponse: (response: any) => {
        return response.result.data.orders;
      },
      providesTags: ["Orders"],
    }),
    createOrder: builder.mutation({
      query: (body: any) => ({
        url: CREATE_ORDER_URL,
        method: "POST",
        body,
      }),
      invalidatesTags: ["Orders"],
    }),
  }),
});
export const {
  useGetRestaurantDetailsQuery,
  useGetMenuQuery,
  useCreateOrderMutation,
  useGetOrdersQuery,
} = branchApi;
