'use client';
import { PHXClientMutationV3, PHXClientQueryV3, PHXFuncGetLoggedInfo } from '../../../../..';
import { IN_BOOKSHELF_STATE_CODE, IN_WAREHOUSE_STATE_CODE, RETURNED_TICKET_CODE } from '../constants';
import { getTicketStateListQuery, returnBorrowBookMutation, returnBorrowTicketMutation } from '../mutation';
import useBookState from './use-book-state';
export default function useBorrowBook() {
    const { listBookState } = useBookState();
    const handleReturnBook = async (inBookshelfArrayId, inWarehouseArrayId, borrowBookId, returnTicketIdArray = []) => {
        var _a, _b, _c;
        const userInfo = PHXFuncGetLoggedInfo();
        try {
            await PHXClientMutationV3({
                query: returnBorrowBookMutation,
                variables: {
                    inWarehouseIdArray: inWarehouseArrayId,
                    inBookshelfIdArray: inBookshelfArrayId,
                    borrowBookIdArray: borrowBookId,
                    inWarehouseStateId: (_a = listBookState.find((item) => item.code === IN_WAREHOUSE_STATE_CODE)) === null || _a === void 0 ? void 0 : _a.id,
                    inBookshelfStateId: (_b = listBookState.find((item) => item.code === IN_BOOKSHELF_STATE_CODE)) === null || _b === void 0 ? void 0 : _b.id,
                },
            });
            if ((returnTicketIdArray === null || returnTicketIdArray === void 0 ? void 0 : returnTicketIdArray.length) > 0) {
                const { data: { library_ticket_state }, } = await PHXClientQueryV3({
                    query: getTicketStateListQuery,
                    variables: { school_id: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id },
                    isDelay: false,
                });
                const returnStateId = (_c = library_ticket_state.find((item) => item.code === RETURNED_TICKET_CODE)) === null || _c === void 0 ? void 0 : _c.id;
                await PHXClientMutationV3({
                    query: returnBorrowTicketMutation,
                    variables: {
                        idArray: returnTicketIdArray,
                        returnStateId,
                    },
                });
            }
        }
        catch (e) {
            console.log(e);
        }
    };
    return { handleReturnBook };
}
//# sourceMappingURL=use-borrow-book.js.map