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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
const sub_table_rslib_entry_collection = {
    name: 'fields',
    targetKey: 'name',
    fields: [
        {
            type: 'string',
            name: 'type',
            interface: 'input',
            uiSchema: {
                title: '{{ t("Storage type") }}',
                type: 'string',
                'x-component': 'Select',
                enum: [
                    {
                        label: 'String',
                        value: 'string'
                    }
                ],
                required: true
            }
        },
        {
            type: 'string',
            name: 'interface',
            interface: 'input',
            uiSchema: {
                title: '{{ t("Field interface") }}',
                type: 'string',
                'x-component': 'Select',
                enum: '{{interfaces}}'
            }
        },
        {
            type: 'string',
            name: 'title',
            interface: 'input',
            uiSchema: {
                title: '{{ t("Field display name") }}',
                type: 'string',
                'x-component': 'Input',
                required: true
            }
        },
        {
            type: 'string',
            name: 'name',
            interface: 'input',
            uiSchema: {
                title: '{{ t("Field name") }}',
                type: 'string',
                'x-component': 'Input'
            }
        }
    ]
};
const DataSourceContext_deprecated = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
DataSourceContext_deprecated.displayName = 'DataSourceContext_deprecated';
const useSelectedRowKeys = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    return [
        ctx.selectedRowKeys,
        ctx.setSelectedRowKeys
    ];
};
const useDataSource_deprecated = (options)=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    return (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(()=>Promise.resolve({
            data: ctx.dataSource
        }), {
        ...options,
        refreshDeps: [
            JSON.stringify(ctx.dataSource)
        ]
    });
};
const useCreateAction = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            dataSource.push((0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(form.values));
            ctx.setDataSource([
                ...dataSource
            ]);
            setVisible(false);
            await form.reset();
        }
    };
};
const useBulkDestroyAction = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    const { selectedRowKeys, setSelectedRowKeys } = ctx;
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource(dataSource.filter((_, index)=>!selectedRowKeys.includes(index)));
            setSelectedRowKeys([]);
        }
    };
};
const useUpdateAction = ()=>{
    const recordIndex = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecordIndex)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    return {
        async run () {
            const dataSource = (null == ctx ? void 0 : ctx.dataSource) || [];
            const values = dataSource.map((item, index)=>{
                if (index === recordIndex) return {
                    ...form.values
                };
                return item;
            });
            ctx.setDataSource([
                ...values
            ]);
            setVisible(false);
        }
    };
};
const useDestroyAction = ()=>{
    const recordIndex = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecordIndex)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext_deprecated);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource(dataSource.filter((_, index)=>recordIndex !== index));
        }
    };
};
const ds = {
    useSelectedRowKeys,
    useDataSource_deprecated,
    useCreateAction,
    useBulkDestroyAction,
    useUpdateAction,
    useDestroyAction
};
const SubFieldDataSourceProvider_deprecated = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const [selectedRowKeys, setSelectedRowKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [dataSource, setDataSource] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const service = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(()=>{
        if ('subTable' === record.interface) {
            var _record_children;
            if (null == record ? void 0 : null === (_record_children = record.children) || void 0 === _record_children ? void 0 : _record_children.length) return Promise.resolve({
                data: (null == record ? void 0 : record.children) || []
            });
            return api.resource('fields').list({
                paginate: false,
                sort: 'sort',
                filter: {
                    parentKey: record.key
                }
            }).then((res)=>null == res ? void 0 : res.data);
        }
        return Promise.resolve({
            data: []
        });
    }, {
        onSuccess (data) {
            setDataSource(null == data ? void 0 : data.data);
        }
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CollectionProvider_deprecated, {
        collection: sub_table_rslib_entry_collection,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataSourceContext_deprecated.Provider, {
            value: {
                rowKey: 'name',
                service,
                dataSource,
                setDataSource,
                selectedRowKeys,
                setSelectedRowKeys
            },
            children: props.children
        })
    });
}, {
    displayName: 'SubFieldDataSourceProvider_deprecated'
});
const DataSourceProvider_deprecated = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { rowKey, collection, association } = props;
    const [selectedRowKeys, setSelectedRowKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [dataSource, setDataSource] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const coll = getCollection(collection);
    const resourceOf = null == record ? void 0 : record[association.targetKey || 'id'];
    const service = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(()=>{
        if (resourceOf) {
            var _coll_fields_filter, _coll_fields;
            return api.request({
                resource: "".concat(association.collectionName, ".").concat(association.name),
                resourceOf,
                action: 'list',
                params: {
                    appends: null == coll ? void 0 : null === (_coll_fields = coll.fields) || void 0 === _coll_fields ? void 0 : null === (_coll_fields_filter = _coll_fields.filter((field)=>field.target)) || void 0 === _coll_fields_filter ? void 0 : _coll_fields_filter.map((field)=>field.name)
                }
            }).then((res)=>res.data);
        }
        return Promise.resolve({
            data: (null == record ? void 0 : record[association.name]) || []
        });
    }, {
        onSuccess (data) {
            setDataSource(null == data ? void 0 : data.data);
        }
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CollectionProvider_deprecated, {
        collection: coll,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataSourceContext_deprecated.Provider, {
            value: {
                rowKey,
                service,
                dataSource,
                setDataSource,
                selectedRowKeys,
                setSelectedRowKeys
            },
            children: props.children
        })
    });
}, {
    displayName: 'DataSourceProvider_deprecated'
});
export { DataSourceContext_deprecated, DataSourceProvider_deprecated, SubFieldDataSourceProvider_deprecated, ds };
