import { jsx } from "react/jsx-runtime";
import react, { useCallback, useEffect, useMemo } from "react";
import { merge, useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useFormBlockContext } from "../../../block-provider/index.mjs";
import { useCollectionField_deprecated, useCollectionFilterOptions, useCollectionManager_deprecated, useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { CollectionFieldProvider } from "../../../data-source/index.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
import { useCompile, useComponent } from "../../../schema-component/index.mjs";
import { formatVariableScop } from "../../../schema-settings/VariableInput/utils/formatVariableScop.mjs";
import { VariableInput, getShouldChange } from "../../../schema-settings/VariableInput/VariableInput.mjs";
import { useLocalVariables, useVariables } from "../../../variables/index.mjs";
const InternalField = (props)=>{
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { uiSchema } = useCollectionField_deprecated();
    const component = useComponent(null == uiSchema ? void 0 : uiSchema['x-component']);
    const compile = 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 = useFormBlockContext();
    useEffect(()=>{
        if (null == ctx ? void 0 : ctx.field) {
            ctx.field.added = ctx.field.added || new Set();
            ctx.field.added.add(fieldSchema.name);
        }
    });
    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 = merge(originalProps, field.componentProps || {});
        field.componentProps = componentProps;
    }, [
        JSON.stringify(uiSchema)
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ react.createElement(component, props, props.children);
};
const CollectionField = (props)=>{
    const fieldSchema = useFieldSchema();
    return /*#__PURE__*/ jsx(CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ jsx(InternalField, {
            ...props
        })
    });
};
var AssignedField_AssignedFieldValueType = /*#__PURE__*/ function(AssignedFieldValueType) {
    AssignedFieldValueType["ConstantValue"] = "constantValue";
    AssignedFieldValueType["DynamicValue"] = "dynamicValue";
    return AssignedFieldValueType;
}({});
const AssignedField = (props)=>{
    const { value, onChange } = props;
    const { getCollectionFields, getAllCollectionsInheritChain } = useCollectionManager_deprecated();
    const collection = useCollection_deprecated();
    const { form } = useFormBlockContext();
    const fieldSchema = useFieldSchema();
    const record = useRecord();
    const variables = useVariables();
    const localVariables = useLocalVariables();
    const currentFormFields = useCollectionFilterOptions(collection);
    const { name, getField } = collection;
    const collectionField = getField(fieldSchema.name);
    const shouldChange = useMemo(()=>getShouldChange({
            collectionField,
            variables,
            localVariables,
            getAllCollectionsInheritChain
        }), [
        collectionField,
        getAllCollectionsInheritChain,
        localVariables,
        variables
    ]);
    const returnScope = useCallback((scope)=>{
        const currentForm = scope.find((item)=>'$nForm' === item.value);
        const fields = getCollectionFields(name);
        if (!(null == fields ? void 0 : fields.length) && currentForm) currentForm.children = formatVariableScop(currentFormFields);
        return scope;
    }, [
        currentFormFields,
        name
    ]);
    const renderSchemaComponent = useCallback((param)=>{
        let { value, onChange } = param;
        return /*#__PURE__*/ jsx(CollectionField, {
            ...props,
            value: value,
            onChange: onChange
        });
    }, [
        JSON.stringify(lodash.omit(props, 'value'))
    ]);
    return /*#__PURE__*/ jsx(VariableInput, {
        form: form,
        record: record,
        value: value,
        onChange: onChange,
        renderSchemaComponent: renderSchemaComponent,
        collectionField: collectionField,
        shouldChange: shouldChange,
        returnScope: returnScope,
        targetFieldSchema: fieldSchema
    });
};
export { AssignedField, AssignedField_AssignedFieldValueType as AssignedFieldValueType };
