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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_deepmerge__ from "deepmerge";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_template__ from "lodash/template";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__ from "../filter-provider/FilterProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "./hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useDataBlockSourceId_mjs__ from "./hooks/useDataBlockSourceId.mjs";
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, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
BlockResourceContext.displayName = 'BlockResourceContext';
const BlockAssociationContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
BlockAssociationContext.displayName = 'BlockAssociationContext';
const BlockRequestContext_deprecated = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
BlockRequestContext_deprecated.displayName = 'BlockRequestContext_deprecated';
const useBlockResource = ()=>{
    const resource = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDataBlockResource)();
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BlockResourceContext) || resource;
};
const MaybeCollectionProvider = (props)=>{
    const { collection } = props;
    return collection ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.CollectionProvider_deprecated, {
        collection: collection,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ACLCollectionProvider, {
            children: props.children
        })
    }) : props.children;
};
const BlockRequestProvider_deprecated = (props)=>{
    var _service_data_meta, _service_data;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const resource = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDataBlockResource)();
    const [allowedActions, setAllowedActions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const service = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDataBlockRequest)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionRecord)();
    const parentRecord = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!serviceAllowedActions) return;
        setAllowedActions((last)=>(0, __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"])(last, null != serviceAllowedActions ? serviceAllowedActions : {}));
    }, [
        serviceAllowedActions
    ]);
    const __parent = useBlockRequestContext();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BlockRequestContext_deprecated.Provider, {
        value: {
            allowedActions,
            block: props.block,
            props,
            field,
            service,
            resource,
            __parent,
            updateAssociationValues: (null == props ? void 0 : props.updateAssociationValues) || []
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BlockRequestContext_deprecated);
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { findComponent } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { styles } = useStyles();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const Component = findComponent(null === (_field_component = field.component) || void 0 === _field_component ? void 0 : _field_component[0]) || __WEBPACK_EXTERNAL_MODULE_react__["default"].Fragment;
    const associationFilterSchema = fieldSchema.reduceProperties((buf, s)=>{
        if ('AssociationFilter' === s['x-component']) return s;
        return buf;
    }, null);
    if (associationFilterSchema) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {
        ...field.componentProps,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Row, {
            className: styles.row,
            gutter: 16,
            wrap: false,
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Col, {
                    className: styles.left,
                    style: props.associationFilterStyle,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                        schema: fieldSchema,
                        onlyRenderProperties: true,
                        filterProperties: (s)=>'AssociationFilter' === s['x-component']
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Col, {
                    className: styles.right,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        className: styles.rightContainer,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                            schema: fieldSchema,
                            onlyRenderProperties: true,
                            filterProperties: (s)=>'AssociationFilter' !== s['x-component']
                        })
                    })
                })
            ]
        })
    });
    return props.children;
};
const BlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
BlockContext.displayName = 'BlockContext';
const useBlockContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BlockContext);
const useCompatDataBlockParentRecord = (props)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    if (fieldSchema['x-use-decorator-props']) return props.parentRecord;
    return (0, __WEBPACK_EXTERNAL_MODULE__hooks_useDataBlockSourceId_mjs__.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, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useAssociationNames)(dataSource);
    const { appends, updateAssociationValues } = getAssociationAppends();
    const params = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            name
        }), [
        name
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BlockContext.Provider, {
        value: blockValue,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.DataBlockProvider, {
            ...props,
            params: params,
            parentRecord: parentRecord || parentRecordFromHook,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BlockRequestProvider_deprecated, {
                ...props,
                updateAssociationValues: updateAssociationValues,
                params: params,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__.DataBlockCollector, {
                    ...props,
                    params: params,
                    children: props.children
                })
            })
        })
    });
};
const useBlockAssociationContext = ()=>{
    const { association } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDataBlockProps)() || {};
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BlockAssociationContext) || association;
};
const useFilterByTk = ()=>{
    const { resource, __parent } = useBlockRequestContext();
    const recordIndex = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecordIndex)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const assoc = useBlockAssociationContext();
    const withoutTableFieldResource = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.WithoutTableFieldResource);
    if (!withoutTableFieldResource) {
        if (resource instanceof __WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager)();
    const parentRecordData = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionParentRecordData)();
    const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const { fields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const { title, to, ...others } = props;
    const compiled = (0, __WEBPACK_EXTERNAL_MODULE_lodash_template__["default"])(to || '');
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Link, {
        ...others,
        to: compiled({
            record: record || {}
        }),
        children: field.title
    });
};
export { BlockAssociationContext, BlockProvider, BlockRequestContext_deprecated, BlockRequestProvider_deprecated, BlockResourceContext, MaybeCollectionProvider, RecordLink, RenderChildrenWithAssociationFilter, useBlockAssociationContext, useBlockContext, useBlockRequestContext, useBlockResource, useFilterByTk, useParamsFromRecord, useSourceIdFromParentRecord, useSourceIdFromRecord };
