import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__ from "@tachybase/test/client";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hoc_index_mjs__ from "../../hoc/index.mjs";
const HelloComponent = (0, __WEBPACK_EXTERNAL_MODULE__hoc_index_mjs__.withDynamicSchemaProps)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("pre", {
        "data-testid": "component",
        children: JSON.stringify(props)
    }));
const HelloDecorator = (0, __WEBPACK_EXTERNAL_MODULE__hoc_index_mjs__.withDynamicSchemaProps)((param)=>{
    let { children, ...others } = param;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("pre", {
                "data-testid": "decorator",
                children: JSON.stringify(others)
            }),
            children
        ]
    });
});
function withTestDemo(schema, scopes) {
    const Demo = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponentProvider, {
            components: {
                HelloComponent,
                HelloDecorator
            },
            scope: scopes,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                schema: {
                    type: 'void',
                    name: 'hello',
                    'x-component': 'HelloComponent',
                    'x-decorator': 'HelloDecorator',
                    ...schema
                }
            })
        });
    return Demo;
}
describe('withDynamicSchemaProps', ()=>{
    test('x-use-component-props', ()=>{
        function useComponentProps() {
            return {
                a: 'a'
            };
        }
        const schema = {
            'x-use-component-props': 'useComponentProps'
        };
        const scopes = {
            useComponentProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('component')).toHaveTextContent(JSON.stringify({
            a: 'a'
        }));
    });
    test('x-use-component-props and x-component-props should merge', ()=>{
        function useComponentProps() {
            return {
                a: 'a'
            };
        }
        const schema = {
            'x-use-component-props': 'useComponentProps',
            'x-component-props': {
                b: 'b'
            }
        };
        const scopes = {
            useComponentProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('component')).toHaveTextContent(JSON.stringify({
            b: 'b',
            a: 'a'
        }));
    });
    test('x-use-decorator-props', ()=>{
        function useDecoratorProps() {
            return {
                a: 'a'
            };
        }
        const schema = {
            'x-use-decorator-props': 'useDecoratorProps'
        };
        const scopes = {
            useDecoratorProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({
            a: 'a'
        }));
    });
    test('x-use-decorator-props and x-decorator-props should merge', ()=>{
        function useDecoratorProps() {
            return {
                a: 'a'
            };
        }
        const schema = {
            'x-use-decorator-props': 'useDecoratorProps',
            'x-decorator-props': {
                b: 'b'
            }
        };
        const scopes = {
            useDecoratorProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({
            b: 'b',
            a: 'a'
        }));
    });
    test('x-use-component-props and x-use-decorator-props exist simultaneously', ()=>{
        function useDecoratorProps() {
            return {
                a: 'a'
            };
        }
        function useComponentProps() {
            return {
                c: 'c'
            };
        }
        const schema = {
            'x-use-decorator-props': 'useDecoratorProps',
            'x-decorator-props': {
                b: 'b'
            },
            'x-use-component-props': 'useComponentProps',
            'x-component-props': {
                d: 'd'
            }
        };
        const scopes = {
            useDecoratorProps,
            useComponentProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({
            b: 'b',
            a: 'a'
        }));
        expect(getByTestId('component')).toHaveTextContent(JSON.stringify({
            d: 'd',
            c: 'c'
        }));
    });
    test('no register scope', ()=>{
        const schema = {
            'x-use-component-props': 'useComponentProps'
        };
        const Demo = withTestDemo(schema);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('component')).toHaveTextContent(JSON.stringify({}));
    });
    test('x-use-component-props should override x-component-props', ()=>{
        function useComponentProps() {
            return {
                a: 'a'
            };
        }
        const schema = {
            'x-use-component-props': 'useComponentProps',
            'x-component-props': {
                a: 'b'
            }
        };
        const scopes = {
            useComponentProps
        };
        const Demo = withTestDemo(schema, scopes);
        const { getByTestId } = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Demo, {}));
        expect(getByTestId('component')).toHaveTextContent(JSON.stringify({
            a: 'a'
        }));
    });
});
