import { ContextModalProps } from "@mantine/modals";
import { DepartmentSchema, DepartmentSectionBaseSchema, DepartmentSectionSchema } from "../../../../../client";
import { DepartmentCrud } from "../hooks/useDepartmentCrud.tsx";
import { DepartmentSectionCrud } from "../hooks/useDepartmentSectionCrud.tsx";
import DepartmentForm from "../components/DepartmentForm.tsx";
import DepartmentSectionForm from "../components/DepartmentSectionForm.tsx";

type Props = {
    departmentsCrud: DepartmentCrud;
    departmentSectionsCrud: DepartmentSectionCrud;
    element?: DepartmentSchema | DepartmentSectionSchema | DepartmentSectionBaseSchema;
    isDepartmentSection: boolean;
    allDepartments: DepartmentSchema[];
}

const DepartmentModal = ({
                             context,
                             id,
                             innerProps,
                         }: ContextModalProps<Props>) => {
    const closeModal = () => {
        context.closeContextModal(id);
    };

    if (innerProps.isDepartmentSection) {
        return (
            <DepartmentSectionForm
                departmentSectionsCrud={innerProps.departmentSectionsCrud}
                allDepartments={innerProps.allDepartments}
                departmentSection={innerProps.element as DepartmentSectionSchema}
                closeModal={closeModal}
            />
        );
    }

    return (
        <DepartmentForm
            departmentsCrud={innerProps.departmentsCrud}
            department={innerProps.element as DepartmentSchema}
            closeModal={closeModal}
        />
    );
};

export default DepartmentModal;
