"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    PluginManager: ()=>PluginManager
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const external_ahooks_namespaceObject = require("ahooks");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_i18n_index_js_namespaceObject = require("../../i18n/index.js");
require("../../style/index.js");
const external_acl_index_js_namespaceObject = require("../acl/index.js");
const external_PluginCard_js_namespaceObject = require("./PluginCard.js");
const external_PluginDetail_js_namespaceObject = require("./PluginDetail.js");
const external_style_js_namespaceObject = require("./style.js");
const ViewAction = (param)=>{
    let { record } = param;
    const [plugin, setPlugin] = (0, external_react_namespaceObject.useState)(void 0);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            plugin && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_PluginDetail_js_namespaceObject.PluginDetail, {
                plugin: plugin,
                onCancel: ()=>setPlugin(void 0)
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                type: "link",
                onClick: ()=>{
                    setPlugin(record);
                },
                children: external_i18n_index_js_namespaceObject.i18n.t('View')
            })
        ]
    });
};
const PluginDescription = (param)=>{
    let { description, record } = param;
    const { theme } = (0, external_style_js_namespaceObject.useStyles)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Typography.Paragraph, {
        type: record.isCompatible ? 'secondary' : 'danger',
        children: record.isCompatible ? description : external_i18n_index_js_namespaceObject.i18n.t('Plugin dependencies check failed')
    });
};
const columns = [
    {
        title: external_i18n_index_js_namespaceObject.i18n.t('Name'),
        dataIndex: 'name',
        key: 'name',
        render: (_, param)=>{
            let { displayName, name, packageName } = param;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                children: displayName || name || packageName
            });
        }
    },
    {
        title: external_i18n_index_js_namespaceObject.i18n.t('Keywords'),
        dataIndex: 'keywords',
        key: 'keywords',
        render: (keywords)=>null == keywords ? void 0 : keywords.map((keyword)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                    children: external_i18n_index_js_namespaceObject.i18n.t(keyword)
                }, keyword))
    },
    {
        title: external_i18n_index_js_namespaceObject.i18n.t('Description'),
        dataIndex: 'description',
        key: 'description',
        render: (description, record)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(PluginDescription, {
                description: description,
                record: record
            })
    },
    {
        title: external_i18n_index_js_namespaceObject.i18n.t('Action'),
        key: 'action',
        render: (_, record)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                size: "middle",
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ViewAction, {
                        record: record
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_PluginCard_js_namespaceObject.SwitchAction, {
                        ...record
                    })
                ]
            })
    }
];
const LocalPlugins = ()=>{
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { theme } = (0, external_style_js_namespaceObject.useStyles)();
    const { data, loading, refresh } = (0, index_js_namespaceObject.useRequest)({
        url: 'pm:list'
    });
    const [searchValue, setSearchValue] = (0, external_react_namespaceObject.useState)('');
    const [enabled, setEnabled] = (0, external_react_namespaceObject.useState)('all');
    const [keywords, setKeywords] = (0, external_react_namespaceObject.useState)([]);
    const filteredList = ((null == data ? void 0 : data.data) || []).filter((data)=>{
        var _data_description;
        return (0, client_namespaceObject.fuzzysearch)(searchValue, data.name) || (0, client_namespaceObject.fuzzysearch)(searchValue, data.packageName) || (0, client_namespaceObject.fuzzysearch)(searchValue, null !== (_data_description = data.description) && void 0 !== _data_description ? _data_description : '') || (0, client_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>{
        let list = (null == data ? void 0 : data.data) || [];
        list = list.reverse();
        return [
            {
                type: 'All',
                list: list
            },
            {
                type: 'Built-in',
                list: external_lodash_default().filter(list, (item)=>item.builtIn)
            },
            {
                type: 'Enabled',
                list: external_lodash_default().filter(list, (item)=>item.enabled)
            },
            {
                type: 'Not enabled',
                list: external_lodash_default().filter(list, (item)=>!item.enabled)
            },
            {
                type: 'Problematic',
                list: external_lodash_default().filter(list, (item)=>!item.isCompatible)
            }
        ];
    }, [
        null == data ? void 0 : data.data
    ]);
    const [filterIndex, setFilterIndex] = (0, external_react_namespaceObject.useState)(0);
    const [keyword, setKeyword] = (0, external_react_namespaceObject.useState)(null);
    const debouncedSearchValue = (0, external_ahooks_namespaceObject.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, external_react_namespaceObject.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, external_react_namespaceObject.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 = external_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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {});
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Card, {
            style: {
                marginTop: '8px'
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Form, {
                    style: {
                        marginBottom: '8px'
                    },
                    layout: "inline",
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Form.Item, {
                            label: t('Status'),
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Form.Item, {
                            label: t('Keywords'),
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Form.Item, {
                            label: t('Search'),
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Input, {
                                value: searchValue,
                                placeholder: t('Name or descriptions'),
                                onChange: (e)=>setSearchValue(e.target.value)
                            })
                        })
                    ]
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Table, {
                    columns: columns,
                    dataSource: filteredList,
                    rowKey: "id",
                    pagination: {
                        pageSize: 100
                    }
                })
            ]
        })
    });
};
const PluginManager = ()=>{
    const { snippets = [] } = (0, external_acl_index_js_namespaceObject.useACLRoleContext)();
    return snippets.includes('pm') ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(LocalPlugins, {}) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Result, {
        status: "404",
        title: "404",
        subTitle: "Sorry, the page you visited does not exist."
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
