import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
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__RoleTable_mjs__ from "./RoleTable.mjs";
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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { onChange } = props;
    const availableActions = (0, __WEBPACK_EXTERNAL_MODULE__RoleTable_mjs__.useAvailableActions)();
    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 scopes = toScopes(field.value);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __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,
                            "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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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
                };
            })
        })
    });
});
export { StrategyActions };
