import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_vitest__ from "vitest";
import * as __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__ from "../useDesignable.mjs";
describe('createDesignable', ()=>{
    let dn;
    let schema;
    beforeEach(()=>{
        schema = new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
            type: 'void',
            name: 'global',
            'x-uid': 'global',
            properties: {
                current: {
                    type: 'void',
                    'x-uid': 'current',
                    properties: {
                        child: {
                            type: 'void',
                            'x-uid': 'child'
                        }
                    }
                }
            }
        });
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.current
        });
    });
    describe('insert a new node', ()=>{
        test('insertBeforeBegin', ()=>{
            dn.insertBeforeBegin({
                name: 'abc'
            });
            expect(schema.toJSON()).toMatchObject({
                type: 'void',
                name: 'global',
                'x-uid': 'global',
                properties: {
                    abc: {
                        name: 'abc',
                        type: 'void'
                    },
                    current: {
                        type: 'void',
                        name: 'current',
                        'x-uid': 'current',
                        properties: {
                            child: {
                                type: 'void',
                                'x-uid': 'child'
                            }
                        }
                    }
                }
            });
            expect(schema.properties.abc['x-uid']).toBeDefined();
            expect(Object.keys(schema.properties)).toEqual([
                'abc',
                'current'
            ]);
        });
        test('insertAfterBegin', ()=>{
            dn.insertAfterBegin({
                name: 'abc'
            });
            expect(Object.keys(schema.properties.current.properties)).toEqual([
                'abc',
                'child'
            ]);
            expect(schema.toJSON()).toMatchObject({
                type: 'void',
                name: 'global',
                'x-uid': 'global',
                properties: {
                    current: {
                        type: 'void',
                        name: 'current',
                        'x-uid': 'current',
                        properties: {
                            abc: {
                                name: 'abc',
                                type: 'void'
                            },
                            child: {
                                type: 'void',
                                'x-uid': 'child'
                            }
                        }
                    }
                }
            });
            expect(schema.properties.current.properties.abc['x-uid']).toBeDefined();
        });
        test('insertBeforeEnd', ()=>{
            dn.insertBeforeEnd({
                name: 'abc'
            });
            expect(schema.toJSON()).toMatchObject({
                type: 'void',
                name: 'global',
                'x-uid': 'global',
                properties: {
                    current: {
                        type: 'void',
                        name: 'current',
                        'x-uid': 'current',
                        properties: {
                            child: {
                                type: 'void',
                                'x-uid': 'child'
                            },
                            abc: {
                                name: 'abc',
                                type: 'void'
                            }
                        }
                    }
                }
            });
            expect(schema.properties.current.properties.abc['x-uid']).toBeDefined();
        });
        test('insertAfterEnd', ()=>{
            dn.insertAfterEnd({
                name: 'abc'
            });
            expect(schema.toJSON()).toMatchObject({
                type: 'void',
                name: 'global',
                'x-uid': 'global',
                properties: {
                    current: {
                        type: 'void',
                        name: 'current',
                        'x-uid': 'current',
                        properties: {
                            child: {
                                type: 'void',
                                'x-uid': 'child'
                            }
                        }
                    },
                    abc: {
                        name: 'abc',
                        type: 'void'
                    }
                }
            });
            expect(schema.properties.abc['x-uid']).toBeDefined();
            expect(Object.keys(schema.properties)).toEqual([
                'current',
                'abc'
            ]);
        });
    });
    describe('move an existing node', ()=>{
        test('insertBeforeBegin', ()=>{
            var _schema_properties_current_properties, _schema_properties_current;
            dn.insertBeforeBegin(schema.properties.current.properties.child);
            expect(Object.keys(schema.properties)).toEqual([
                'child',
                'current'
            ]);
            expect(null === (_schema_properties_current = schema.properties.current) || void 0 === _schema_properties_current ? void 0 : null === (_schema_properties_current_properties = _schema_properties_current.properties) || void 0 === _schema_properties_current_properties ? void 0 : _schema_properties_current_properties.child).toBeUndefined();
        });
        test('insertBeforeBegin', ()=>{
            dn.insertBeforeBegin({
                type: 'void',
                name: 'a1'
            });
            dn.insertBeforeBegin({
                type: 'void',
                name: 'a2'
            });
            dn.insertBeforeBegin({
                type: 'void',
                name: 'a3'
            });
            expect(Object.keys(schema.properties)).toEqual([
                'a1',
                'a2',
                'a3',
                'current'
            ]);
            dn.insertBeforeBegin(schema.properties.a2);
            expect(Object.keys(schema.properties)).toEqual([
                'a1',
                'a3',
                'a2',
                'current'
            ]);
        });
        test('insertAfterBegin', ()=>{
            dn.insertBeforeBegin({
                name: 'a1'
            });
            dn.insertAfterBegin(schema.properties.a1);
            expect(Object.keys(schema.properties.current.properties)).toEqual([
                'a1',
                'child'
            ]);
            expect(schema.properties.a1).toBeUndefined();
        });
        test('insertBeforeEnd', ()=>{
            dn.insertBeforeBegin({
                name: 'a1'
            });
            dn.insertBeforeEnd(schema.properties.a1);
            expect(Object.keys(schema.properties.current.properties)).toEqual([
                'child',
                'a1'
            ]);
            expect(schema.properties.a1).toBeUndefined();
        });
        test('insertAfterEnd', ()=>{
            var _schema_properties_current_properties, _schema_properties_current;
            dn.insertAfterEnd(schema.properties.current.properties.child);
            expect(Object.keys(schema.properties)).toEqual([
                'current',
                'child'
            ]);
            expect(null === (_schema_properties_current = schema.properties.current) || void 0 === _schema_properties_current ? void 0 : null === (_schema_properties_current_properties = _schema_properties_current.properties) || void 0 === _schema_properties_current_properties ? void 0 : _schema_properties_current_properties.child).toBeUndefined();
        });
        test('insertAfterEnd', ()=>{
            dn.insertAfterEnd({
                type: 'void',
                name: 'a1'
            });
            dn.insertAfterEnd({
                type: 'void',
                name: 'a2'
            });
            dn.insertAfterEnd({
                type: 'void',
                name: 'a3'
            });
            expect(Object.keys(schema.properties)).toEqual([
                'current',
                'a3',
                'a2',
                'a1'
            ]);
            dn.insertAfterEnd(schema.properties.a2);
            expect(Object.keys(schema.properties)).toEqual([
                'current',
                'a2',
                'a3',
                'a1'
            ]);
        });
    });
    describe('recursiveRemoveIfNoChildren', ()=>{
        test('has child nodes', ()=>{
            dn.recursiveRemoveIfNoChildren(schema.properties.current);
            expect(schema.properties.current).toBeDefined();
        });
        test('no child nodes', ()=>{
            var _schema_properties_current_properties, _schema_properties_current;
            dn.recursiveRemoveIfNoChildren(schema.properties.current.properties.child);
            expect(null === (_schema_properties_current = schema.properties.current) || void 0 === _schema_properties_current ? void 0 : null === (_schema_properties_current_properties = _schema_properties_current.properties) || void 0 === _schema_properties_current_properties ? void 0 : _schema_properties_current_properties.child).toBeUndefined();
        });
    });
    describe('remove', ()=>{
        test('without schema', ()=>{
            var _schema_properties;
            dn.remove();
            expect(null == schema ? void 0 : null === (_schema_properties = schema.properties) || void 0 === _schema_properties ? void 0 : _schema_properties.current).toBeUndefined();
        });
        test('without options', ()=>{
            var _schema_properties_current_properties, _schema_properties_current;
            dn.remove(schema.properties.current.properties.child);
            expect(schema.properties.current).toBeDefined();
            expect(null === (_schema_properties_current = schema.properties.current) || void 0 === _schema_properties_current ? void 0 : null === (_schema_properties_current_properties = _schema_properties_current.properties) || void 0 === _schema_properties_current_properties ? void 0 : _schema_properties_current_properties.child).toBeUndefined();
        });
        test('removeParentsIfNoChildren + breakRemoveOn json', ()=>{
            var _schema_properties;
            dn.remove(schema.properties.current.properties.child, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-uid': 'global'
                }
            });
            expect(null == schema ? void 0 : null === (_schema_properties = schema.properties) || void 0 === _schema_properties ? void 0 : _schema_properties.current).toBeUndefined();
        });
        test('removeParentsIfNoChildren + breakRemoveOn function', ()=>{
            var _schema_properties;
            dn.remove(schema.properties.current.properties.child, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: (s)=>'global' === s['x-uid']
            });
            expect(null == schema ? void 0 : null === (_schema_properties = schema.properties) || void 0 === _schema_properties ? void 0 : _schema_properties.current).toBeUndefined();
        });
    });
});
describe('x-index', ()=>{
    let dn;
    let schema;
    beforeEach(()=>{
        schema = new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
            type: 'void',
            name: 'col1',
            'x-uid': 'col1',
            properties: {
                block1: {
                    type: 'void',
                    'x-index': 1
                },
                block2: {
                    type: 'void',
                    'x-index': 2
                },
                block3: {
                    type: 'void',
                    'x-index': 3
                },
                block4: {
                    type: 'void',
                    'x-index': 4
                },
                block5: {
                    type: 'void',
                    'x-index': 5
                }
            }
        });
    });
    test('insertBeforeBeginOrAfterEnd', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.block2
        });
        dn.insertBeforeBeginOrAfterEnd(schema.properties.block5);
        expect(Object.keys(schema.properties)).toEqual([
            'block1',
            'block5',
            'block2',
            'block3',
            'block4'
        ]);
    });
    test('insertBeforeBegin', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.block2
        });
        dn.insertBeforeBegin({
            name: 'block0'
        });
        expect(Object.keys(schema.properties)).toEqual([
            'block1',
            'block0',
            'block2',
            'block3',
            'block4',
            'block5'
        ]);
    });
    test('insertAfterEnd', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.block2
        });
        dn.insertAfterEnd({
            name: 'block0'
        });
        expect(Object.keys(schema.properties)).toEqual([
            'block1',
            'block2',
            'block0',
            'block3',
            'block4',
            'block5'
        ]);
    });
    test('insertAfterBegin', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema
        });
        dn.insertAfterBegin({
            name: 'block0'
        });
        expect(Object.keys(schema.properties)).toEqual([
            'block0',
            'block1',
            'block2',
            'block3',
            'block4',
            'block5'
        ]);
    });
    test('insertBeforeEnd', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema
        });
        dn.insertBeforeEnd({
            name: 'block0'
        });
        expect(Object.keys(schema.properties)).toEqual([
            'block1',
            'block2',
            'block3',
            'block4',
            'block5',
            'block0'
        ]);
    });
});
describe('wrap', ()=>{
    let dn;
    let schema;
    const colWrap = (s)=>({
            name: 'col12',
            type: 'void',
            'x-uid': 'col12',
            properties: {
                [s.name]: s
            }
        });
    const rowWrap = (s)=>({
            name: 'row2',
            type: 'void',
            'x-uid': 'row2',
            properties: {
                col21: {
                    type: 'void',
                    'x-uid': 'col21',
                    properties: {
                        [s.name]: s
                    }
                }
            }
        });
    beforeEach(()=>{
        schema = new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
            type: 'void',
            name: 'grid1',
            'x-uid': 'grid1',
            properties: {
                row1: {
                    type: 'void',
                    'x-uid': 'row1',
                    properties: {
                        col11: {
                            type: 'void',
                            'x-uid': 'col11',
                            properties: {
                                block1: {
                                    type: 'void'
                                }
                            }
                        }
                    }
                }
            }
        });
    });
    test('insertBeforeBegin', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.row1.properties.col11
        });
        const s = {
            type: 'void',
            name: 'block2'
        };
        dn.insertBeforeBegin(s, {
            wrap: colWrap
        });
        expect(Object.keys(schema.properties.row1.properties)).toEqual([
            'col12',
            'col11'
        ]);
        expect(schema.properties.row1.properties.col12.properties.block2).toBeDefined();
    });
    test('insertAfterEnd', ()=>{
        dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.row1.properties.col11
        });
        const s = {
            type: 'void',
            name: 'block2'
        };
        dn.insertAfterEnd(s, {
            wrap: colWrap
        });
        expect(Object.keys(schema.properties.row1.properties)).toEqual([
            'col11',
            'col12'
        ]);
        expect(schema.properties.row1.properties.col12.properties.block2).toBeDefined();
    });
    test('removeParentsIfNoChildren', ()=>{
        const coldn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.row1.properties.col11
        });
        const s = {
            type: 'void',
            name: 'block2'
        };
        coldn.insertAfterEnd(s, {
            wrap: colWrap
        });
        const griddn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema
        });
        griddn.insertBeforeEnd(schema.properties.row1.properties.col12.properties.block2, {
            wrap: rowWrap,
            removeParentsIfNoChildren: true
        });
        expect(Object.keys(schema.properties.row1.properties)).toEqual([
            'col11'
        ]);
        expect(Object.keys(schema.properties.row2.properties)).toEqual([
            'col21'
        ]);
        expect(schema.properties.row2.properties.col21.properties.block2).toBeDefined();
    });
});
describe('parentsIn', ()=>{
    let schema;
    beforeEach(()=>{
        schema = new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
            type: 'void',
            name: 'page',
            properties: {
                menu: {
                    type: 'void',
                    'x-uid': 'menu',
                    properties: {
                        item1: {
                            type: 'void',
                            'x-uid': 'item1'
                        },
                        item2: {
                            type: 'void',
                            'x-uid': 'item2'
                        }
                    }
                }
            }
        });
    });
    test('parentsIn', ()=>{
        const dn = (0, __WEBPACK_EXTERNAL_MODULE__useDesignable_mjs__.createDesignable)({
            current: schema.properties.menu.properties.item1
        });
        const callback = __WEBPACK_EXTERNAL_MODULE_vitest__.vi.fn();
        dn.on('error', callback);
        dn.insertAfterBegin(schema.properties.menu);
        expect(schema.properties.menu).toBeDefined();
        expect(callback.mock.calls[0][0].code).toBe('parent_is_not_allowed');
    });
});
