import { jsx, jsxs } from "react/jsx-runtime";
import { useCallback, useMemo } from "react";
import { ArrayFieldComponent, ObjectFieldComponent, observer, useField } from "@tachybase/schema";
import { Space } from "antd";
import { useTranslation } from "react-i18next";
import { withDynamicSchemaProps } from "../../application/hoc/withDynamicSchemaProps.mjs";
import { useProps } from "../../schema-component/hooks/useProps.mjs";
import { RemoveActionContext } from "./context.mjs";
import { FormButtonLinkageRuleAction, FormFieldLinkageRuleAction } from "./LinkageRuleAction.mjs";
const LinkageRuleActions = observer((props)=>{
    var _field_value;
    const { type, linkageOptions } = props;
    const field = useField();
    return null == field ? void 0 : null == (_field_value = field.value) ? void 0 : _field_value.map((item, index)=>/*#__PURE__*/ jsx(RemoveActionContext.Provider, {
            value: ()=>field.remove(index),
            children: /*#__PURE__*/ jsx(ObjectFieldComponent, {
                name: index,
                component: [
                    'button' === type ? FormButtonLinkageRuleAction : FormFieldLinkageRuleAction,
                    {
                        ...props,
                        options: linkageOptions
                    }
                ]
            })
        }, index));
}, {
    displayName: 'LinkageRuleActions'
});
const LinkageRuleActionGroup = withDynamicSchemaProps((props)=>{
    const { t } = useTranslation();
    const field = useField();
    const logic = 'actions';
    const { type, linkageOptions, collectionName } = useProps(props);
    const style = useMemo(()=>({
            marginLeft: 10
        }), []);
    const components = useMemo(()=>[
            LinkageRuleActions,
            {
                type,
                linkageOptions,
                collectionName
            }
        ], [
        collectionName,
        linkageOptions,
        type
    ]);
    const spaceStyle = useMemo(()=>({
            marginTop: 8,
            marginBottom: 8
        }), []);
    const onClick = useCallback(()=>{
        const f = field.query('.actions').take();
        const items = f.value || [];
        items.push({});
        f.value = items;
    }, [
        field
    ]);
    return /*#__PURE__*/ jsxs("div", {
        style: style,
        children: [
            /*#__PURE__*/ jsx(ArrayFieldComponent, {
                name: logic,
                component: components,
                disabled: false
            }),
            /*#__PURE__*/ jsx(Space, {
                size: 16,
                style: spaceStyle,
                children: /*#__PURE__*/ jsx("a", {
                    onClick: onClick,
                    children: t('Add property')
                })
            })
        ]
    });
}, {
    displayName: 'LinkageRuleActionGroup'
});
export { LinkageRuleActionGroup, LinkageRuleActions };
