import { useEffect, useState } from 'react';
import ScanToReturn from './scan-to-return';
import useBorrowBook from './use-borrow-book';
import React from 'react';
import { PHXClientQueryV3, PHXFuncGetLoggedInfo, PHXGetCurrentYearWithTermV3, PHXModal, PHXSkeleton, } from '../../../../..';
import useBookState from './use-book-state';
import { processBorrowBook, triggerMessage } from '../helper';
import { getWarehouseIdByEducationalLevelCodeQuery } from '../query';
import { FORM_TYPE } from '../../../constant';
export default function ModalReturnBook({ educationalCodes, route, setShow, show, }) {
    var _a, _b;
    const userInfo = PHXFuncGetLoggedInfo();
    const { listBookState } = useBookState();
    const [warehouseId, setWarehouseId] = useState(null);
    const [initLoading, setInitLoading] = useState(false);
    const { handleReturnBook } = useBorrowBook();
    const [listBookReturn, setListBookReturn] = useState([]);
    const [showModalConfirm, setShowModalConfirm] = useState(false);
    const [returnLoading, setReturnLoading] = useState(false);
    const schoolYearInfo = PHXGetCurrentYearWithTermV3();
    const handleReturn = async () => {
        const listBorrowBookLength = listBookReturn.length;
        if (listBorrowBookLength === 0)
            return;
        setReturnLoading(true);
        try {
            const { inBookshelfBookArray, inWarehouseBookArray, returnAllTicketIdArray, returnBorrowBook } = processBorrowBook(listBookReturn, listBookState);
            await handleReturnBook(inBookshelfBookArray, inWarehouseBookArray, returnBorrowBook, returnAllTicketIdArray);
            triggerMessage({
                type: FORM_TYPE.CUSTOM,
                message: 'Trả sách thành công',
            });
            setListBookReturn([]);
        }
        catch (e) {
            console.log(e);
            triggerMessage({
                type: FORM_TYPE.CUSTOM,
                message: 'Đã xảy ra lỗi. Vui lòng thử lại',
                isFailure: true,
            });
        }
        finally {
            setReturnLoading(false);
            setShowModalConfirm(false);
        }
    };
    function onHide() {
        setShow(false);
        setShowModalConfirm(false);
        setListBookReturn([]);
    }
    async function getInitData() {
        var _a;
        setInitLoading(true);
        try {
            const { data: { library_warehouse_educational_level }, } = await PHXClientQueryV3({
                query: getWarehouseIdByEducationalLevelCodeQuery,
                variables: {
                    educationalLevelCode: educationalCodes[0],
                    schoolId: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id,
                },
            });
            setWarehouseId((_a = library_warehouse_educational_level === null || library_warehouse_educational_level === void 0 ? void 0 : library_warehouse_educational_level[0]) === null || _a === void 0 ? void 0 : _a.warehouse_id);
        }
        catch (e) {
            console.log(e);
        }
        finally {
            setInitLoading(false);
        }
    }
    useEffect(() => {
        if (show) {
            getInitData();
        }
    }, [show]);
    return (React.createElement(PHXModal, { key: 'scanModal', customWidth: 'max-w-[calc(100vw-2rem)] xl:max-w-[70rem]', disableOutsideClick: true, disableSubmit: listBookReturn.length === 0, headerClassName: 'bg-white', inCard: false, onHide: onHide, onSubmit: () => {
            setShowModalConfirm(true);
        }, primaryActionText: 'Tr\u1EA3 t\u1EA5t c\u1EA3', show: show, title: 'Phi\u1EBFu tr\u1EA3 s\u00E1ch', type: 'no-padding' }, !initLoading ? (React.createElement(React.Fragment, null,
        React.createElement(PHXModal, { key: 'confirmModal', disableCloseButton: returnLoading, inCard: false, onHide: () => {
                setShowModalConfirm(false);
            }, onSubmit: handleReturn, primaryActionText: 'Tr\u1EA3 s\u00E1ch', primaryLoading: returnLoading, show: showModalConfirm, title: 'X\u00E1c nh\u1EADn tr\u1EA3 s\u00E1ch' },
            React.createElement("p", { className: '  text-xs text-gray-600' }, "B\u1EA1n c\u00F3 ch\u1EAFc ch\u1EAFn mu\u1ED1n tr\u1EA3 t\u1EA5t c\u1EA3 c\u00E1c s\u00E1ch \u0111\u00E3 ch\u1ECDn kh\u00F4ng? Sau khi x\u00E1c nh\u1EADn, t\u1EA5t c\u1EA3 s\u00E1ch s\u1EBD \u0111\u01B0\u1EE3c c\u1EADp nh\u1EADt v\u1EC1 tr\u1EA1ng th\u00E1i m\u1EB7c \u0111\u1ECBnh v\u00E0 c\u00E1c phi\u1EBFu m\u01B0\u1EE3n s\u00E1ch s\u1EBD chuy\u1EC3n sang tr\u1EA1ng th\u00E1i \u0110\u00E3 tr\u1EA3 v\u00E0 kh\u00F4ng th\u1EC3 thay \u0111\u1ED5i.")),
        warehouseId && (React.createElement(ScanToReturn, { data: listBookReturn, onChange: (data) => setListBookReturn(data), route: route, schoolYearId: (_b = (_a = schoolYearInfo === null || schoolYearInfo === void 0 ? void 0 : schoolYearInfo.current_school_year) === null || _a === void 0 ? void 0 : _a.schoolYear) === null || _b === void 0 ? void 0 : _b.id, warehouseId: warehouseId })))) : (React.createElement(PHXSkeleton, { type: 'content-modal' }))));
}
//# sourceMappingURL=modal-return-book.js.map