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_client__ from "@tachybase/client";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
const DataSourceContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
DataSourceContext.displayName = 'DataSourceContext';
const useSelectedRowKeys = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return [
        ctx.selectedRowKeys,
        ctx.setSelectedRowKeys
    ];
};
const useDataSource = (options)=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_client__.useRequest)(()=>Promise.resolve({
            data: ctx.dataSource
        }), {
        ...options,
        refreshDeps: [
            JSON.stringify(ctx.dataSource)
        ]
    });
};
const useCreateAction = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            dataSource.push({
                id: (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(),
                name: (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()
            });
            ctx.setDataSource([
                ...dataSource
            ]);
        }
    };
};
const useBulkDestroyAction = ()=>{
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    const { selectedRowKeys, setSelectedRowKeys } = ctx;
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource(dataSource.filter((item)=>!selectedRowKeys.includes(item.id)));
            setSelectedRowKeys([]);
        }
    };
};
const useUpdateAction = ()=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_client__.useRecord)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource([
                ...dataSource.map((item)=>{
                    if (record.id === item.id) {
                        record.name = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)();
                        return record;
                    }
                    return item;
                })
            ]);
        }
    };
};
const useDestroyAction = ()=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_client__.useRecord)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource(dataSource.filter((item)=>record.id !== item.id));
        }
    };
};
const ds = {
    useSelectedRowKeys,
    useDataSource,
    useCreateAction,
    useBulkDestroyAction,
    useUpdateAction,
    useDestroyAction
};
const schema = {
    type: 'object',
    properties: {
        context: {
            type: 'void',
            'x-component': 'DataSourceProvider',
            properties: {
                action1: {
                    title: '添加',
                    'x-component': 'Action',
                    'x-component-props': {
                        useAction: '{{ ds.useCreateAction }}'
                    }
                },
                action2: {
                    title: '删除',
                    'x-component': 'Action',
                    'x-component-props': {
                        useAction: '{{ ds.useBulkDestroyAction }}'
                    }
                },
                input: {
                    type: 'array',
                    title: "编辑模式",
                    'x-component': 'Table.Array',
                    'x-component-props': {
                        rowKey: 'id',
                        pagination: false,
                        rowSelection: {
                            type: 'checkbox'
                        },
                        useSelectedRowKeys: '{{ ds.useSelectedRowKeys }}',
                        useDataSource: '{{ ds.useDataSource }}'
                    },
                    properties: {
                        column1: {
                            type: 'void',
                            title: 'Name',
                            'x-component': 'Table.Column',
                            properties: {
                                name: {
                                    type: 'string',
                                    'x-component': 'Input',
                                    'x-read-pretty': true
                                }
                            }
                        },
                        column2: {
                            type: 'void',
                            title: 'Actions',
                            'x-component': 'Table.Column',
                            properties: {
                                action1: {
                                    title: '编辑',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        useAction: '{{ ds.useUpdateAction }}'
                                    }
                                },
                                action2: {
                                    title: '删除',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        useAction: '{{ ds.useDestroyAction }}'
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};
const DataSourceProvider = (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 service = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_client__.useRequest)(()=>Promise.resolve({
            data: [
                {
                    id: 1,
                    name: 'Name1'
                },
                {
                    id: 2,
                    name: 'Name2'
                },
                {
                    id: 3,
                    name: 'Name3'
                }
            ]
        }), {
        onSuccess (data) {
            setDataSource(data.data);
        }
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataSourceContext.Provider, {
        value: {
            service,
            dataSource,
            setDataSource,
            selectedRowKeys,
            setSelectedRowKeys
        },
        children: props.children
    });
}, {
    displayName: 'DataSourceProvider'
});
const Root = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_client__.SchemaComponentProvider, {
        scope: {
            ds
        },
        components: {
            Action: __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Action,
            DataSourceProvider,
            Table: __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Table,
            Input: __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Input
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_client__.SchemaComponent, {
            schema: schema
        })
    });
const app = new __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Application({
    providers: [
        Root
    ]
});
const demo4_rslib_entry_ = app.getRootComponent();
export { demo4_rslib_entry_ as default };
