import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { ClockCircleOutlined, DeleteOutlined, ReadOutlined, ReloadOutlined, SettingOutlined } from "@ant-design/icons";
import { App, Card, Divider, Popconfirm, Space, Switch, Typography } from "antd";
import classnames from "classnames";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { useAPIClient } from "../../api-client/index.mjs";
import { useApp } from "../../application/index.mjs";
import { PluginDetail } from "./PluginDetail.mjs";
import { NPM_REGISTRY_ADDRESS } from "./PluginForm/form/PluginNpmForm.mjs";
import { PluginUpgradeModal } from "./PluginForm/modal/PluginUpgradeModal.mjs";
import { useStyles } from "./style.mjs";
const SwitchAction = (props)=>{
    const { name, enabled, builtIn, error, isCompatible } = props;
    const api = useAPIClient();
    const { t } = useTranslation();
    const { modal, message } = App.useApp();
    return /*#__PURE__*/ jsx(Switch, {
        "aria-label": "enable",
        size: 'small',
        disabled: builtIn || error,
        onChange: async (checked, e)=>{
            e.stopPropagation();
            if (!isCompatible && checked) return void message.error(t("Dependencies check failed, can't enable."));
            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 = useApp();
    const { name, displayName, isCompatible, packageName, updatable, builtIn, enabled, description, error, homepage } = data;
    const { styles, theme } = useStyles();
    const navigate = useNavigate();
    const { t } = useTranslation();
    const api = useAPIClient();
    const { modal, message } = App.useApp();
    const [showUploadForm, setShowUploadForm] = useState(false);
    const title = displayName || name || packageName;
    const quickUpdate = ()=>{
        api.request({
            url: 'pm:update',
            method: 'post',
            data: {
                name,
                packageName,
                registry: NPM_REGISTRY_ADDRESS
            }
        });
    };
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            showUploadForm && /*#__PURE__*/ jsx(PluginUpgradeModal, {
                isShow: showUploadForm,
                pluginData: data,
                onClose: (isRefresh)=>{
                    setShowUploadForm(false);
                }
            }),
            /*#__PURE__*/ jsx(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__*/ jsx("div", {
                    children: title
                }),
                hoverable: true,
                className: styles.card,
                actions: [
                    /*#__PURE__*/ jsxs(Space, {
                        split: /*#__PURE__*/ jsx(Divider, {
                            type: "vertical"
                        }),
                        children: [
                            /*#__PURE__*/ jsxs("a", {
                                href: homepage,
                                target: "_blank",
                                onClick: (event)=>{
                                    event.stopPropagation();
                                },
                                rel: "noreferrer",
                                children: [
                                    /*#__PURE__*/ jsx(ReadOutlined, {}),
                                    " ",
                                    t('Docs')
                                ]
                            }, '5'),
                            updatable && /*#__PURE__*/ jsxs(Fragment, {
                                children: [
                                    /*#__PURE__*/ jsxs("a", {
                                        onClick: (e)=>{
                                            e.stopPropagation();
                                            setShowUploadForm(true);
                                        },
                                        children: [
                                            /*#__PURE__*/ jsx(ReloadOutlined, {}),
                                            " ",
                                            t('Update')
                                        ]
                                    }, '3'),
                                    /*#__PURE__*/ jsxs("a", {
                                        onClick: (e)=>{
                                            e.stopPropagation();
                                            quickUpdate();
                                        },
                                        children: [
                                            /*#__PURE__*/ jsx(ClockCircleOutlined, {}),
                                            " ",
                                            t('Quick Update')
                                        ]
                                    })
                                ]
                            }),
                            enabled ? app.systemSettingsManager.has(name) && /*#__PURE__*/ jsxs("a", {
                                onClick: (e)=>{
                                    e.stopPropagation();
                                    navigate(app.systemSettingsManager.getRoutePath(name));
                                },
                                children: [
                                    /*#__PURE__*/ jsx(SettingOutlined, {}),
                                    " ",
                                    t('Settings')
                                ]
                            }) : /*#__PURE__*/ jsx(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__*/ jsxs("a", {
                                    onClick: (e)=>{
                                        e.stopPropagation();
                                    },
                                    className: classnames({
                                        [styles.cardActionDisabled]: builtIn
                                    }),
                                    children: [
                                        /*#__PURE__*/ jsx(DeleteOutlined, {}),
                                        " ",
                                        t('Remove')
                                    ]
                                })
                            }, 'delete')
                        ]
                    }, '1'),
                    /*#__PURE__*/ jsx(Switch, {
                        "aria-label": "enable",
                        size: 'small',
                        disabled: builtIn || error,
                        onChange: async (checked, e)=>{
                            e.stopPropagation();
                            if (!isCompatible && checked) return void message.error(t("Dependencies check failed, can't enable."));
                            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__*/ jsx(Card.Meta, {
                    description: error ? /*#__PURE__*/ jsx(Typography.Text, {
                        type: "danger",
                        children: t('Plugin loading failed. Please check the server logs.')
                    }) : /*#__PURE__*/ jsx(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] = useState(void 0);
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            plugin && /*#__PURE__*/ jsx(PluginDetail, {
                plugin: plugin,
                onCancel: ()=>setPlugin(void 0)
            }),
            /*#__PURE__*/ jsx(PluginInfo, {
                onClick: ()=>{
                    setPlugin(data);
                },
                data: data
            })
        ]
    });
};
export { PluginCard, SwitchAction };
