"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__, {
    RoleResourceCollectionContext: ()=>RoleResourceCollectionContext,
    RolesResourcesActions: ()=>RolesResourcesActions
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../../index.js");
const external_style_js_namespaceObject = require("../style.js");
const external_RoleTable_js_namespaceObject = require("./RoleTable.js");
const external_ScopeSelect_js_namespaceObject = require("./ScopeSelect.js");
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, external_lodash_namespaceObject.isEmpty)(action.scope) ? null : action.scope;
        }
    });
    return obj;
};
const RoleResourceCollectionContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
RoleResourceCollectionContext.displayName = 'RoleResourceCollectionContext';
const RolesResourcesActions = (0, schema_namespaceObject.connect)((props)=>{
    var _collectionFields_filter;
    const { styles } = (0, external_style_js_namespaceObject.useStyles)();
    const onChange = (values)=>{
        const items = values.map((item)=>({
                ...item,
                scope: (0, external_lodash_namespaceObject.isEmpty)(item.scope) ? null : item.scope
            }));
        props.onChange(items);
    };
    (0, schema_namespaceObject.useForm)();
    const roleCollection = (0, external_index_js_namespaceObject.useRecord)();
    const availableActions = (0, external_RoleTable_js_namespaceObject.useAvailableActions)();
    const { getCollection, getCollectionFields } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    const collection = getCollection(roleCollection.collectionName);
    const collectionFields = getCollectionFields(roleCollection.collectionName);
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RoleResourceCollectionContext.Provider, {
            value: collection,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(components_namespaceObject.FormLayout, {
                layout: 'vertical',
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormItem, {
                        label: t('Action permission'),
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                                            color: 'green',
                                            children: t('Action on new records')
                                        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                                            color: 'geekblue',
                                            children: t('Action on existing records')
                                        })
                                },
                                {
                                    dataIndex: 'enabled',
                                    title: t('Allow'),
                                    render: (enabled, action)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Checkbox, {
                                            checked: enabled,
                                            onChange: ()=>{
                                                toggleAction(action.name);
                                            }
                                        })
                                },
                                {
                                    dataIndex: 'scope',
                                    title: t('Data scope'),
                                    render: (value, action)=>!action.onNewRecord && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ScopeSelect_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormItem, {
                        label: t('Field permission'),
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                                            children: [
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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));
                                                }
                                            })
                                    };
                                })
                            ]
                        })
                    })
                ]
            })
        })
    });
});
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
});
