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__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__ from "../action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
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, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DeleteCollectionAction, {
        item: record,
        ...props
    });
};
const useDestroyActionAndRefreshCM = ()=>{
    const { run } = useDestroyAction();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    return {
        async run () {
            await run();
            await refreshCM();
        }
    };
};
const useBulkDestroyActionAndRefreshCM = ()=>{
    const { run } = useBulkDestroyAction();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    return {
        async run () {
            await run();
            await refreshCM();
        }
    };
};
const useDestroyAction = ()=>{
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useResourceActionContext)();
    const { resource, targetKey } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useResourceContext)();
    const { [targetKey]: filterByTk } = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { cascade } = (null == form ? void 0 : form.values) || {};
    return {
        async run () {
            await resource.destroy({
                filterByTk,
                cascade
            });
            refresh();
        }
    };
};
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        modal: css(_templateObject())
    };
});
const useBulkDestroyAction = ()=>{
    const { state, setState, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useResourceActionContext)();
    const { resource } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useResourceContext)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { styles } = useStyles();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const getDestroyCollectionAction = ()=>{
        if (isBulk) return useBulkDestroyActionAndRefreshCM;
        if (useAction) return useAction;
        return useDestroyActionAndRefreshCM;
    };
    const Title = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("span", {
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.ExclamationCircleFilled, {
                    style: {
                        color: '#faad14',
                        marginRight: '12px',
                        fontSize: '22px'
                    }
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                    style: {
                        fontSize: '19px'
                    },
                    children: t('Delete collection')
                })
            ]
        });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.ActionContextProvider, {
            value: {
                visible,
                setVisible
            },
            children: [
                isBulk ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                    icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DeleteOutlined, {}),
                    onClick: ()=>setVisible(true),
                    children: children || t('Delete')
                }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                    onClick: ()=>setVisible(true),
                    ...otherProps,
                    children: children || t('Delete')
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                    schema: {
                        type: 'object',
                        properties: {
                            deleteCollection: {
                                type: 'void',
                                'x-decorator': 'Form',
                                'x-component': 'Action.Modal',
                                title: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs__
                    },
                    scope: {
                        getContainer,
                        useDestroyCollectionAction: getDestroyCollectionAction(),
                        useCancelAction: __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useCancelAction,
                        ...scope
                    }
                })
            ]
        })
    });
};
DeleteCollectionAction.displayName = 'DeleteCollectionAction';
export { DeleteCollection, DeleteCollectionAction, useBulkDestroyAction, useBulkDestroyActionAndRefreshCM, useDestroyAction, useDestroyActionAndRefreshCM };
