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__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PluginDetail_mjs__ from "./PluginDetail.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PluginForm_form_PluginNpmForm_mjs__ from "./PluginForm/form/PluginNpmForm.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PluginForm_modal_PluginUpgradeModal_mjs__ from "./PluginForm/modal/PluginUpgradeModal.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const SwitchAction = (props)=>{
    const { name, enabled, builtIn, error, isCompatible } = props;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { modal, message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Switch, {
        "aria-label": "enable",
        size: 'small',
        disabled: builtIn || error,
        onChange: async (checked, e)=>{
            e.stopPropagation();
            if (!isCompatible && checked) {
                message.error(t("Dependencies check failed, can't enable."));
                return;
            }
            if (checked) await api.request({
                url: "pm:enable",
                params: {
                    filterByTk: name
                }
            });
            else modal.confirm({
                title: t('Are you sure to disable this plugin?'),
                onOk: async ()=>{
                    await api.request({
                        url: "pm:disable",
                        params: {
                            filterByTk: name
                        }
                    });
                }
            });
        },
        checked: !!enabled
    }, 'enable');
};
function PluginInfo(props) {
    const { data, onClick } = props;
    const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const { name, displayName, isCompatible, packageName, updatable, builtIn, enabled, description, error, homepage } = data;
    const { styles, theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useNavigate)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { modal, message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const [showUploadForm, setShowUploadForm] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const title = displayName || name || packageName;
    const quickUpdate = ()=>{
        api.request({
            url: 'pm:update',
            method: 'post',
            data: {
                name,
                packageName,
                registry: __WEBPACK_EXTERNAL_MODULE__PluginForm_form_PluginNpmForm_mjs__.NPM_REGISTRY_ADDRESS
            }
        });
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: [
            showUploadForm && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__PluginForm_modal_PluginUpgradeModal_mjs__.PluginUpgradeModal, {
                isShow: showUploadForm,
                pluginData: data,
                onClose: (isRefresh)=>{
                    setShowUploadForm(false);
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
                role: "button",
                "aria-label": title,
                size: 'small',
                bordered: false,
                onClick: ()=>{
                    error || onClick();
                },
                headStyle: {
                    border: 'none',
                    minHeight: 'inherit',
                    paddingTop: 14
                },
                bodyStyle: {
                    paddingTop: 10
                },
                title: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: title
                }),
                hoverable: true,
                className: styles.card,
                actions: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                        split: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Divider, {
                            type: "vertical"
                        }),
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("a", {
                                href: homepage,
                                target: "_blank",
                                onClick: (event)=>{
                                    event.stopPropagation();
                                },
                                rel: "noreferrer",
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.ReadOutlined, {}),
                                    " ",
                                    t('Docs')
                                ]
                            }, '5'),
                            updatable && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("a", {
                                        onClick: (e)=>{
                                            e.stopPropagation();
                                            setShowUploadForm(true);
                                        },
                                        children: [
                                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.ReloadOutlined, {}),
                                            " ",
                                            t('Update')
                                        ]
                                    }, '3'),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("a", {
                                        onClick: (e)=>{
                                            e.stopPropagation();
                                            quickUpdate();
                                        },
                                        children: [
                                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.ClockCircleOutlined, {}),
                                            " ",
                                            t('Quick Update')
                                        ]
                                    })
                                ]
                            }),
                            enabled ? app.systemSettingsManager.has(name) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("a", {
                                onClick: (e)=>{
                                    e.stopPropagation();
                                    navigate(app.systemSettingsManager.getRoutePath(name));
                                },
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.SettingOutlined, {}),
                                    " ",
                                    t('Settings')
                                ]
                            }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Popconfirm, {
                                disabled: builtIn,
                                title: t('Are you sure to delete this plugin?'),
                                onConfirm: async (e)=>{
                                    e.stopPropagation();
                                    api.request({
                                        url: "pm:remove",
                                        params: {
                                            filterByTk: name
                                        }
                                    });
                                },
                                onCancel: (e)=>e.stopPropagation(),
                                okText: t('Yes'),
                                cancelText: t('No'),
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("a", {
                                    onClick: (e)=>{
                                        e.stopPropagation();
                                    },
                                    className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])({
                                        [styles.cardActionDisabled]: builtIn
                                    }),
                                    children: [
                                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DeleteOutlined, {}),
                                        " ",
                                        t('Remove')
                                    ]
                                })
                            }, 'delete')
                        ]
                    }, '1'),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Switch, {
                        "aria-label": "enable",
                        size: 'small',
                        disabled: builtIn || error,
                        onChange: async (checked, e)=>{
                            e.stopPropagation();
                            if (!isCompatible && checked) {
                                message.error(t("Dependencies check failed, can't enable."));
                                return;
                            }
                            if (checked) await api.request({
                                url: "pm:enable",
                                params: {
                                    filterByTk: name
                                }
                            });
                            else modal.confirm({
                                title: t('Are you sure to disable this plugin?'),
                                onOk: async ()=>{
                                    await api.request({
                                        url: "pm:disable",
                                        params: {
                                            filterByTk: name
                                        }
                                    });
                                }
                            });
                        },
                        checked: !!enabled
                    }, 'enable')
                ].filter(Boolean),
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card.Meta, {
                    description: error ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Typography.Text, {
                        type: "danger",
                        children: t('Plugin loading failed. Please check the server logs.')
                    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Typography.Paragraph, {
                        style: {
                            height: theme.fontSize * theme.lineHeight * 3
                        },
                        type: isCompatible ? 'secondary' : 'danger',
                        ellipsis: {
                            rows: 3
                        },
                        children: isCompatible ? description : t('Plugin dependencies check failed')
                    })
                })
            })
        ]
    });
}
const PluginCard = (props)=>{
    const { data } = props;
    const [plugin, setPlugin] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(void 0);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: [
            plugin && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__PluginDetail_mjs__.PluginDetail, {
                plugin: plugin,
                onCancel: ()=>setPlugin(void 0)
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(PluginInfo, {
                onClick: ()=>{
                    setPlugin(data);
                },
                data: data
            })
        ]
    });
};
export { PluginCard, SwitchAction };
