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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_VariableInput_utils_formatVariableScop_mjs__ from "../../../schema-settings/VariableInput/utils/formatVariableScop.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_VariableInput_VariableInput_mjs__ from "../../../schema-settings/VariableInput/VariableInput.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../../variables/index.mjs";
const InternalField = (props)=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { uiSchema } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionField_deprecated)();
    const component = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useComponent)(null == uiSchema ? void 0 : uiSchema['x-component']);
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const setFieldProps = (key, value)=>{
        field[key] = void 0 === field[key] ? value : field[key];
    };
    const setRequired = ()=>{
        if (void 0 === fieldSchema['required']) field.required = !!uiSchema['required'];
    };
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (null == ctx ? void 0 : ctx.field) {
            ctx.field.added = ctx.field.added || new Set();
            ctx.field.added.add(fieldSchema.name);
        }
    });
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!uiSchema) return;
        setFieldProps('content', uiSchema['x-content']);
        setFieldProps('title', uiSchema.title);
        setFieldProps('description', uiSchema.description);
        setFieldProps('initialValue', uiSchema.default);
        if (true === fieldSchema['x-disabled']) field.disabled = true;
        if (true === fieldSchema['x-read-pretty']) field.readPretty = true;
        setRequired();
        field.dataSource = uiSchema.enum;
        const originalProps = compile(uiSchema['x-component-props']) || {};
        const componentProps = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.merge)(originalProps, field.componentProps || {});
        field.componentProps = componentProps;
    }, [
        JSON.stringify(uiSchema)
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(component, props, props.children);
};
const CollectionField = (props)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalField, {
            ...props
        })
    });
};
var AssignedField_rslib_entry_AssignedFieldValueType = /*#__PURE__*/ function(AssignedFieldValueType) {
    AssignedFieldValueType["ConstantValue"] = "constantValue";
    AssignedFieldValueType["DynamicValue"] = "dynamicValue";
    return AssignedFieldValueType;
}({});
const AssignedField = (props)=>{
    const { value, onChange } = props;
    const { getCollectionFields, getAllCollectionsInheritChain } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
    const currentFormFields = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionFilterOptions)(collection);
    const { name, getField } = collection;
    const collectionField = getField(fieldSchema.name);
    const shouldChange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_VariableInput_VariableInput_mjs__.getShouldChange)({
            collectionField,
            variables,
            localVariables,
            getAllCollectionsInheritChain
        }), [
        collectionField,
        getAllCollectionsInheritChain,
        localVariables,
        variables
    ]);
    const returnScope = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((scope)=>{
        const currentForm = scope.find((item)=>'$nForm' === item.value);
        const fields = getCollectionFields(name);
        if (!(null == fields ? void 0 : fields.length) && currentForm) currentForm.children = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_VariableInput_utils_formatVariableScop_mjs__.formatVariableScop)(currentFormFields);
        return scope;
    }, [
        currentFormFields,
        name
    ]);
    const renderSchemaComponent = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { value, onChange } = param;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionField, {
            ...props,
            value: value,
            onChange: onChange
        });
    }, [
        JSON.stringify(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].omit(props, 'value'))
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_VariableInput_VariableInput_mjs__.VariableInput, {
        form: form,
        record: record,
        value: value,
        onChange: onChange,
        renderSchemaComponent: renderSchemaComponent,
        collectionField: collectionField,
        shouldChange: shouldChange,
        returnScope: returnScope,
        targetFieldSchema: fieldSchema
    });
};
export { AssignedField, AssignedField_rslib_entry_AssignedFieldValueType as AssignedFieldValueType };
