import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { ArrayCollapse } from "@tachybase/components";
import { observer, useFieldSchema } from "@tachybase/schema";
import { css } from "antd-style";
import { FormBlockContext } from "../../block-provider/index.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { RecordProvider } from "../../record-provider/index.mjs";
import { SchemaComponent } from "../../schema-component/index.mjs";
import { VariableInput, getShouldChange } from "../VariableInput/index.mjs";
import { FilterContext } from "./context.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n                    position: relative;\n                    width: 100%;\n                    margin-left: 10px;\n                  "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const FormFilterScope = observer((props)=>{
    const fieldSchema = useFieldSchema();
    const { useProps, dynamicComponent } = props;
    const { options, defaultValues, collectionName, form, formBlockType, variables, localVariables, record } = useProps();
    const { getAllCollectionsInheritChain } = useCollectionManager_deprecated();
    const components = useMemo(()=>({
            ArrayCollapse: ArrayCollapse
        }), []);
    const schema = useMemo(()=>({
            type: 'object',
            properties: {
                condition: {
                    'x-component': 'Filter',
                    default: defaultValues,
                    'x-component-props': {
                        collectionName,
                        useProps () {
                            return {
                                options,
                                className: css(_templateObject())
                            };
                        },
                        dynamicComponent: (props)=>{
                            const { collectionField } = props;
                            return /*#__PURE__*/ jsx(VariableInput, {
                                ...props,
                                form: form,
                                record: record,
                                collectionField: props.collectionField,
                                shouldChange: getShouldChange({
                                    collectionField,
                                    variables,
                                    localVariables,
                                    getAllCollectionsInheritChain
                                })
                            });
                        }
                    }
                }
            }
        }), [
        collectionName,
        defaultValues,
        form,
        localVariables,
        options,
        props,
        record,
        variables
    ]);
    const value = useMemo(()=>({
            field: options,
            fieldSchema,
            dynamicComponent,
            options: options || []
        }), [
        dynamicComponent,
        fieldSchema,
        options
    ]);
    return /*#__PURE__*/ jsx(FormBlockContext.Provider, {
        value: {
            form,
            type: formBlockType,
            collectionName: collectionName
        },
        children: /*#__PURE__*/ jsx(RecordProvider, {
            record: record,
            children: /*#__PURE__*/ jsx(FilterContext.Provider, {
                value: value,
                children: /*#__PURE__*/ jsx(SchemaComponent, {
                    components: components,
                    schema: schema
                })
            })
        })
    });
}, {
    displayName: 'FormFilterScope'
});
export { FormFilterScope };
