import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { observer, useFieldSchema } from "@tachybase/schema";
import { createStyles } from "antd-style";
import { withDynamicSchemaProps } from "../../application/hoc/withDynamicSchemaProps.mjs";
import { FormBlockContext } from "../../block-provider/index.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { useCollectionParentRecordData } from "../../data-source/collection-record/CollectionRecordProvider.mjs";
import { RecordProvider } from "../../record-provider/index.mjs";
import { SchemaComponent, useProps } from "../../schema-component/index.mjs";
import { FilterContext } from "../../schema-component/antd/filter/context.mjs";
import { VariableInput, getShouldChange } from "../VariableInput/VariableInput.mjs";
import { EnableLinkage } from "./components/EnableLinkage.mjs";
import { ArrayCollapse } from "./components/LinkageHeader.mjs";
import { LinkageRuleActionGroup } from "./LinkageRuleActionGroup.mjs";
export * from "./type.mjs";
export * from "./action-hooks.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 useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        filter: css(_templateObject())
    };
});
const FormLinkageRules = withDynamicSchemaProps(observer((props)=>{
    const { styles } = useStyles();
    const fieldSchema = useFieldSchema();
    const { options, defaultValues, collectionName, form, formBlockType, variables, localVariables, record, dynamicComponent } = useProps(props);
    const { getAllCollectionsInheritChain } = useCollectionManager_deprecated();
    const parentRecordData = useCollectionParentRecordData();
    const components = useMemo(()=>({
            ArrayCollapse: ArrayCollapse
        }), []);
    const schema = useMemo(()=>({
            type: 'object',
            properties: {
                rules: {
                    type: 'array',
                    default: defaultValues,
                    'x-component': 'ArrayCollapse',
                    'x-decorator': 'FormItem',
                    'x-component-props': {
                        accordion: true
                    },
                    items: {
                        type: 'object',
                        'x-component': 'ArrayCollapse.CollapsePanel',
                        'x-component-props': {
                            extra: /*#__PURE__*/ jsx(EnableLinkage, {})
                        },
                        properties: {
                            layout: {
                                type: 'void',
                                'x-component': 'FormLayout',
                                'x-component-props': {
                                    labelStyle: {
                                        marginTop: '6px'
                                    },
                                    labelCol: 8,
                                    wrapperCol: 16
                                },
                                properties: {
                                    conditions: {
                                        'x-component': 'h4',
                                        'x-content': '{{ t("Condition") }}'
                                    },
                                    condition: {
                                        'x-component': 'Filter',
                                        'x-use-component-props': ()=>({
                                                options,
                                                className: styles.filter
                                            }),
                                        'x-component-props': {
                                            collectionName,
                                            dynamicComponent: (props)=>{
                                                const { collectionField } = props;
                                                return /*#__PURE__*/ jsx(VariableInput, {
                                                    ...props,
                                                    form: form,
                                                    record: record,
                                                    shouldChange: getShouldChange({
                                                        collectionField,
                                                        variables,
                                                        localVariables,
                                                        getAllCollectionsInheritChain
                                                    })
                                                });
                                            }
                                        }
                                    },
                                    actions: {
                                        'x-component': 'h4',
                                        'x-content': '{{ t("Properties") }}'
                                    },
                                    action: {
                                        type: 'void',
                                        'x-component': LinkageRuleActionGroup,
                                        'x-component-props': {
                                            ...props
                                        }
                                    }
                                }
                            },
                            remove: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.Remove'
                            },
                            moveUp: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.MoveUp'
                            },
                            moveDown: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.MoveDown'
                            },
                            copy: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.Copy'
                            }
                        }
                    },
                    properties: {
                        add: {
                            type: 'void',
                            title: '{{ t("Add linkage rule") }}',
                            'x-component': 'ArrayCollapse.Addition',
                            'x-reactions': {
                                dependencies: [
                                    'rules'
                                ],
                                fulfill: {
                                    state: {}
                                }
                            }
                        }
                    }
                }
            }
        }), [
        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
        },
        children: /*#__PURE__*/ jsx(RecordProvider, {
            record: record,
            parent: parentRecordData,
            children: /*#__PURE__*/ jsx(FilterContext.Provider, {
                value: value,
                children: /*#__PURE__*/ jsx(SchemaComponent, {
                    components: components,
                    schema: schema
                })
            })
        })
    });
}), {
    displayName: 'FormLinkageRules'
});
export { FormLinkageRules };
