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_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_FormBlockProvider_mjs__ from "../../block-provider/FormBlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__ from "../../schema-settings/hooks/useIsAllowToSetDefaultValue.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__CollectionFieldProvider_mjs__ from "./CollectionFieldProvider.mjs";
const CollectionFieldInternalField = (props)=>{
    const { component } = props;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { uiSchema: uiSchemaOrigin, defaultValue } = (0, __WEBPACK_EXTERNAL_MODULE__CollectionFieldProvider_mjs__.useCollectionField)();
    const { isAllowToSetDefaultValue } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.useIsAllowToSetDefaultValue)();
    const uiSchema = compile(uiSchemaOrigin);
    const Component = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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 = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_FormBlockProvider_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_ahooks__.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 = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.merge)(originalProps, field.componentProps || {});
        field.component = [
            Component,
            componentProps
        ];
    }, [
        uiSchema
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {
        ...props
    });
};
const CollectionField = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CollectionFieldProvider_mjs__.CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionFieldInternalField, {
            ...props
        })
    });
});
CollectionField.displayName = 'CollectionField';
export { CollectionField, CollectionFieldInternalField };
