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_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__ from "../../i18n/index.mjs";
import "../../style/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__acl_index_mjs__ from "../acl/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PluginCard_mjs__ from "./PluginCard.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PluginDetail_mjs__ from "./PluginDetail.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const ViewAction = (param)=>{
    let { record } = param;
    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)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                type: "link",
                onClick: ()=>{
                    setPlugin(record);
                },
                children: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('View')
            })
        ]
    });
};
const PluginDescription = (param)=>{
    let { description, record } = param;
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Typography.Paragraph, {
        type: record.isCompatible ? 'secondary' : 'danger',
        children: record.isCompatible ? description : __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('Plugin dependencies check failed')
    });
};
const columns = [
    {
        title: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('Name'),
        dataIndex: 'name',
        key: 'name',
        render: (_, param)=>{
            let { displayName, name, packageName } = param;
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                children: displayName || name || packageName
            });
        }
    },
    {
        title: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('Keywords'),
        dataIndex: 'keywords',
        key: 'keywords',
        render: (keywords)=>null == keywords ? void 0 : keywords.map((keyword)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                    children: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t(keyword)
                }, keyword))
    },
    {
        title: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('Description'),
        dataIndex: 'description',
        key: 'description',
        render: (description, record)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(PluginDescription, {
                description: description,
                record: record
            })
    },
    {
        title: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t('Action'),
        key: 'action',
        render: (_, record)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                size: "middle",
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ViewAction, {
                        record: record
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__PluginCard_mjs__.SwitchAction, {
                        ...record
                    })
                ]
            })
    }
];
const LocalPlugins = ()=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const { data, loading, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)({
        url: 'pm:list'
    });
    const [searchValue, setSearchValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('');
    const [enabled, setEnabled] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('all');
    const [keywords, setKeywords] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const filteredList = ((null == data ? void 0 : data.data) || []).filter((data)=>{
        var _data_description;
        return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.fuzzysearch)(searchValue, data.name) || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.fuzzysearch)(searchValue, data.packageName) || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.fuzzysearch)(searchValue, null !== (_data_description = data.description) && void 0 !== _data_description ? _data_description : '') || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.fuzzysearch)(searchValue, data.displayName);
    }).filter((data)=>'all' === enabled || 'enabled' === enabled && data.enabled || 'disabled' === enabled && !data.enabled).filter((data)=>0 === keywords.length || keywords.some((keyword)=>{
            var _data_keywords;
            return null === (_data_keywords = data.keywords) || void 0 === _data_keywords ? void 0 : _data_keywords.includes(keyword);
        }));
    const filterList = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        let list = (null == data ? void 0 : data.data) || [];
        list = list.reverse();
        return [
            {
                type: 'All',
                list: list
            },
            {
                type: 'Built-in',
                list: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(list, (item)=>item.builtIn)
            },
            {
                type: 'Enabled',
                list: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(list, (item)=>item.enabled)
            },
            {
                type: 'Not enabled',
                list: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(list, (item)=>!item.enabled)
            },
            {
                type: 'Problematic',
                list: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(list, (item)=>!item.isCompatible)
            }
        ];
    }, [
        null == data ? void 0 : data.data
    ]);
    const [filterIndex, setFilterIndex] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
    const [keyword, setKeyword] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    const debouncedSearchValue = (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useDebounce)(searchValue, {
        wait: 100
    });
    const keyWordlists = [
        'Data model tools',
        'Data sources',
        'Collections',
        'Collection fields',
        'Blocks',
        'Actions',
        'Workflow',
        'Users & permissions',
        'Authentication',
        'System management',
        'Logging and monitoring',
        'Others'
    ];
    const keyWordsfilterList = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const list = keyWordlists.map((i)=>{
            if ('Others' === i) {
                const result = null == data ? void 0 : data.data.filter((v)=>!v.keywords || !v.keywords.every((k)=>keyWordlists.includes(k)));
                return {
                    key: i,
                    list: result
                };
            }
            const result = null == data ? void 0 : data.data.filter((v)=>{
                var _v_keywords;
                return null === (_v_keywords = v.keywords) || void 0 === _v_keywords ? void 0 : _v_keywords.includes(i);
            });
            return {
                key: i,
                list: result
            };
        });
        return list;
    }, [
        keyWordlists
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _filterList_filterIndex;
        let list = (null === (_filterList_filterIndex = filterList[filterIndex]) || void 0 === _filterList_filterIndex ? void 0 : _filterList_filterIndex.list) || [];
        if (!filterIndex && keyword) list = keyWordsfilterList.find((v)=>v.key === keyword).list;
        else if (filterIndex && keyword) {
            const keyList = keyWordsfilterList.find((v)=>v.key === keyword).list;
            list = keyList.filter((value)=>list.find((k)=>k.name === value.name));
        }
        const searchLowerCaseValue = debouncedSearchValue.toLocaleLowerCase().trim();
        if (searchLowerCaseValue) list = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(list, (item)=>String(item.displayName || '').toLocaleLowerCase().includes(searchLowerCaseValue) || String(item.description || '').toLocaleLowerCase().includes(searchLowerCaseValue));
        return list.sort((a, b)=>a.displayName.localeCompare(b.displayName));
    }, [
        filterIndex,
        filterList,
        debouncedSearchValue,
        keyword
    ]);
    if (loading) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {});
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
            style: {
                marginTop: '8px'
            },
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Form, {
                    style: {
                        marginBottom: '8px'
                    },
                    layout: "inline",
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Form.Item, {
                            label: t('Status'),
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Radio.Group, {
                                options: [
                                    {
                                        label: t('all'),
                                        value: 'all'
                                    },
                                    {
                                        label: t('enabled'),
                                        value: 'enabled'
                                    },
                                    {
                                        label: t('disabled'),
                                        value: 'disabled'
                                    }
                                ],
                                onChange: (e)=>setEnabled(e.target.value),
                                value: enabled,
                                optionType: "button"
                            })
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Form.Item, {
                            label: t('Keywords'),
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
                                mode: "multiple",
                                style: {
                                    minWidth: '20em'
                                },
                                allowClear: true,
                                value: keywords,
                                placeholder: t('Please select'),
                                onChange: setKeywords,
                                options: keyWordlists.map((keyword)=>({
                                        label: t(keyword),
                                        value: keyword
                                    }))
                            })
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Form.Item, {
                            label: t('Search'),
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input, {
                                value: searchValue,
                                placeholder: t('Name or descriptions'),
                                onChange: (e)=>setSearchValue(e.target.value)
                            })
                        })
                    ]
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Table, {
                    columns: columns,
                    dataSource: filteredList,
                    rowKey: "id",
                    pagination: {
                        pageSize: 100
                    }
                })
            ]
        })
    });
};
const PluginManager = ()=>{
    const { snippets = [] } = (0, __WEBPACK_EXTERNAL_MODULE__acl_index_mjs__.useACLRoleContext)();
    return snippets.includes('pm') ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(LocalPlugins, {}) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Result, {
        status: "404",
        title: "404",
        subTitle: "Sorry, the page you visited does not exist."
    });
};
export { PluginManager };
