"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    CollectionField: ()=>CollectionField,
    CollectionFieldInternalField: ()=>CollectionFieldInternalField
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_ahooks_namespaceObject = require("ahooks");
const external_lodash_namespaceObject = require("lodash");
const FormBlockProvider_js_namespaceObject = require("../../block-provider/FormBlockProvider.js");
const index_js_namespaceObject = require("../../schema-component/index.js");
const useIsAllowToSetDefaultValue_js_namespaceObject = require("../../schema-settings/hooks/useIsAllowToSetDefaultValue.js");
const external_CollectionFieldProvider_js_namespaceObject = require("./CollectionFieldProvider.js");
const CollectionFieldInternalField = (props)=>{
    const { component } = props;
    const compile = (0, index_js_namespaceObject.useCompile)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { uiSchema: uiSchemaOrigin, defaultValue } = (0, external_CollectionFieldProvider_js_namespaceObject.useCollectionField)();
    const { isAllowToSetDefaultValue } = (0, useIsAllowToSetDefaultValue_js_namespaceObject.useIsAllowToSetDefaultValue)();
    const uiSchema = compile(uiSchemaOrigin);
    const Component = (0, index_js_namespaceObject.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, FormBlockProvider_js_namespaceObject.useFormBlockContext)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (null == ctx ? void 0 : ctx.field) {
            ctx.field.added = ctx.field.added || new Set();
            ctx.field.added.add(fieldSchema.name);
        }
    });
    (0, external_ahooks_namespaceObject.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, external_lodash_namespaceObject.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, schema_namespaceObject.merge)(originalProps, field.componentProps || {});
        field.component = [
            Component,
            componentProps
        ];
    }, [
        uiSchema
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Component, {
        ...props
    });
};
const CollectionField = (0, schema_namespaceObject.connect)((props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CollectionFieldProvider_js_namespaceObject.CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CollectionFieldInternalField, {
            ...props
        })
    });
});
CollectionField.displayName = 'CollectionField';
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
