"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEditRecordModal = exports.useEditRecordModalWithCustomMutationHook = void 0;
const react_1 = require("@chakra-ui/react");
const react_hook_form_1 = require("react-hook-form");
const react_query_1 = require("@tanstack/react-query");
const useEditRecordModalWithCustomMutationHook = ({ title, formKeys, defaultValues, onFormSubmit: _onFormSubmit = () => __awaiter(void 0, void 0, void 0, function* () { }), useMutationHook, disclosureProps, }) => {
    const disclosure = (0, react_1.useDisclosure)(disclosureProps);
    const form = (0, react_hook_form_1.useForm)({ defaultValues });
    const onSuccess = () => {
        console.log("success");
    };
    const onError = () => {
        console.log("error");
    };
    const mutation = useMutationHook({ onSuccess, onError });
    const onFormSubmit = (data) => __awaiter(void 0, void 0, void 0, function* () {
        yield _onFormSubmit(mutation, data);
        // await mutationResponse.mutateAsync(data);
        disclosure.onClose();
    });
    const onFormError = (errors) => {
        // eslint-disable-next-line no-console -- error logging
        console.error(errors);
    };
    return {
        title,
        formKeys,
        mutationResponse: mutation,
        form,
        onFormSubmit,
        onFormError,
        disclosure,
    };
};
exports.useEditRecordModalWithCustomMutationHook = useEditRecordModalWithCustomMutationHook;
/**
 * Generic abstraction of the useMutation required to run the CreateRecordModal
 * A function can be passed into the mutationFn to run the record creation process on save
 * @returns
 */
const useEditRecordModal = ({ title, formKeys, mutationFn, defaultValues, }) => {
    return (0, exports.useEditRecordModalWithCustomMutationHook)({
        title,
        formKeys,
        defaultValues,
        useMutationHook: ({ onSuccess, onError }) => (0, react_query_1.useMutation)({
            mutationFn,
            onSuccess,
            onError,
        }),
        onFormSubmit: (mutation, data) => __awaiter(void 0, void 0, void 0, function* () {
            yield mutation.mutateAsync(data);
        }),
    });
};
exports.useEditRecordModal = useEditRecordModal;
//# sourceMappingURL=use-edit-record-modal.js.map