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_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__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "../style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__MenuItemsProvider_mjs__ from "./MenuItemsProvider.mjs";
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, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { items } = (0, __WEBPACK_EXTERNAL_MODULE__MenuItemsProvider_mjs__.useMenuItems)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const allUids = findUids(items);
    const [uids, setUids] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const { loading, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_lodash__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                        checked: checked,
                        onChange: ()=>handleChange(checked, schema)
                    });
                }
            }
        ],
        dataSource: translateTitle(items)
    });
};
export { MenuConfigure };
