import { useField, useForm } from "@tachybase/schema";
import { message } from "antd";
import { cloneDeep } from "lodash";
import { useTranslation } from "react-i18next";
import { useAPIClient } from "../../../api-client/index.mjs";
import { useActionContext } from "../../../schema-component/index.mjs";
import { useCollectionManager_deprecated } from "../../hooks/index.mjs";
import { useResourceActionContext } from "../../ResourceActionProvider.mjs";
const xlsxImportAction = ()=>{
    const form = useForm();
    const ctx = useActionContext();
    const field = useField();
    const api = useAPIClient();
    const { refresh } = useResourceActionContext();
    const { refreshCM } = useCollectionManager_deprecated();
    const { t } = useTranslation();
    return {
        async onClick () {
            field.data = field.data || {};
            field.data.loading = true;
            try {
                await form.submit();
                const values = cloneDeep(form.values);
                const { collectionData, ...collections } = values;
                if (!collections.autoCreateReverseField) delete collections.reverseField;
                delete collections.autoCreateReverseField;
                await api.resource('collections').create({
                    values: {
                        logging: true,
                        ...collections
                    }
                });
                api.resource("".concat(collections.name)).create({
                    values: collectionData
                });
                ctx.setVisible(false);
                null == refresh || refresh();
                await refreshCM();
                field.data.loading = false;
            } catch (error) {
                field.data.loading = false;
                message.error(t('Operation failed'));
            }
        }
    };
};
export { xlsxImportAction };
