import apiClient from "./apiClient";
import {Order} from "../types/order";
import * as inspector from "inspector";
import {OrderStatus} from "../features/ordersFilter/ordersFilterSlice";
import {ShippingWarehouse} from "../types/shippingWarehouse";
import {City} from "../types/city";

const router = '/orders';

const ordersApi = {
    getOrders: async (page: number, orderBy: string, desc: boolean, shipmentDate: string, status: number, shipmentWarehouseId: number): Promise<Order[]> => {
        const params = {
            page: page,
            orderBy: orderBy,
            desc: Number(desc), // Преобразование boolean в Number (0 или 1)
            status: status,
            shipmentDate: shipmentDate,
            shipmentWarehouseId: shipmentWarehouseId
        };
        let response = await apiClient.get(`${router}/getOrders`, {params})
        return response.data;
    },
    getOrdersByProduct: async (params: {
        productId: number,
        orderBy: "createdOn" | "shipmentDate",
        desc: boolean,
        status: OrderStatus,
        shipmentDate: string,
        shippingWarehouse: ShippingWarehouse,
        city: City
    }): Promise<Order[]> => {
        let response = await apiClient.get(`${router}/getByProductId`, {
            params: {
                ...params,
                shippingWarehouse: params.shippingWarehouse.id,
                city: params.city.id
            }
        });
        return response.data;
    },
    getOrderById: async (orderId: number): Promise<Order> => {
        let response = await apiClient.get(`${router}/getOrderById?orderId=${orderId}`);
        return response.data;
    }
}

export default ordersApi;