import { CrudClient } from "../infra/crud-client"
import type { ApiClientOptions } from "../infra/client"
import type { ApiPath } from "../infra/types"

export const PURCHASE_ORDER_ROUTES = {
    INDEX: "/api/purchase-orders",
    BY_ID: "/api/purchase-orders/{id}",
} as const satisfies Record<string, ApiPath>

export class PurchaseOrdersClient extends CrudClient<
    typeof PURCHASE_ORDER_ROUTES.INDEX,
    typeof PURCHASE_ORDER_ROUTES.BY_ID
> {
    constructor(baseUrl?: string, defaultOptions?: ApiClientOptions) {
        super(baseUrl, defaultOptions, PURCHASE_ORDER_ROUTES.INDEX, PURCHASE_ORDER_ROUTES.BY_ID)
    }

    async getById(id: string) {
        const res=  await this.list({query: { id }})
        return { ...res, data: (res as any)?.data[0] ?? res }
    }
}
