import { jsx } from "react/jsx-runtime";
import "react";
import { connect, useField } from "@tachybase/schema";
import { Checkbox, Select, Table, Tag } from "antd";
import { useTranslation } from "react-i18next";
import { useCompile } from "../../../index.mjs";
import { useAvailableActions } from "./RoleTable.mjs";
const toScopes = (value)=>{
    var _value_forEach;
    if (!value) return {};
    const scopes = {};
    null == value || null == (_value_forEach = 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 = connect((props)=>{
    const { onChange } = props;
    const availableActions = useAvailableActions();
    const compile = useCompile();
    const { t } = useTranslation();
    const field = useField();
    const scopes = toScopes(field.value);
    return /*#__PURE__*/ jsx("div", {
        children: /*#__PURE__*/ jsx(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__*/ jsx(Tag, {
                            color: 'green',
                            children: t('Action on new records')
                        }) : /*#__PURE__*/ jsx(Tag, {
                            color: 'geekblue',
                            children: t('Action on existing records')
                        })
                },
                {
                    dataIndex: 'enabled',
                    title: t('Allow'),
                    render: (enabled, action)=>/*#__PURE__*/ jsx(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__*/ jsx(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 };
