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