import { jsx } from "react/jsx-runtime";
import { useEffect } from "react";
import { observer, useField, useFieldSchema } from "@tachybase/schema";
import { useRequest } from "../../../api-client/index.mjs";
import { withDynamicSchemaProps } from "../../../application/hoc/withDynamicSchemaProps.mjs";
import { useProps } from "../../hooks/useProps.mjs";
import { FilterContext } from "./context.mjs";
import { FilterActionDesigner } from "./Filter.Action.Designer.mjs";
import { FilterAction } from "./FilterAction.mjs";
import { FilterGroup } from "./FilterGroup.mjs";
import { SaveDefaultValue } from "./SaveDefaultValue.mjs";
const useDef = (options)=>{
    const field = useField();
    return useRequest(()=>Promise.resolve({
            data: field.dataSource
        }), options);
};
const Filter = withDynamicSchemaProps(observer((props)=>{
    const { useDataSource = useDef } = props;
    const { options, dynamicComponent, className, collectionName } = useProps(props);
    const field = useField();
    const fieldSchema = useFieldSchema();
    useDataSource({
        onSuccess (data) {
            field.dataSource = (null == data ? void 0 : data.data) || [];
        }
    });
    useEffect(()=>{
        if (fieldSchema.defaultValue) field.initialValue = fieldSchema.defaultValue;
    }, [
        fieldSchema.defaultValue
    ]);
    return /*#__PURE__*/ jsx("div", {
        className: className,
        children: /*#__PURE__*/ jsx(FilterContext.Provider, {
            value: {
                field,
                fieldSchema,
                dynamicComponent,
                options: options || field.dataSource || [],
                disabled: props.disabled,
                collectionName
            },
            children: /*#__PURE__*/ jsx(FilterGroup, {
                ...props,
                bordered: false
            })
        })
    });
}), {
    displayName: 'Filter'
});
Filter.SaveDefaultValue = SaveDefaultValue;
Filter.Action = FilterAction;
Filter.Action.Designer = FilterActionDesigner;
export { Filter };
