"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    MenuConfigure: ()=>MenuConfigure
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../api-client/index.js");
const external_record_provider_index_js_namespaceObject = require("../../../record-provider/index.js");
const external_style_js_namespaceObject = require("../style.js");
const external_MenuItemsProvider_js_namespaceObject = require("./MenuItemsProvider.js");
const findUids = (items)=>{
    if (!Array.isArray(items)) return [];
    const uids = [];
    for (const item of items){
        uids.push(item.uid);
        uids.push(...findUids(item.children));
    }
    return uids;
};
const getParentUids = function(tree, func) {
    let path = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : [];
    if (!tree) return [];
    for (const data of tree){
        path.push(data.uid);
        if (func(data)) return path;
        if (data.children) {
            const findChildren = getParentUids(data.children, func, path);
            if (findChildren.length) return findChildren;
        }
        path.pop();
    }
    return [];
};
const getChildrenUids = function() {
    let data = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], arr = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
    for (const item of data){
        arr.push(item.uid);
        if (item.children && item.children.length) getChildrenUids(item.children, arr);
    }
    return arr;
};
const MenuConfigure = ()=>{
    const { styles } = (0, external_style_js_namespaceObject.useStyles)();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const { message } = external_antd_namespaceObject.App.useApp();
    const { items } = (0, external_MenuItemsProvider_js_namespaceObject.useMenuItems)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const allUids = findUids(items);
    const [uids, setUids] = (0, external_react_namespaceObject.useState)([]);
    const { loading, refresh } = (0, index_js_namespaceObject.useRequest)({
        resource: 'roles.menuUiSchemas',
        resourceOf: record.name,
        action: 'list',
        params: {
            paginate: false
        }
    }, {
        onSuccess (data) {
            var _data_data;
            setUids((null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.map((schema)=>schema['x-uid'])) || []);
        }
    });
    const resource = api.resource('roles.menuUiSchemas', record.name);
    const allChecked = allUids.length === uids.length;
    const handleChange = async (checked, schema)=>{
        const parentUids = getParentUids(items, (data)=>data.uid === schema.uid);
        const childrenUids = getChildrenUids(null == schema ? void 0 : schema.children, []);
        if (checked) {
            const totalUids = childrenUids.concat(schema.uid);
            const newUids = uids.filter((v)=>!totalUids.includes(v));
            setUids([
                ...newUids
            ]);
            await resource.remove({
                values: totalUids
            });
        } else {
            const totalUids = childrenUids.concat(parentUids);
            setUids((prev)=>(0, external_lodash_namespaceObject.uniq)([
                    ...prev,
                    ...totalUids
                ]));
            await resource.add({
                values: totalUids
            });
        }
        message.success(t('Saved successfully'));
    };
    const translateTitle = (menus)=>menus.map((menu)=>{
            const title = t(menu.title);
            if (menu.children) return {
                ...menu,
                title,
                children: translateTitle(menu.children)
            };
            return {
                ...menu,
                title
            };
        });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Table, {
        className: styles,
        loading: loading,
        rowKey: 'uid',
        pagination: false,
        expandable: {
            defaultExpandAllRows: true
        },
        columns: [
            {
                dataIndex: 'title',
                title: t('Menu item title')
            },
            {
                dataIndex: 'accessible',
                title: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Checkbox, {
                            checked: allChecked,
                            onChange: async (value)=>{
                                if (allChecked) await resource.set({
                                    values: []
                                });
                                else await resource.set({
                                    values: allUids
                                });
                                refresh();
                                message.success(t('Saved successfully'));
                            }
                        }),
                        ' ',
                        t('Accessible')
                    ]
                }),
                render: (_, schema)=>{
                    const checked = uids.includes(schema.uid);
                    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Checkbox, {
                        checked: checked,
                        onChange: ()=>handleChange(checked, schema)
                    });
                }
            }
        ],
        dataSource: translateTitle(items)
    });
};
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
});
