import { useContext } from "react";
import { useFieldSchema, useForm } from "@tachybase/schema";
import { App } from "antd";
import { useTranslation } from "react-i18next";
import { useIsDetailBlock } from "../../../block-provider/FormBlockProvider.mjs";
import { ActionContext, OpenMode } from "./context.mjs";
const useA = ()=>({
        async run () {}
    });
const useActionContext = ()=>{
    const ctx = useContext(ActionContext);
    const { t } = useTranslation();
    const { modal } = App.useApp();
    return {
        ...ctx,
        setVisible (visible) {
            let confirm = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : false;
            if ((null == ctx ? void 0 : ctx.openMode) !== OpenMode.PAGE) if (visible) {
                var _ctx_setVisible1;
                null == ctx || null == (_ctx_setVisible1 = ctx.setVisible) || _ctx_setVisible1.call(ctx, visible);
            } else if (confirm && ctx.formValueChanged) modal.confirm({
                title: t('Unsaved changes'),
                content: t("Are you sure you don't want to save?"),
                async onOk () {
                    var _ctx_setVisible;
                    ctx.setFormValueChanged(false);
                    null == (_ctx_setVisible = ctx.setVisible) || _ctx_setVisible.call(ctx, false);
                }
            });
            else {
                var _ctx_setVisible;
                null == ctx || null == (_ctx_setVisible = ctx.setVisible) || _ctx_setVisible.call(ctx, false);
            }
        }
    };
};
const useCloseAction = ()=>{
    const { setVisible } = useContext(ActionContext);
    const form = useForm();
    return {
        async run () {
            setVisible(false);
            form.submit((values)=>{
                console.warn(values);
            });
        }
    };
};
const useLinkageAction = ()=>{
    const fieldSchema = useFieldSchema();
    const isRecordAction = useIsDetailBlock();
    const isAction = [
        'Action.Link',
        'Action'
    ].includes(fieldSchema['x-component']);
    return isAction && isRecordAction;
};
export { useA, useActionContext, useCloseAction, useLinkageAction };
