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_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
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__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "../style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__RoleTable_mjs__ from "./RoleTable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ScopeSelect_mjs__ from "./ScopeSelect.mjs";
const toActionMap = (arr)=>{
    var _arr_forEach;
    const obj = {};
    null == arr || null === (_arr_forEach = arr.forEach) || void 0 === _arr_forEach || _arr_forEach.call(arr, (action)=>{
        if (action.name) {
            obj[action.name] = action;
            obj[action.name]['scope'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.isEmpty)(action.scope) ? null : action.scope;
        }
    });
    return obj;
};
const RoleResourceCollectionContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
RoleResourceCollectionContext.displayName = 'RoleResourceCollectionContext';
const RolesResourcesActions = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    var _collectionFields_filter;
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const onChange = (values)=>{
        const items = values.map((item)=>({
                ...item,
                scope: (0, __WEBPACK_EXTERNAL_MODULE_lodash__.isEmpty)(item.scope) ? null : item.scope
            }));
        props.onChange(items);
    };
    (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const roleCollection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const availableActions = (0, __WEBPACK_EXTERNAL_MODULE__RoleTable_mjs__.useAvailableActions)();
    const { getCollection, getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const collection = getCollection(roleCollection.collectionName);
    const collectionFields = getCollectionFields(roleCollection.collectionName);
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const actionMap = toActionMap(field.value || []);
    const inAction = (actionName, fieldName)=>{
        var _action_fields;
        const action = null == actionMap ? void 0 : actionMap[actionName];
        if (!action) return false;
        return null == action ? void 0 : null === (_action_fields = action.fields) || void 0 === _action_fields ? void 0 : _action_fields.includes(fieldName);
    };
    const availableActionsWithFields = availableActions.filter((action)=>action.allowConfigureFields);
    const fieldPermissions = null == collectionFields ? void 0 : null === (_collectionFields_filter = collectionFields.filter((field)=>field.interface)) || void 0 === _collectionFields_filter ? void 0 : _collectionFields_filter.map((field)=>{
        const permission = {
            ...field
        };
        for (const action of availableActionsWithFields)permission[action.name] = inAction(action.name, field.name);
        return permission;
    });
    const toggleAction = (actionName)=>{
        if (actionMap[actionName]) delete actionMap[actionName];
        else {
            var _collectionFields_filter_map, _collectionFields_filter;
            actionMap[actionName] = {
                name: actionName,
                fields: null == collectionFields ? void 0 : null === (_collectionFields_filter = collectionFields.filter((field)=>field.interface)) || void 0 === _collectionFields_filter ? void 0 : null === (_collectionFields_filter_map = _collectionFields_filter.map) || void 0 === _collectionFields_filter_map ? void 0 : _collectionFields_filter_map.call(_collectionFields_filter, (item)=>item.name)
            };
        }
        onChange(Object.values(actionMap));
    };
    const setScope = (actionName, scope)=>{
        if (actionMap[actionName]) {
            actionMap[actionName]['scope'] = scope;
            onChange(Object.values(actionMap));
        } else {
            toggleAction(actionName);
            actionMap[actionName]['scope'] = scope;
        }
    };
    const allChecked = {};
    for (const action of availableActionsWithFields){
        var _collectionFields_filter1, _actionMap_action_name_fields, _actionMap_action_name;
        allChecked[action.name] = (null == collectionFields ? void 0 : null === (_collectionFields_filter1 = collectionFields.filter((field)=>field.interface)) || void 0 === _collectionFields_filter1 ? void 0 : _collectionFields_filter1.length) === (null == actionMap ? void 0 : null === (_actionMap_action_name = actionMap[action.name]) || void 0 === _actionMap_action_name ? void 0 : null === (_actionMap_action_name_fields = _actionMap_action_name.fields) || void 0 === _actionMap_action_name_fields ? void 0 : _actionMap_action_name_fields.length);
    }
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RoleResourceCollectionContext.Provider, {
            value: collection,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                layout: 'vertical',
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem, {
                        label: t('Action permission'),
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Table, {
                            className: styles,
                            size: 'small',
                            pagination: false,
                            columns: [
                                {
                                    dataIndex: 'displayName',
                                    title: t('Action display name'),
                                    render: (value)=>compile(value)
                                },
                                {
                                    dataIndex: 'onNewRecord',
                                    title: t('Action type'),
                                    render: (onNewRecord)=>onNewRecord ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                                            color: 'green',
                                            children: t('Action on new records')
                                        }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                                            color: 'geekblue',
                                            children: t('Action on existing records')
                                        })
                                },
                                {
                                    dataIndex: 'enabled',
                                    title: t('Allow'),
                                    render: (enabled, action)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                                            checked: enabled,
                                            onChange: ()=>{
                                                toggleAction(action.name);
                                            }
                                        })
                                },
                                {
                                    dataIndex: 'scope',
                                    title: t('Data scope'),
                                    render: (value, action)=>!action.onNewRecord && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ScopeSelect_mjs__.ScopeSelect, {
                                            value: value,
                                            onChange: (scope)=>{
                                                setScope(action.name, scope);
                                            }
                                        })
                                }
                            ],
                            dataSource: null == availableActions ? void 0 : availableActions.map((item)=>{
                                let enabled = false;
                                let scope = null;
                                if (actionMap[item.name]) {
                                    enabled = true;
                                    if (!item.onNewRecord) scope = actionMap[item.name]['scope'];
                                }
                                return {
                                    ...item,
                                    enabled,
                                    scope
                                };
                            })
                        })
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem, {
                        label: t('Field permission'),
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Table, {
                            className: styles,
                            pagination: false,
                            dataSource: fieldPermissions,
                            columns: [
                                {
                                    dataIndex: [
                                        'uiSchema',
                                        'title'
                                    ],
                                    title: t('Field display name'),
                                    render: (value)=>compile(value)
                                },
                                ...availableActionsWithFields.map((action)=>{
                                    const checked = null == allChecked ? void 0 : allChecked[action.name];
                                    return {
                                        dataIndex: action.name,
                                        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: checked,
                                                    onChange: ()=>{
                                                        const item = actionMap[action.name] || {
                                                            name: action.name
                                                        };
                                                        if (checked) item.fields = [];
                                                        else {
                                                            var _collectionFields_map;
                                                            item.fields = null == collectionFields ? void 0 : null === (_collectionFields_map = collectionFields.map) || void 0 === _collectionFields_map ? void 0 : _collectionFields_map.call(collectionFields, (item)=>item.name);
                                                        }
                                                        actionMap[action.name] = item;
                                                        onChange(Object.values(actionMap));
                                                    }
                                                }),
                                                ' ',
                                                compile(action.displayName)
                                            ]
                                        }),
                                        render: (checked, field)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                                                checked: checked,
                                                "aria-label": "".concat(action.name, "_checkbox"),
                                                onChange: ()=>{
                                                    const item = actionMap[action.name] || {
                                                        name: action.name
                                                    };
                                                    const fields = item.fields || [];
                                                    if (checked) {
                                                        const index = fields.indexOf(field.name);
                                                        fields.splice(index, 1);
                                                    } else fields.push(field.name);
                                                    item.fields = fields;
                                                    actionMap[action.name] = item;
                                                    onChange(Object.values(actionMap));
                                                }
                                            })
                                    };
                                })
                            ]
                        })
                    })
                ]
            })
        })
    });
});
export { RoleResourceCollectionContext, RolesResourcesActions };
