"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__, {
    useBulkDestroyActionAndRefreshCM: ()=>useBulkDestroyActionAndRefreshCM,
    DeleteCollection: ()=>DeleteCollection,
    DeleteCollectionAction: ()=>DeleteCollectionAction,
    useBulkDestroyAction: ()=>useBulkDestroyAction,
    useDestroyAction: ()=>useDestroyAction,
    useDestroyActionAndRefreshCM: ()=>useDestroyActionAndRefreshCM
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_action_hooks_js_namespaceObject = require("../action-hooks.js");
const external_components_index_js_namespaceObject = require("./components/index.js");
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      .ant-modal-body {\n        margin-left: 35px;\n        margin-bottom: 35px;\n        .ant-checkbox-wrapper {\n          height: 25px;\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const DeleteCollection = (props)=>{
    const record = (0, index_js_namespaceObject.useRecord)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DeleteCollectionAction, {
        item: record,
        ...props
    });
};
const useDestroyActionAndRefreshCM = ()=>{
    const { run } = useDestroyAction();
    const { refreshCM } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    return {
        async run () {
            await run();
            await refreshCM();
        }
    };
};
const useBulkDestroyActionAndRefreshCM = ()=>{
    const { run } = useBulkDestroyAction();
    const { refreshCM } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    return {
        async run () {
            await run();
            await refreshCM();
        }
    };
};
const useDestroyAction = ()=>{
    const { refresh } = (0, external_index_js_namespaceObject.useResourceActionContext)();
    const { resource, targetKey } = (0, external_index_js_namespaceObject.useResourceContext)();
    const { [targetKey]: filterByTk } = (0, index_js_namespaceObject.useRecord)();
    const form = (0, schema_namespaceObject.useForm)();
    const { cascade } = (null == form ? void 0 : form.values) || {};
    return {
        async run () {
            await resource.destroy({
                filterByTk,
                cascade
            });
            refresh();
        }
    };
};
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        modal: css(_templateObject())
    };
});
const useBulkDestroyAction = ()=>{
    const { state, setState, refresh } = (0, external_index_js_namespaceObject.useResourceActionContext)();
    const { resource } = (0, external_index_js_namespaceObject.useResourceContext)();
    const ctx = (0, external_index_js_namespaceObject.useActionContext)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { message } = external_antd_namespaceObject.App.useApp();
    const form = (0, schema_namespaceObject.useForm)();
    const { cascade } = (null == form ? void 0 : form.values) || {};
    return {
        async run () {
            var _state_selectedRowKeys, _ctx_setVisible;
            if (!(null == state ? void 0 : null === (_state_selectedRowKeys = state.selectedRowKeys) || void 0 === _state_selectedRowKeys ? void 0 : _state_selectedRowKeys.length)) return message.error(t('Please select the records you want to delete'));
            await resource.destroy({
                filterByTk: (null == state ? void 0 : state.selectedRowKeys) || [],
                cascade
            });
            form.reset();
            null == ctx || null === (_ctx_setVisible = ctx.setVisible) || void 0 === _ctx_setVisible || _ctx_setVisible.call(ctx, false);
            null == setState || setState({
                selectedRowKeys: []
            });
            refresh();
        }
    };
};
const DeleteCollectionAction = (props)=>{
    const { scope, getContainer, item: record, children, isBulk, useAction, ...otherProps } = props;
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { styles } = useStyles();
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const getDestroyCollectionAction = ()=>{
        if (isBulk) return useBulkDestroyActionAndRefreshCM;
        if (useAction) return useAction;
        return useDestroyActionAndRefreshCM;
    };
    const Title = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.ExclamationCircleFilled, {
                    style: {
                        color: '#faad14',
                        marginRight: '12px',
                        fontSize: '22px'
                    }
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    style: {
                        fontSize: '19px'
                    },
                    children: t('Delete collection')
                })
            ]
        });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RecordProvider, {
        record: record,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_component_index_js_namespaceObject.ActionContextProvider, {
            value: {
                visible,
                setVisible
            },
            children: [
                isBulk ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                    icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DeleteOutlined, {}),
                    onClick: ()=>setVisible(true),
                    children: children || t('Delete')
                }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
                    onClick: ()=>setVisible(true),
                    ...otherProps,
                    children: children || t('Delete')
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
                    schema: {
                        type: 'object',
                        properties: {
                            deleteCollection: {
                                type: 'void',
                                'x-decorator': 'Form',
                                'x-component': 'Action.Modal',
                                title: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Title, {}),
                                'x-component-props': {
                                    width: 520,
                                    getContainer: '{{ getContainer }}',
                                    className: styles.modal
                                },
                                properties: {
                                    info: {
                                        type: 'string',
                                        'x-component': 'div',
                                        'x-content': "{{t('Are you sure you want to delete it?')}}"
                                    },
                                    cascade: {
                                        type: 'boolean',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Checkbox',
                                        default: false,
                                        'x-content': t('Automatically drop objects that depend on the collection (such as views), and in turn all objects that depend on those objects')
                                    },
                                    footer: {
                                        type: 'void',
                                        'x-component': 'Action.Modal.Footer',
                                        properties: {
                                            action1: {
                                                title: '{{ t("Cancel") }}',
                                                'x-component': 'Action',
                                                'x-component-props': {
                                                    useAction: '{{ useCancelAction }}'
                                                }
                                            },
                                            action2: {
                                                title: '{{ t("Ok") }}',
                                                'x-component': 'Action',
                                                'x-component-props': {
                                                    type: 'primary',
                                                    useAction: '{{ useDestroyCollectionAction }}',
                                                    style: {
                                                        marginLeft: '8px'
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    components: {
                        ...external_components_index_js_namespaceObject
                    },
                    scope: {
                        getContainer,
                        useDestroyCollectionAction: getDestroyCollectionAction(),
                        useCancelAction: external_action_hooks_js_namespaceObject.useCancelAction,
                        ...scope
                    }
                })
            ]
        })
    });
};
DeleteCollectionAction.displayName = 'DeleteCollectionAction';
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
});
