"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    default: ()=>demo4_rslib_entry_
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/client");
const schema_namespaceObject = require("@tachybase/schema");
const DataSourceContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
DataSourceContext.displayName = 'DataSourceContext';
const useSelectedRowKeys = ()=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext);
    return [
        ctx.selectedRowKeys,
        ctx.setSelectedRowKeys
    ];
};
const useDataSource = (options)=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext);
    return (0, client_namespaceObject.useRequest)(()=>Promise.resolve({
            data: ctx.dataSource
        }), {
        ...options,
        refreshDeps: [
            JSON.stringify(ctx.dataSource)
        ]
    });
};
const useCreateAction = ()=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            dataSource.push({
                id: (0, schema_namespaceObject.uid)(),
                name: (0, schema_namespaceObject.uid)()
            });
            ctx.setDataSource([
                ...dataSource
            ]);
        }
    };
};
const useBulkDestroyAction = ()=>{
    const ctx = (0, external_react_namespaceObject.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, client_namespaceObject.useRecord)();
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext);
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            ctx.setDataSource([
                ...dataSource.map((item)=>{
                    if (record.id === item.id) {
                        record.name = (0, schema_namespaceObject.uid)();
                        return record;
                    }
                    return item;
                })
            ]);
        }
    };
};
const useDestroyAction = ()=>{
    const record = (0, client_namespaceObject.useRecord)();
    const ctx = (0, external_react_namespaceObject.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, schema_namespaceObject.observer)((props)=>{
    const [selectedRowKeys, setSelectedRowKeys] = (0, external_react_namespaceObject.useState)([]);
    const [dataSource, setDataSource] = (0, external_react_namespaceObject.useState)([]);
    const service = (0, client_namespaceObject.useRequest)(()=>Promise.resolve({
            data: [
                {
                    id: 1,
                    name: 'Name1'
                },
                {
                    id: 2,
                    name: 'Name2'
                },
                {
                    id: 3,
                    name: 'Name3'
                }
            ]
        }), {
        onSuccess (data) {
            setDataSource(data.data);
        }
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DataSourceContext.Provider, {
        value: {
            service,
            dataSource,
            setDataSource,
            selectedRowKeys,
            setSelectedRowKeys
        },
        children: props.children
    });
}, {
    displayName: 'DataSourceProvider'
});
const Root = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(client_namespaceObject.SchemaComponentProvider, {
        scope: {
            ds
        },
        components: {
            Action: client_namespaceObject.Action,
            DataSourceProvider,
            Table: client_namespaceObject.Table,
            Input: client_namespaceObject.Input
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(client_namespaceObject.SchemaComponent, {
            schema: schema
        })
    });
const app = new client_namespaceObject.Application({
    providers: [
        Root
    ]
});
const demo4_rslib_entry_ = app.getRootComponent();
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
});
