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