import { jsx } from "react/jsx-runtime";
import { useEffect } from "react";
import { connect, merge, useField, useFieldSchema } from "@tachybase/schema";
import { useDeepCompareEffect } from "ahooks";
import { concat } from "lodash";
import { useFormBlockContext } from "../../block-provider/FormBlockProvider.mjs";
import { useCompile, useComponent } from "../../schema-component/index.mjs";
import { useIsAllowToSetDefaultValue } from "../../schema-settings/hooks/useIsAllowToSetDefaultValue.mjs";
import { CollectionFieldProvider, useCollectionField } from "./CollectionFieldProvider.mjs";
const CollectionFieldInternalField = (props)=>{
    const { component } = props;
    const compile = useCompile();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { uiSchema: uiSchemaOrigin, defaultValue } = useCollectionField();
    const { isAllowToSetDefaultValue } = useIsAllowToSetDefaultValue();
    const uiSchema = compile(uiSchemaOrigin);
    const Component = useComponent(component || (null == uiSchema ? void 0 : uiSchema['x-component']) || 'Input');
    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);
        }
    });
    useDeepCompareEffect(()=>{
        if (!uiSchema) return;
        setFieldProps('content', uiSchema['x-content']);
        setFieldProps('title', uiSchema.title);
        setFieldProps("description", uiSchema.description);
        if (null == ctx ? void 0 : ctx.form) {
            const defaultVal = isAllowToSetDefaultValue() ? fieldSchema.default || defaultValue : void 0;
            null != defaultVal && setFieldProps('initialValue', defaultVal);
        }
        if (!field.validator && (uiSchema['x-validator'] || fieldSchema['x-validator'])) {
            const concatSchema = concat([], uiSchema['x-validator'] || [], fieldSchema['x-validator'] || []);
            field.validator = concatSchema;
        }
        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.component = [
            Component,
            componentProps
        ];
    }, [
        uiSchema
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ jsx(Component, {
        ...props
    });
};
const CollectionField = connect((props)=>{
    const fieldSchema = useFieldSchema();
    return /*#__PURE__*/ jsx(CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ jsx(CollectionFieldInternalField, {
            ...props
        })
    });
});
CollectionField.displayName = 'CollectionField';
export { CollectionField, CollectionFieldInternalField };
