import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo } from "react";
import { autorun, createForm, onFormValuesChange, useField } from "@tachybase/schema";
import { forEach } from "@tachybase/utils/client";
import { Spin } from "antd";
import { useCollectionParentRecordData } from "../data-source/collection-record/CollectionRecordProvider.mjs";
import { RecordProvider } from "../record-provider/index.mjs";
import { BlockProvider, useBlockRequestContext } from "./BlockProvider.mjs";
import { useFormBlockContext } from "./FormBlockProvider.mjs";
const FormFieldContext = /*#__PURE__*/ createContext({});
FormFieldContext.displayName = 'FormFieldContext';
const InternalFormFieldProvider = (props)=>{
    var _formBlockCtx_updateAssociationValues, _service_data;
    const { action, readPretty, fieldName } = props;
    const formBlockCtx = useFormBlockContext();
    const parentRecordData = useCollectionParentRecordData();
    if (!(null == formBlockCtx ? void 0 : null == (_formBlockCtx_updateAssociationValues = formBlockCtx.updateAssociationValues) ? void 0 : _formBlockCtx_updateAssociationValues.includes(fieldName))) {
        var _formBlockCtx_updateAssociationValues1;
        null == formBlockCtx || null == (_formBlockCtx_updateAssociationValues1 = formBlockCtx.updateAssociationValues) || _formBlockCtx_updateAssociationValues1.push(fieldName);
    }
    const field = useField();
    const form = useMemo(()=>createForm({
            effects () {
                onFormValuesChange((form)=>{
                    var _formBlockCtx_form;
                    null == formBlockCtx || null == (_formBlockCtx_form = formBlockCtx.form) || _formBlockCtx_form.setValuesIn(fieldName, form.values);
                });
            },
            readPretty
        }), []);
    useEffect(()=>{
        const dispose = autorun(()=>{
            var _formBlockCtx_form;
            const data = (null == formBlockCtx ? void 0 : null == (_formBlockCtx_form = formBlockCtx.form) ? void 0 : _formBlockCtx_form.values[fieldName]) || {};
            form.reset();
            forEach(data, (value, key)=>{
                if (value) form.values[key] = value;
            });
        });
        return dispose;
    }, []);
    const { resource, service } = useBlockRequestContext();
    if (service.loading) return /*#__PURE__*/ jsx(Spin, {});
    return /*#__PURE__*/ jsx(RecordProvider, {
        record: null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data,
        parent: parentRecordData,
        children: /*#__PURE__*/ jsx(FormFieldContext.Provider, {
            value: {
                action,
                form,
                field,
                service,
                resource,
                fieldName
            },
            children: props.children
        })
    });
};
const WithoutFormFieldResource = /*#__PURE__*/ createContext(null);
WithoutFormFieldResource.displayName = 'WithoutFormFieldResource';
const FormFieldProvider = (props)=>/*#__PURE__*/ jsx(WithoutFormFieldResource.Provider, {
        value: false,
        children: /*#__PURE__*/ jsx(BlockProvider, {
            name: "form-field",
            block: 'FormField',
            ...props,
            children: /*#__PURE__*/ jsx(InternalFormFieldProvider, {
                ...props
            })
        })
    });
const useFormFieldContext = ()=>useContext(FormFieldContext);
const useFormFieldProps = ()=>{
    const ctx = useFormFieldContext();
    useEffect(()=>{
        var _ctx_service_data, _ctx_service, _ctx_form;
        null == ctx || null == (_ctx_form = ctx.form) || _ctx_form.setInitialValues(null == (_ctx_service = ctx.service) ? void 0 : null == (_ctx_service_data = _ctx_service.data) ? void 0 : _ctx_service_data.data);
    }, []);
    return {
        form: ctx.form
    };
};
export { FormFieldContext, FormFieldProvider, WithoutFormFieldResource, useFormFieldContext, useFormFieldProps };
