import { jsx } from "react/jsx-runtime";
import { useContext } from "react";
import { useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaComponentContext, createDesignable } from "../../index.mjs";
import { useAPIClient } from "../../../api-client/index.mjs";
import { useBlockRequestContext } from "../../../block-provider/index.mjs";
import { mergeFilter } from "../../../filter-provider/utils.mjs";
import { ActionInitializer } from "../../../schema-initializer/items/ActionInitializer.mjs";
const ActionBarAssociationFilterAction = (props)=>{
    const { refresh } = useContext(SchemaComponentContext);
    const fieldSchema = useFieldSchema();
    const api = useAPIClient();
    const { t } = useTranslation();
    const dn = createDesignable({
        t,
        api,
        refresh,
        current: fieldSchema
    });
    const { service, props: blockProps } = useBlockRequestContext();
    dn.loadAPIClientEvents();
    const handleInsert = (s)=>{
        dn.insertBeforeBegin(s);
    };
    const handleRemove = (schema, remove)=>{
        var _service_params, _blockProps_params;
        remove(schema);
        service.run({
            ...null == (_service_params = service.params) ? void 0 : _service_params[0],
            filter: mergeFilter([
                null == blockProps ? void 0 : null == (_blockProps_params = blockProps.params) ? void 0 : _blockProps_params.filter
            ])
        });
    };
    const schema = {
        type: 'void',
        'x-action': 'associateFilter',
        'x-initializer': 'AssociationFilter.Initializer',
        'x-component': 'AssociationFilter',
        properties: {}
    };
    const newProps = {
        ...props,
        insert: handleInsert,
        remove: handleRemove,
        wrap: (s)=>s
    };
    return /*#__PURE__*/ jsx(ActionInitializer, {
        ...newProps,
        schema: schema
    });
};
export { ActionBarAssociationFilterAction };
