"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__, {
    ds: ()=>ds,
    SubFieldDataSourceProvider_deprecated: ()=>SubFieldDataSourceProvider_deprecated,
    DataSourceContext_deprecated: ()=>DataSourceContext_deprecated,
    DataSourceProvider_deprecated: ()=>DataSourceProvider_deprecated
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
const external_index_js_namespaceObject = require("../index.js");
const index_js_namespaceObject = require("../api-client/index.js");
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, external_react_namespaceObject.createContext)(null);
DataSourceContext_deprecated.displayName = 'DataSourceContext_deprecated';
const useSelectedRowKeys = ()=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext_deprecated);
    return [
        ctx.selectedRowKeys,
        ctx.setSelectedRowKeys
    ];
};
const useDataSource_deprecated = (options)=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext_deprecated);
    return (0, external_index_js_namespaceObject.useRequest)(()=>Promise.resolve({
            data: ctx.dataSource
        }), {
        ...options,
        refreshDeps: [
            JSON.stringify(ctx.dataSource)
        ]
    });
};
const useCreateAction = ()=>{
    const ctx = (0, external_react_namespaceObject.useContext)(DataSourceContext_deprecated);
    const form = (0, schema_namespaceObject.useForm)();
    const { setVisible } = (0, external_index_js_namespaceObject.useActionContext)();
    return {
        async run () {
            const dataSource = ctx.dataSource || [];
            dataSource.push((0, external_lodash_namespaceObject.cloneDeep)(form.values));
            ctx.setDataSource([
                ...dataSource
            ]);
            setVisible(false);
            await form.reset();
        }
    };
};
const useBulkDestroyAction = ()=>{
    const ctx = (0, external_react_namespaceObject.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, external_index_js_namespaceObject.useRecordIndex)();
    const form = (0, schema_namespaceObject.useForm)();
    const { setVisible } = (0, external_index_js_namespaceObject.useActionContext)();
    const ctx = (0, external_react_namespaceObject.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, external_index_js_namespaceObject.useRecordIndex)();
    const ctx = (0, external_react_namespaceObject.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, schema_namespaceObject.observer)((props)=>{
    const [selectedRowKeys, setSelectedRowKeys] = (0, external_react_namespaceObject.useState)([]);
    const [dataSource, setDataSource] = (0, external_react_namespaceObject.useState)([]);
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const service = (0, external_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.CollectionProvider_deprecated, {
        collection: sub_table_rslib_entry_collection,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DataSourceContext_deprecated.Provider, {
            value: {
                rowKey: 'name',
                service,
                dataSource,
                setDataSource,
                selectedRowKeys,
                setSelectedRowKeys
            },
            children: props.children
        })
    });
}, {
    displayName: 'SubFieldDataSourceProvider_deprecated'
});
const DataSourceProvider_deprecated = (0, schema_namespaceObject.observer)((props)=>{
    const { rowKey, collection, association } = props;
    const [selectedRowKeys, setSelectedRowKeys] = (0, external_react_namespaceObject.useState)([]);
    const [dataSource, setDataSource] = (0, external_react_namespaceObject.useState)([]);
    const record = (0, external_index_js_namespaceObject.useRecord)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const { getCollection } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    const coll = getCollection(collection);
    const resourceOf = null == record ? void 0 : record[association.targetKey || 'id'];
    const service = (0, external_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.CollectionProvider_deprecated, {
        collection: coll,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DataSourceContext_deprecated.Provider, {
            value: {
                rowKey,
                service,
                dataSource,
                setDataSource,
                selectedRowKeys,
                setSelectedRowKeys
            },
            children: props.children
        })
    });
}, {
    displayName: 'DataSourceProvider_deprecated'
});
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
});
