"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__, {
    BlockRequestProvider_deprecated: ()=>BlockRequestProvider_deprecated,
    RecordLink: ()=>RecordLink,
    useParamsFromRecord: ()=>useParamsFromRecord,
    BlockProvider: ()=>BlockProvider,
    BlockResourceContext: ()=>BlockResourceContext,
    RenderChildrenWithAssociationFilter: ()=>RenderChildrenWithAssociationFilter,
    useBlockContext: ()=>useBlockContext,
    useFilterByTk: ()=>useFilterByTk,
    useSourceIdFromRecord: ()=>useSourceIdFromRecord,
    BlockRequestContext_deprecated: ()=>BlockRequestContext_deprecated,
    useBlockAssociationContext: ()=>useBlockAssociationContext,
    useBlockResource: ()=>useBlockResource,
    useBlockRequestContext: ()=>useBlockRequestContext,
    useSourceIdFromParentRecord: ()=>useSourceIdFromParentRecord,
    MaybeCollectionProvider: ()=>MaybeCollectionProvider,
    BlockAssociationContext: ()=>BlockAssociationContext
});
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_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_deepmerge_namespaceObject = require("deepmerge");
var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
const template_namespaceObject = require("lodash/template");
var template_default = /*#__PURE__*/ __webpack_require__.n(template_namespaceObject);
const external_react_router_dom_namespaceObject = require("react-router-dom");
const external_index_js_namespaceObject = require("../index.js");
const index_js_namespaceObject = require("../collection-manager/index.js");
const FilterProvider_js_namespaceObject = require("../filter-provider/FilterProvider.js");
const external_record_provider_index_js_namespaceObject = require("../record-provider/index.js");
const external_hooks_index_js_namespaceObject = require("./hooks/index.js");
const useDataBlockSourceId_js_namespaceObject = require("./hooks/useDataBlockSourceId.js");
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      height: 100%;\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      width: 200px;\n      flex: 0 0 auto;\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      flex: 1 1 auto;\n      min-width: 0;\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
function _templateObject3() {
    const data = _tagged_template_literal([
        "\n      display: flex;\n      flex-direction: column;\n      height: 100%;\n    "
    ]);
    _templateObject3 = function() {
        return data;
    };
    return data;
}
const BlockResourceContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
BlockResourceContext.displayName = 'BlockResourceContext';
const BlockAssociationContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
BlockAssociationContext.displayName = 'BlockAssociationContext';
const BlockRequestContext_deprecated = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
BlockRequestContext_deprecated.displayName = 'BlockRequestContext_deprecated';
const useBlockResource = ()=>{
    const resource = (0, external_index_js_namespaceObject.useDataBlockResource)();
    return (0, external_react_namespaceObject.useContext)(BlockResourceContext) || resource;
};
const MaybeCollectionProvider = (props)=>{
    const { collection } = props;
    return collection ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.CollectionProvider_deprecated, {
        collection: collection,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.ACLCollectionProvider, {
            children: props.children
        })
    }) : props.children;
};
const BlockRequestProvider_deprecated = (props)=>{
    var _service_data_meta, _service_data;
    const field = (0, schema_namespaceObject.useField)();
    const resource = (0, external_index_js_namespaceObject.useDataBlockResource)();
    const [allowedActions, setAllowedActions] = (0, external_react_namespaceObject.useState)({});
    const service = (0, external_index_js_namespaceObject.useDataBlockRequest)();
    const record = (0, external_index_js_namespaceObject.useCollectionRecord)();
    const parentRecord = (0, external_index_js_namespaceObject.useCollectionParentRecord)();
    const serviceAllowedActions = null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : null === (_service_data_meta = _service_data.meta) || void 0 === _service_data_meta ? void 0 : _service_data_meta.allowedActions;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!serviceAllowedActions) return;
        setAllowedActions((last)=>external_deepmerge_default()(last, null != serviceAllowedActions ? serviceAllowedActions : {}));
    }, [
        serviceAllowedActions
    ]);
    const __parent = useBlockRequestContext();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BlockRequestContext_deprecated.Provider, {
        value: {
            allowedActions,
            block: props.block,
            props,
            field,
            service,
            resource,
            __parent,
            updateAssociationValues: (null == props ? void 0 : props.updateAssociationValues) || []
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
            isNew: null == record ? void 0 : record.isNew,
            record: null == record ? void 0 : record.data,
            parent: null == parentRecord ? void 0 : parentRecord.data,
            children: props.children
        })
    });
};
const useBlockRequestContext = ()=>(0, external_react_namespaceObject.useContext)(BlockRequestContext_deprecated);
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        row: css(_templateObject()),
        left: css(_templateObject1()),
        right: css(_templateObject2()),
        rightContainer: css(_templateObject3())
    };
});
const RenderChildrenWithAssociationFilter = (props)=>{
    var _field_component;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { findComponent } = (0, external_index_js_namespaceObject.useDesignable)();
    const { styles } = useStyles();
    const field = (0, schema_namespaceObject.useField)();
    const Component = findComponent(null === (_field_component = field.component) || void 0 === _field_component ? void 0 : _field_component[0]) || external_react_default().Fragment;
    const associationFilterSchema = fieldSchema.reduceProperties((buf, s)=>{
        if ('AssociationFilter' === s['x-component']) return s;
        return buf;
    }, null);
    if (associationFilterSchema) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Component, {
        ...field.componentProps,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Row, {
            className: styles.row,
            gutter: 16,
            wrap: false,
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Col, {
                    className: styles.left,
                    style: props.associationFilterStyle,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                        schema: fieldSchema,
                        onlyRenderProperties: true,
                        filterProperties: (s)=>'AssociationFilter' === s['x-component']
                    })
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Col, {
                    className: styles.right,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        className: styles.rightContainer,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                            schema: fieldSchema,
                            onlyRenderProperties: true,
                            filterProperties: (s)=>'AssociationFilter' !== s['x-component']
                        })
                    })
                })
            ]
        })
    });
    return props.children;
};
const BlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
BlockContext.displayName = 'BlockContext';
const useBlockContext = ()=>(0, external_react_namespaceObject.useContext)(BlockContext);
const useCompatDataBlockParentRecord = (props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    if (fieldSchema['x-use-decorator-props']) return props.parentRecord;
    return (0, useDataBlockSourceId_js_namespaceObject.useDataBlockParentRecord)(props);
};
const BlockProvider = (props)=>{
    const { name, dataSource, useParams, parentRecord } = props;
    const parentRecordFromHook = useCompatDataBlockParentRecord(props);
    const paramsFromHook = null == useParams ? void 0 : useParams();
    const { getAssociationAppends } = (0, external_hooks_index_js_namespaceObject.useAssociationNames)(dataSource);
    const { appends, updateAssociationValues } = getAssociationAppends();
    const params = (0, external_react_namespaceObject.useMemo)(()=>{
        var _props_params;
        if (!(null === (_props_params = props.params) || void 0 === _props_params ? void 0 : _props_params['appends'])) return {
            ...props.params,
            appends,
            ...paramsFromHook
        };
        return {
            ...props.params,
            ...paramsFromHook
        };
    }, [
        appends,
        paramsFromHook,
        props.params
    ]);
    const blockValue = (0, external_react_namespaceObject.useMemo)(()=>({
            name
        }), [
        name
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BlockContext.Provider, {
        value: blockValue,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.DataBlockProvider, {
            ...props,
            params: params,
            parentRecord: parentRecord || parentRecordFromHook,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BlockRequestProvider_deprecated, {
                ...props,
                updateAssociationValues: updateAssociationValues,
                params: params,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FilterProvider_js_namespaceObject.DataBlockCollector, {
                    ...props,
                    params: params,
                    children: props.children
                })
            })
        })
    });
};
const useBlockAssociationContext = ()=>{
    const { association } = (0, external_index_js_namespaceObject.useDataBlockProps)() || {};
    return (0, external_react_namespaceObject.useContext)(BlockAssociationContext) || association;
};
const useFilterByTk = ()=>{
    const { resource, __parent } = useBlockRequestContext();
    const recordIndex = (0, external_record_provider_index_js_namespaceObject.useRecordIndex)();
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const collection = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const assoc = useBlockAssociationContext();
    const withoutTableFieldResource = (0, external_react_namespaceObject.useContext)(external_index_js_namespaceObject.WithoutTableFieldResource);
    if (!withoutTableFieldResource) {
        if (resource instanceof external_index_js_namespaceObject.TableFieldResource || (null == __parent ? void 0 : __parent.block) === 'TableField') return recordIndex;
    }
    if (assoc) {
        const association = getCollectionField(assoc);
        return null == record ? void 0 : record[association.targetKey || 'id'];
    }
    return null == record ? void 0 : record[collection.filterTargetKey || 'id'];
};
const useSourceIdFromRecord = ()=>{
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const { getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const association = useBlockAssociationContext();
    if (association) {
        const collectionField = getCollectionField(association);
        return null == record ? void 0 : record[collectionField.sourceKey || 'id'];
    }
};
const useSourceIdFromParentRecord = ()=>{
    const cm = (0, external_index_js_namespaceObject.useCollectionManager)();
    const parentRecordData = (0, external_index_js_namespaceObject.useCollectionParentRecordData)();
    const { getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const association = useBlockAssociationContext();
    if (association) {
        const collectionField = getCollectionField(association);
        const collection = cm.getCollection(collectionField.collectionName);
        return null == parentRecordData ? void 0 : parentRecordData[collectionField.sourceKey || collection.filterTargetKey || collection.getPrimaryKey() || 'id'];
    }
};
const useParamsFromRecord = ()=>{
    var _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1;
    const filterByTk = useFilterByTk();
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const { fields } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getCollectionJoinField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const collectionField = getCollectionJoinField((null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops ? void 0 : _fieldSchema_xdecoratorprops.resource) || (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops1 ? void 0 : _fieldSchema_xdecoratorprops1.association));
    const filterFields = fields.filter((v)=>[
            'boolean',
            'date',
            'integer',
            'radio',
            'sort',
            'string',
            'time',
            'uid',
            'uuid'
        ].includes(v.type)).map((v)=>v.name);
    const filter = Object.keys(record).filter((key)=>filterFields.includes(key)).reduce((result, key)=>{
        result[key] = record[key];
        return result;
    }, {});
    const obj = {
        filterByTk: filterByTk
    };
    if (record.__collection && collectionField && ![
        'oho',
        'm2o',
        'obo'
    ].includes(collectionField.interface)) obj['targetCollection'] = record.__collection;
    if (!filterByTk && Object.keys(filter).length > 0) obj['filter'] = filter;
    return obj;
};
const RecordLink = (props)=>{
    const field = (0, schema_namespaceObject.useField)();
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const { title, to, ...others } = props;
    const compiled = template_default()(to || '');
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Link, {
        ...others,
        to: compiled({
            record: record || {}
        }),
        children: field.title
    });
};
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
});
