import { Client } from '@vulog/aima-client';

import { z } from 'zod';

import { RefundableAmount } from './types';

const schema = z.string().trim().min(1);

export const getRefundableAmount = async (client: Client, invoiceId: string): Promise<RefundableAmount> => {
    const result = schema.safeParse(invoiceId);
    if (!result.success) {
        throw new TypeError('Invalid args', {
            cause: result.error.issues,
        });
    }

    return client
        .get<RefundableAmount>(
            `/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${invoiceId}/refundableAmount`
        )
        .then(({ data }) => data);
};
