import { jsx } from "react/jsx-runtime";
import { useEffect } from "react";
import { FormLayout } from "@tachybase/components";
import { RecursionField, SchemaOptionsContext, observer, useField, useFieldSchema } from "@tachybase/schema";
import { createStyles } from "antd-style";
import { ACLCollectionProvider, useACLActionParamsContext } from "../../../built-in/acl/index.mjs";
import { CollectionProvider_deprecated } from "../../../collection-manager/index.mjs";
import { FormItem, useSchemaOptionsContext } from "../../index.mjs";
import Select from "../select/Select.mjs";
import { useAssociationFieldContext, useInsertSchema } from "./hooks.mjs";
import schema from "./schema.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      .ant-formily-item-bordered-none {\n        .ant-input-number-group-addon {\n          border: none !important;\n          background: none;\n        }\n        .ant-checkbox-wrapper {\n          margin-left: 8px;\n        }\n        .ant-table {\n          margin: 0px !important;\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        container: css(_templateObject())
    };
});
const InternalSubTable = observer(()=>{
    var _field_componentProps;
    const { styles } = useStyles();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const insert = useInsertSchema('SubTable');
    const insertSelector = useInsertSchema('Selector');
    const { options } = useAssociationFieldContext();
    const { actionName } = useACLActionParamsContext();
    useEffect(()=>{
        insert(schema.SubTable);
        field.required = fieldSchema['required'];
    }, []);
    useEffect(()=>{
        var _field_componentProps;
        if (null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.allowSelectExistingRecord) insertSelector(schema.Selector);
    }, [
        null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.allowSelectExistingRecord
    ]);
    const option = useSchemaOptionsContext();
    const components = {
        ...option.components,
        FormItem: FormItem,
        'Radio.Group': Select,
        'Checkbox.Group': (props)=>/*#__PURE__*/ jsx(Select, {
                multiple: true,
                mode: "multiple",
                ...props
            })
    };
    return /*#__PURE__*/ jsx(CollectionProvider_deprecated, {
        name: options.target,
        children: /*#__PURE__*/ jsx(ACLCollectionProvider, {
            actionPath: "".concat(options.target, ":").concat(actionName || 'view'),
            children: /*#__PURE__*/ jsx(FormLayout, {
                className: styles.container,
                layout: 'vertical',
                bordered: false,
                children: /*#__PURE__*/ jsx(SchemaOptionsContext.Provider, {
                    value: {
                        scope: option.scope,
                        components
                    },
                    children: /*#__PURE__*/ jsx(RecursionField, {
                        onlyRenderProperties: true,
                        basePath: field.address,
                        schema: fieldSchema,
                        filterProperties: (s)=>'AssociationField.SubTable' === s['x-component']
                    })
                })
            })
        })
    });
}, {
    displayName: 'InternalSubTable'
});
export { InternalSubTable };
