"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    AssignedField: ()=>AssignedField,
    AssignedFieldValueType: ()=>AssignedField_rslib_entry_AssignedFieldValueType
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const index_js_namespaceObject = require("../../../block-provider/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_data_source_index_js_namespaceObject = require("../../../data-source/index.js");
const external_record_provider_index_js_namespaceObject = require("../../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const formatVariableScop_js_namespaceObject = require("../../../schema-settings/VariableInput/utils/formatVariableScop.js");
const VariableInput_js_namespaceObject = require("../../../schema-settings/VariableInput/VariableInput.js");
const external_variables_index_js_namespaceObject = require("../../../variables/index.js");
const InternalField = (props)=>{
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { uiSchema } = (0, external_collection_manager_index_js_namespaceObject.useCollectionField_deprecated)();
    const component = (0, external_schema_component_index_js_namespaceObject.useComponent)(null == uiSchema ? void 0 : uiSchema['x-component']);
    const compile = (0, external_schema_component_index_js_namespaceObject.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, index_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_react_namespaceObject.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, schema_namespaceObject.merge)(originalProps, field.componentProps || {});
        field.componentProps = componentProps;
    }, [
        JSON.stringify(uiSchema)
    ]);
    if (!uiSchema) return null;
    return /*#__PURE__*/ external_react_default().createElement(component, props, props.children);
};
const CollectionField = (props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_data_source_index_js_namespaceObject.CollectionFieldProvider, {
        name: fieldSchema.name,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const collection = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { form } = (0, index_js_namespaceObject.useFormBlockContext)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const localVariables = (0, external_variables_index_js_namespaceObject.useLocalVariables)();
    const currentFormFields = (0, external_collection_manager_index_js_namespaceObject.useCollectionFilterOptions)(collection);
    const { name, getField } = collection;
    const collectionField = getField(fieldSchema.name);
    const shouldChange = (0, external_react_namespaceObject.useMemo)(()=>(0, VariableInput_js_namespaceObject.getShouldChange)({
            collectionField,
            variables,
            localVariables,
            getAllCollectionsInheritChain
        }), [
        collectionField,
        getAllCollectionsInheritChain,
        localVariables,
        variables
    ]);
    const returnScope = (0, external_react_namespaceObject.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, formatVariableScop_js_namespaceObject.formatVariableScop)(currentFormFields);
        return scope;
    }, [
        currentFormFields,
        name
    ]);
    const renderSchemaComponent = (0, external_react_namespaceObject.useCallback)((param)=>{
        let { value, onChange } = param;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CollectionField, {
            ...props,
            value: value,
            onChange: onChange
        });
    }, [
        JSON.stringify(external_lodash_default().omit(props, 'value'))
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(VariableInput_js_namespaceObject.VariableInput, {
        form: form,
        record: record,
        value: value,
        onChange: onChange,
        renderSchemaComponent: renderSchemaComponent,
        collectionField: collectionField,
        shouldChange: shouldChange,
        returnScope: returnScope,
        targetFieldSchema: fieldSchema
    });
};
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
});
