"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__, {
    StrategyActions: ()=>StrategyActions
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../../index.js");
const external_RoleTable_js_namespaceObject = require("./RoleTable.js");
const toScopes = (value)=>{
    var _value_forEach;
    if (!value) return {};
    const scopes = {};
    null == value || null === (_value_forEach = value.forEach) || void 0 === _value_forEach || _value_forEach.call(value, (item)=>{
        const [name, scope] = item.split(':');
        scopes[name] = scope || 'all';
    });
    return scopes;
};
const toFieldValue = (scopes)=>{
    const values = [];
    for(const name in scopes)if (Object.prototype.hasOwnProperty.call(scopes, name)) {
        const scope = scopes[name];
        if ('all' === scope) values.push(name);
        else values.push("".concat(name, ":").concat(scope));
    }
    return values;
};
const StrategyActions = (0, schema_namespaceObject.connect)((props)=>{
    const { onChange } = props;
    const availableActions = (0, external_RoleTable_js_namespaceObject.useAvailableActions)();
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.useField)();
    const scopes = toScopes(field.value);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Table, {
            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,
                            "aria-label": "".concat(action.name, "_checkbox"),
                            onChange: (e)=>{
                                if (enabled) delete scopes[action.name];
                                else scopes[action.name] = 'all';
                                onChange(toFieldValue(scopes));
                            }
                        })
                },
                {
                    dataIndex: 'scope',
                    title: t('Data scope'),
                    render: (scope, action)=>!action.onNewRecord && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                            "data-testid": "select-data-scope",
                            popupMatchSelectWidth: false,
                            size: 'small',
                            value: scope,
                            options: [
                                {
                                    label: t('All records'),
                                    value: 'all'
                                },
                                {
                                    label: t('Own records'),
                                    value: 'own'
                                }
                            ],
                            onChange: (value)=>{
                                scopes[action.name] = value;
                                onChange(toFieldValue(scopes));
                            }
                        })
                }
            ],
            dataSource: null == availableActions ? void 0 : availableActions.map((item)=>{
                let scope = 'all';
                let enabled = false;
                if (scopes[item.name]) {
                    enabled = true;
                    scope = scopes[item.name];
                }
                return {
                    ...item,
                    enabled,
                    scope
                };
            })
        })
    });
});
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
});
